Skip to content

Codegen: a drop of a by-place closure capture frees the captured place, not the closure's pointer slot (#1486, str half) - #1584

Merged
ehartford merged 1 commit into
fix-1471-loop-closure-capturefrom
fix-1486-closure-capture-assign
Sep 24, 2026
Merged

ehartford merged 1 commit into
fix-1471-loop-closure-capturefrom
fix-1486-closure-capture-assign

Conversation

@ehartford

Copy link
Copy Markdown
Contributor

Stacked on the #1471 PR (base fix-1471-loop-closure-capture). Addresses the str half of #1486; the i32 half needs a ruling (below) and is not changed.

Spec §12.4: "For non-Copy values, default capture is by place: the closure observes or mutates the original place according to its body." A non-escaping closure that assigns a captured non-Copy place drops the old value at the original place and stores the new one, exactly once each.

Root cause src/CodegenDispatch.w: the three drop paths (mir_emit_drop_place_current_origin, mir_emit_box_drop_place, mir_emit_refcount_drop_place) freed at mir_place_ptr — the local's storage. For an indirect value local (a by-place capture, whose storage holds a pointer to the outer slot) the load path (mir_try_place_ptr_for_ref) and store path (Assign arm) read through mir_indirect_value_local_ptr; the drop paths did not. Closure IR of the issue repro (WITH_DUMP_LLIR_PRE=1):

%3 = alloca ptr ; store ptr %5, ptr %3             ; outer c's address
call void @with_str_free_drop_origin(ptr %3, ..)   ; frees the SLOT as a str
%7 = load ptr, ptr %3 ; store %str %8, ptr %7      ; the store derefs

→ SIGSEGV in main at the call (lldb: r1486a.w:5). For a struct capture the drop type was the slot's ptr type (no glue), so the old value was never dropped (leak count=1).

Fix one mir_drop_place_ptr for all three paths — a projection-free indirect local drops through its pointer — and the structural path uses the indirect value's LLVM type for its glue.

Matrix (stage1 run --debug-alloc, leak count=0 everywhere): str assign (was SIGSEGV), Vec assign (was SIGSEGV), Vec push, struct field assign, struct whole assign (was leak), str assign in a loop and twice (were SIGSEGV), str read, str moved out through a direct-arg closure. Fixture test/debug_alloc/da_closure_by_place_capture_assign.w (passes under tools/debug_drop.w check). analyze audit:all: violations=0.

Audit cells added tools/drop_audit.w was blind to closures: closure_assign × {bare, field, tuple, option, enum, boxbare, rcbare, boxfield} and closure_assign_twice. Stage1 vs seed: 185 cells, 0 non-PASS, 0 regressions; every new cell FIXED (seed: RUN-FAIL / LEAK / COMPILE-FAIL). move_audit: 73 cells, 0 vs-expected failures. The option cell spells Some(..) because .Some(..) inside a closure loses its captures (#1570, filed). Iterate tier only — no battery yet.

Open question (i32 half of #1486) — STOPPED, not changed. §12.4 says both "For Copy values, default capture copies the value" and "if a closure consumes, mutates, returns, or returns a view derived from a capture, those effects apply to the originating captured place." For run_unit(() => n += 1) these disagree: the first makes n a snapshot (today's behavior: n stays 1, mutation silently lost), the second writes n back (the issue's expectation, 2 and 7). Rust (unique-borrow capture) writes back; the spec's own example only covers a read (let g = || n + 1 … n remains unchanged). Needs Eric's ruling: is an assigned Copy capture by place or by copy? (If by copy, a silent lost write is still a diagnostic gap.)

@ehartford
ehartford added this pull request to stack #1585 September 23, 2026 22:04
@ehartford
ehartford force-pushed the fix-1486-closure-capture-assign branch from 60fa01f to ffbc573 Compare September 23, 2026 23:34
@ehartford ehartford changed the title Codegen: a drop of a by-place closure capture frees the captured place, not the closure's pointer slot (#1486, str half) Closures: a by-place capture's drop frees the captured place; a write to a Copy capture is a compile error (#1486) Sep 23, 2026
@ehartford

Copy link
Copy Markdown
Contributor Author

Rebased on 02301f8 and squashed to one commit. Now also the i32 half, per the decision from §12.4 ("For Copy values, default capture copies the value" + the let g = || n + 1 … n remains unchanged example): the copy is the specified behavior, and since no spelling mutates an outer Copy binding from a closure (#1586), an assignment or compound assignment to a Copy capture of a non-move, non-scoped-spawn closure is a compile error: "assignment to n writes the closure's copy of a Copy capture; the outer n is unchanged (§12.4). Declare n inside the closure, or keep the value in a non-Copy cell (a one-element Vec) and mutate that". Fixtures: err_closure_copy_capture_assign{,_compound_assign,_assign_let_bound,_assign_in_loop,_assign_nested}.w; behav_closure_copy_capture_modes.w rewritten and closure_ref_capture.w retired (their Copy writes are the error); move closures keep assigning their own copy (test/mut_move_closure.w). Stack continues: #1481 layer, then #1567 layer.

@ehartford
ehartford force-pushed the fix-1486-closure-capture-assign branch from ffbc573 to b7680b5 Compare September 24, 2026 03:24
@ehartford ehartford changed the title Closures: a by-place capture's drop frees the captured place; a write to a Copy capture is a compile error (#1486) Codegen: a drop of a by-place closure capture frees the captured place, not the closure's pointer slot (#1486, str half) Sep 24, 2026
@ehartford

Copy link
Copy Markdown
Contributor Author

Reworked under D62 (Eric's §12.4 ruling): the Copy-capture-assignment diagnostic is retired — this PR is back to the str-half codegen fix only (drop through the by-place capture pointer, drop_audit closure cells). Copy captures are now by place in #1591.

…e, not the closure's pointer slot — `() => c = compute("x")` segfaulted (#1486, str half)

Spec §12.4: "For non-`Copy` values, default capture is by place: the
closure observes or mutates the original place according to its body."
A non-escaping closure that assigns a captured `str`/`Vec`/struct/Box/Rc
drops the old value at the ORIGINAL place and stores the new one there,
exactly once each.

Root cause (src/CodegenDispatch.w): the three drop paths
(mir_emit_drop_place_current_origin, mir_emit_box_drop_place,
mir_emit_refcount_drop_place) took `mir_place_ptr` — the local's storage —
as the address to free. For an INDIRECT value local (a by-place capture,
whose storage holds a pointer to the outer slot) the load path
(mir_try_place_ptr_for_ref) and the store path (the Assign arm) read
through `mir_indirect_value_local_ptr`; the drop paths did not. Evidence,
the closure's pre-optimize IR (WITH_DUMP_LLIR_PRE=1, issue repro):
  %3 = alloca ptr ; store ptr %5, ptr %3            (the outer c's address)
  call void @with_str_free_drop_origin(ptr %3, ..)  (frees the SLOT as a str)
  %7 = load ptr, ptr %3 ; store %str %8, ptr %7     (the store derefs)
so the "str" freed had the outer address as its data pointer: SIGSEGV in
main at the call (lldb, r1486a.w:5). For a struct capture the drop type
was the slot's `ptr` type, which has no drop glue, so the old value was
never dropped at all (leak count=1 on `s = S { .. }`).

Fix: one `mir_drop_place_ptr` for all three paths — a projection-free
indirect local drops through its pointer — and the structural path takes
the indirect value's LLVM type for its glue.

Matrix (stage1 run --debug-alloc, leak count=0 everywhere): str assign
(was SIGSEGV), Vec assign (was SIGSEGV), Vec push, struct field assign,
struct whole assign (was leak 1), str assign in a loop and twice (were
SIGSEGV), str read, str moved out through a direct-arg closure. Fixture:
test/debug_alloc/da_closure_by_place_capture_assign.w (passes under
tools/debug_drop.w `check`). `analyze audit:all`: violations=0.

Audit: tools/drop_audit.w was blind to closures; nine cells added
(closure_assign × bare/field/tuple/option/enum/boxbare/rcbare/boxfield,
closure_assign_twice). Stage1 vs seed: 185 cells, 0 non-PASS, 0
regressions; every new cell FIXED (seed: RUN-FAIL/LEAK/COMPILE-FAIL).
tools/move_audit.w: 73 cells, 0 vs-expected failures. The option cell
spells `Some(..)`: `.Some(..)` inside a closure loses its captures
(#1570, filed).

The issue's i32 half (`() => n += 1` leaving `n` unchanged) is not
changed here: §12.4 says a Copy capture "copies the value" and also that a
mutating closure's effects "apply to the originating captured place" —
which one governs an assigned Copy capture needs a ruling.
@ehartford
ehartford force-pushed the fix-1486-closure-capture-assign branch from 68a6de0 to 18df749 Compare September 24, 2026 14:59
@ehartford
ehartford merged commit ad6107c into main Sep 24, 2026
@ehartford
ehartford deleted the fix-1486-closure-capture-assign branch September 24, 2026 16:56
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