From 89cba595b41d424195939e5fb3c5ce288912e702 Mon Sep 17 00:00:00 2001 From: ASDAlexander77 Date: Fri, 30 Oct 2026 00:02:23 +0000 Subject: [PATCH 1/2] An arrow reading a get accessor declared later is typed by it, not as returning nothing `fn = () => this.prop` with `get prop()` failed in every model with "can't find return variable, seems your function type has 'void' return type", and so did `const f = () => this.prop` in a method or constructor declared before the accessor (a method's with only "failed statement"). A concise arrow body that reads something not resolved yet - the accessor isn't registered until its own turn - had no value in the discovery pass, and was taken as a body returning nothing: the arrow was typed `() => void` for good. Now, as a return statement's expression does, a property, element or name read with no value in a discovery pass fails it, and the member is asked again once the accessor is declared. A call can still return nothing. 00class_arrow_getter: a field's arrow, one in the constructor and in methods declared before the getter, a getter with its type given, and an arrow calling a setter. Co-Authored-By: Claude Opus 5.5 --- tslang/lib/TypeScript/MLIRGenStatements.cpp | 18 ++++++ tslang/test/tester/CMakeLists.txt | 2 + .../test/tester/tests/00class_arrow_getter.ts | 62 +++++++++++++++++++ 3 files changed, 82 insertions(+) create mode 100644 tslang/test/tester/tests/00class_arrow_getter.ts diff --git a/tslang/lib/TypeScript/MLIRGenStatements.cpp b/tslang/lib/TypeScript/MLIRGenStatements.cpp index 3d1118727..7dd888c7d 100644 --- a/tslang/lib/TypeScript/MLIRGenStatements.cpp +++ b/tslang/lib/TypeScript/MLIRGenStatements.cpp @@ -39,6 +39,24 @@ namespace mlirgen auto result = mlirGen(body.as(), genContext); EXIT_IF_FAILED(result) auto resultValue = V(result); + // a read with no value in a discovery pass is one not resolved yet (`() => this.prop`, a get + // accessor declared later): asked again, as the return statement's is, not taken as a body + // returning nothing. A call can return nothing. + if (!resultValue && genContext.allowPartialResolve && !genContext.discardReturnValue) + { + auto expression = body.as(); + while (expression == SyntaxKind::ParenthesizedExpression) + { + expression = expression.as()->expression; + } + + if (expression == SyntaxKind::PropertyAccessExpression || expression == SyntaxKind::ElementAccessExpression || + expression == SyntaxKind::Identifier) + { + return mlir::failure(); + } + } + if (resultValue && !genContext.discardReturnValue) { // A concise arrow body (`() => expr`) returns without going through the return diff --git a/tslang/test/tester/CMakeLists.txt b/tslang/test/tester/CMakeLists.txt index 6d0cf0ed4..1125dc8e5 100644 --- a/tslang/test/tester/CMakeLists.txt +++ b/tslang/test/tester/CMakeLists.txt @@ -743,6 +743,7 @@ tslang_add_test(NAME test-compile-00-closure-fewer-parameters COMMAND test-runne tslang_add_test(NAME test-compile-00-in-interface-union COMMAND test-runner "${PROJECT_SOURCE_DIR}/test/tester/tests/00in_interface_union.ts") tslang_add_test(NAME test-compile-00-class-values COMMAND test-runner "${PROJECT_SOURCE_DIR}/test/tester/tests/00class_values.ts") tslang_add_test(NAME test-compile-00-class-arrow-fields COMMAND test-runner "${PROJECT_SOURCE_DIR}/test/tester/tests/00class_arrow_fields.ts") +tslang_add_test(NAME test-compile-00-class-arrow-getter COMMAND test-runner "${PROJECT_SOURCE_DIR}/test/tester/tests/00class_arrow_getter.ts") tslang_add_test(NAME test-compile-00-interface-generic-method COMMAND test-runner "${PROJECT_SOURCE_DIR}/test/tester/tests/00interface_generic_method.ts") tslang_add_test(NAME test-compile-00-interface-union-literal COMMAND test-runner "${PROJECT_SOURCE_DIR}/test/tester/tests/00interface_union_literal.ts") tslang_add_test(NAME test-compile-01-try-finally COMMAND test-runner "${PROJECT_SOURCE_DIR}/test/tester/tests/01try_finally.ts") @@ -1376,6 +1377,7 @@ tslang_add_test(NAME test-jit-00-closure-fewer-parameters COMMAND test-runner -j tslang_add_test(NAME test-jit-00-in-interface-union COMMAND test-runner -jit "${PROJECT_SOURCE_DIR}/test/tester/tests/00in_interface_union.ts") tslang_add_test(NAME test-jit-00-class-values COMMAND test-runner -jit "${PROJECT_SOURCE_DIR}/test/tester/tests/00class_values.ts") tslang_add_test(NAME test-jit-00-class-arrow-fields COMMAND test-runner -jit "${PROJECT_SOURCE_DIR}/test/tester/tests/00class_arrow_fields.ts") +tslang_add_test(NAME test-jit-00-class-arrow-getter COMMAND test-runner -jit "${PROJECT_SOURCE_DIR}/test/tester/tests/00class_arrow_getter.ts") tslang_add_test(NAME test-jit-00-interface-generic-method COMMAND test-runner -jit "${PROJECT_SOURCE_DIR}/test/tester/tests/00interface_generic_method.ts") tslang_add_test(NAME test-jit-00-interface-union-literal COMMAND test-runner -jit "${PROJECT_SOURCE_DIR}/test/tester/tests/00interface_union_literal.ts") tslang_add_test(NAME test-jit-01-try-finally COMMAND test-runner -jit "${PROJECT_SOURCE_DIR}/test/tester/tests/01try_finally.ts") diff --git a/tslang/test/tester/tests/00class_arrow_getter.ts b/tslang/test/tester/tests/00class_arrow_getter.ts new file mode 100644 index 000000000..71f705008 --- /dev/null +++ b/tslang/test/tester/tests/00class_arrow_getter.ts @@ -0,0 +1,62 @@ +// An arrow with no return type that reads a get accessor (`() => this.prop`): typed once the +// accessor is declared, wherever the arrow is - a field initializer, a method or constructor +// declared before the accessor - and whether the accessor's type is given or inferred. +class A { + n = 3; + private _v = 0; + + fn = () => this.prop; + typed = () => this.typedProp; + set2 = (v: number) => { + this.val = v; + }; + + constructor() { + const f = () => this.prop; + this.first = f(); + } + + first = 0; + + viaMethod() { + const f = () => this.prop; + return f(); + } + + paren() { + const f = () => (this.prop); + return f(); + } + + get prop() { + return this.n * 2; + } + + get typedProp(): number { + return this.n * 3; + } + + get val() { + return this._v; + } + + set val(v: number) { + this._v = v; + } +} + +function main() { + const a = new A(); + assert(a.fn() == 6, "a field's arrow reads a getter"); + assert(a.typed() == 9, "a getter with its type given"); + assert(a.first == 6, "an arrow in the constructor"); + assert(a.viaMethod() == 6 && a.paren() == 6, "an arrow in a method declared before the getter"); + + a.n = 5; + assert(a.fn() == 10, "reads the getter when called"); + + a.set2(7); + assert(a.val == 7, "an arrow that calls a setter"); + + print("done."); +} From 3bb9157e52c4fbbf39e64e2be67aed43396782fc Mon Sep 17 00:00:00 2001 From: ASDAlexander77 Date: Fri, 30 Oct 2026 00:24:46 +0000 Subject: [PATCH 2/2] -mm=own: what a call through a field's function returns is the caller's, not a borrow of the field `const s = a.fn()` with a field `fn: () => string` (or `fn = () => this.s`) failed under `-mm=own --no-default-lib` with "'this value' borrows a field but is used here after it may be released or overwritten". With the default library the module is not a closed world, so the call's string was not marked owned and nothing released it after `a`. walkBorrowed followed the call's result as a value that might borrow the field's function, so the string's release after `a` looked like a use of the field after it was freed. The function value a call goes through gives nothing of itself to the result: being the callee (operand 0) no longer walks into the call's results. A method's `this` still does, as a method can return a borrow of it. own_field_function_result (own, plus own's no-counting check): results of calls through a typed field, an arrow field reading `this` and a function read out of a field, one kept after its object is released, and one of a temporary's field in a loop. Co-Authored-By: Claude Opus 5.5 --- .../OwnershipInferenceBorrowers.cpp | 7 +++ tslang/test/tester/CMakeLists.txt | 4 +- .../tester/own/own_field_function_result.ts | 53 +++++++++++++++++++ 3 files changed, 62 insertions(+), 2 deletions(-) create mode 100644 tslang/test/tester/own/own_field_function_result.ts diff --git a/tslang/lib/TypeScript/OwnershipInferenceBorrowers.cpp b/tslang/lib/TypeScript/OwnershipInferenceBorrowers.cpp index 93ff3ad1a..151c8e412 100644 --- a/tslang/lib/TypeScript/OwnershipInferenceBorrowers.cpp +++ b/tslang/lib/TypeScript/OwnershipInferenceBorrowers.cpp @@ -475,6 +475,13 @@ namespace own_inference continue; } + // the function value a call goes through: what it returns is the caller's, not a part + // of the function (`a.fn()` of a field `fn: () => string`, a string kept past `a`) + if (isCall(user) && use.getOperandNumber() == 0 && calleeValue(user) == pending.value) + { + continue; + } + // A result that borrows what it was given borrows this too, and is walked as it. for (auto result : user->getResults()) { diff --git a/tslang/test/tester/CMakeLists.txt b/tslang/test/tester/CMakeLists.txt index 1125dc8e5..555bd1e46 100644 --- a/tslang/test/tester/CMakeLists.txt +++ b/tslang/test/tester/CMakeLists.txt @@ -4067,7 +4067,7 @@ foreach(own_test own_fresh_string own_fresh_array own_return_new own_try_local o own_generator_this_param own_copy_within_returned_param own_swap_elements own_callback_forwarded own_optional_record_read own_param_default_kept own_literal_into_array own_generator_take own_rest_array_borrows own_string_copies - own_captured_string_callback own_callback_and_call own_callback_captured own_literal_closures own_literal_variable_closures own_temporary_field_take own_union_narrowed_calls own_named_field_take own_let_field_take own_literal_captures own_literal_object_captures own_literal_interface_captures own_literal_let_captures own_interface_generic_method own_back_reference + own_captured_string_callback own_callback_and_call own_callback_captured own_literal_closures own_literal_variable_closures own_temporary_field_take own_union_narrowed_calls own_named_field_take own_let_field_take own_literal_captures own_literal_object_captures own_literal_interface_captures own_literal_let_captures own_interface_generic_method own_back_reference own_field_function_result own_callback_shapes own_callback_extended own_callback_interface own_generic_specializations own_callback_cast own_callback_interface_slot) tslang_add_test(NAME test-jit-own-${own_test} COMMAND test-runner -jit -mm=own "${PROJECT_SOURCE_DIR}/test/tester/own/${own_test}.ts") @@ -4105,7 +4105,7 @@ foreach(keeps_mm rc gc none) own_copy_within_returned_param own_swap_elements own_callback_forwarded own_optional_record_read own_param_default_kept own_literal_into_array own_generator_take own_rest_array_borrows own_string_copies - own_captured_string_callback own_callback_and_call own_callback_captured own_literal_closures own_literal_variable_closures own_temporary_field_take own_union_narrowed_calls own_named_field_take own_let_field_take own_literal_captures own_literal_object_captures own_literal_interface_captures own_literal_let_captures own_interface_generic_method own_back_reference + own_captured_string_callback own_callback_and_call own_callback_captured own_literal_closures own_literal_variable_closures own_temporary_field_take own_union_narrowed_calls own_named_field_take own_let_field_take own_literal_captures own_literal_object_captures own_literal_interface_captures own_literal_let_captures own_interface_generic_method own_back_reference own_field_function_result own_callback_shapes own_callback_extended own_callback_interface own_generic_specializations own_callback_cast own_callback_interface_slot) tslang_add_test(NAME test-jit-${keeps_mm}-${keeps_test} COMMAND test-runner -jit -mm=${keeps_mm} "${PROJECT_SOURCE_DIR}/test/tester/own/${keeps_test}.ts") diff --git a/tslang/test/tester/own/own_field_function_result.ts b/tslang/test/tester/own/own_field_function_result.ts new file mode 100644 index 000000000..69ac65dfc --- /dev/null +++ b/tslang/test/tester/own/own_field_function_result.ts @@ -0,0 +1,53 @@ +// What a call through a field's function returns is the caller's own, not a part of the function +// or of the object holding it: a string kept after the object is gone, read through a field typed +// as a function, an arrow field reading `this`, and a function value read out of the field first. +// Each step churns, so anything freed too early reads garbage; every model runs it. +class Junk { + constructor(public n: number) {} +} + +function churn() { + const junk: Junk[] = []; + for (let i = 0; i < 64; i++) junk.push(new Junk(999)); +} + +class A { + name = "a"; + typed: () => string = () => "typed"; + arrow = () => this.name + "!"; +} + +function make(name: string) { + const a = new A(); + a.name = name; + return a; +} + +function kept() { + const a = make("b"); + return a.arrow(); +} + +function main() { + const a = new A(); + const s = a.typed(); + const t = a.arrow(); + const f = a.typed; + const u = f(); + churn(); + assert(s == "typed" && t == "a!" && u == "typed", "results of calls through fields"); + + const k = kept(); + churn(); + assert(k == "b!", "a result kept after its object is released"); + + let all = ""; + for (let i = 0; i < 10; i++) { + all = all + make("x").arrow(); + } + + churn(); + assert(all.length == 20, "results of temporaries' fields"); + + print("done."); +}