Skip to content

Report a call with too few arguments (TS2554) instead of padding it with undef - #341

Merged
ASDAlexander77 merged 4 commits into
mainfrom
fix-call-arity
Sep 22, 2026
Merged

ASDAlexander77 merged 4 commits into
mainfrom
fix-call-arity

Conversation

@ASDAlexander77

Copy link
Copy Markdown
Owner

Summary

A call that passes fewer arguments than the callee requires is now a compile error, as in TypeScript (TS2554). Until now tslang padded the missing arguments with undef and compiled on.

That is not theoretical. It hid a real bug in the default library: RegExp.replaceAll called the native regexp_replace with 3 of its 4 arguments. At x64 the missing argument happened to still be in a register, so it worked; at i686 it was an uninitialised stack slot, and the program crashed. The plan is in docs/superpowers/plans/2026-09-23-call-arity-check.md.

The check

mlirGenPrepareCallOperands is the one place missing arguments were padded. Before padding, it now reports:

  • Expected 2 arguments, but got 1.
  • Expected 2-3 arguments, but got 1. when trailing parameters may be omitted
  • Expected at least 1 arguments, but got 0. for varargs (TypeScript's own grammar)

A parameter may be omitted when its type is optional (both b?: T and a default-valued b = 5), undefined or void. Everything else is an error.

The numbers count what the user wrote. The compiler inserts operands of its own (this, a bound method's receiver, a closure's captures), and mlirGenCallFunction — the only caller — now passes that count explicitly rather than having it guessed from operand types.

f?.(...) no longer swallows the failure. With a void callee the call was silently dropped; otherwise the module failed verification with no message about arguments.

Class expressions no longer run the constructor

let X = class { constructor(a: number, b: number) {...} } evaluated to a constructed instance, which meant the constructor ran at the class definition with no arguments for its parameters. 00interface_new printed an extra Call ctor : 0, 0. A class expression's value only has to carry the class, so it is now allocated with its vtable and no constructor call. Interface casts still work, because the value is a real object.

Tests

  • 9 new tests: 8 negative (plain function, optional parameter, method, constructor, closure with a capture, declared function, varargs, optional call), each asserting the exact message, plus a positive file for the calls that must keep compiling.
  • Two existing tests were calling functions with missing arguments, which TypeScript rejects: new S() against a two-parameter constructor, and gen<string[]>(). Both now pass their arguments.

Not in this PR

  • Too many arguments is still not checked.
  • A method with an explicit this: parameter now gets a false error with a wrong count (m(this: O, a: number, b: number) called o.m(1, 2) reports Expected 3 arguments, but got 2.), for class, interface and object-literal methods. The cause is older than this PR: such signatures carry the receiver twice, so these calls previously miscompiled in silence, putting the first argument into the this slot and leaving the last parameter undef. There is no live use of that shape in this repo or the default library (the only one is commented out in 00lambdas.ts), and extension and namespace functions using this: — every real use, including the default library's ~150 — count correctly. Worth its own issue.
  • Overload signatures crash the compiler, with or without a call (function f(a: number): number; function f(a: number, b: number): number; function f(a: number, b?: number) {...}). This predates the change: a debug build asserts in ArrayRef. Also worth its own issue.

Test plan

  • ctest -j 16 -C Release: 2786/2786. That's 2771 plus 6 registrations for the class-expression test and 9 arity tests.
  • Each negative test fails to compile with the exact expected message, and failed before the change.
  • The default library builds under gc, rc and none with the new compiler, and its own 150-test suite is unchanged in jit mode (300/300 across both configs). Its compile mode fails identically before and after on this machine, because of a stale TSLANG_LIB_PATH in the environment.
  • Probed for false errors: spread calls, generators and it.next(), generic classes, methods and default parameters, inheritance with explicit and implicit super(), interface and bound methods, object-literal methods, and default-library calls that rely on default parameters (toFixed(), padEnd(5), split(), join(), sort()). All compile.
  • Linux CI.

🤖 Generated with Claude Code

ASDAlexander77 and others added 4 commits September 22, 2026 18:30
Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
…ith undef

A missing argument is now padded only for a parameter that may be omitted
(!ts.optional - optional and default-valued parameters -, !ts.undefined,
!ts.void) or the rest parameter. Any other missing parameter is reported as
"Expected N arguments, but got M." ("N-M" with omittable trailing
parameters, "at least N" with a rest parameter). The counts are what the
user wrote: mlirGenCallFunction passes how many operands it inserted in
front (the `this` of a bound function - method, constructor, closure
capture - or of an extension function).

A conditional call (`f?.(...)`) swallowed the failure of the inner call and
left an empty branch, which for a void callee dropped the call silently; it
now fails.

The two suite tests that relied on the padding now pass the arguments.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
…nsertion guard

- positive.ts: reword the undefined-parameter comment; tsc does not omit
  `b: undefined` (TS2554), only `void`, which tslang cannot even write as
  a parameter type.
- CMakeLists.txt: add FAIL_REGULAR_EXPRESSION to the call-arity negative
  tests so a crash after the expected message no longer passes silently.
- MLIRGenAccessCall.cpp: guard the optional-call (f?.(...)) insertion
  point so EXIT_IF_FAILED's early return can't leave the builder stuck
  inside the IfOp's then-block.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
@ASDAlexander77
ASDAlexander77 merged commit b019699 into main Sep 22, 2026
2 checks passed
@ASDAlexander77
ASDAlexander77 deleted the fix-call-arity branch September 22, 2026 18:46
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