From 4fb861c6dc5d769dfc217e5079cda5d0003b9deb Mon Sep 17 00:00:00 2001 From: ASDAlexander77 Date: Thu, 29 Oct 2026 14:08:14 +0000 Subject: [PATCH] An arrow that captures can take fewer parameters than its function type `apply((a: number) => a + k)` for `apply(f: (a: number, b: number) => number)` was "it takes 2 argument(s), 2 expected": the arrow's captures counted as a parameter, and #733's thunk covered only named functions and arrows that capture nothing. fewerParametersThunk also takes a closure (ts.CreateBoundFunction with __owns_capture over a symbol): the thunk takes the captures first, then every parameter of the type, and calls the arrow with the captures and the first ones. The closure is rebound to the thunk in place, so what owns its captures (rc's retain and release, own's box) is unchanged. Test: 00closure_fewer_parameters (compile and JIT; gc, rc, none, own). Co-Authored-By: Claude Opus 5.5 --- tslang/lib/TypeScript/MLIRGenCast.cpp | 36 +++++++++++++--- tslang/test/tester/CMakeLists.txt | 3 ++ .../tests/00closure_fewer_parameters.ts | 43 +++++++++++++++++++ 3 files changed, 76 insertions(+), 6 deletions(-) create mode 100644 tslang/test/tester/tests/00closure_fewer_parameters.ts diff --git a/tslang/lib/TypeScript/MLIRGenCast.cpp b/tslang/lib/TypeScript/MLIRGenCast.cpp index bc160e1fe..1018d4a89 100644 --- a/tslang/lib/TypeScript/MLIRGenCast.cpp +++ b/tslang/lib/TypeScript/MLIRGenCast.cpp @@ -2083,14 +2083,20 @@ namespace mlirgen // `(a: number, b: number) => number` and `one` takes `a` only): TypeScript lets it ignore the rest. A named // function or an arrow that captures nothing is given as a function of its own that takes them all and calls // it with the first ones - called with every argument the type says, it would read them where it does not - // expect them. Null where it is not that (a closure, a bound method, parameters or results that differ). + // expect them. An arrow that captures is the same, bound over its captures: the function of its own takes the + // captures first, as the arrow does, and the closure is bound to it instead. Null where it is not that (a bound + // method, parameters or results that differ). mlir::Value MLIRGenImpl::fewerParametersThunk(mlir::Location location, mlir::Value value, mlir::Type type) { - auto symbolRefOp = value.getDefiningOp(); - auto inFuncType = dyn_cast(value.getType()); + auto boundOp = value.getDefiningOp(); + auto symbolRefOp = boundOp ? boundOp.getFunc().getDefiningOp() + : value.getDefiningOp(); + auto inFuncType = symbolRefOp ? dyn_cast(symbolRefOp.getType()) : mlir_ts::FunctionType(); auto resFuncType = mth.GetFunctionType(type); + // a closure's own value is its function bound over the captures (a method's is bound to its object) + auto closure = boundOp && boundOp->hasAttr(OWNS_CAPTURE_ATTR_NAME); if (!symbolRefOp || !inFuncType || !resFuncType || inFuncType.isVarArg() || inFuncType.isCVarArg() - || resFuncType.isVarArg() || resFuncType.isCVarArg()) + || resFuncType.isVarArg() || resFuncType.isCVarArg() || (boundOp && (!closure || inFuncType.getInputs().empty()))) { return mlir::Value(); } @@ -2101,11 +2107,17 @@ namespace mlirgen }; auto inInputs = inFuncType.getInputs(); - auto resInputs = resFuncType.getInputs(); + SmallVector resInputs; + if (boundOp) + { + resInputs.push_back(inInputs.front()); + } + + resInputs.append(resFuncType.getInputs().begin(), resFuncType.getInputs().end()); auto sameResults = inFuncType.getResults() == resFuncType.getResults() || (isVoid(inFuncType.getResults()) && isVoid(resFuncType.getResults())); if (inInputs.size() >= resInputs.size() || !sameResults - || !llvm::equal(inInputs, resInputs.take_front(inInputs.size()))) + || !llvm::equal(inInputs, llvm::ArrayRef(resInputs).take_front(inInputs.size()))) { return mlir::Value(); } @@ -2145,6 +2157,18 @@ namespace mlirgen theModule.push_back(funcOp); } + if (boundOp) + { + // the closure itself is bound to it, keeping what owns its captures (its retain and release) + mlir::OpBuilder::InsertionGuard guard(builder); + builder.setInsertionPoint(boundOp); + auto thunkRef = builder.create( + location, thunkType, mlir::FlatSymbolRefAttr::get(builder.getContext(), thunkName)); + boundOp.getFuncMutable().assign(thunkRef); + boundOp.getResult().setType(mlir_ts::BoundFunctionType::get(builder.getContext(), thunkType)); + return boundOp.getResult(); + } + return builder.create(location, thunkType, mlir::FlatSymbolRefAttr::get(builder.getContext(), thunkName)); } diff --git a/tslang/test/tester/CMakeLists.txt b/tslang/test/tester/CMakeLists.txt index 1874676cd..edd4fdb44 100644 --- a/tslang/test/tester/CMakeLists.txt +++ b/tslang/test/tester/CMakeLists.txt @@ -739,6 +739,7 @@ tslang_add_test(NAME test-compile-00-try-catch-return COMMAND test-runner "${PRO tslang_add_test(NAME test-compile-00-try-finally COMMAND test-runner "${PROJECT_SOURCE_DIR}/test/tester/tests/00try_finally.ts") tslang_add_test(NAME test-compile-00-try-finally-throws COMMAND test-runner "${PROJECT_SOURCE_DIR}/test/tester/tests/00try_finally_throws.ts") tslang_add_test(NAME test-compile-00-function-fewer-parameters COMMAND test-runner "${PROJECT_SOURCE_DIR}/test/tester/tests/00function_fewer_parameters.ts") +tslang_add_test(NAME test-compile-00-closure-fewer-parameters COMMAND test-runner "${PROJECT_SOURCE_DIR}/test/tester/tests/00closure_fewer_parameters.ts") tslang_add_test(NAME test-compile-01-try-finally COMMAND test-runner "${PROJECT_SOURCE_DIR}/test/tester/tests/01try_finally.ts") tslang_add_test(NAME test-compile-00-try-finally-return COMMAND test-runner "${PROJECT_SOURCE_DIR}/test/tester/tests/00try_finally_return.ts") tslang_add_test(NAME test-compile-00-try-finally-funclet COMMAND test-runner "${PROJECT_SOURCE_DIR}/test/tester/tests/00try_finally_funclet.ts") @@ -1366,6 +1367,7 @@ tslang_add_test(NAME test-jit-00-try-catch-return COMMAND test-runner -jit "${PR tslang_add_test(NAME test-jit-00-try-finally COMMAND test-runner -jit "${PROJECT_SOURCE_DIR}/test/tester/tests/00try_finally.ts") tslang_add_test(NAME test-jit-00-try-finally-throws COMMAND test-runner -jit "${PROJECT_SOURCE_DIR}/test/tester/tests/00try_finally_throws.ts") tslang_add_test(NAME test-jit-00-function-fewer-parameters COMMAND test-runner -jit "${PROJECT_SOURCE_DIR}/test/tester/tests/00function_fewer_parameters.ts") +tslang_add_test(NAME test-jit-00-closure-fewer-parameters COMMAND test-runner -jit "${PROJECT_SOURCE_DIR}/test/tester/tests/00closure_fewer_parameters.ts") tslang_add_test(NAME test-jit-01-try-finally COMMAND test-runner -jit "${PROJECT_SOURCE_DIR}/test/tester/tests/01try_finally.ts") tslang_add_test(NAME test-jit-00-try-finally-return COMMAND test-runner -jit "${PROJECT_SOURCE_DIR}/test/tester/tests/00try_finally_return.ts") tslang_add_test(NAME test-jit-00-try-finally-funclet COMMAND test-runner -jit "${PROJECT_SOURCE_DIR}/test/tester/tests/00try_finally_funclet.ts") @@ -2487,6 +2489,7 @@ set(TSLANG_CORPUS 00try_finally.ts 00try_finally_throws.ts 00function_fewer_parameters.ts + 00closure_fewer_parameters.ts 00try_using_catch.ts 00tuple_named.ts 00tuple_with_array.ts diff --git a/tslang/test/tester/tests/00closure_fewer_parameters.ts b/tslang/test/tester/tests/00closure_fewer_parameters.ts new file mode 100644 index 000000000..c936e6dd9 --- /dev/null +++ b/tslang/test/tester/tests/00closure_fewer_parameters.ts @@ -0,0 +1,43 @@ +// An arrow that captures, taking fewer parameters than the function type it is given as, ignores the rest: it +// is bound over its captures to a function that takes them all and calls it with the first ones. It was an +// error, "it takes 2 argument(s), 2 expected" (the captures counted as a parameter). +class Box { + constructor(public n: number) {} +} + +function each(names: string[], visit: (name: string, index: number) => void) { + for (let i = 0; i < names.length; i++) { + visit(names[i], i); + } +} + +function apply(f: (a: number, b: number) => number) { + return f(2, 3); +} + +function churn() { + const junk: Box[] = []; + for (let i = 0; i < 64; i++) junk.push(new Box(999)); +} + +function main() { + const k: number = 10; + assert(apply((a: number) => a + k) == 12); + + let count = 0; + each(["ab", "c"], (name: string) => { count += name.length; }); + assert(count == 3); + + const b = new Box(4); + const f = (a: number) => a * b.n; + assert(apply(f) == 8); + churn(); + assert(apply(f) == 8); + assert(b.n == 4); + + let calls = 0; + each(["x", "y", "z"], () => { calls++; }); + assert(calls == 3); + + print("done."); +}