Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions tslang/include/TypeScript/MLIRLogic/MLIRGenStore.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
11 changes: 11 additions & 0 deletions tslang/lib/TypeScript/MLIRGenFunctions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<string>) 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
Expand Down
13 changes: 13 additions & 0 deletions tslang/lib/TypeScript/MLIRGenGenerics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand All @@ -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(), ""};
}

Expand Down
10 changes: 10 additions & 0 deletions tslang/test/tester/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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'"
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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<T> (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.
Expand Down
54 changes: 54 additions & 0 deletions tslang/test/tester/tests/00generic_self_recursion.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
// An instance of a generic function calls itself: countDown<string> 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<T>(value: T, n: number): number {
if (n > 0) {
return countDown(value, n - 1) + 1;
}

return 0;
}

function countDownTyped<T>(value: T, n: number): number {
return n > 0 ? countDownTyped<T>(value, n - 1) + 1 : 0;
}

class Counter {
count<T>(value: T, n: number): number {
return n > 0 ? this.count(value, n - 1) + 1 : 0;
}
}

function isEven<T>(value: T, n: number): boolean {
return n == 0 ? true : isOdd(value, n - 1);
}

function isOdd<T>(value: T, n: number): boolean {
return n == 0 ? false : isEven(value, n - 1);
}

function last<T>(items: T[], i: number): T {
return i + 1 < items.length ? last(items, i + 1) : items[i];
}

function reversed<T>(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.");
}
16 changes: 16 additions & 0 deletions tslang/test/tester/tests/generic_self_recursion_untyped_error.ts
Original file line number Diff line number Diff line change
@@ -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<T>(value: T, n: number) {
if (n > 0) {
return countUp(value, n - 1) + 1;
}

return 0;
}

function main() {
print(countUp("x", 3));
print("done.");
}
4 changes: 3 additions & 1 deletion tslang/test/tester/verify-ownership.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Loading