Skip to content

Ownership Analyzer

github-actions[bot] edited this page Nov 30, 2026 · 3 revisions

Ownership Analyzer (Ferrous.Ownership)

Ferrous.Ownership runs a forward fixed-point data-flow analysis over Roslyn's ControlFlowGraph to enforce Rust-style affine move semantics (FE0200–FE0201) and borrowing exclusivity & escape rules (FE0202–FE0203) in C#.


Move Semantics (FE0200 & FE0201)

In vanilla .NET, handing off a pooled byte[], IMemoryOwner<byte>, or socket frame to a background writer leaves the original reference live on the stack—so accidentally mutating it after handoff silently corrupts data owned by another thread:

// Vanilla C#: compiles cleanly, corrupts a handed-off buffer
var packet = pool.RentPacket();
writer.EnqueueAndOwn(packet);
packet.Header = 0xFF; // Use-after-move

Annotate the handoff parameter with [Consuming]. Now any subsequent read, mutation, or second transfer—even through a local alias (var alias = packet;) or across conditional branches and loops—is a compile-time error with a clickable jump to the exact line where the value was moved:

using Ferrous;

void EnqueueAndOwn([Consuming] Packet packet) { /* takes ownership */ }

var packet = pool.RentPacket();
EnqueueAndOwn(packet);
packet.Header = 0xFF;
// error FE0200: use of moved value 'packet': value moved into 'EnqueueAndOwn()' at line 6;
//   help: reassign 'packet' before reusing it, or avoid consuming it

Conditional moves and loop back-edges are caught by FE0201:

var handle = new Handle();
if (shouldClose)
{
    Close(handle);
}

_ = handle.Id; // error FE0201: use of possibly moved value 'handle'

Reassigning a variable (or initializing it via out) revives it cleanly:

EnqueueAndOwn(packet);
packet = pool.RentPacket(); // revived
_ = packet.Header;          // OK

Borrowing & Escape Checks (FE0202 & FE0203)

In vanilla .NET, nothing stops a caller from aliasing the same buffer as both src and dst (Copy(buf, buf) or Swap(ref x, ref x)), nor does anything stop a callee from stashing a temporary borrowed reference into a field or closure where it outlives the caller's scope. [Borrow] and [BorrowMut] enforce Rust's aliasing-XOR-mutability and lexical borrow scope at compile time:

using Ferrous;

public sealed class Processor
{
    private Buffer? _leaked;

    public static void Copy([Borrow] Buffer src, [BorrowMut] Buffer dst) { /* ... */ }

    public void BadStore([Borrow] Buffer buf)
    {
        _leaked = buf;
        // error FE0203: borrowed parameter 'buf' cannot escape 'BadStore()' via storing it in a field or property;
        //   help: copy or clone the needed data instead of letting a borrowed reference outlive the call
    }

    public static void Caller()
    {
        var buf = new Buffer();
        Copy(buf, buf);
        // error FE0202: cannot borrow 'buf' as mutable because it is also borrowed as immutable in 'Copy()';
        //   help: pass distinct values or avoid aliasing a mutable borrow
    }
}

Diagnostics

ID Severity Description
FE0200 Error Definite use of a moved/consumed variable ([Consuming] parameter or [ConsumesSelf] receiver).
FE0201 Error Use of a possibly moved variable across conditional branches (if/else, switch) or loop iterations.
FE0202 Error Conflicting mutable borrow (aliasing-XOR-mutability) on [Borrow], [BorrowMut], or ref/in/out arguments in a single invocation.
FE0203 Error [Borrow] or [BorrowMut] parameter escapes its method via return, field/property assignment, or closure capture.

Clone this wiki locally