Skip to content

Tests: drop semantics coverage (§2) #496

Closed

Description

@ehartford

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 var binding 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) — old h dropped 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 Drop impl.

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 by pop_scope_inline/pop_scope_with_goto reverse-emit (src/MirLower.w:345-408 over drop_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 in lower_assign (src/MirLower.w:3245-3252; the original survey note "MirLower.w:3244" points here): before the store it pushes StmtKind.Drop when the destination type is a non-Copy TY_STRUCT whose name passes Sema.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 Drop impl 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 classifies Wrap non-Copy, so MirLower schedules DK_VALUE; the failure is downstream: (a) lower_assign's reassignment guard requires has_drop_method(type_name) on the outer type, so no reassignment drop is emitted for Wrap; (b) the scope-exit StmtKind.Drop exists 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's drop_ty resolution 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_ptr returns 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

  1. Add test/spec/spec_ss02_4_drop_order_and_reassignment.w using the established trace pattern (module-level var 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.
  2. 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.
  3. While in the area, rename-or-annotate is out of scope: leave behav_drop.w/behav_drop_order.w as-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 inside if/else arms 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.w exists, 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 :test pass.

Dependencies

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

  1. ehartford commented on Jul 3, 2026

    @ehartford
    ContributorAuthor

    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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions