Skip to content

-mm=own: a thrown parameter is kept; an awaited borrowed string is copied - #768

Merged
ASDAlexander77 merged 2 commits into
mainfrom
own-throw-param-and-await-borrow
Nov 5, 2026
Merged

ASDAlexander77 merged 2 commits into
mainfrom
own-throw-param-and-await-borrow

Conversation

@ASDAlexander77

Copy link
Copy Markdown
Owner

Two -mm=own gaps found while adding Promise to the default library (ASDAlexander77/TypeScriptCompilerDefaultLib#65).

A thrown parameter is kept (OwnershipSignaturePass.cpp)

function reject<T, R>(reason: R): T { throw reason; } didn't compile under own either way:

  • Without @keeps, the throw's retain gave "'reason' is a parameter, kept here, but -mm=own cannot make the callers of 'reject' give it up".
  • With @keeps("reason"), it gave "'@keeps' of 'reject' names 'reason', but its body keeps nothing".

OwnershipSignaturePass::keeps had no case for a throw. It now counts a throw of the value (ts.Throw, ts.ThrowCall, ts.ThrowUnwind) as keeping it, since the exception takes it. Inference already treats that as a move: releaseKeptParamsAtExits doesn't release a parameter at a throw that takes it. So callers move the argument in, a use of the argument afterwards is a use-after-move error, and a function that throws on one path and returns on the other releases the parameter on the return path.

An awaited string that borrows its argument is copied (OwnershipInferencePlaceReads.cpp)

const s = await identity(greet("you")) gave "borrows the result of 'identity' and cannot be stored, returned or captured". The same line without await compiled.

The async lowering moves the await's region into its own func.func, which stores the value through a reference to the caller's local. That function's temporaries (greet("you")) are released when it ends, so a borrow of them can't be handed back. A store of a copyable string (or a handle) through that reference now takes a copy, as a store into a place already does (spec 50, storesCopyOf). An object result that borrows its argument (await identity(new B(3))) still gets the error.

Test

tests/00own_throw_param_await_borrow.ts:

  • Throws: a plain, a generic and a named-local thrown parameter, plus a throw on one path inside a 100-iteration loop.
  • Await: an awaited borrowed string in a 100-iteration loop, and an awaited borrowed literal.

It's registered for compile and JIT under the default model and own, and runs under rc and none through TSLANG_CORPUS. It also passes with --verify-ownership.

Verification

  • Release ctest -C Release: 6637 tests, all passed (3 disabled as before).
  • DefaultLib#65 built with this compiler, tests.ps1 release compile + jit: gc 186/186; rc, none and own 185/186 (the gc-only skip). Under own this includes promise_reject.ts, which more no default opts finished #65 no longer skips there.

🤖 Generated with Claude Code

…pied

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 <noreply@anthropic.com>
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 <noreply@anthropic.com>
@ASDAlexander77
ASDAlexander77 merged commit e341821 into main Nov 5, 2026
2 checks passed
@ASDAlexander77
ASDAlexander77 deleted the own-throw-param-and-await-borrow branch November 5, 2026 19:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant