From 1dedb62b1ce281b2fb1724eed7aa627efb6a9bd9 Mon Sep 17 00:00:00 2001 From: ASDAlexander77 Date: Thu, 5 Nov 2026 17:43:30 +0000 Subject: [PATCH 1/2] -mm=own: a thrown parameter is kept; an awaited borrowed string is copied A function that throws its parameter (`function reject(reason: E) { throw reason; }`) failed under own both ways: without `@keeps` the throw's retain was "a parameter, kept here, but -mm=own cannot make the callers give it up", and with `@keeps("reason")` the signature pass said "its body keeps nothing". OwnershipSignaturePass::keeps now counts a throw of the value as keeping it: the exception takes it, and releaseKeptParamsAtExits already places no release at such a throw. The callers move the argument in, and a use of it afterwards is a use after move. `await identity(greet("you"))`, a string result that borrows its argument, was "borrows the result of 'identity' and cannot be stored". The async lowering outlines the await's region into a func.func that stores the value through a reference to the caller's local; its temporaries are released when it ends. Such a store of a copyable string now takes a copy, as a store into a place does (spec 50). An object result that borrows its argument is still the error. Found adding Promise to the DefaultLib (TypeScriptCompilerDefaultLib#65). Co-Authored-By: Claude Opus 5.5 --- .../lib/TypeScript/OwnershipInferencePass.h | 3 + .../OwnershipInferencePlaceReads.cpp | 14 +++- .../lib/TypeScript/OwnershipSignaturePass.cpp | 7 ++ tslang/test/tester/CMakeLists.txt | 5 ++ .../tests/00own_throw_param_await_borrow.ts | 84 +++++++++++++++++++ 5 files changed, 112 insertions(+), 1 deletion(-) create mode 100644 tslang/test/tester/tests/00own_throw_param_await_borrow.ts diff --git a/tslang/lib/TypeScript/OwnershipInferencePass.h b/tslang/lib/TypeScript/OwnershipInferencePass.h index 414e585f0..56f8afafc 100644 --- a/tslang/lib/TypeScript/OwnershipInferencePass.h +++ b/tslang/lib/TypeScript/OwnershipInferencePass.h @@ -868,6 +868,9 @@ class OwnershipInferencePass // Does `store` put `value`, a string read out of a place, into another place with no retain of // rc's for it, so that the place can take a copy instead (callCopies)? bool storesCopyOf(mlir::Operation *store, mlir::Value value); + // Is `storeOp` an `await`'s store of its value, from the function the async lowering outlined + // its region into, through the reference to the caller's local the code after the await reads? + bool isAwaitResultStore(mlir_ts::StoreOp storeOp); // A local that holds a borrow through `user`, and the field path under it that holds it (empty: // the local's value itself): a store of it into the local, or into a field of the local's value diff --git a/tslang/lib/TypeScript/OwnershipInferencePlaceReads.cpp b/tslang/lib/TypeScript/OwnershipInferencePlaceReads.cpp index 43f988945..40567e916 100644 --- a/tslang/lib/TypeScript/OwnershipInferencePlaceReads.cpp +++ b/tslang/lib/TypeScript/OwnershipInferencePlaceReads.cpp @@ -774,11 +774,23 @@ namespace own_inference llvm::count(call->getOperands(), value) == 1; } + bool OwnershipInferencePass::isAwaitResultStore(mlir_ts::StoreOp storeOp) + { + // `await f(x)` runs the call in an `async.execute` region, which the async lowering outlines + // into a `func.func` given a reference to the caller's local, and stores the value through + // it for the code after the await to read. The outlined function's temporaries (`x`) are + // released when it ends, so what borrows them cannot be given back as it is. + auto argument = mlir::dyn_cast(storeOp.getReference()); + auto *owner = argument ? argument.getOwner() : nullptr; + return owner && owner->isEntryBlock() && owner->getParentOp() && + owner->getParentOp()->getName().getStringRef() == "func.func"; + } + bool OwnershipInferencePass::storesCopyOf(mlir::Operation *store, mlir::Value value) { auto storeOp = mlir::dyn_cast(store); if (!storeOp || storeOp.getValue() != value || !(isCopyableValueAt(store, value.getType()) || isHandle(value)) || - !isPlace(storeOp.getReference())) + !(isPlace(storeOp.getReference()) || isAwaitResultStore(storeOp))) { return false; } diff --git a/tslang/lib/TypeScript/OwnershipSignaturePass.cpp b/tslang/lib/TypeScript/OwnershipSignaturePass.cpp index f0ffd622f..09d94e69d 100644 --- a/tslang/lib/TypeScript/OwnershipSignaturePass.cpp +++ b/tslang/lib/TypeScript/OwnershipSignaturePass.cpp @@ -910,6 +910,13 @@ namespace own_signature return llvm::is_contained(pushOp.getItems(), used); } + // `throw reason` takes the value with the exception: the catch owns it, not the caller + // (releaseKeptParamsAtExits places no release at such a throw) + if (mlir::isa(user)) + { + return user->getOperand(0) == used; + } + // `new Shared(x)` moves `x` into the block, as a store into a place does (spec 23.1) if (auto sharedNewOp = mlir::dyn_cast(user)) { diff --git a/tslang/test/tester/CMakeLists.txt b/tslang/test/tester/CMakeLists.txt index 3b35cca7c..f8d9173c8 100644 --- a/tslang/test/tester/CMakeLists.txt +++ b/tslang/test/tester/CMakeLists.txt @@ -923,6 +923,8 @@ tslang_add_test(NAME test-compile-00-new-alias-and-ctor-overloads COMMAND test-r tslang_add_test(NAME test-compile-own-00-atomic-builtins COMMAND test-runner -mm=own "${PROJECT_SOURCE_DIR}/test/tester/tests/00atomic_builtins.ts") tslang_add_test(NAME test-compile-own-00-generic-literal-inference COMMAND test-runner -mm=own "${PROJECT_SOURCE_DIR}/test/tester/tests/00generic_literal_inference.ts") tslang_add_test(NAME test-compile-own-00-new-alias-and-ctor-overloads COMMAND test-runner -mm=own "${PROJECT_SOURCE_DIR}/test/tester/tests/00new_alias_and_ctor_overloads.ts") +tslang_add_test(NAME test-compile-00-own-throw-param-await-borrow COMMAND test-runner "${PROJECT_SOURCE_DIR}/test/tester/tests/00own_throw_param_await_borrow.ts") +tslang_add_test(NAME test-compile-own-00-own-throw-param-await-borrow COMMAND test-runner -mm=own "${PROJECT_SOURCE_DIR}/test/tester/tests/00own_throw_param_await_borrow.ts") tslang_add_test(NAME test-compile-load-store-decorators COMMAND test-runner "${PROJECT_SOURCE_DIR}/test/tester/tests/load_store_decorators.ts") tslang_add_test(NAME test-compile-reference-null-bug COMMAND test-runner "${PROJECT_SOURCE_DIR}/test/tester/tests/00reference_null_bug.ts") tslang_add_test(NAME test-compile-reference-index-bug COMMAND test-runner "${PROJECT_SOURCE_DIR}/test/tester/tests/00reference_index_bug.ts") @@ -1574,6 +1576,8 @@ tslang_add_test(NAME test-jit-00-new-alias-and-ctor-overloads COMMAND test-runne tslang_add_test(NAME test-jit-own-00-atomic-builtins COMMAND test-runner -jit -mm=own "${PROJECT_SOURCE_DIR}/test/tester/tests/00atomic_builtins.ts") tslang_add_test(NAME test-jit-own-00-generic-literal-inference COMMAND test-runner -jit -mm=own "${PROJECT_SOURCE_DIR}/test/tester/tests/00generic_literal_inference.ts") tslang_add_test(NAME test-jit-own-00-new-alias-and-ctor-overloads COMMAND test-runner -jit -mm=own "${PROJECT_SOURCE_DIR}/test/tester/tests/00new_alias_and_ctor_overloads.ts") +tslang_add_test(NAME test-jit-00-own-throw-param-await-borrow COMMAND test-runner -jit "${PROJECT_SOURCE_DIR}/test/tester/tests/00own_throw_param_await_borrow.ts") +tslang_add_test(NAME test-jit-own-00-own-throw-param-await-borrow COMMAND test-runner -jit -mm=own "${PROJECT_SOURCE_DIR}/test/tester/tests/00own_throw_param_await_borrow.ts") tslang_add_test(NAME test-jit-load-store-decorators COMMAND test-runner -jit "${PROJECT_SOURCE_DIR}/test/tester/tests/load_store_decorators.ts") tslang_add_test(NAME test-jit-reference-null-bug COMMAND test-runner -jit "${PROJECT_SOURCE_DIR}/test/tester/tests/00reference_null_bug.ts") tslang_add_test(NAME test-jit-reference-index-bug COMMAND test-runner -jit "${PROJECT_SOURCE_DIR}/test/tester/tests/00reference_index_bug.ts") @@ -2168,6 +2172,7 @@ set(TSLANG_CORPUS 00atomic_builtins.ts 00generic_literal_inference.ts 00new_alias_and_ctor_overloads.ts + 00own_throw_param_await_borrow.ts 00any_truthiness_objects.ts 00add_promotes_both_operands.ts 00conditional_owned_result.ts diff --git a/tslang/test/tester/tests/00own_throw_param_await_borrow.ts b/tslang/test/tester/tests/00own_throw_param_await_borrow.ts new file mode 100644 index 000000000..dcebccd89 --- /dev/null +++ b/tslang/test/tester/tests/00own_throw_param_await_borrow.ts @@ -0,0 +1,84 @@ +// -mm=own: a function that throws its parameter keeps it (the exception takes it), and an awaited +// call whose string result borrows its argument gives back a copy (the argument is released when +// the await's region ends). + +class E { + constructor(public m: string) {} +} + +function reject(reason: R): T { + throw reason; +} + +function rejectE(reason: E): int { + throw reason; +} + +// throws on one path only: the other releases the parameter +function maybeThrow(reason: E, really: boolean): int { + if (really) { + throw reason; + } + + return 1; +} + +function identity(value: T): T { + return value; +} + +async function greet(name: string): Promise { + return "hello " + name; +} + +function main() { + let caught = ""; + try { + rejectE(new E("plain")); + } catch (e: E) { + caught = e.m; + } + + assert(caught == "plain", "a thrown parameter"); + + try { + reject(new E("generic")); + } catch (e: E) { + caught = e.m; + } + + assert(caught == "generic", "a thrown generic parameter"); + + const named = new E("named"); + try { + rejectE(named); + } catch (e: E) { + caught = e.m; + } + + assert(caught == "named", "a named local given up to the throw"); + + let r = 0; + for (let i = 0; i < 100; i++) { + try { + r += maybeThrow(new E("x"), i % 2 == 0); + } catch (e: E) { + r += 10; + } + } + + assert(r == 550, "a throw on one path"); + + let total = 0; + for (let i = 0; i < 100; i++) { + const s = await identity(greet("you")); + total += s.length; + } + + assert(total == 900, "an awaited borrowed string, copied"); + + const t = await identity("lit"); + assert(t == "lit", "an awaited borrowed literal"); + + print("done."); +} From e236dbafa18181d8324c46ba6e6ef42f62701d54 Mon Sep 17 00:00:00 2001 From: ASDAlexander77 Date: Thu, 5 Nov 2026 19:01:23 +0000 Subject: [PATCH 2/2] An await in a try body declares its result slot in front of the try The Linux CI failed the new test under rc and own: "operand #0 does not dominate this use". The await's result slot, released by the scope, was declared in the body of a TryOp while the TryOp's cleanup region released it. On Itanium a block with a typed `catch` is wrapped in such a TryOp, which is why only Linux saw it; an await in any `try` body failed the same way on Windows too. The slot is now declared in front of the TryOp, as an owned local is (allocateScopeOwnedVarsOutsideOfOperation). Under own the value is then moved out of the slot into a hoisted local, which is declared with no value and filled by a store right before rc's RetainSlot. takerOf named the declaration as the move, which ran before the value was made, so the move looked like one inside a loop; the store is the move now. Still open under own on Itanium: a value moved out of an await's slot at the top level of a block wrapped for a typed `catch` is "moved on some paths only" (the wrapper's cleanup releases the slot too). The test's awaits are in functions of their own. Co-Authored-By: Claude Opus 5.5 --- tslang/lib/TypeScript/MLIRGenExpressions.cpp | 12 ++++++++ .../TypeScript/OwnershipInferenceMoves.cpp | 11 +++++++ .../tests/00own_throw_param_await_borrow.ts | 29 ++++++++++++++++++- 3 files changed, 51 insertions(+), 1 deletion(-) diff --git a/tslang/lib/TypeScript/MLIRGenExpressions.cpp b/tslang/lib/TypeScript/MLIRGenExpressions.cpp index c38d284b3..f8ba7d746 100644 --- a/tslang/lib/TypeScript/MLIRGenExpressions.cpp +++ b/tslang/lib/TypeScript/MLIRGenExpressions.cpp @@ -467,6 +467,18 @@ namespace mlirgen mlir::Value resultSlot; if (resultType) { + // A slot that will own the result is released by the scope, on the unwind leg too: in + // the body of a TryOp - a `try`, or a block wrapped for its cleanup (on Itanium, any + // block with a typed `catch`) - its storage goes in front of the TryOp, as an owned + // local's does (allocateScopeOwnedVarsOutsideOfOperation), or the cleanup region + // names a slot the body declares + mlir::OpBuilder::InsertionGuard insertGuard(builder); + if (genContext.allocateScopeOwnedVarsOutsideOfOperation && genContext.currentOperation && + compileOptions.tracksOwnership() && mth.ownsHeapMemory(location, resultType)) + { + builder.setInsertionPoint(genContext.currentOperation); + } + resultSlot = builder.create(location, mlir_ts::RefType::get(resultType), mlir::Value(), builder.getBoolAttr(false), builder.getIndexAttr(0)); diff --git a/tslang/lib/TypeScript/OwnershipInferenceMoves.cpp b/tslang/lib/TypeScript/OwnershipInferenceMoves.cpp index e9d7b0402..255b398ba 100644 --- a/tslang/lib/TypeScript/OwnershipInferenceMoves.cpp +++ b/tslang/lib/TypeScript/OwnershipInferenceMoves.cpp @@ -1166,6 +1166,17 @@ namespace own_inference { if (auto retainSlotOp = mlir::dyn_cast(retain)) { + // an owned local hoisted in front of a TryOp (allocateScopeOwnedVarsOutsideOfOperation) + // is declared with no value and given it by the store right before the retain: that + // store is the move, not the declaration, which ran before the value was made + auto varOp = retainSlotOp.getSlot().getDefiningOp(); + auto storeOp = mlir::dyn_cast_or_null(retain->getPrevNode()); + if (varOp && !varOp.getInitializer() && storeOp && storeOp.getReference() == retainSlotOp.getSlot() && + storeOp.getValue() == value) + { + return storeOp; + } + return retainSlotOp.getSlot().getDefiningOp(); } diff --git a/tslang/test/tester/tests/00own_throw_param_await_borrow.ts b/tslang/test/tester/tests/00own_throw_param_await_borrow.ts index dcebccd89..dcf5cded3 100644 --- a/tslang/test/tester/tests/00own_throw_param_await_borrow.ts +++ b/tslang/test/tester/tests/00own_throw_param_await_borrow.ts @@ -69,6 +69,16 @@ function main() { assert(r == 550, "a throw on one path"); + awaitBorrowed(); + awaitInTry(); + + print("done."); +} + +// Its own function: on Itanium (Linux) a block with a typed `catch` is wrapped in a cleanup, and +// under -mm=own a value moved out of an await's slot at that block's top level is still "moved on +// some paths only" (the wrapper's cleanup releases the slot too). +function awaitBorrowed() { let total = 0; for (let i = 0; i < 100; i++) { const s = await identity(greet("you")); @@ -79,6 +89,23 @@ function main() { const t = await identity("lit"); assert(t == "lit", "an awaited borrowed literal"); +} - print("done."); +// an await in a try body: the slot it gives its value back in is released on the unwind path +// too, so it is declared in front of the try, as an owned local is +function awaitInTry() { + let inTry = 0; + for (let i = 0; i < 100; i++) { + try { + const u = await identity(greet("again")); + inTry += u.length; + if (i == 99) { + throw new E("last"); + } + } catch (e: E) { + inTry += e.m.length; + } + } + + assert(inTry == 1104, "an await in a try body"); }