Skip to content

Soundness: if/else move-state is not merged — conditional reinit hides a move (use-after-move compiles) #612

Closed

Description

@ehartford

Summary

Sema's move checker does not merge move-state across if/else branches. When
one branch moves a binding and the other reinitializes it, the move is silently
lost and a subsequent use is accepted — a use-after-move that compiles.

Repro

type R { id: i32 }
impl Drop for R:
    fn drop(move self: Self): let _ = self.id
fn take(r: R): ()
fn make() -> R: R { id: 1 }
fn use2(r: R): ()

fn main(d: bool):
    var r = make()
    if d:
        take(r)        // r moved on this path
    else:
        r = make()     // r reinitialized on this path
    use2(r)            // ACCEPTED today — but on d==true, r was moved → use-after-move

with check exits 0. On the d == true path take(r) consumes r, then
use2(r) uses the consumed value.

Contrast (correctly rejected today):

if d: take(r) else: ()
use2(r)   // error: use of moved value  ✓

So the bug is specifically conditional-reinit in the other branch overwriting the
move.

Root cause

  • VarState is binary LIVE/MOVED (src/Sema.w:52); there is no maybe-moved state.
  • Sema.check_if_expr (src/SemaCheck.w:7333+) checks the branches linearly
    and only restore_scope_states when a branch diverges (TY_NEVER). It never
    merges the two branches' states. The else branch's r = make() (sets LIVE,
    :8072) overwrites the then branch's take(r) (sets MOVED, :18645), so the
    post-if state is LIVE.

The sound merge is the conservative union: a binding is LIVE after the if only if
it is LIVE on both branches; if either branch leaves it moved-without-reinit, it
is (maybe-)moved after the if.

Spec

  • §2.2 Move Semantics — "after a move, the source binding is invalid."
  • §21.1 rule 3 Use-after-move — "a binding that has been moved from must not be used."
  • mission.md — "exactly as safe as Rust." Rust rejects this repro.

Why it matters

This is foundational. It is the under-rejection face of the same missing
infrastructure that #579 hits as over-rejection (a diverging match arm makes the
owner look moved on the fallthrough). It also blocks sound loop maybe-init analysis:
any loop that accepts a moved-then-conditionally-reinit binding would inherit this
hole and accept a cross-iteration use-after-move.

Expected

The repro must be a compile error (use of moved value). if/else (and match)
must merge move-state as a conservative union, divergence-aware (a branch that
diverges does not contribute its moves to the join — already handled via the
TY_NEVER restore).

Activity

  1. ehartford commented on Jun 27, 2026

    @ehartford
    ContributorAuthor

    Fixed. The use-after-move soundness hole was the linear (non-merged) move-state in if/else and match. Implemented a divergence-aware conservative-union join: snapshot the entry move-state, analyze each branch/arm from it, and union the non-diverging exits (a binding is MOVED after the construct iff MOVED on any non-diverging path — the MaybeUninitialized use-checking half; MIR drop flags remain the elaboration half).

    The repro now reports 'use of moved value'; the symmetric over-rejections (move-on-both-branches; #579's returning arm) now compile. Full fresh :test green, fixpoint green, no self-build cascade. if-let/while-let/&&/||/? were verified already sound (conservative), so this closes the soundness bug; remaining flow-sensitivity items are tracked separately: loops (#613), place-granular field moves (Slice E), and the design/spec write-up in docs/branch-merge-soundness.md.

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