Skip to content

Generator return(value) and throw(e); a throw after a resumed yield is caught by its try - #975

Merged
ASDAlexander77 merged 4 commits into
mainfrom
generator-return-throw
Nov 21, 2026
Merged

ASDAlexander77 merged 4 commits into
mainfrom
generator-return-throw

Conversation

@ASDAlexander77

Copy link
Copy Markdown
Owner

Follow-up to #974 (TSLANG 153).

return(value) and throw(e) of a generator typed Generator<T, ...>, as in JavaScript.

  • A generator paused at a yield resumes there. return(value) finishes it there, so its finally blocks run. throw(e) throws e there; the generator's own try/catch may catch it, and it then goes on to its next yield.
  • A generator not started yet, or already finished, does not run: return(value) gives { value, done: true } and throw(e) throws e.
  • The generator object holds a mode that its yields check when they resume (addGeneratorResumeMethods, mlirGenGeneratorResumeChecks).
  • Under -mm=own, return() is only added for a T that owns nothing (a number or a boolean). For any other T its result would hold the value the caller only lends.

Fixed on the way: generators and try, in every memory model.

  • A throw or a call after a resumed yield inside a try went past it: its catch did not catch and its finally did not run. A yield splits the try's body at its label, and only the first block of the body got unwind edges: TryOpLowering walked it with a continue inside a do ... while (false).
  • A throw after a yield in a loop inside a try did not compile ("operand #0 does not dominate this use" at the try). The catch-type constants the landing pad reads were made at the start of the body, which a resumed turn does not pass. In a generator they now go to the function's entry block.
  • A let declared in a try and read after a yield did not compile, with the same error. Its storage was put in front of the try instead of in front of the generator's switch (GenContext::generatorSwitchOperation).
  • Using a catch variable in a generator crashed. The variable was a field of the generator object, but the catch handler copies the exception into a slot of the stack frame. It is now a local of the frame and lives for one turn.
  • A yield in a catch block is now an error at the yield. Resuming there would resume inside the exception handler; after the fixes above it crashed in LLVM's instruction selection, and before them the catch never caught.

Still open: a yield in a finally block is skipped, as before. The default library's Generator will declare next(value?), return? and throw? once a compiler release includes #974 and this PR.

Tests: 00generator_return_throw.ts (compile, JIT, rc, own) and diagnostic-location generator_yield_in_catch.ts. Full ctest locally: 8781 tests, 0 failed. The tsc compiler-tests sweep shows no regressions (its only difference is a test that sometimes times out).

🤖 Generated with Claude Code

ASDAlexander77 and others added 4 commits November 21, 2026 15:03
…s caught by its try

A generator typed Generator<T, ...> gets return(value) and throw(e), as JavaScript's: paused at a yield, it resumes
there finishing (its finally blocks run) or throwing e (its own try/catch may catch it); one not started or
finished does not run. Under -mm=own return() only for a T that owns nothing.

Fixed on the way, in generators: a throw or a call after a resumed yield in a `try` went past it (only the
first block of the try's body got unwind edges); its catch-type constants did not dominate a resumed turn; a
`let` in a `try` read after a yield did not compile (its storage was put in front of the try, not of the
generator's switch); a catch variable was a field of the generator object and its use crashed. A yield in a
catch block is now an error at the yield (it crashed in LLVM's instruction selection).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…d in finally still open

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…al is released twice under -mm=own

The generator object's `.thrown` field was `<any>undefined`. Under -mm=own a field of an object literal
holding `<any>undefined` (or `<any>null`) is released twice - the store of its box is not counted as a move -
which wasm's checked heap and Linux caught in every generator typed Generator<...> (00generator_next_value,
00generator_return_throw, the default library's iteration_types). The underlying own bug is recorded in
TSLANG 153.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…literal field (open)

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@ASDAlexander77
ASDAlexander77 merged commit 2537cad into main Nov 21, 2026
8 checks passed
@ASDAlexander77
ASDAlexander77 deleted the generator-return-throw branch November 21, 2026 16:31
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