Repository navigation
Fix object-rest lowering for statements directly in a for-of body - #65578
Closed
5 (tydavid5) wants to merge 1 commit into
Closed
5 (tydavid5) wants to merge 1 commit into
5 (tydavid5) wants to merge 1 commit into
Conversation
The object-rest transformer rebuilt a for-of loop with a rest binding by visiting the body statements with VisitEachChild, which visits a node's children without dispatching the node's own visitor. A nested for-of with a rest binding therefore never got its own lowering, so it was emitted as a for-of whose variable has an initializer, which Node rejects, and a function declaration in the body never got its parameter destructuring prologue, so it threw at runtime. Both branches now dispatch each body statement through VisitNode, which is also what the loop's own binding statement already used, so nested loops and function declarations are lowered the same way as anywhere else. A regression test covers the nested loop, the unbraced form, and the function declaration case. Fixes microsoft#65513
Contributor
There was a problem hiding this comment.
🟢 Approval recommended
No active review issues block approval of the object-rest transformer change.
No open issues 🎉
What changed in this PR
Fixes #65513 by ensuring the object-rest transformer visits direct for-of body statements themselves, allowing nested loops and function declarations to be lowered correctly.
Changes:
- Uses
VisitNodefor both braced and unbraced loop bodies. - Adds an ES2015 regression test covering nested loops and function parameter destructuring.
- Adds JavaScript, type, and symbol baselines.
| File | Description |
|---|---|
tsc/testdata/tests/cases/compiler/objectRestForOfNestedLowering.ts |
Tests nested loops, unbraced bodies, and function declarations. |
tsc/testdata/baselines/reference/compiler/objectRestForOfNestedLowering.types |
Records expected type information. |
tsc/testdata/baselines/reference/compiler/objectRestForOfNestedLowering.symbols |
Records expected symbol bindings. |
tsc/testdata/baselines/reference/compiler/objectRestForOfNestedLowering.js |
Records correctly lowered loops and function parameters. |
tsc/internal/transformers/estransforms/objectrestspread.go |
Dispatches direct body statements through VisitNode. |
Review effort: Balanced
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.
The object-rest transformer rebuilt a for-of loop with a rest binding by visiting the body statements with VisitEachChild, which visits a node's children without dispatching the node's own visitor. A nested for-of with a rest binding therefore never got its own lowering, so it was emitted as a for-of whose variable has an initializer, which Node rejects, and a function declaration in the body never got its parameter destructuring prologue, so it threw at runtime. Both branches now dispatch each body statement through VisitNode, which is also what the loop's own binding statement already used, so nested loops and function declarations are lowered the same way as anywhere else. A regression test covers the nested loop, the unbraced form, and the function declaration case.
Fixes #65513
Verification: the nested loop repro printed nothing before the fix, Node rejected the emit with a SyntaxError, and after the fix it prints 3. The function declaration case threw ReferenceError before and prints 6 after. The new test passes, and with the fix reverted it fails on the emit baseline. Ten edge cases were checked by hand against the fixed build, including the unbraced form, per-iteration closures, continue, and calls before the function declaration.