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."); +} 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)