Repository navigation
Description
Tests: drop order coverage (§20, §21)
Summary
Reverse-declaration-order destruction is implemented in MIR lowering and is normative in two places (§20's deterministic-destruction guarantee and §21.1's drop-point insertion rule), but no test observes the order. Existing "drop" behavior tests only verify that defer does not fire prematurely. This issue adds positive coverage that destruction order is reverse declaration order on every scope-exit path.
Spec
- §20 Performance Guarantees — requirement
20.1.1.28: "Deterministic destruction. Reverse declaration order." - §21.1 Rule 7 (docs/with-specification.md:10577-10580) — requirement
21.1.1.11: "The compiler inserts implicit drop points at scope exit in reverse declaration order." - §14.7 cancellation point 4 (destructors run "in reverse declaration order, just as with normal scope exit") is the async variant of the same guarantee (runtime-order coverage there belongs to Tests: task surface coverage (§14) #531).
Current state
Implemented, untested (requirement verdicts: 20.1.1.28 "reverse drop order untested", 21.1.1.11 "codegen drops reverse order"):
MirBuilderkeeps a drop stackdrop_local_ids/drop_kinds(src/MirLower.w:43, pushed at:180-187) and emits drops by walking it from the end (var i = len - 1; while i >= drop_start: emit_drop_entry(...); i = i - 1) on every exit path:pop_scope_with_goto(src/MirLower.w:345-358),pop_scope_inline(:379-391),emit_drops_for_break(:410-414),emit_drops_for_range(:423-427),emit_cleanup_to_target(:429-446),emit_drops_for_return(:448-452).Droptrait exists (lib/std/traits.w:38);emit_drop_entry(:273-343) lowersDK_VALUEto aStmtKind.Drop, plus task/guard/scope kinds.- Existing tests do not cover order:
test/behavior/behav_drop_order.wandbehav_drop.wonly assert defers haven't fired inside the scope (no ordering observation at all);test/spec/spec_ss02_4_by_value_drop.wand thespec_ss02_4_*family cover by-value/field-move drop semantics, not multi-local ordering;spec_ss04_3_record_update_drops_overwritten_fields.wcovers record update only.
Gap: nothing fails today if someone reverses the while i >= drop_start walk to forward order.
Implementation plan
Test-only issue — no compiler changes expected. If any test below exposes a real ordering bug, that bug becomes its own issue with a repro (root-cause rule; do not bend the test).
- Build a shared fixture shape: a
Tracker { name: str }struct withimpl Dropwhosedropprintsdrop:<name>; declare localsa,b,cin order and assert stdout showsdrop:c,drop:b,drop:a. - Cover each MIR exit path explicitly (they are distinct code paths in
src/MirLower.wand must each be observed): normal scope end, earlyreturn,breakout of a loop, labeledbreakacross nested scopes (emit_cleanup_to_target),goto(pop_scope_with_goto), and nested-block boundaries (inner block's locals drop at inner exit, before later outer statements). - Interleave with
deferin one test to pin the relative order of defers vs drops at the same scope exit as currently implemented (document observed order in the test header; if spec is silent, the test records implementation behavior deliberately).
Test plan
All //! expect-stdout behavior/spec tests with exact multi-line expected output:
test/spec/spec_ss20_1_reverse_drop_order.w— three Drop locals in one scope; expectdrop:cdrop:bdrop:a(req20.1.1.28).test/spec/spec_ss21_1_drop_points_scope_exit.w— nested blocks: inner locals drop at inner block end (before subsequent outer prints), outer locals at function end, each group reversed (req21.1.1.11).test/behavior/behav_drop_order_early_return.w— earlyreturnfrom a branch: all live locals drop in reverse order before return.test/behavior/behav_drop_order_break.w— loop-local Drop values drop onbreak, reverse order, loop-iteration locals drop each iteration.test/behavior/behav_drop_order_labeled_break.w—break 'outerfrom a doubly-nested loop drops both levels' locals, innermost scope first, reverse order within each scope.test/behavior/behav_drop_order_goto.w—goto 'donepast the end of a scope runs that scope's drops in reverse order (also guards Unreachable-code detection: goto and Never-call divergence (§20b.5) #543's goto-divergence work from disturbing drop lowering).test/behavior/behav_drop_order_with_defer.w— Drop locals +deferstatements interleaved; exact combined order asserted.
Negative coverage is not applicable (the requirement is an ordering guarantee, not a diagnostic); the "negative" is that any order regression flips the expected stdout.
Acceptance criteria
- Each of the six distinct drop-emission paths in
src/MirLower.whas at least one test observing reverse declaration order through printedDropimpls. - Tests pass against the current compiler unmodified (or any discovered ordering bug is filed separately with its repro, referenced here).
-
with build && with build :fixpoint && with build :testpass;with build :test-greenrecorded.
Dependencies
- Tests: drop semantics coverage (§2) #496 (drop semantics tests §2) — adjacent coverage; coordinate fixture naming to avoid duplicate Tracker scaffolding.
- Bug: Drop expression temporaries are never dropped (§2.4) #430 (Drop expression temporaries never dropped) — known bug adjacent to this surface; these tests use bound locals only, so they should pass regardless, but the temporaries bug must not be masked by them.
- Borrow checker Rule 7: implicit drop is a use (§21.1) #444 (Rule 7 drop-as-use) — relies on the reverse order these tests pin down.
- Tests: task surface coverage (§14) #531 (task surface tests) — owns the async/cancellation-unwinding variant of drop-order coverage.
Traceability
From the requirements campaign (docs/requirements.md, spec v7.1). Affected requirement IDs: 20.1.1.28, 21.1.1.11.
Activity
ehartford commentedon Jul 3, 2026
Closing — reverse drop order pinned by spec_ss20_1_reverse_drop_order (6c59303): normal fallthrough (cba), early-return (ba), break (ba), each observing its own trace. §20/§21.
test: coverage-sweep batch 1 (#495 #496 #521 #518 #529 #492 #499)