Skip to content

Move checker: value moved BEFORE a loop is wrongly flagged at the loop's back-edge #696

Closed

Description

@ehartford

Pre-existing latent move-checker imprecision (the loop sibling of #695), found inventorying #691's flip. Reproduces on the shipped compiler with any Drop-bearing value.

Repro (exit 1)

type D { id: i32 }
impl Drop for D:
    fn drop(move self: Self): ()
fn consume(d: D): ()
fn f(n: i32):
    let fresh = D { id: 1 }
    consume(move fresh)        // moved BEFORE the loop, never used inside it
    for i in 0..n:
        if i > 0:
            continue           // ERROR: `fresh` is moved inside a loop and not reinitialized
        let _x = 0

error: use of moved value: fresh is moved inside a loop and not reinitialized before the loop repeats at the continue.

Root cause

The loop move-state check (finalize_loop_move_state) flags any variable that is MOVED-and-not-reinitialized at a loop back-edge (continue / end-of-body). But fresh is moved before the loop and is neither used nor moved inside it — it is not loop-carried, so it has no bearing on iteration. The check must only consider variables that are moved WITHIN the loop body (or live-and-moved across the back-edge), not values already moved on entry to the loop.

Why it matters

  • Correctness on the shipped compiler: a legal "move a value, then loop" pattern is rejected.
  • Dominant cluster of the Campaign: the wide flip — containers own their buffers (POD-element drop elaboration) #691 wide-flip migration: the compiler's own codegen (mir_emit_function etc.) sets up let fresh_x = Vec.new(); self.x = fresh_x before per-parameter loops with continue. Once the flip makes Vec need drop, ~105 of the flip's ~127 error sites are exactly this false positive. Fixing it here dissolves that cluster.

Fix direction

At the loop back-edge move-state check, restrict the "moved-not-reinitialized" diagnostic to variables whose move occurred inside the loop body (loop-carried), excluding those already in the moved state at loop entry. Repro becomes a behavior fixture; add a genuine loop-carried-move case that MUST still error, to pin the boundary.

Activity

  1. ehartford commented on Jul 21, 2026

    @ehartford
    ContributorAuthor

    Fixed in ee4e5f4.

    Root cause (proven at the instruction level, not inferred). The move checker computes "moved across this edge without reinit?" separately at each loop back-edge, and the two implementations had drifted:

    • finalize_loop_move_state (fall-through back-edge): fires on entry[i]==LIVE && cur[i]==MOVED && needs_drop ✓
    • check_loop_continue_carried_move (continue back-edge): fired on cur[i]==MOVED && needs_drop — missing the entry==LIVE guard.

    The continue check never had the loop-entry snapshot, so it could not distinguish "moved before the loop" from "moved inside it." A WITH_TRACE_MOVE run on the repro confirmed it directly: for fresh, entry=MOVED(1) at_continue=MOVED(1) → old_verdict=1 (fired) new_verdict=0 (suppressed).

    This shipped in #613 (1ee372c) with zero tests for the diagnostic. It is not flip-specific — a plain Drop struct moved before a for/while/loop with a continue reproduces it on the shipped seed, no Vec involved.

    Fix. Capture the loop-entry move-state per frame (loop_entry_flat, parallel to loop_break_flat, sharing label_break_off) at alloc_loop_break_region, and apply the same entry==LIVE guard in the continue check. One change covers all three loop constructs (they share the continue path).

    Regression coverage.

    Verified: full battery green (build, fixpoint, audit:all, move-audit, drop-audit, test, test-green, last-green).

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