Repository navigation
Description
Summary
Commit test coverage for the §2.4 drop-semantics rules that the requirements campaign verified only manually: reverse-declaration drop order (2.4.1.6), drop-on-reassignment (2.4.1.7), and the loop-reassignment leak guarantee (2.4.1.8). While designing this coverage, probing exposed that two adjacent §2.4 behaviors are actually broken (transitive drop of fields in a struct without its own Drop impl, and drop of enum payloads) — those need to be split into their own bug issue; this issue lands the green tests and the failing repros.
Spec
§2.4 Destructors and defer (normative):
- "Drop order within a scope is reverse declaration order." (2.4.1.6)
- "Drop on reassignment: When a
varbinding of a Drop type is reassigned, the compiler drops the old value before storing the new one." (2.4.1.7) - "This prevents resource leaks in loops:" with the example
var h = create_resource(); for i in 0..n: h = transform(h)— oldhdropped each iteration (2.4.1.8) - Context for the split-out bug: "fields you use in your drop body are consumed, remaining fields are dropped automatically" — field destructors are part of the model, independent of whether the containing type has its own
Dropimpl.
Current state
Verified at 1c5258007a362fcd8c7a145b3e9c3be4c84ac47d with out/bin/with-stage2 trace probes (global TRACE appended in Drop.drop, same harness as test/spec/spec_ss02_4_by_value_drop.w):
Implemented and correct (needs committed tests, none exist):
- Reverse-order scope exit: three locals
a, b, c→[drop c][drop b][drop a]. Lowered bypop_scope_inline/pop_scope_with_gotoreverse-emit (src/MirLower.w:345-408overdrop_local_ids/drop_kinds, fields at MirLower.w:43-45). - Drop on reassignment:
var h = make("1"); h = make("2")→[drop 1]at the assignment,[drop 2]at scope exit. Emitted inlower_assign(src/MirLower.w:3245-3252; the original survey note "MirLower.w:3244" points here): before the store it pushesStmtKind.Dropwhen the destination type is a non-CopyTY_STRUCTwhose name passesSema.has_drop_method(src/Sema.w:3370). - Loop reassignment:
var h = make("0"); for i in 0..3: h = make(...)→[drop 0][drop 1][drop 2]inside the loop,[drop 3]at scope exit. No leak.
Existing test files do not cover any of this: test/behavior/behav_drop.w and behav_drop_order.w are misnomers — they only assert that defer does not run prematurely; no Drop impl appears in either. The real Drop-trait tests (test/spec/spec_ss02_4_*.w) cover by-value drop, field moves in drop bodies, and partial-move rejection, but not 2.4.1.6/7/8.
Discovered broken while writing the matrix (split out as a new bug issue):
- Struct with a Drop field but no own
Dropimpl never drops the field:type Wrap { inner: R }(R: Drop) → no drop at scope exit and no drop on reassignment.Sema.is_copy(src/Sema.w:3314-3323) correctly classifiesWrapnon-Copy, so MirLower schedulesDK_VALUE; the failure is downstream: (a)lower_assign's reassignment guard requireshas_drop_method(type_name)on the outer type, so no reassignment drop is emitted forWrap; (b) the scope-exitStmtKind.Dropexists but produces nothing at runtime —Codegen.mir_emit_drop_ptr(src/CodegenDispatch.w:3240) does have field recursion (mir_emit_drop_fields_ptr, :3215), so the residual failure point (type-sym resolution at :3244-3251 returning 0, or the Drop stmt'sdrop_tyresolution at :3477-3487) needs lldb confirmation in the split issue. - Enum payload holding a Drop value is never dropped:
enum Holder { Empty | Full(R) },let h = Holder.Full(make("es"))→ nothing.mir_emit_drop_fields_ptrreturns early for non-struct LLVM kinds and there is no per-variant payload drop glue.
The needed sema predicate for both is "type transitively needs drop", which does not exist today (has_drop_method is name-based and non-recursive).
Implementation plan
- Add
test/spec/spec_ss02_4_drop_order_and_reassignment.wusing the established trace pattern (module-levelvar TRACE = "",impl Drop for R: fn drop(move self: Self): TRACE = TRACE ++ ...). Cases as in the Test plan below. Pure test addition — no compiler, rt, or Link changes; no bootstrap hazard. - Add the two failing repros as expected-fail documentation in the new split bug issue (transitive drop glue), not as green tests here: file the issue with the §2.4 spec reference, both minimal repros, the file:line analysis above, and link it from this issue. Do NOT commit tests that assert the broken behavior as correct.
- While in the area, rename-or-annotate is out of scope: leave
behav_drop.w/behav_drop_order.was-is (they are valid defer tests); the new spec test carries the real coverage.
Test plan
test/spec/spec_ss02_4_drop_order_and_reassignment.w (//! expect-stdout: ok-style, or exact-trace asserts):
- 2.4.1.6: function declaring
a("a"),b("b"),c("c")then returning → assert trace== "[drop c][drop b][drop a]". Include an interleaved case (Copy locals between Drop locals) to pin order against future temp work (Bug: Drop expression temporaries are never dropped (§2.4) #430). - 2.4.1.7:
var h = make("1"); h = make("2"), marker between → assert"[drop 1]|marker"prefix, then scope exit appends[drop 2]. Negative ordering assertion: old value dropped before the new value is visible (drop body reads a global counter). - 2.4.1.8:
var h = make("0"); for i in 0..3: h = make(i+1)→ assert in-loop trace"[drop 0][drop 1][drop 2]"and final[drop 3]at scope exit; assert exactly 4 drops total (no leak, no double-drop). - Also pin the
match/branch case: reassignment insideif/elsearms drops the old value on both paths. - Negative coverage (already exists, verify still green):
test/compile_errors/err_partial_move_drop_field.w,err_conditional_drop_field_move.w.
Split bug issue inherits: Wrap { inner: R } scope-exit + reassignment repro, Holder.Full(R) repro, each as //! expect-stdout tests that land with the fix.
Acceptance criteria
-
test/spec/spec_ss02_4_drop_order_and_reassignment.wexists, asserts exact drop traces for 2.4.1.6/7/8, and passes. - Total drop counts asserted (no silent leak/double-drop can slip through a substring match).
- Transitive-drop bug (struct-without-own-Drop field drop; enum payload drop) is filed as its own issue with both repros and the file:line analysis, and is linked here — not papered over with weakened tests.
- docs/requirements.md checkboxes for 2.4.1.6/7/8 flip from "verified manually" to test-backed.
-
with build && with build :fixpoint && with build :testpass.
Dependencies
- Bug: Drop expression temporaries are never dropped (§2.4) #430 (expression temporaries / move handoff) — shares the trace harness; Bug: Drop expression temporaries are never dropped (§2.4) #430's fix must not regress these tests, so land this first.
- New split issue (transitive drop glue for Drop-field structs and enum payloads) — blocked-by nothing, blocks full §2.4 conformance.
- Borrow checker Rule 7: implicit drop is a use (§21.1) #444 (implicit drop is a use) — adjacent borrow-checker rule, no test overlap.
Traceability
From the requirements campaign (docs/requirements.md, spec v7.1). Affected requirement IDs: 2.4.1.6, 2.4.1.7, 2.4.1.8.
Activity
ehartford commentedon Jul 3, 2026
Closing — §2.4.1.6/7/8 pinned by spec_ss02_4_drop_reassignment (6c59303): reverse order, drop-before-overwrite on reassignment (1→2), loop-variable reinit (0,L,L,L = 4 drops, no leak/double-free). The transitive struct-field/enum-payload drops this issue flagged as a separate concern are covered by behav_drop_enum_payload / behav_drop_option_payload + the A6/A7 fixtures.
test: coverage-sweep batch 1 (#495 #496 #521 #518 #529 #492 #499)