for-let closures should capture per-iteration binding#1713
Open
RealColdFry wants to merge 5 commits into
Open
Conversation
…upport destructuring initializers, add tests
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.
TSTL lowers
for (let i)to onelocal ioutside awhile, so every closure shares that upvalue. ES spec gives each iteration a fresh binding.Fix: when a let-declared loop var is captured in body/cond/incr, wrap the body in a per-iter
do ... endwithlocal i = i. Lua makes a fresh upvalue per block activation, so each iteration's closures get their owni. A sync slot carries body mutations back to the outer before the incrementor, matching spec semantics (closure ati=2when body doesif (i===2) i = 8reads 8, not 9).Sync is also injected before every continue-exit (goto, repeat-break, native), so
continuestill propagates mutations.No-capture loops keep today's output.
Known gap (pre-existing, not regressed): closures placed in the loop's condition or incrementor still capture the outer binding, since those run outside the per-iter
do. Out of scope here; tracked separately.Tests: