Repository navigation
fix(compiler): keep captured locals fresh across loop iterations - #441
Open
davydog187 wants to merge 2 commits into
Open
davydog187 wants to merge 2 commits into
davydog187 wants to merge 2 commits into
Conversation
A write to a captured local that preceded the capturing closure in source order inside a loop compiled to a plain register move, missing the cell an earlier iteration's closure had opened. Loops and label blocks that contain functions are now resolved once to learn the captured names, then again with those names marked, so such writes go through the open upvalue.
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.
A write to a captured local that came before the capturing closure in source order, inside a loop, compiled to a plain register move. It missed the cell an earlier iteration's closure had opened, so the closure read a stale value (
local x; for i=1,2 do x=i; fns[i]=function() return x end endgave 1,1; PUC-Lua gives 1,2).Fix: loops and label blocks that contain functions are resolved once to learn which names are captured, then again with those names pre-marked (loops: only locals declared outside the loop). Function declarations to a captured local now also write the register. Loops that capture nothing generate identical code.
Tests: 20 new in test/language/loop_captured_local_test.exs (numeric/generic for, while, repeat, goto; write before/after the closure; nested closures). 10 fail on main. Full suite passes. benchmarks/closures.exs: medians unchanged within noise.