fix(transform): #1047 — async early return followed by an unreached await#1062
Merged
Merged
Conversation
…wait In the generator transform, user-level `return X` statements inside a state body were only rewritten (to set `__gen_done = true` and wrap the value in an iter-result with `done = true`) when the state's exit was `StateExit::Done`. States ending in `StateExit::Yield` left those returns as raw `Stmt::Return(X)`. When a `Yield` state had an early return in front of the synthesized `state = next; return iter_result(value, false)` tail — i.e. the canonical `if (existing) return existing.kid; ... await ...; return kid;` shape — next() returned the bare expression value. The AsyncStepChain caller treated the missing `.done` as `false` and re-entered the same state with `state_id` unchanged (the synthesized advance never ran). Result: infinite loop, re-evaluating state 1's body. The fix is one block: run the same `body_contains_return` → `prepend_done_before_returns` + `rewrite_returns_as_done` sequence on Yield-state bodies that StateExit::Done already does. Done-flag set and iter-result wrapping make every early-return path terminal so the async-step driver short-circuits via `js_iter_result_get_done()`. Verified with `test-files/test_issue_1047_async_early_return_unreached_await.ts` byte-for-byte against `node --experimental-strip-types`.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixes #1047. The generator transform only rewrote user-level
return Xstatements when the state's exit wasStateExit::Done. States ending inStateExit::Yield(i.e. states followed by anawait) left those returns as rawStmt::Return(X), so when an early return was taken next() returned the bare expression value; the AsyncStepChain caller treated the missing.doneasfalseand re-entered the same state — infinite loop.The fix runs the same
body_contains_return→prepend_done_before_returns+rewrite_returns_as_donesequence on Yield-state bodies that StateExit::Done already does.Reproducer
Before: infinite loop,
[inner] existing: the-kidprinted millions of times.After:
k: the-kid typeof: string— matches Node byte-for-byte.Test plan
test-files/test_issue_1047_async_early_return_unreached_await.tsmatchesnode --experimental-strip-typescargo build --release -p perry-transform -p perry)