Skip to content

Fix object-rest lowering for statements directly in a for-of body - #65578

Closed
5 (tydavid5) wants to merge 1 commit into
microsoft:mainfrom
tydavid5:fix/object-rest-forof-lowering-65513
Closed

5 (tydavid5) wants to merge 1 commit into
microsoft:mainfrom
tydavid5:fix/object-rest-forof-lowering-65513

Conversation

@tydavid5

Copy link
Copy Markdown

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.

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
Copilot AI balanced review requested due to automatic review settings November 19, 2026 11:30
@github-project-automation github-project-automation Bot moved this to Not started in PR Backlog Nov 19, 2026
@typescript-automation typescript-automation Bot added the For Backlog Bug PRs that fix a backlog bug label Nov 19, 2026

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 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 VisitNode for 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

@github-project-automation github-project-automation Bot moved this from Not started to Done in PR Backlog Nov 19, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

For Backlog Bug PRs that fix a backlog bug

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

Object-rest for-of lowering skips transformations on direct body statements

3 participants