Skip to content

Tests: drop order coverage (§20, §21) #495

Closed

Description

@ehartford

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"):

  • MirBuilder keeps a drop stack drop_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).
  • Drop trait exists (lib/std/traits.w:38); emit_drop_entry (:273-343) lowers DK_VALUE to a StmtKind.Drop, plus task/guard/scope kinds.
  • Existing tests do not cover order: test/behavior/behav_drop_order.w and behav_drop.w only assert defers haven't fired inside the scope (no ordering observation at all); test/spec/spec_ss02_4_by_value_drop.w and the spec_ss02_4_* family cover by-value/field-move drop semantics, not multi-local ordering; spec_ss04_3_record_update_drops_overwritten_fields.w covers 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).

  1. Build a shared fixture shape: a Tracker { name: str } struct with impl Drop whose drop prints drop:<name>; declare locals a, b, c in order and assert stdout shows drop:c, drop:b, drop:a.
  2. Cover each MIR exit path explicitly (they are distinct code paths in src/MirLower.w and must each be observed): normal scope end, early return, break out of a loop, labeled break across 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).
  3. Interleave with defer in 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; expect drop:c drop:b drop:a (req 20.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 (req 21.1.1.11).
  • test/behavior/behav_drop_order_early_return.w — early return from a branch: all live locals drop in reverse order before return.
  • test/behavior/behav_drop_order_break.w — loop-local Drop values drop on break, reverse order, loop-iteration locals drop each iteration.
  • test/behavior/behav_drop_order_labeled_break.w — break 'outer from a doubly-nested loop drops both levels' locals, innermost scope first, reverse order within each scope.
  • test/behavior/behav_drop_order_goto.w — goto 'done past 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 + defer statements 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.w has at least one test observing reverse declaration order through printed Drop impls.
  • 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 :test pass; with build :test-green recorded.

Dependencies

Traceability

From the requirements campaign (docs/requirements.md, spec v7.1). Affected requirement IDs: 20.1.1.28, 21.1.1.11.

Activity

  1. ehartford commented on Jul 3, 2026

    @ehartford
    ContributorAuthor

    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.

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