Skip to content

A yield in a finally block of a generator is an error, not skipped - #980

Merged
ASDAlexander77 merged 1 commit into
mainfrom
yield-in-finally
Nov 21, 2026
Merged

ASDAlexander77 merged 1 commit into
mainfrom
yield-in-finally

Conversation

@ASDAlexander77

Copy link
Copy Markdown
Owner

try { yield 1; } finally { print("f"); yield 2; } used to give 1, and then neither printed "f" nor yielded 2.

Cause. TryOpLowering copies a try's finally once for each way out of the try: the end of its body, the unwind, and each return/break/continue. Every copy carries the yield's resume label. The generator's switch dispatches by position, so these copies shifted every label after them, and a resume landed after the yield in another copy.

This PR.

  • A yield in a finally block is now an error at the yield (YieldInFinally), as a yield in a catch already is. Resuming in the copy that runs while an exception unwinds is not possible.
  • A repeated resume label is now an error in the lowering, instead of a resume somewhere else.
  • A generator declared inside a finally block is its own function and may yield.

Supporting a yield in a finally needs that finally lowered as straight code after the try: a catch-all that keeps the exception, a record of how the try was left, and that way out taken again afterwards. This is recorded as open in TSLANG 153.

Tests.

  • diagnostic-location/generator_yield_in_finally.ts
  • 00generator_finally.ts: finally ordering around yields, and a generator declared in a finally (compile, jit, rc, own)

Verified.

  • ctest: 8793 passed, 0 failed.
  • wasm32 tester: rc, none and own clean.
  • tsc sweep: identical to the baseline (3256 pass).

🤖 Generated with Claude Code

TryOpLowering copies a try's finally for each way out of the try (the end of its body, the unwind, each
return/break/continue), each copy with the yield's resume label, and the generator's switch dispatches by
position: the copies shifted every label after them, so `try { yield 1; } finally { print("f"); yield 2; }`
gave 1, never 2, and resumed after the yield in another copy. Now an error at the yield (YieldInFinally),
as a yield in a catch is; a repeated resume label is an error in the lowering rather than a resume somewhere
else. Supporting it needs the finally lowered as straight code after the try (TSLANG 153, open).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
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