From 80067dc78da42d21a1922590a71d032709bfcea8 Mon Sep 17 00:00:00 2001 From: ASDAlexander77 Date: Fri, 13 Nov 2026 00:37:12 +0000 Subject: [PATCH 1/2] A generic function's instance calls itself with its declared return type `function countDown(value: T, n: number): number { ... return countDown(value, n - 1) + 1; }` failed with "can't instantiate function. 'countDown' not all generic types can be identified": an instance's type went into the function type map only after its body was discovered, and the body's call of itself found no type, so its result had nothing to add to. A call whose result was not used compiled. A function at the top level is registered by the pass over the module's declarations before its body is discovered; an instance is made only when it is called. Now an instance whose return type is declared is registered with it before its body is discovered. One whose return type is found from the body still can't call itself (TypeScript gives such a function `any`); that now says "'countUp' calls itself, and its return type is found from its body: declare its return type". Tests: 00generic_self_recursion.ts (a call of itself, with a type argument, a generic method, two calling each other, returning T and T[]; gc, own, rc, none, compile and JIT) and generic_self_recursion_untyped_error.ts. Co-Authored-By: Claude Opus 5.5 --- .../TypeScript/MLIRLogic/MLIRGenStore.h | 2 + tslang/lib/TypeScript/MLIRGenFunctions.cpp | 11 ++++ tslang/lib/TypeScript/MLIRGenGenerics.cpp | 13 +++++ tslang/test/tester/CMakeLists.txt | 10 ++++ .../tester/tests/00generic_self_recursion.ts | 54 +++++++++++++++++++ .../generic_self_recursion_untyped_error.ts | 16 ++++++ 6 files changed, 106 insertions(+) create mode 100644 tslang/test/tester/tests/00generic_self_recursion.ts create mode 100644 tslang/test/tester/tests/generic_self_recursion_untyped_error.ts diff --git a/tslang/include/TypeScript/MLIRLogic/MLIRGenStore.h b/tslang/include/TypeScript/MLIRLogic/MLIRGenStore.h index 378868c08..58aef52ff 100644 --- a/tslang/include/TypeScript/MLIRLogic/MLIRGenStore.h +++ b/tslang/include/TypeScript/MLIRLogic/MLIRGenStore.h @@ -48,6 +48,8 @@ struct GenericFunctionInfo // the instances being generated, by full name: a call of one of them from its own body (or from an // instance it calls) is recursion and takes its type; any other instance can still be made then llvm::StringSet<> processing; + // instances called from their own bodies before their types were known (a return type found from the body) + llvm::StringSet<> calledBeforeTyped; bool processed = false; // declared inside a function, whose locals its body may read; otherwise an instantiation sees no diff --git a/tslang/lib/TypeScript/MLIRGenFunctions.cpp b/tslang/lib/TypeScript/MLIRGenFunctions.cpp index 3206835e5..f376e31c9 100644 --- a/tslang/lib/TypeScript/MLIRGenFunctions.cpp +++ b/tslang/lib/TypeScript/MLIRGenFunctions.cpp @@ -577,6 +577,17 @@ namespace mlirgen // seems we need to discover it all the time due to captured vars auto detectReturnType = (!funcType || funcProtoGenContext.forceDiscover || !functionDiscovered) && !funcProto->getIsGeneric(); + // An instance of a generic function (countDown) whose return type is declared: its type is known + // before its body is, and a call of itself in the body (`return countDown(value, n - 1) + 1`) takes it + // from here (instantiateSpecializedFunction). A function at the top level was registered by the pass + // over the module's declarations before its body was discovered; an instance is made only when called. + auto isGenericInstance = + functionLikeDeclarationBaseAST->typeParameters.size() > 0 && !genContext.typeParamsWithArgs.empty(); + if (detectReturnType && isGenericInstance && funcType && !functionDiscovered) + { + getFunctionTypeMap().insert({fullName, funcType}); + } + if (detectReturnType) { // register function to be able to call it if used in recursive call diff --git a/tslang/lib/TypeScript/MLIRGenGenerics.cpp b/tslang/lib/TypeScript/MLIRGenGenerics.cpp index 25a93dc6d..19db25078 100644 --- a/tslang/lib/TypeScript/MLIRGenGenerics.cpp +++ b/tslang/lib/TypeScript/MLIRGenGenerics.cpp @@ -1256,6 +1256,8 @@ namespace mlirgen return {mlir::success(), funcType, fullName}; } + // its return type is found from its body, which calls it: said below if that fails + functionGenericTypeInfo->calledBeforeTyped.insert(fullName); if (instantiateGenContext.allowPartialResolve) { return {mlir::success(), mlir_ts::FunctionType(), fullName}; @@ -1280,6 +1282,17 @@ namespace mlirgen functionGenericTypeInfo->processing.erase(fullName); if (mlir::failed(result)) { + // An instance whose return type is declared is called from its own body with that type + // (mlirGenFunctionPrototype). One whose return type is found from its body could not be: + // the body needed the result of a call of itself first. + if (functionGenericTypeInfo->calledBeforeTyped.contains(fullName) + && !functionGenericTypeInfo->functionDeclaration->type) + { + emitError(location) << "'" << functionGenericTypeInfo->name + << "' calls itself, and its return type is found from its body: " + "declare its return type"; + } + return {mlir::failure(), mlir_ts::FunctionType(), ""}; } diff --git a/tslang/test/tester/CMakeLists.txt b/tslang/test/tester/CMakeLists.txt index a4be444f0..d04075238 100644 --- a/tslang/test/tester/CMakeLists.txt +++ b/tslang/test/tester/CMakeLists.txt @@ -1926,6 +1926,11 @@ add_test(NAME test-compile-ref-element-layout-error set_tests_properties(test-compile-ref-element-layout-error PROPERTIES PASS_REGULAR_EXPRESSION "a reference to number can't be used as a reference to f32" FAIL_REGULAR_EXPRESSION "Stack dump|Assertion failed") # ...a string given to a number, or a number to a string, is an error (TS2322) +# a generic instance calling itself with its return type found from its body: the error says to declare it +add_test(NAME test-compile-generic-self-recursion-untyped-error + COMMAND test-runner "${PROJECT_SOURCE_DIR}/test/tester/tests/generic_self_recursion_untyped_error.ts") +set_tests_properties(test-compile-generic-self-recursion-untyped-error PROPERTIES PASS_REGULAR_EXPRESSION "'countUp' calls itself, and its return type is found from its body: declare its return type" + FAIL_REGULAR_EXPRESSION "Stack dump|Assertion failed") add_test(NAME test-compile-assign-string-to-number-error COMMAND test-runner "${PROJECT_SOURCE_DIR}/test/tester/tests/assign_string_to_number_error.ts") set_tests_properties(test-compile-assign-string-to-number-error PROPERTIES PASS_REGULAR_EXPRESSION "type '\"x\"' is not assignable to type 'number'" @@ -2519,6 +2524,7 @@ set(TSLANG_CORPUS 00callback_result_ignored.ts 00generic_default_no_inference.ts 00generic_instance_from_instance.ts + 00generic_self_recursion.ts 00promise_type.ts 00class_deconst.ts 00class_def_constr.ts @@ -4847,6 +4853,10 @@ tslang_add_test(NAME test-compile-00-generic-instance-from-instance COMMAND test tslang_add_test(NAME test-compile-own-00-generic-instance-from-instance COMMAND test-runner -mm=own "${PROJECT_SOURCE_DIR}/test/tester/tests/00generic_instance_from_instance.ts") tslang_add_test(NAME test-jit-00-generic-instance-from-instance COMMAND test-runner -jit "${PROJECT_SOURCE_DIR}/test/tester/tests/00generic_instance_from_instance.ts") tslang_add_test(NAME test-jit-own-00-generic-instance-from-instance COMMAND test-runner -jit -mm=own "${PROJECT_SOURCE_DIR}/test/tester/tests/00generic_instance_from_instance.ts") +tslang_add_test(NAME test-compile-00-generic-self-recursion COMMAND test-runner "${PROJECT_SOURCE_DIR}/test/tester/tests/00generic_self_recursion.ts") +tslang_add_test(NAME test-compile-own-00-generic-self-recursion COMMAND test-runner -mm=own "${PROJECT_SOURCE_DIR}/test/tester/tests/00generic_self_recursion.ts") +tslang_add_test(NAME test-jit-00-generic-self-recursion COMMAND test-runner -jit "${PROJECT_SOURCE_DIR}/test/tester/tests/00generic_self_recursion.ts") +tslang_add_test(NAME test-jit-own-00-generic-self-recursion COMMAND test-runner -jit -mm=own "${PROJECT_SOURCE_DIR}/test/tester/tests/00generic_self_recursion.ts") # Shared (spec 23): counted handles. These are not in the own_test list, whose # test-own-no-counting check forbids __tslang_inc_ref: a handle is counted under own by design. diff --git a/tslang/test/tester/tests/00generic_self_recursion.ts b/tslang/test/tester/tests/00generic_self_recursion.ts new file mode 100644 index 000000000..6d7584697 --- /dev/null +++ b/tslang/test/tester/tests/00generic_self_recursion.ts @@ -0,0 +1,54 @@ +// An instance of a generic function calls itself: countDown from its own body. Its declared +// return type is its type before its body is known, so the call has a result to use. It failed with +// "not all generic types can be identified" - only a call whose result was not used compiled. + +function countDown(value: T, n: number): number { + if (n > 0) { + return countDown(value, n - 1) + 1; + } + + return 0; +} + +function countDownTyped(value: T, n: number): number { + return n > 0 ? countDownTyped(value, n - 1) + 1 : 0; +} + +class Counter { + count(value: T, n: number): number { + return n > 0 ? this.count(value, n - 1) + 1 : 0; + } +} + +function isEven(value: T, n: number): boolean { + return n == 0 ? true : isOdd(value, n - 1); +} + +function isOdd(value: T, n: number): boolean { + return n == 0 ? false : isEven(value, n - 1); +} + +function last(items: T[], i: number): T { + return i + 1 < items.length ? last(items, i + 1) : items[i]; +} + +function reversed(items: T[], i: number): T[] { + if (i < 0) { + return []; + } + + const rest = reversed(items, i - 1); + return [items[i], ...rest]; +} + +function main() { + assert(countDown("x", 5) == 5, "a call of itself, its result used"); + assert(countDown(1, 2) == 2, "another instance"); + assert(countDownTyped(true, 3) == 3, "with its type argument"); + assert(new Counter().count("x", 4) == 4, "a generic method"); + assert(isEven(1, 10) && isOdd("a", 7), "two that call each other"); + assert(last([1, 2, 3], 0) == 3 && last(["a", "b"], 0) == "b", "returning T"); + const back = reversed([1, 2, 3], 2); + assert(back.length == 3 && back[0] == 3 && back[2] == 1, "returning T[]"); + print("done."); +} diff --git a/tslang/test/tester/tests/generic_self_recursion_untyped_error.ts b/tslang/test/tester/tests/generic_self_recursion_untyped_error.ts new file mode 100644 index 000000000..5f694556c --- /dev/null +++ b/tslang/test/tester/tests/generic_self_recursion_untyped_error.ts @@ -0,0 +1,16 @@ +// An instance of a generic function calling itself needs its return type declared: it is found from the +// body otherwise, and the body needs the result of the call first. Said as that, not as "not all generic +// types can be identified". + +function countUp(value: T, n: number) { + if (n > 0) { + return countUp(value, n - 1) + 1; + } + + return 0; +} + +function main() { + print(countUp("x", 3)); + print("done."); +} From d149ba914f369b1156d11d2905fdc138adfad49b Mon Sep 17 00:00:00 2001 From: ASDAlexander77 Date: Fri, 13 Nov 2026 00:48:07 +0000 Subject: [PATCH 2/2] verify-ownership: generic_self_recursion_untyped_error.ts is an error by design Co-Authored-By: Claude Opus 5.5 --- tslang/test/tester/verify-ownership.cmake | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/tslang/test/tester/verify-ownership.cmake b/tslang/test/tester/verify-ownership.cmake index c93991caa..85eb4ea27 100644 --- a/tslang/test/tester/verify-ownership.cmake +++ b/tslang/test/tester/verify-ownership.cmake @@ -69,7 +69,9 @@ set(not_compilable_alone # an error by design (test-compile-cdecl-closure-error) cdecl_closure_error.ts # an error by design (test-compile-computed-name-failed-error) - computed_name_failed_error.ts) + computed_name_failed_error.ts + # an error by design (test-compile-generic-self-recursion-untyped-error) + generic_self_recursion_untyped_error.ts) file(GLOB corpus "${TESTS_DIR}/*.ts") list(SORT corpus)