Skip to main content

Module ownership

Module ownership 

Source
Expand description

Native ownership analysis for use-after-move detection.

Lightweight data-flow analysis that catches use-after-move at check-time (milliseconds), before Rust compilation. This pass tracks Owned, Moved, and Borrowed states through control flow.

Collections have value semantics in every tier: a call’s plain argument and Let b be a each receive a copy, and the caller’s value stays usable. Only GIVING moves — the Give x to y. statement, and give x as a call argument (Call consume with give data.), which the backend emits as a real move.

§State Transitions

         Let x be value
              │
              ▼
          [Owned] ── f(x) · Let y be x ──▶ [Owned]
         /        \
   Give x       Show x
 … with give x      │
       │            ▼
       ▼       [Borrowed]
   [Moved]          │
       │        use x? ✓
   use x?
       │
    ERROR: use-after-move

§Control Flow Awareness

The checker handles branches and loops by merging states:

  • Moved on one path + Owned on another = MaybeMoved, remembering whether a branch or a loop made the move uncertain
  • A loop may run no pass at all, so its body’s moves merge with the state before it
  • Using a MaybeMoved variable produces an error, wherever it is read: an If or While condition, the collection a Repeat walks and the value an Inspect examines included

§The Program as Written

Ownership is a property of the program its author wrote, so every entry point runs this checker on the freshly-parsed statements, before any rewrite. Once x is a known constant, the optimizer’s propagation turns Give x to p. into Give 5 to p. and Show x. into Show 5., and folding flattens a constant If into its body: a checker fed that program would pass a use after a possible move, or call a possible move certain.

§Example

Let x be 5.
Give x to y.
Show x to show.  ← Error: Cannot use 'x' after giving it away

Structs§

OwnershipChecker
Ownership checker - tracks variable states through control flow
OwnershipError
Error type for ownership violations
OwnershipFinding
One ownership finding with its statement coordinates: the top-level statement that ERRED, and (when known) the one that CAUSED it by moving the variable — both map 1:1 onto Parser::stmt_spans() — plus the exact statements, nested ones included, for Parser::nested_stmt_spans(): trail is the erring statement and each one around it, innermost first, and cause is the statement that gave the value away.

Enums§

MaybeGivenIn
Where a value may have been given away: the construct whose paths disagree about it.
OwnershipErrorKind
VarState
Ownership state for a variable