Repository navigation
Codegen: a drop of a by-place closure capture frees the captured place, not the closure's pointer slot (#1486, str half) - #1584
Conversation
60fa01f to
ffbc573
Compare
|
Rebased on 02301f8 and squashed to one commit. Now also the i32 half, per the decision from §12.4 ("For |
ffbc573 to
b7680b5
Compare
|
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. |
b7680b5 to
68a6de0
Compare
…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.
68a6de0 to
18df749
Compare
Stacked on the #1471 PR (base
fix-1471-loop-closure-capture). Addresses thestrhalf of #1486; thei32half needs a ruling (below) and is not changed.Spec §12.4: "For non-
Copyvalues, 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 atmir_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 throughmir_indirect_value_local_ptr; the drop paths did not. Closure IR of the issue repro (WITH_DUMP_LLIR_PRE=1):→ SIGSEGV in
mainat the call (lldb:r1486a.w:5). For a struct capture the drop type was the slot'sptrtype (no glue), so the old value was never dropped (leak count=1).Fix one
mir_drop_place_ptrfor 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=0everywhere): 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. Fixturetest/debug_alloc/da_closure_by_place_capture_assign.w(passes undertools/debug_drop.w check).analyze audit:all: violations=0.Audit cells added
tools/drop_audit.wwas blind to closures:closure_assign × {bare, field, tuple, option, enum, boxbare, rcbare, boxfield}andclosure_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 spellsSome(..)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
Copyvalues, 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." Forrun_unit(() => n += 1)these disagree: the first makesna snapshot (today's behavior:nstays 1, mutation silently lost), the second writesnback (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.)