diff --git a/docs/superpowers/plans/2026-09-23-call-arity-check.md b/docs/superpowers/plans/2026-09-23-call-arity-check.md new file mode 100644 index 000000000..9953aa9d1 --- /dev/null +++ b/docs/superpowers/plans/2026-09-23-call-arity-check.md @@ -0,0 +1,116 @@ +# Call Arity Check Implementation Plan + +> **For agentic workers:** REQUIRED SUB-SKILL: Use superpowers:subagent-driven-development (recommended) or superpowers:executing-plans to implement this plan task-by-task. Steps use checkbox (`- [ ]`) syntax for tracking. + +**Goal:** A call that passes fewer arguments than the callee's required parameters is a compile error, like TypeScript's TS2554 ("Expected N arguments, but got M."). Today tslang silently pads the missing arguments with `undef`. That hid a real bug: `RegExp.replaceAll` in the default library called the native `regexp_replace` with 3 of its 4 arguments. It worked at x64 by register luck and crashed at i686. + +**Architecture:** Missing arguments are padded in one place, `mlirGenPrepareCallOperands` in `tslang/lib/TypeScript/MLIRGenImpl.h`. The padding stays for parameters that may be omitted: `!ts.optional<...>` (optional `b?: T` and default-valued `b = 5` parameters are both typed optional), `!ts.undefined` and `!ts.void`. For any other parameter it becomes an error. A probe with exactly that rule (2026-09-23, main 6900ee67) failed 34 ctest registrations, from 5 test files and one compiler-internal call: +- **Compiler-internal:** `mlirGen(ClassExpression)` (`MLIRGenExpressions.cpp` ~1703) turns a class expression into a class-typed value by calling `NewClassInstance(..., undefined, ...)`. That is a constructor call with NO arguments. This is itself a bug: the constructor runs at the class definition with garbage arguments (`00interface_new` prints an extra `Call ctor : 0, 0`). The fix is to allocate the object and set its vtable WITHOUT calling the constructor: `NewClassInstanceLogicAsOp(location, classInfo, false, genContext)`. An experiment showed it passes 00interface_new, 00class_expression, 00class_expression2, 00class_expression3 and 00union_to_any under gc and rc. The extra ctor line disappears. A typed `ts.Undef` does NOT work: `00interface_new` casts the value to an interface, which needs a real object and vtable. `ts.ClassRef` alone has no lowering. +- **Tests written against the silent padding:** + - `01types_utility.ts:178` `new S()` for `constructor(s: string, n: number)`; + - `00funcs_generic_with_typeof.ts:20` `gen()` for `gen(t: T)`. + + TypeScript rejects both. + +**Tech Stack:** C++17 (MLIRGen), CMake/CTest. + +**Spec:** none separate. This plan is the design record. Semantics follow TypeScript's TS2554 for the "too few arguments" direction only. + +## Global Constraints + +- **Omittable parameters, and only these:** `mlir_ts::OptionalType`, `mlir_ts::UndefinedType`, `mlir_ts::VoidType`. Every other missing parameter is an error. Varargs (`isVarArg`, the last parameter) keep their current handling. +- **Message:** `Expected {N} arguments, but got {M}.`. When the callee has omittable trailing parameters, use `Expected {min}-{max} arguments, but got {M}.`, as TypeScript does. N, min, max and M count only what the USER sees: + - exclude compiler-inserted leading operands (`this`, bound-method `this`, capture/closure refs); + - a vararg parameter counts as not required and is left out of max, so for varargs the message is `Expected at least {min} arguments, but got {M}.`. + + The numbers must be verified by tests for a plain function, a method, a constructor (`new`), and a closure that captures a variable. +- **Too many arguments is out of scope.** Do not add that check. Record it as a follow-up in the final report. +- **Emission:** use `emitError(location)` with the call's location, and return failure. No warnings, no flag to turn it off. +- **Test totals:** the default suite (`ctest -j 16 -C Release`) must end green. Its count grows only by the new negative tests. +- **DefaultLib:** its sources must still compile under the new check. Build from DefaultLib `main`, which has the replaceAll fix (merged with DefaultLib #7 on 2026-09-23). +- **Commits:** end with `Co-Authored-By: Claude Opus 5 `. GPG signing: retry, pause, `gpgconf --kill scdaemon` / `gpg-agent`, then leave the change staged with a message file for the user. Never bypass signing. +- **Branch:** `fix-call-arity` (already created from origin/main 6900ee67). +- **Build and test commands:** + - build: `cmake --build I:/TypeScriptCompiler/__build/tslang/windows-msbuild-2026-release --config Release --target tslang -j 8`; + - tests: `cd` into that build dir, then `ctest -j 16 -C Release`. `-C Release` is mandatory. + - The machine's env has stale `TSLANG_LIB_PATH`/`GC_LIB_PATH=I:\tslang`. Pass lib paths explicitly for any manual `--emit=exe`. + +--- + +### Task 1: Class expressions stop calling the constructor + +**Files:** +- Modify: `tslang/lib/TypeScript/MLIRGenExpressions.cpp` (`mlirGen(ClassExpression)`, ~1698-1703) +- Test: a new suite test `tslang/test/tester/tests/00class_expression_no_ctor.ts`, registered like its neighbours (read `tslang/test/tester/CMakeLists.txt` for how `00class_expression3.ts` is registered and copy that shape). + +- [ ] **Step 1: Failing test.** The class's constructor prints, and the test asserts the constructor ran exactly once: only for the explicit `new`, not at the class definition. Use a module-level counter: + +```ts +let ctorCalls = 0; + +let Point = class { + constructor(public x: number, public y: number) { ctorCalls++; } +}; + +function main() { + assert(ctorCalls == 0, "class expression must not call the constructor"); + const p = new Point(3, 4); + assert(ctorCalls == 1); + assert(p.x == 3 && p.y == 4); + print("done."); +} +``` + +Build and run it: `--emit=jit` with `--no-default-lib` and `--shared-libs=/TypeScriptRuntime.dll`, under gc and rc. Expected before the fix: the first assert fails. +- [ ] **Step 2: Fix.** Replace the `NewClassInstance(location, classValue, undefined, undefined, false, genContext)` return with an allocation that does not call the constructor: + +```cpp + // A class expression's value only has to carry the class (its type and vtable), so + // that `new X(...)`, casts to an interface and the like work on it. Allocate it and + // set the vtable, but do not run the constructor: that would execute user code at + // the class definition, with no arguments for its parameters. + return V(NewClassInstanceLogicAsOp(location, classInfo, false, genContext)); +``` + +Drop the now-unused `classValue` / `ClassRefOp` creation if nothing else uses it. +- [ ] **Step 3: Verify.** + - The new test passes under gc and rc. + - `00interface_new`, `00class_expression`, `00class_expression2`, `00class_expression3` and `00union_to_any` still pass. `00interface_new` no longer prints `Call ctor : 0, 0`: check its registered expected output, if it has one. + - Full `ctest -j 16 -C Release` is green, with 3 new registrations or however many the neighbour pattern creates. +- [ ] **Step 4: Commit.** Message: "Do not run the constructor when a class expression is evaluated". + +### Task 2: The arity check + +**Files:** +- Modify: `tslang/lib/TypeScript/MLIRGenImpl.h` (`mlirGenPrepareCallOperands`, and whatever passes it the count of compiler-inserted leading operands) +- Modify: `tslang/test/tester/tests/01types_utility.ts:178` (`new S()` becomes `new S("s", 1)`, keeping what the test checks), `tslang/test/tester/tests/00funcs_generic_with_typeof.ts:20` (`gen()` becomes `gen(undefined)`; the file is `@strict-null false`, so this is valid TypeScript and keeps the "no" result) +- Create: `tslang/test/tester/call-arity/*.ts` negative cases, registered in `tslang/test/tester/CMakeLists.txt` as plain `add_test(NAME test-compile-call-arity- COMMAND --emit=obj --no-default-lib -mm=none -o )` with `PASS_REGULAR_EXPRESSION ""`. It must not be matched by the `tslang_add_test` twin machinery. + +**Interfaces:** +- Consumes: Task 1, which removed the one compiler-internal short call. +- Produces: the error texts from Global Constraints. + +- [ ] **Step 1: Failing tests.** Negative cases, each a file with one bad call and the message it must produce: + - plain function `req(a: number, b: number)` called `req(1)`: `Expected 2 arguments, but got 1.` + - `opt(a: number, b: number, c?: number)` called `opt(1)`: `Expected 2-3 arguments, but got 1.` + - a method `o.m(a: number, b: number)` called `o.m(1)`: `Expected 2 arguments, but got 1.` (the `this` operand must not be counted) + - a constructor `new C(1)` for `constructor(a: number, b: number)`: `Expected 2 arguments, but got 1.` + - a closure capturing an outer variable, `(a: number, b: number) => a + b + captured`, called with one argument: `Expected 2 arguments, but got 1.` + - a `declare function nat(a: string, b: string): void;` called `nat("x")`: `Expected 2 arguments, but got 1.` + - varargs `va(a: number, ...rest: number[])` called `va()`: `Expected at least 1 arguments, but got 0.` + + Positive (in one file that must compile and run): `opt(1, 2)`, `def(1)` with `b = 5`, `va(1)`, `va(1, 2, 3)`, and a call omitting a `void` parameter. + + Register the negative tests and run them. Expected: all FAIL, because the compile succeeds today. +- [ ] **Step 2: Implement.** + - In `mlirGenPrepareCallOperands`, before padding, check each missing parameter against the Global Constraints rule. On the first non-omittable one, emit the TS2554-style message and return failure. + - To report user-visible counts, the function needs the number of compiler-inserted leading operands. Find how `this` / capture operands are prepended on each call path (`mlirGenCallFunction` inserts `thisValue`; constructor calls pass `this` as operand 0; bound functions and captures: trace them). Pass the offset in explicitly, as a new parameter with every caller updated, or derive it from the function type if the type marks them. Do not guess from operand types. + - Compute min = the number of user-visible parameters that are neither omittable nor varargs, and max = the user-visible parameter count excluding varargs. +- [ ] **Step 3: Fix the two tests** (`01types_utility.ts`, `00funcs_generic_with_typeof.ts`) as listed under Files. +- [ ] **Step 4: Verify.** + - The negative tests pass and the positive file compiles and runs. + - `ctest -j 16 -C Release` is fully green. + - DefaultLib compiles under the new check. From `I:\TypeScriptCompilerDefaultLib`, on `main`, pointed at the new compiler (`build.bat` uses `..\TypeScriptCompiler\__build\tslang\windows-msbuild-2026-release\bin`), run `build.bat release`. All 3 models must build. + - Then run DefaultLib `tests.ps1` and compare against a pre-change baseline. Compile mode fails on this machine because of the stale env, so compare failure lists; jit must not regress. + - Report any other DefaultLib or corpus file the check rejects. If one is a real missing-argument bug in DefaultLib, list it; do not fix it in this task. +- [ ] **Step 5: Commit.** Message: "Report a call with too few arguments (TS2554) instead of padding it with undef". diff --git a/tslang/lib/TypeScript/MLIRGenAccessCall.cpp b/tslang/lib/TypeScript/MLIRGenAccessCall.cpp index 5311755d5..a5d5fb8ab 100644 --- a/tslang/lib/TypeScript/MLIRGenAccessCall.cpp +++ b/tslang/lib/TypeScript/MLIRGenAccessCall.cpp @@ -1417,6 +1417,7 @@ namespace mlirgen ? builder.create(location, getOptionalType(resultType), condValue, true) : builder.create(location, condValue, false); + mlir::OpBuilder::InsertionGuard guard(builder); builder.setInsertionPointToStart(&ifOp.getThenRegion().front()); // value if true @@ -1425,6 +1426,9 @@ namespace mlirgen builder.create(location, optFuncRef.getElementType(), actualFuncRefValue); auto result = mlirGenCallExpression(location, innerFuncRef, typeArguments, operands, genContext); + // a failed call (e.g. too few arguments) must fail the expression, not leave an + // empty branch behind - for a void callee that silently dropped the call + EXIT_IF_FAILED(result) auto value = V(result); if (value) { diff --git a/tslang/lib/TypeScript/MLIRGenExpressions.cpp b/tslang/lib/TypeScript/MLIRGenExpressions.cpp index ab5f8d86f..6d19b54fc 100644 --- a/tslang/lib/TypeScript/MLIRGenExpressions.cpp +++ b/tslang/lib/TypeScript/MLIRGenExpressions.cpp @@ -1695,12 +1695,11 @@ namespace mlirgen } else { - auto classValue = builder.create( - location, classInfo->classType, - mlir::FlatSymbolRefAttr::get(builder.getContext(), classInfo->classType.getName().getValue())); - - // TODO: find out if you need to pass generics info, typeParams + typeArgs - return NewClassInstance(location, classValue, undefined, undefined, false, genContext); + // A class expression's value only has to carry the class (its type and vtable), so + // that `new X(...)`, casts to an interface and the like work on it. Allocate it and + // set the vtable, but do not run the constructor: that would execute user code at + // the class definition, with no arguments for its parameters. + return V(NewClassInstanceLogicAsOp(location, classInfo, false, genContext)); } } diff --git a/tslang/lib/TypeScript/MLIRGenImpl.h b/tslang/lib/TypeScript/MLIRGenImpl.h index 545b5ac31..ef34c6ec6 100644 --- a/tslang/lib/TypeScript/MLIRGenImpl.h +++ b/tslang/lib/TypeScript/MLIRGenImpl.h @@ -7161,13 +7161,19 @@ class MLIRGenImpl mlir::Value thisValue, SmallVector &operands, const GenContext &genContext) { + // `thisValue` is the one operand a call gets that the user did not write: the object of + // a method or constructor call (a bound function), the capture of a closure (also a + // bound function) or the receiver of an extension function. Callees reached without + // it take only what the user wrote. + auto hiddenOperandsCount = 0; if (thisValue) { operands.insert(operands.begin(), thisValue); + hiddenOperandsCount = 1; } if (mlir::failed(mlirGenPrepareCallOperands(location, operands, calledFuncType.getInputs(), calledFuncType.isVarArg(), - genContext))) + hiddenOperandsCount, genContext))) { return mlir::failure(); } @@ -7233,9 +7239,74 @@ class MLIRGenImpl return mlir::success(); } + // A call with fewer arguments than the callee needs is TypeScript's TS2554. Only a parameter + // whose type admits `undefined` itself may be left out - an optional (`b?: T`) or a + // default-valued (`b = 5`) one, both typed !ts.optional, and an `undefined` or `void` one. + // A rest parameter is never required. Any other missing parameter is an error; padding it + // with undef would hand the callee garbage. + // + // `operandsCount` and the indices of `argFuncTypes` include the `hiddenOperandsCount` + // leading operands the compiler inserted (`this`, a closure's capture); the message counts + // only what the user wrote. Too many arguments is not checked here. + mlir::LogicalResult checkCallArity(mlir::Location location, int operandsCount, mlir::ArrayRef argFuncTypes, + bool isVarArg, int hiddenOperandsCount) + { + auto isOmittable = [](mlir::Type type) { + return isa(type); + }; + + // the parameters before the rest parameter, if any + int paramsEnd = argFuncTypes.size() - (isVarArg ? 1 : 0); + + auto missingRequired = false; + for (auto i = operandsCount; i < paramsEnd; i++) + { + if (!isOmittable(argFuncTypes[i])) + { + missingRequired = true; + break; + } + } + + if (!missingRequired) + { + return mlir::success(); + } + + // as TypeScript counts it: every parameter up to the last one that can't be left out + auto minCount = 0; + for (auto i = hiddenOperandsCount; i < paramsEnd; i++) + { + if (!isOmittable(argFuncTypes[i])) + { + minCount = i + 1 - hiddenOperandsCount; + } + } + + auto maxCount = paramsEnd - hiddenOperandsCount; + auto gotCount = operandsCount - hiddenOperandsCount; + + auto diag = emitError(location) << "Expected "; + if (isVarArg) + { + diag << "at least " << minCount; + } + else if (minCount == maxCount) + { + diag << minCount; + } + else + { + diag << minCount << "-" << maxCount; + } + + diag << " arguments, but got " << gotCount << "."; + return mlir::failure(); + } + mlir::LogicalResult mlirGenPrepareCallOperands(mlir::Location location, SmallVector &operands, mlir::ArrayRef argFuncTypes, bool isVarArg, - const GenContext &genContext) + int hiddenOperandsCount, const GenContext &genContext) { int opArgsCount = operands.size(); int funcArgsCount = argFuncTypes.size(); @@ -7247,6 +7318,11 @@ class MLIRGenImpl if (funcArgsCount > opArgsCount) { + if (mlir::failed(checkCallArity(location, opArgsCount, argFuncTypes, isVarArg, hiddenOperandsCount))) + { + return mlir::failure(); + } + auto lastArgIndex = argFuncTypes.size() - 1; // -1 to exclude count params diff --git a/tslang/test/tester/CMakeLists.txt b/tslang/test/tester/CMakeLists.txt index 714dfd11d..b687c624c 100644 --- a/tslang/test/tester/CMakeLists.txt +++ b/tslang/test/tester/CMakeLists.txt @@ -441,6 +441,7 @@ tslang_add_test(NAME test-compile-00-class-recursive COMMAND test-runner "${PROJ tslang_add_test(NAME test-compile-00-class-expression COMMAND test-runner "${PROJECT_SOURCE_DIR}/test/tester/tests/00class_expression.ts") tslang_add_test(NAME test-compile-00-class-expression-2 COMMAND test-runner "${PROJECT_SOURCE_DIR}/test/tester/tests/00class_expression2.ts") tslang_add_test(NAME test-compile-00-class-expression-3 COMMAND test-runner "${PROJECT_SOURCE_DIR}/test/tester/tests/00class_expression3.ts") +tslang_add_test(NAME test-compile-00-class-expression-no-ctor COMMAND test-runner "${PROJECT_SOURCE_DIR}/test/tester/tests/00class_expression_no_ctor.ts") tslang_add_test(NAME test-compile-00-class-deconst COMMAND test-runner "${PROJECT_SOURCE_DIR}/test/tester/tests/00class_deconst.ts") tslang_add_test(NAME test-compile-00-class-virtual-call COMMAND test-runner "${PROJECT_SOURCE_DIR}/test/tester/tests/00class_virtual_call.ts") tslang_add_test(NAME test-compile-00-class-virtual-table COMMAND test-runner "${PROJECT_SOURCE_DIR}/test/tester/tests/00class_virtual_table.ts") @@ -862,6 +863,7 @@ tslang_add_test(NAME test-jit-00-class-recursive COMMAND test-runner -jit "${PRO tslang_add_test(NAME test-jit-00-class-expression COMMAND test-runner -jit "${PROJECT_SOURCE_DIR}/test/tester/tests/00class_expression.ts") tslang_add_test(NAME test-jit-00-class-expression-2 COMMAND test-runner -jit "${PROJECT_SOURCE_DIR}/test/tester/tests/00class_expression2.ts") tslang_add_test(NAME test-jit-00-class-expression-3 COMMAND test-runner -jit "${PROJECT_SOURCE_DIR}/test/tester/tests/00class_expression3.ts") +tslang_add_test(NAME test-jit-00-class-expression-no-ctor COMMAND test-runner -jit "${PROJECT_SOURCE_DIR}/test/tester/tests/00class_expression_no_ctor.ts") tslang_add_test(NAME test-jit-00-class-deconst COMMAND test-runner -jit "${PROJECT_SOURCE_DIR}/test/tester/tests/00class_deconst.ts") tslang_add_test(NAME test-jit-00-class-virtual-call COMMAND test-runner -jit "${PROJECT_SOURCE_DIR}/test/tester/tests/00class_virtual_call.ts") tslang_add_test(NAME test-jit-00-class-virtual-table COMMAND test-runner -jit "${PROJECT_SOURCE_DIR}/test/tester/tests/00class_virtual_table.ts") @@ -1503,6 +1505,7 @@ set(TSLANG_CORPUS 00class_expression.ts 00class_expression2.ts 00class_expression3.ts + 00class_expression_no_ctor.ts 00class_from_tuple.ts 00class_generic_iface.ts 00class_generic_method.ts @@ -2164,6 +2167,40 @@ foreach(ownership_shard RANGE ${ownership_last}) -P "${CMAKE_CURRENT_SOURCE_DIR}/verify-ownership.cmake") endforeach() +# A call with fewer arguments than the callee's required parameters is an error (TS2554), +# not padded with undef. Each file in call-arity/ makes one bad call; the test passes when +# the compiler prints the expected message. The counts are what the user wrote, so `this` +# (a method, a constructor) and a closure's capture are not counted. Plain add_test: these +# compile nothing, so they need no x86 twin. +set(call_arity_cases + "plain|Expected 2 arguments, but got 1\\." + "optional|Expected 2\\-3 arguments, but got 1\\." + "method|Expected 2 arguments, but got 1\\." + "constructor|Expected 2 arguments, but got 1\\." + "closure|Expected 2 arguments, but got 1\\." + "declared|Expected 2 arguments, but got 1\\." + "varargs|Expected at least 1 arguments, but got 0\\." + "optional_call|Expected 2 arguments, but got 1\\.") +foreach(call_arity_case ${call_arity_cases}) + string(FIND "${call_arity_case}" "|" call_arity_sep) + string(SUBSTRING "${call_arity_case}" 0 ${call_arity_sep} call_arity_name) + math(EXPR call_arity_sep "${call_arity_sep} + 1") + string(SUBSTRING "${call_arity_case}" ${call_arity_sep} -1 call_arity_message) + add_test(NAME test-compile-call-arity-${call_arity_name} + COMMAND $ --emit=obj --no-default-lib -mm=none + "${PROJECT_SOURCE_DIR}/test/tester/call-arity/${call_arity_name}.ts" + -o "${CMAKE_CURRENT_BINARY_DIR}/call-arity-${call_arity_name}.obj") + # PASS_REGULAR_EXPRESSION alone ignores the exit code, so a crash after printing the + # expected message would still pass; FAIL_REGULAR_EXPRESSION catches that. + set_tests_properties(test-compile-call-arity-${call_arity_name} + PROPERTIES PASS_REGULAR_EXPRESSION "${call_arity_message}" + FAIL_REGULAR_EXPRESSION "Stack dump|Assertion failed") +endforeach() + +# The calls the check must still accept: an omitted optional, default-valued or void +# parameter, and varargs with and without extra arguments. +tslang_add_test(NAME test-compile-call-arity-positive COMMAND test-runner "${PROJECT_SOURCE_DIR}/test/tester/call-arity/positive.ts") + # The shared-component tier under the other two models. A shared library records the model # it was built under, so both halves of a pair are built with the same flag - which is what # these run. The file pairs are the default model's, verbatim. diff --git a/tslang/test/tester/call-arity/closure.ts b/tslang/test/tester/call-arity/closure.ts new file mode 100644 index 000000000..83e6c5c75 --- /dev/null +++ b/tslang/test/tester/call-arity/closure.ts @@ -0,0 +1,5 @@ +function main() { + let captured = 10; + const add = (a: number, b: number) => a + b + captured; + print(add(1)); +} diff --git a/tslang/test/tester/call-arity/constructor.ts b/tslang/test/tester/call-arity/constructor.ts new file mode 100644 index 000000000..33a6d8685 --- /dev/null +++ b/tslang/test/tester/call-arity/constructor.ts @@ -0,0 +1,8 @@ +class C { + constructor(public a: number, public b: number) {} +} + +function main() { + const c = new C(1); + print(c.a); +} diff --git a/tslang/test/tester/call-arity/declared.ts b/tslang/test/tester/call-arity/declared.ts new file mode 100644 index 000000000..fe9385ba6 --- /dev/null +++ b/tslang/test/tester/call-arity/declared.ts @@ -0,0 +1,5 @@ +declare function nat(a: string, b: string): void; + +function main() { + nat("x"); +} diff --git a/tslang/test/tester/call-arity/method.ts b/tslang/test/tester/call-arity/method.ts new file mode 100644 index 000000000..65c5b1ed9 --- /dev/null +++ b/tslang/test/tester/call-arity/method.ts @@ -0,0 +1,10 @@ +class O { + m(a: number, b: number) { + return a + b; + } +} + +function main() { + const o = new O(); + print(o.m(1)); +} diff --git a/tslang/test/tester/call-arity/optional.ts b/tslang/test/tester/call-arity/optional.ts new file mode 100644 index 000000000..4767ae930 --- /dev/null +++ b/tslang/test/tester/call-arity/optional.ts @@ -0,0 +1,7 @@ +function opt(a: number, b: number, c?: number) { + return a + b; +} + +function main() { + print(opt(1)); +} diff --git a/tslang/test/tester/call-arity/optional_call.ts b/tslang/test/tester/call-arity/optional_call.ts new file mode 100644 index 000000000..0b48a3b80 --- /dev/null +++ b/tslang/test/tester/call-arity/optional_call.ts @@ -0,0 +1,4 @@ +function main() { + let f: ((a: number, b: number) => void) | undefined = (a: number, b: number) => { print(a + b); }; + f?.(1); +} diff --git a/tslang/test/tester/call-arity/plain.ts b/tslang/test/tester/call-arity/plain.ts new file mode 100644 index 000000000..ad2d25b8e --- /dev/null +++ b/tslang/test/tester/call-arity/plain.ts @@ -0,0 +1,7 @@ +function req(a: number, b: number) { + return a + b; +} + +function main() { + print(req(1)); +} diff --git a/tslang/test/tester/call-arity/positive.ts b/tslang/test/tester/call-arity/positive.ts new file mode 100644 index 000000000..9174120bf --- /dev/null +++ b/tslang/test/tester/call-arity/positive.ts @@ -0,0 +1,27 @@ +function opt(a: number, b: number, c?: number) { + return c === undefined ? a + b : a + b + c; +} + +function def(a: number, b = 5) { + return a + b; +} + +function va(a: number, ...rest: number[]) { + return a + rest.length; +} + +// tslang additionally treats an `undefined` parameter as omittable; tsc does not (a call +// omitting `b: undefined` is TS2554). tsc's own omittable case is a `void` parameter, but +// that cannot be written in tslang: it rejects `void` as a parameter type. +function withUndefined(a: number, u: undefined) { + return a; +} + +function main() { + assert(opt(1, 2) == 3); + assert(def(1) == 6); + assert(va(1) == 1); + assert(va(1, 2, 3) == 3); + assert(withUndefined(7) == 7); + print("done."); +} diff --git a/tslang/test/tester/call-arity/varargs.ts b/tslang/test/tester/call-arity/varargs.ts new file mode 100644 index 000000000..37f9f02fa --- /dev/null +++ b/tslang/test/tester/call-arity/varargs.ts @@ -0,0 +1,7 @@ +function va(a: number, ...rest: number[]) { + return a + rest.length; +} + +function main() { + print(va()); +} diff --git a/tslang/test/tester/tests/00class_expression_no_ctor.ts b/tslang/test/tester/tests/00class_expression_no_ctor.ts new file mode 100644 index 000000000..c3fafd01a --- /dev/null +++ b/tslang/test/tester/tests/00class_expression_no_ctor.ts @@ -0,0 +1,13 @@ +let ctorCalls = 0; + +let Point = class { + constructor(public x: number, public y: number) { ctorCalls++; } +}; + +function main() { + assert(ctorCalls == 0, "class expression must not call the constructor"); + const p = new Point(3, 4); + assert(ctorCalls == 1); + assert(p.x == 3 && p.y == 4); + print("done."); +} diff --git a/tslang/test/tester/tests/00funcs_generic_with_typeof.ts b/tslang/test/tester/tests/00funcs_generic_with_typeof.ts index 03dce9e73..c8046b370 100644 --- a/tslang/test/tester/tests/00funcs_generic_with_typeof.ts +++ b/tslang/test/tester/tests/00funcs_generic_with_typeof.ts @@ -17,7 +17,7 @@ const v2 = gen(null); assert(v2 == "no") print(v2); -const v3 = gen(); +const v3 = gen(undefined); assert(v3 == "no") print(v3); diff --git a/tslang/test/tester/tests/01types_utility.ts b/tslang/test/tester/tests/01types_utility.ts index 35bad8cc6..c78d68db7 100644 --- a/tslang/test/tester/tests/01types_utility.ts +++ b/tslang/test/tester/tests/01types_utility.ts @@ -175,7 +175,7 @@ function main() { assert(aC[1] === 50); // instance - let aI: TI0 = new S(); + let aI: TI0 = new S("s", 1); let bI: TI1; // ThisParameterType