Repository navigation
Codegen: a closure created in a loop gets its own environment per iteration (#1471) - #1583
Merged
Merged
Conversation
This was referenced Sep 23, 2026
ehartford
force-pushed
the
fix-1471-loop-closure-capture
branch
3 times, most recently
from
September 24, 2026 13:50
a4dff1d to
35510a5
Compare
ehartford
marked this pull request as ready for review
September 24, 2026 14:48
…ration — every closure from one site shared one entry-block slot, so all saw the last iteration's value (#1471) Spec §12.4: "`move ||` transfers ownership, which for a Copy value is a copy." A `move` closure created in a loop body copies the Copy local at ITS creation, so `for i in 0..3: fs.push(move () => i * 10)` makes three closures over 0, 10, 20, and threads spawned in a loop each run with their own index. (A non-move closure holds the iteration's local by place; storing it beyond the iteration is a view escape, not this bug.) Root cause (src/CodegenDispatch.w gen_closure, the "Build capture struct on stack" block): the environment was `create_entry_alloca(cap_struct_type)` — one slot per closure SITE, hoisted to the function entry. Each iteration re-stored its captures into the same slot and handed out a fat pointer to it, so every closure from the site read the final iteration's environment. Evidence: the pre-optimize IR of the repro (WITH_DUMP_LLIR_PRE=1) has one `alloca { i32 }` in main's entry for the site and one `store` per iteration into it; `run --debug-alloc` printed `20 20 20`. Fix: MirLower tags the CK_CLOSURE constant (d2 = 1) when the closure expression sits inside a loop of the body (`loop_break_bbs` non-empty), and gen_closure allocates such an environment where the closure is created (`wl_build_alloca` at the insertion point), so each evaluation owns its storage for the rest of the frame. A site outside a loop runs once per frame and keeps its entry slot; a non-escaping closure inside a loop pays one alloca per iteration, which the frame bounds. The non-escaping mark could not select the case: a direct argument is non-escaping by §12.3 even when the callee stores it (#1566), and `Vec.push`/`spawn_os` closures are exactly the ones that must not share. `--dump-mir` prints the tag as `const closure(nodeN, in-loop)`. Matrix (stage1 run --debug-alloc, all leak count=0): for binding / let in body / while counter / Copy struct / f64 / two captures / nested loop / through a user fn / let-bound in loop, each 0 10 20 (was 20 20 20); non-escaping closure in a loop 33; four spawn_os threads in a loop sum 600 (was every thread with the last index). Fixtures: test/behavior/behav_closure_loop_env_per_iteration.w, test/behavior/behav_closure_loop_spawn_os_index.w. `analyze audit:all` on the fixture: violations=0. tools/drop_audit.w stage1 vs seed: 176 cells, 0 regressions; tools/move_audit.w: 73 cells, 0 vs-expected failures. Not covered here: a closure returned from its creating function still points at that dead frame (#1567), and the direct-argument escape classification (#1566).
ehartford
force-pushed
the
fix-1471-loop-closure-capture
branch
from
September 24, 2026 14:59
35510a5 to
f96156c
Compare
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.
Fixes #1471. Stack bottom (base
main); #1486's PR stacks on this.Spec §12.4: "For
Copyvalues, default capture copies the value." A closure created in a loop body copies the Copy local at its own creation.Root cause
src/CodegenDispatch.wgen_closure, "Build capture struct on stack": the environment wascreate_entry_alloca(cap_struct_type)— one slot per closure site, hoisted to function entry. Every iteration re-stored its captures into that slot and handed out a fat pointer to it, so all closures from the site read the last iteration's values. Evidence: pre-optimize IR (WITH_DUMP_LLIR_PRE=1) of the repro has onealloca { i32 }in main's entry and onestoreper iteration into it;run --debug-allocprinted20 20 20.Fix MirLower tags the
CK_CLOSUREconstant (d2 = 1, printed asconst closure(nodeN, in-loop)by--dump-mir) when the closure expression sits inside a loop;gen_closurethen allocates the environment at the creation point, so each evaluation owns its storage for the rest of the frame. A site outside a loop keeps its entry slot. The non-escaping mark could not select the case: a direct argument is non-escaping by §12.3 even when the callee stores it (#1566).Matrix (stage1
run --debug-alloc, allleak count=0): for binding / let in body / while counter / Copy struct / f64 / two captures / nested loop / via a user fn / let-bound in loop →0 10 20each (were20 20 20); non-escaping closure in a loop33; fourspawn_osthreads in a loop sum600(every thread ran with the last index before).Fixtures
test/behavior/behav_closure_loop_env_per_iteration.w,test/behavior/behav_closure_loop_spawn_os_index.w.analyze audit:allon the fixture: violations=0.Audits (stage1 vs pinned seed):
tools/drop_audit.w176 cells, 0 regressions;tools/move_audit.w73 cells, 0 vs-expected failures. All existing closure/spawn behavior, debug_alloc and compile_errors fixtures pass on stage1. Iterate tier only — no battery yet.Filed, not fixed #1566 (direct-arg closure classified non-escaping even when the callee stores/returns it), #1567 (a closure returned from its creating function points at the dead frame — needs an env-ownership ruling).