Skip to content
Systems programming for what’s next.
— Vex

Pin — Move Prevention for Self-References ​

Pin is a prelude marker contract used by the compiler to prevent moves that would invalidate self-references. It is not a Pin<T> wrapper and it does not use a $ sigil.

Why pinning is needed ​

A value becomes address-sensitive when one of its fields can point to the value itself or to storage whose address would change with the owner:

vex
struct Node {
    value: i32,
    next: Ptr<Node>,
}

A pointer to the enclosing type can refer to the same allocation as its owner. Moving an object with such a self-address could leave the pointer at the old location. Detection is structural: the compiler conservatively marks this type as pinned; it does not inspect each pointer value to decide whether it currently points back to itself.

The marker contract ​

The prelude declaration has no methods:

vex
contract Pin {}

Pinning is a semantic capability, not an allocation strategy. It does not allocate, reference-count, or make raw pointers safe.

Compiler detection ​

The compiler analyzes structural patterns including:

  • a direct reference or pointer to the enclosing type;
  • tuples, arrays, and declared view/storage shapes containing such an address;
  • an explicit declaration of the canonical Pin contract.

Detected types receive Pin move restrictions automatically. User code does not need to construct a wrapper or call a pinning function. Recursive ownership alone, such as a Box<Self> field, is not a self-address and does not imply pinning. Moving a string handle also does not relocate its heap buffer; ordinary borrow rules still decide whether views into it remain valid.

What is restricted ​

Once the compiler treats a value as pinned, operations that relocate ownership are rejected. Borrowing fields and calling methods in place remain valid. Dropping the value is valid when its ownership/lifetime rules permit it.

Do not assume that copying a pointer makes the pointed-to object pinned. The guarantee is attached to the analyzed value/type and enforced by the ownership checker.

Async frames ​

An async frame can retain owned storage across suspension. A reference into that storage still requires both a live owner and a stable address; pinning supplies the address condition, not a longer lifetime. See the suspension safety table for frame-owned data and escaping references. Use await or a documented implicit suspension operation when reasoning about where execution suspends.

Practical guidance ​

  1. Let compiler detection apply pin semantics.
  2. Keep self-referential raw pointers rare and initialization tightly audited.
  3. Do not place an already self-referential value into a relocating container.
  4. Prefer indices, handles, or owned indirection when they avoid self-reference.
  5. Treat a pinning diagnostic as an ownership/layout issue, not a request for a cast.

Released under the MIT License.