Skip to content

fix(compiler): keep captured locals fresh across loop iterations - #441

Open
davydog187 wants to merge 2 commits into
mainfrom
fix/loop-captured-local
Open

davydog187 wants to merge 2 commits into
mainfrom
fix/loop-captured-local

Conversation

@davydog187

Copy link
Copy Markdown
Contributor

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 end gave 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.

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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant