Repository navigation
Ownership Analyzer
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#.
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-moveAnnotate 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 itConditional 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; // OKIn 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
}
}| 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. |
Ferrous — Rust quality-of-life primitives, source generators, and analyzers for .NET (.NET 10 / .NET 11, NativeAOT-ready, MIT License).