Skip to content

Say which part of a function signature does not match - #331

Merged
ASDAlexander77 merged 1 commit into
mainfrom
func-to-union-return-widening
Sep 15, 2026
Merged

ASDAlexander77 merged 1 commit into
mainfrom
func-to-union-return-widening

Conversation

@ASDAlexander77

Copy link
Copy Markdown
Owner

Problem

Assigning a function to a function type it does not match reported only the two types:

() => s32 is not matching type () => number
invalid cast from () => s32 to string | () => number

Neither says what is wrong, and the second names the whole union rather than the function member that was meant.

The return type is the usual reason, and it is easy to hit by accident: an integer literal is s32 in this language (let x = 1; x = 2.5 keeps 2), so function one() { return 1; } is () => s32 and does not match () => number — as a plain assignment, a function argument, an arrow function, or a union member.

Change

emitFunctionTypeMismatch names the part of the signature that differs — argument count, argument position, or return type — and for a return type points at declaring it:

can't assign '() => s32' to '() => number': return type 's32' is not 'number'.
Declare the return type to get one, for example 'function f(): number'

can't assign '(p0: number, p1: number) => number' to '(p0: number) => number':
it takes 2 argument(s), 1 expected

can't assign '(p0: string) => number' to '(p0: number) => number': argument #1 does not match

A function assigned to a union is now reported against the union's function member rather than the union as a whole.

This is diagnostics only: what compiles and what does not is unchanged. The alternative — a generated wrapper that casts the result, so () => s32 would be assignable to () => number — was considered and not taken; declaring the return type is the answer here.

Tests

No test is added: the repository has no mechanism for asserting compiler error text, and this change alters no accepted or rejected program.

Full Windows debug suite (ctest -R "^test-|^unittest-"): 2759/2759 passed.

🤖 Generated with Claude Code

Assigning a function to a function type it does not match reported only the
two types:

    () => s32 is not matching type () => number
    invalid cast from () => s32 to string | () => number

Neither says what is wrong, and the second names the whole union rather than
the member that was meant. The return type is the usual reason: an integer
literal is s32 here, so `function one() { return 1; }` is `() => s32` and
does not match `() => number`.

emitFunctionTypeMismatch now names the part that differs - argument count,
argument position, or return type - and for a return type points at
declaring it:

    can't assign '() => s32' to '() => number': return type 's32' is not
    'number'. Declare the return type to get one, for example
    'function f(): number'

A function assigned to a union reports against the union's function member
instead of the union.

Diagnostics only: what compiles and what does not is unchanged.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
@ASDAlexander77
ASDAlexander77 merged commit e9e0514 into main Sep 15, 2026
2 checks passed
@ASDAlexander77
ASDAlexander77 deleted the func-to-union-return-widening branch September 15, 2026 22:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant