Decline for-loop environment reuse when a pattern default embeds a closure#2739
Merged
Merged
Conversation
…osure
ForLoopMayCapture scanned init declarator initializers, test, update and
body for capturing closures, but never the declarator binding patterns
themselves — a destructuring default like
for (let i = 0, { f = () => i } = {}; i < 3; i++) { fns.push(f); }
embeds a closure the scan missed, so the per-iteration environment was
reused/pooled in place and the escaped closure observed the final value
of i (3) instead of the captured iteration's (0, matching V8), or a
reset binding after the loop re-entered.
Same eligibility-scan family as the for-header TDZ fix (sebastienros#2709); the
gate only declines an optimization, generic per-iteration semantics
take over.
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_0115tQFNyyQqc1HQGPLUgZND
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.
Pre-release review find in the same eligibility-scan family as #2709.
ForLoopMayCapturescanned the init declarators' initializers (plus test/update/body) for capturing closures, but never the declarator binding patterns themselves. A destructuring default embeds an expression the scan missed:With the closure invisible to the scan, the loop reused/pooled its per-iteration environment in place, so the escaped closure observed the final
i— and with environment pooling, re-entering the loop could reset the binding the closure captured. The fix runs the existing escape visitor over non-identifier declarator ids; the gate only declines an optimization, so generic per-iteration semantics take over.Repro verified against V8 before/after. Full suite plus Test262 green.
🤖 Generated with Claude Code
https://claude.ai/code/session_0115tQFNyyQqc1HQGPLUgZND