Skip to content

Loop maybe-init dataflow: accept reinit (§2.4) and move-then-break; reject only genuine use-after-move #613

Closed

Description

@ehartford

Summary

Whole-value moves of an outer binding inside a loop are blanket-rejected today
with a misleading message. Per §2.4 and Rust-equivalence, the loop case should be
flow-sensitive: accept reinitialization and move-then-break; reject only a move that
reaches the back-edge without reinit (a genuine use-after-move).

Current behavior (all rejected, same message)

// (1) §2.4's own example — SHOULD COMPILE
var h = make(); while c: h = transform(h)
// (2) move-then-break — SHOULD COMPILE (Rust accepts)
loop: if d: take(move r); break
// (3) move every iteration, no reinit — SHOULD REJECT as use-after-move
while k: take(r)

All three: error: conditional move of Drop value requires drop-state tracking
(src/SemaCheck.w:5241/:18637, fired via outer_binding_has_unsupported_move_context
because loops push move_control_flow_context(0)).

Ought to work

  • reinit before back-edge (1) → accept; value is LIVE at loop exit → normal drop.
  • move only on an exiting path (2) → accept; drop flag for the exit cleanup.
  • move reaches the back-edge without reinit (3) → reject with use of moved value
    (Rust: "value moved in previous iteration of loop"). This stays rejected — it is a
    correct permanent rejection, NOT a missing feature.

Design

Loops push_move_control_flow_context(1) (allow the move at the move-point), then a
back-edge check after the loop body: a binding that was LIVE at loop entry and is
MOVED at body-end was moved without reinit → emit use-after-move. The existing
TY_NEVER restore_scope_states already makes move-then-break end LIVE (accepted);
straight-line reinit ends LIVE (accepted); err_loop ends MOVED (rejected).

Blocked by

#612 (branch-merge soundness). The back-edge check trusts the body-end move-state.
While #612 is open, while c: if d: take(r) else: r=make() reports r LIVE at
body-end (the merge gap) and would be unsoundly accepted (cross-iteration UAM on the
d-true path). Fix #612 first, then this is sound.

Spec

§2.4 (drop-on-reassignment shown explicitly for loops), §21.1 rule 3 (use-after-move),
mission.md ("exactly as safe as Rust"). Part of the #607 substrate / docs/drop-move-ownership.md
Milestone 7.

Activity

  1. ehartford commented on Jun 28, 2026

    @ehartford
    ContributorAuthor

    Implemented in 1ee372c. All four loop constructs (while, do-while, loop, for) now allow conditional Drop moves with a sound flow-sensitive analysis: back-edge use-after-move check, continue-carried-move check, and a per-loop break accumulator for the post-loop state. Move-then-break compiles; loop-carried move (no reinit) and post-loop use of a break-moved value are rejected with 'use of moved value'. Reinit-in-loop now compiles. Scoped to needs-drop values (POD Vec move is a copy today, #607). Gates green: build, fixpoint, debug-alloc-tests, fresh test.

    Note: the §2.4 reinit pattern's exact drop count is blocked by #614 (pre-existing reassign-after-move double-drop); the reinit fixtures assert compile+run until #614 lands.

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