Repository navigation
Codegen: a Box dropped as a Box drops its pointee by type — Box[L] of a recursive enum no longer leaks the Box inside the L (#1456) - #1546
Merged
Conversation
ehartford
force-pushed
the
fix-1456-box-recursive-enum-drop
branch
from
September 23, 2026 20:17
a6b42a5 to
4949479
Compare
ehartford
force-pushed
the
fix-1456-box-recursive-enum-drop
branch
from
September 23, 2026 20:43
4949479 to
bee5c56
Compare
This was referenced Sep 23, 2026
Merged
Contributor
Author
|
Stack #1547 battery (seed-driven, new seed, cold c_import cache, with :drop-audit/:move-audit) on main ce7508c + #1537 → … → #1546 (head bee5c56; every PR head is a tested commit; includes the #1451 same-named-enum fix and the render.w rebase over #1513): build ok, :fixpoint ok, :drop-audit ok, :move-audit ok, :test ok (no reds), :test-green ok, :last-green ok. Merges cleanly into main 02301f8. Merge the stack: |
ehartford
marked this pull request as ready for review
September 23, 2026 21:34
ehartford
force-pushed
the
fix-1456-box-recursive-enum-drop
branch
from
September 23, 2026 23:51
bee5c56 to
f4290fa
Compare
… a recursive enum no longer leaks the Box inside the L (#1456) `let b = Box.new(L.Cons(5, Box.new(L.Nil)))` leaked 32 bytes (one box). Route: debug allocator, then the drop's IR, then lldb on the emitter. Evidence. `with run --debug-alloc`: LEAK size=32 origin=with_alloc, leak count=1. Under lldb (ASLR off) WITH_DEBUG_ALLOC_TRAP_FREE on that address: one `trap-alloc hit=1`, no free — the inner box. `with ir`: main's scope-exit drop of `b` is `call @with_free_drop_origin(ptr %26)` and nothing else; L's drop glue is never called. lldb on stage1 with breakpoints on Codegen.mir_emit_box_drop_place (x3 = sema type) and Codegen.mir_emit_drop_ptr: for x3 = 249 (Box[L]; the IR names `Box.new__receiver__249_235_235`, L = 235) the box drop calls mir_emit_drop_ptr — CodegenDispatch.w mir_emit_box_drop_place, `self.mir_emit_drop_ptr(heap_ptr, payload_ty)`. That drops by LLVM type only: `%L` resolves to the enum's symbol, which has no Drop impl, and the struct-field glue walks no fields of an enum, so the pointee's payload was never dropped. A Box inside an enum payload drops through Box's own Drop (by type), which is why one level of L did not leak. Fix. The Box place drop drops its pointee with mir_emit_drop_ptr_for_sema_type (the payload's sema type) before the free; the LLVM-type path stays for a pointee with no sema type (a `dyn` box). Same null guard as before. Matrix (da_box_pointee_dropped_by_type, checked with tools/debug_drop.w, the debug-alloc lane's driver): the issue's Box of a two-level list, a four-level list, a list moved into a match binding by value, a Box moved into `fn take(b: Box[L])`, Box[str], Box[Vec[str]], a Box of a struct with str and Vec[str] fields, Box[Option[str]], a Box of a tree enum holding Vec[Box[T]], a Vec[Box[L]], a reassigned `var` Box. Base: 23 leaks, the lane FAILs the fixture. Now: leak count 0; also 0 with WITH_ALLOC_NO_REUSE=1 and no double-free report. Filed while here: a Box in an Option payload is never freed at all (`Option[Box[str]]` leaks the box and the str; the payload path drops Box only through a pre-frozen concrete Drop, #1535).
ehartford
force-pushed
the
fix-1456-box-recursive-enum-drop
branch
from
September 23, 2026 23:54
f4290fa to
1322ade
Compare
ehartford
added a commit
that referenced
this pull request
Sep 24, 2026
…uilt Vec[i32]; main red at stage2 after #1546 + #1581 (#1596) * MIR: the three switch tables built from Vec[i32] take Vec[i64] — discriminants are i64 end to end (#1541) and the width check (#1368) now rejects the narrowed table; main was red at stage2 * drop-audit: the field_join_view match cell indents its arms under the match — the seed parsed the flush arms by accident; §29.13 (#1391) rejects them * MirValidationTests: the last switch table built from Vec[i32] takes Vec[i64] — the internals tests compile this file, src/main.w does not --------- Co-authored-by: Eric Hartford <[email protected]>
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.
let b = Box.new(L.Cons(5, Box.new(L.Nil)))leaked 32 bytes (one box).Route: debug allocator, then the drop's IR, then lldb on the emitter.
Evidence.
with run --debug-alloc: LEAK size=32 origin=with_alloc, leakcount=1. Under lldb (ASLR off) WITH_DEBUG_ALLOC_TRAP_FREE on that
address: one
trap-alloc hit=1, no free — the inner box.with ir:main's scope-exit drop of
biscall @with_free_drop_origin(ptr %26)and nothing else; L's drop glue is never called. lldb on stage1 with
breakpoints on Codegen.mir_emit_box_drop_place (x3 = sema type) and
Codegen.mir_emit_drop_ptr: for x3 = 249 (Box[L]; the IR names
Box.new__receiver__249_235_235, L = 235) the box drop callsmir_emit_drop_ptr — CodegenDispatch.w mir_emit_box_drop_place,
self.mir_emit_drop_ptr(heap_ptr, payload_ty). That drops by LLVM typeonly:
%Lresolves to the enum's symbol, which has no Drop impl, andthe struct-field glue walks no fields of an enum, so the pointee's
payload was never dropped. A Box inside an enum payload drops through
Box's own Drop (by type), which is why one level of L did not leak.
Fix. The Box place drop drops its pointee with
mir_emit_drop_ptr_for_sema_type (the payload's sema type) before the
free; the LLVM-type path stays for a pointee with no sema type (a
dynbox). Same null guard as before.Matrix (da_box_pointee_dropped_by_type, checked with tools/debug_drop.w,
the debug-alloc lane's driver): the issue's Box of a two-level list, a
four-level list, a list moved into a match binding by value, a Box moved
into
fn take(b: Box[L]), Box[str], Box[Vec[str]], a Box of a structwith str and Vec[str] fields, Box[Option[str]], a Box of a tree enum
holding Vec[Box[T]], a Vec[Box[L]], a reassigned
varBox. Base: 23leaks, the lane FAILs the fixture. Now: leak count 0; also 0 with
WITH_ALLOC_NO_REUSE=1 and no double-free report. Filed while here: a Box
in an Option payload is never freed at all (
Option[Box[str]]leaks thebox and the str; the payload path drops Box only through a pre-frozen
concrete Drop, #1535).
Layer of the enums/discriminants stack (#1455 → #1451 → #1452 → #1453 → #1454 → #1441 → #1442 → #1439 → #1456). Draft until the stack's seed-driven battery is green (D56).