Zane Memory Model
This document specifies Zane’s memory model: ownership, references, and arena layout. Lexical lifetime rules, moves, and deterministic destruction are specified in lifetimes.md.
See also:
lifetimes.mdfor scope rules, moves, and destruction.types.md§2 for value and reference types.effects.md§2 formut.concurrency.md§4 for water-tower lifetimes.syntax.md§1 and §2 for storage forms.
1. Overview
Zane eliminates dangling references by combining single ownership, an owner that never moves once anything may point at it, and lexical lifetime rules.
Settled and roaming owners. An owner is either settled — it may be referenced, and it never moves again — or roaming — it may move anywhere, and nothing references it. A roaming owner settles by moving into a settled place (§2.1, §2.8.1).References name settled owners. An&— a reference — is a non-owning handle to a settled owner of a reference type (a#-marked type, or a reference-type intrinsic such as@primitives$ListRef<T>). A value type has no identity to point at, so it is shared by copy or borrow, never by a stored reference (§2.4).A value copy is deep. A value owns whatever it holds out of line, so copying one copies its backing stores and boxed payloads into fresh storage instead of sharing them (§2.3, §2.10).Three passing modes. A bare reference-type parameter, and every subject, is a borrow;^Ttakes the owner;&Ttakes a reference. A value-type parameter is always a borrow, and nothing else in a call writes what the call borrows (§2.9, §2.9.1).Lexical lifetime enforcement. Every store is checked against declaration scopes alone (seelifetimes.md§1), and objects are destroyed when their owner’s scope drains; there is no tracing garbage collector (seelifetimes.md§2).Regioned arena placement. Every scope has separate fixed-size and dynamic regions. Statically sized storage is placed inline in the fixed-size region; resizable data and the payloads of boxed members use the dynamic region (§3).A reference is an offset from the program. A settled owner never moves, and it lies within reach of the program’s image, so a reference stores its owner’s distance from the image in 32 bits (§4).
The source language uses two words for the relationship: an object is held by an owner, and a reference (&T) may access it without storing it or controlling its lifetime. A reference may point only at a settled owner, and a settled owner stays where it is until its scope drains, so a reference never needs to follow anything.
Story:
stories/memory.md— “Safety without a collector and without lifetimes”. Story:stories/memory.md— “Settled and roaming: the host that stopped moving”.
2. Ownership and Storage
2.1 Every reference-type instance has exactly one owner, settled or roaming
Every instance of a reference type (a #-marked type, see types.md §2.1) is owned by exactly one symbol, field, element, or payload at a time. An owner is in one of two states:
- A settled owner may be referenced (§2.8). It never moves: no expression takes its object out of it.
- A roaming owner may be moved (see
lifetimes.md§1.2). Nothing references it, or anything inside it.
A symbol, parameter, return type, or abort type written with ^ is roaming, except a type parameter filled with a value type, which is an ordinary value (§2.9); a bare symbol of a reference type is settled. An object a host holds, which an exported lambda returned to C, is settled in a top-level scope of its own until an exported lambda ends it (packages.md §8.5). A field or an ArrayRef element takes the state of the root it is reached from, and a ListRef element or variant payload is always roaming (§2.8.1). A roaming owner settles when it moves into a settled place, and a settled owner never becomes roaming.
spare ^Engine = Engine(Int(1)); // roaming
engine Engine = spare; // settles here; spare is spent
view &Engine = engine; // legal: engine is settled
moved Engine = engine; // ILLEGAL: a settled owner never moves
Story:
stories/memory.md— “Settled and roaming: the host that stopped moving”. Story:stories/memory.md— “Owner and reference replace host and guest, and the store rule compares scopes”.
2.2 A settled owner is overwritten in place
Any owning storage position for a reference-type instance — a symbol, field, element, or payload — MUST be directly initialized, and MAY later be overwritten.
tank Tank(...);
tank = Tank(...); // legal
Overwriting a settled owner destroys the old occupant and writes the replacement at the same address. A foreign handle in such a place is overwritten through its setter, which keeps its C object at the same address, and one whose type has no setter is never overwritten there (ffi.md §4.3). Each boxed member reached from the owner through struct fields and ArrayRef elements (§3.3) is written into the block the occupant’s member already holds, recursively, so no such member moves to a new block (§3.6). A variant payload is not followed, since nothing references into one (§2.8.1). A fresh replacement is constructed directly in that storage; a moved-in replacement is copied into it, and the blocks that held its boxed members are returned. A reference to the owner, or to any field of it, therefore observes the replacement. No reference names a boxed member: a boxed payload is a variant’s, and a boxed field lies on a cycle of fields alone, whose types have no value (adt.md §4).
car Car(...);
r &Engine = car.engine;
car.engine = Engine(); // the old engine is destroyed; r observes the replacement
Overwriting a roaming owner, a ListRef element, or a variant payload destroys the old occupant unless the operation first moves it elsewhere. Nothing references it, so nothing observes the change of occupant.
An &T stored as an element value is different: rewriting that element merely replaces one reference value with another.
Story:
stories/memory.md— “Settled overwrites stay in place, and only an escape relocates”.
2.3 Value types are copied whole, mutable in place, and freely overwritable
Value types have no identity. A value is mutated in place through a mut method whose this is a borrow of the value’s storage (see effects.md §2.3, functions.md §2.4), and its storage slot may also be reassigned wholesale.
pos Vec2(1.0, 2.0);
pos!setX(Float(3.0)); // in-place field write through a borrow of pos
pos = Vec2(3.0, 4.0); // whole-slot overwrite
A value-producing expression initializes storage according to whether it denotes an existing value. A place expression (§2.8) denotes existing storage; binding its value into a different slot copies the whole value. A non-place expression produces a fresh value and MUST construct that value directly in its eventual destination rather than first materializing an independent temporary and then copying it. This rule passes the destination recursively through nested value-producing forms: product construction, value-variant case forms, function results, match arms, and other fresh results build their members directly in the storage that will own them.
v Vector2 = Vector2(Int(3), Int(4)); // constructs v, v.x, and v.y directly
w Vector2 = v; // copies the existing value in v
A value-type parameter is a read-only borrow rather than a copy (§2.9), so passing one costs nothing. Binding through that borrow into fresh storage is a copy because the parameter denotes the caller’s existing place. Where a copy does happen, it copies the whole value, including any storage that value owns. For a value whose members are all laid out inline, that is a copy of its inline bytes and nothing more; this is every value type that owns neither a backing store nor a boxed member (§2.10, §3.3). A value that owns out-of-line storage is copied deeply: the copy allocates a block for each backing store or boxed payload and copies its live contents into it, recursively, so the original and the copy share no storage at all. This includes String, List, and their primitives (types.md §2.7, generics.md §8.3).
A copy is deep because “copied” requires it once a value may own out-of-line storage. A value’s central promise is that nothing reachable from it is reachable from anywhere else, and a shallow copy would break that promise by leaving two values that name one payload.
The cost is real and is accepted: copying such a value allocates and takes time proportional to its owned bytes and structure, where copying a flat value is one fixed-size write. Fresh construction does not pay that copy cost merely because its result is nested: Countdown.more(Countdown.more(Countdown.done(Unit()))) constructs each node once in its final owning payload rather than repeatedly copying each completed prefix. Where a copied place is never read again — the argument a callee stores was the caller’s last use of it — an implementation may move it instead of copying, since a value has no identity by which the two could be told apart.
An overwrite evaluates its right-hand side against the destination’s pre-overwrite state. If the source is the destination itself or any place reached through it, the replacement value MUST be completely copied or otherwise materialized before the old occupant is destroyed and its owned blocks are returned. This makes x = x, x = x.child, and equivalent overlapping forms safe. An implementation may construct a non-place replacement directly in the destination slot when it proves that doing so preserves this order; the semantic rule does not require an observable temporary.
Allocation is no more a language-visible failure mode here than it is when a List outgrows its backing store (§3.6).
Destruction is the mirror. When a value dies — its owner dies, its container dies, its scope drains, or its slot is overwritten (see lifetimes.md §2.1) — every block it owns is returned, recursively (§3.2).
Story:
stories/memory.md— “What a copy is for, and the ban that survived it”. Story:stories/memory.md— “The borrow comes back, without a sigil”.
2.4 & is a reference: non-owning storage
& creates a reference: non-owning storage that points at a settled owner of a reference type. An &T requires T to be a reference type — a declared #struct/#variant/#enum, a foreign handle (ffi.md §4.1), or a reference-type intrinsic such as @primitives$ListRef<T> — because only a reference type has an owner to point at. A value type is shared by copying it or by a borrow (see functions.md §2.4), never by a stored reference. Writing &Node names a reference to a reference type; a bare &Int over a value type is ill-formed.
An explicitly declared &T slot is reference-only: it stores only the reference and can never own a T. A slot declared as T or ^T is an owning slot. After a roaming owner’s value moves out, that same full-size slot is spent (lifetimes.md §1.6): it denotes no object until a store refills it.
A reference may be declared as:
- a local symbol
- a reference-type field
- an element type inside another storage type
- a function or constructor parameter
- a function return type
An & type is legal in storage sites (local symbols, fields, nested storage types), function parameter positions, and function return-type positions.
Declaring an & symbol is legal; §2.8 governs what may initialize it.
Story:
stories/memory.md— “Two vocabularies: host and guest above anchor and tether”. Story:stories/memory.md— “Owner and reference replace host and guest, and the store rule compares scopes”.
2.5 References are repointable
An & symbol or & field may be assigned a different reference later, either by copying an existing &T value or by minting one from a settled place (§2.8), as long as the store rule in lifetimes.md §1.1 is satisfied. For an & field or element, the scope that rule compares is the field’s root symbol’s, not the field’s own.
2.6 References are independent
Assigning or passing a reference gives the destination its own reference to the same owner. Rebinding one reference’s storage site later changes only that storage site; it does not retarget other references that already point to that owner.
2.7 References and owners use the same surface operations
At use sites, a reference is used with the same surface syntax as a direct owner. Method calls, field access, and mut calls use the ordinary syntax. The distinction between owner and reference matters only at the storage site: a reference stores a non-owning link, while an owner stores the object itself.
2.8 Place expressions and new & values
A place expression is an expression that denotes an existing storage location.
The following are place expressions:
- a named local, field-backed, or owning/
&storage symbol such asengine - a field access whose base is a place, such as
car.engineorthis.engine - a subscript expression
list[index]whenlistis a place expression and[]is defined as a place projection for that subject type - an
&Tparameter inside the callee body (§2.9)
Only a settled place may mint a new reference. A new & value may be minted from:
- a bare settled symbol — a local or a package constant
- a path from a settled root that passes only through struct fields and
ArrayRefelements, such ascar.engineorsquad[2].weapon - an
&Tparameter, or such a path from one
Four sources are rejected:
- A roaming owner, and any place reached from one, is never a reference source. Nothing references an owner that may still move.
- A subscript is a reference source only when the place it projects is an
ArrayRefelement (functions.md§2.9). AListRefelement is roaming, soplayers[100]andplayers[100].weaponon aListRefare both excluded. - A variant case payload is never a reference source, and neither is a path that continues through one.
- Temporaries and other value-only expressions are not place expressions at all. Constructor calls and ordinary function results such as
Engine()andmakeEngine()are not places.
engine &Engine = Engine(); // ILLEGAL: Engine() is a temporary, not a place expression
car Car();
r &Engine = car.engine; // legal: a field of a settled root
s &Car = car; // legal: a bare settled symbol
spare ^Car = Car();
t &Car = spare; // ILLEGAL: spare is roaming
weapon &Weapon = players[100].weapon; // ILLEGAL: players is a ListRef; its elements are roaming
squad ArrayRef.roaming([Player(), Player()]);
lead &Weapon = squad[1].weapon; // legal: squad is settled, and its elements are too
For a ListRef element or a case payload, keep the reference at the settled container and perform the access through it when needed. A reference to a container may subscript that container, and a reference to a variant may read whichever case is live; neither access may mint a new reference to the element or case payload.
Reading an &T value that is already stored behind such an access remains legal:
type Rack = #struct {
weapon &Weapon;
}
armory Armory();
racks ListRef<Rack> = [Rack(armory.primary), Rack(armory.backup)];
current &Weapon = racks[1].weapon; // legal: reads an `&Weapon` already stored in an element
The last line copies an existing reference value; it does not mint a new & from an element.
Non-& owner bindings may be initialized from any expression, including temporaries. The owner materializes the value into its storage.
engine Engine(); // legal: plain owner binding; Engine() temporary is materialized into engine
Story:
stories/memory.md— “Contingent hosts float to their owner”. Story:stories/memory.md— “ArrayRef: a fixed reference container whose elements can be guested”.
2.8.1 A roaming owner settles where it lands
A roaming owner settles by moving into a settled place: a settled symbol, or a field of a settled root. The move is the last time the object moves. From then on it may be referenced, and every reference to it names the same address until its scope drains (lifetimes.md §2.1).
^Car make(power Int) {
car ^Car = Car(Engine(power));
car.engine!tune(); // a roaming owner is used like any other
return car; // and moves
}
parked Car = make(Int(1)); // settles here
garage.car = make(Int(2)); // settles in garage.car when garage is settled
The parts of an owner take their state, settled or roaming, from the owner’s position:
- Fixed storage inherits its root’s state. A struct’s fields and an
ArrayRef’s elements are settled under a settled root and roaming under a roaming one: they are fixed in number and all initialized at construction (generics.md§8.4). AnArrayand aListare value types (generics.md§8.1, §8.3), so they hold no owner at all (§2.10). - Dynamic storage is always roaming. A
ListRef’s elements and a variant’s payload come and go while theListRefor variant holding them lives, so they are roaming even under a settled root. A settledListRefmay be referenced as a whole; its elements may not.
A field is never declared roaming. An ArrayRef element is never moved out, under either kind of root: its index is a runtime value, so which element is spent could not be tracked. A field of a roaming root may be moved out, because nothing can observe the root; the root is then partly spent, tracked in its declaration block as a spent symbol is (lifetimes.md §1.6). A field of a settled root is overwritten, never moved out (§2.2).
A roaming value cannot hold a reference into its own insides, because nothing inside a roaming owner is referenceable. An object is wired to its own parts after it settles, from outside it:
kit Kit(Engine(Int(1)), Mount());
kit.mount!attach(kit.engine); // legal: kit and its fields are settled
The & fields a roaming value holds may name settled owners elsewhere; every store of the value compares them against its destination (lifetimes.md §1.10).
Story:
stories/memory.md— “Settled and roaming: the host that stopped moving”. Story:stories/memory.md— “Where a new ref may come from”. Story:stories/memory.md— “ArrayRef: a fixed reference container whose elements can be guested”.
2.9 Function parameters: borrow, take, and reference
A reference type parameter has three passing modes, one per surface form:
| Mode | Written | Caller supplies | The callee may |
|---|---|---|---|
| Borrow | T |
any owner, settled or roaming, or a temporary | read it; never write it, store it, return it, or move it |
| Take | ^T |
a roaming owner, which is spent, or a temporary (lifetimes.md §1.2) |
move it, store it, or return it; it dies with the body otherwise |
| Reference | &T |
a settled place that mints a reference, or an existing &T value (§2.8) |
read it, return it as &T, or store it; where a stored reference comes to rest is part of the signature (lifetimes.md §1.11) |
- A borrow is non-owning, non-escaping access to the caller’s owner for the duration of the call. It has no address the callee could keep: a borrow cannot be stored, returned, or minted into a reference. Like every parameter other than
this, it is read-only (effects.md§2.4), so it is never assigned or the subject of a!call; the subject is the only borrow amutmethod may write. The caller stays a full owner. - A take moves the owner into the callee. The parameter is a roaming owner of the body; the body moves it on — into another parameter’s object, into the result, into a local — or it dies when the body’s scope drains.
- A reference parameter is an ordinary reference. Inside the callee body the parameter acts as a place expression that may be read or returned as
&Tunderlifetimes.md§1.7. Binding it into an&field is decided at each call: the callee records that the parameter comes to rest in that field (lifetimes.md§1.11), and each call compares the scopes of the two argument paths it actually wrote.
Float topSpeed(engine Engine) => engine.speed; // borrow
engine Engine();
s Float = topSpeed(engine); // legal: engine stays a full owner
spare ^Engine = Engine();
t Float = topSpeed(spare); // legal: a borrow takes a roaming owner too
A value type parameter has one mode, the borrow: a read-only borrow of the caller’s slot for the duration of the call. A borrow is not storage, but that restriction is on the borrow, not on what is read through one. Binding through a borrow into a fresh slot (an assignment, a new declaration, or a field or return store) copies the value (§2.3). The copy outlives the call perfectly well; what does not escape is the borrow. Neither ^ nor & is written on a value-type parameter. A type parameter written ^T takes an owner when T is a reference type, and is a borrow when T is a value type. T there is never an & type: a reference has no owner to hand over, so filling it with one is ill-formed (generics.md §3.6). A type parameter written &T takes a reference, so T is always filled with a reference type there (generics.md §3.6).
Passing a value by borrow is the semantic model rather than an optimization; where a read-only borrow is indistinguishable from a copy, the compiler may still pass a small value by copy, the same latitude placement has (§3.5). The distinction becomes observable under concurrent sharing, where a spawned reader sees the borrowed value live (see concurrency.md §4.4).
type Car = #struct {
engine &Engine; // an `&` field
spare Engine; // an owning field
_value Int;
}
// a take: the engine moves into an owning field of this
Unit setSpare(this Car, engine ^Engine) mut {
this.spare = engine;
return Unit();
}
// a borrow, used only to read
Int inspect(this Car, engine Engine) {
return this._value + engine.speed;
}
// a reference stored into an `&` field: the signature records where it lands
Unit setEngine(this Car, engine &Engine) mut {
this.engine = engine;
return Unit();
}
setEngine stores a reference it was handed. The callee sees two parameters and cannot tell whether the caller’s engine is owned above or below the object this names, so the callee makes no comparison: its signature records that engine comes to rest at this.engine (lifetimes.md §1.11), and each call substitutes the argument paths it was given and compares scopes (lifetimes.md §1.1).
car Car(...);
engine Engine();
car!setEngine(engine); // → car.engine = engine; one scope holds both: legal
do() {
spare Engine();
car!setEngine(spare); // ILLEGAL: this block does not outlive car's
}
The subject is always a borrow. this — the first parameter, and only it (functions.md §2.1) — is a borrow of the object the method was called on, settled or roaming, and mutable under mut. A method never moves this, never stores it, and never returns it as &T. Nothing is written on this, because a subject has no other mode. A verb that hands out a reference into an object takes that object as an &T parameter instead:
&Weapon weaponOf(player &Player) => player.weapon; // legal: rooted in an `&T` parameter
&Weapon weapon(this Player) => this.weapon; // ILLEGAL: this is a borrow
This rule preserves uniform call syntax. The call site writes inspect(e), setSpare(e), or setEngine(e) identically; only the callee’s signature declares which mode applies, and therefore what the caller must supply and what state the caller is left in.
Story:
stories/memory.md— “Three ways to hand over an object”. Story:stories/memory.md— “The borrow comes back, without a sigil”. Story:stories/memory.md— “No growable list of references”. Story:stories/memory.md— “Passing stays implicit, and anArrayReftakes its element type”. Story:stories/memory.md— “AnArrayRefconstructor names the mode of its elements”.
2.9.1 Nothing else in a call writes what the call borrows
A call’s borrows are its subject and every argument it passes to a borrow: a bare parameter, or a ^T parameter whose T is a value type. Each lends its place for the whole call (lifetimes.md §1.5). Other parts of the same call run while that loan stands, and a write from one of them can destroy the storage a borrow names: a list that grows moves its elements to a new block, and a variant that changes case destroys its payload. Even where nothing is destroyed, the callee would watch a parameter change under it. So nothing else in a call may write a place the call borrows.
Two places overlap when they are the same place, or one is reached from the other by field steps, element steps, or a case payload. Any two elements of one container overlap, because an index is a runtime value. A part of a call writes a place when it assigns to it or makes it the subject of a ! call (effects.md §4.1), anywhere inside that part.
A ! call also writes everything its subject reaches through a reference it holds, in an & field or an & element, and passing a block parameter on to a call runs it, which writes whatever the block passed in writes.
A place reached through an & is part of an object its path does not show, and another path may reach the same object. Where the checker can name that object, overlap is judged on it:
- An
&symbol stands for every place it is initialized from or repointed to (§2.5) anywhere in the body, including a repointing written after the call, since a loop body or a block argument may run it before the call runs again. Withrinitialized fromnamesand never repointed,r[Int(1)]isnames[Int(1)]. - An
&Tparameter names an object of the caller’s that the body takes to be apart from the subject and from every block parameter. Where the body relies on that, the verb’s signature records the pair, as it records where a parameter comes to rest (lifetimes.md§1.11), and each call checks it: the object its&Targument names may not overlap its subject, or a place the matching block argument writes. A call that passes its own&Tparameter on records the pair in its own signature instead, so the check lands on the first caller that knows both places. A function type records no pairs, so a call through a function value keeps every&Targument apart from itsmutsubject and from each block argument.
Any other place reached through an & — an & field, an & element, or a reference a call returns — may be any object of its type, so it overlaps every place that object could be, hold, or lie inside. A place the body owns lies inside no such object, and neither do the subject and the borrowed parameters, whose callers keep them apart from what the call writes.
A call is a compile-time error when one of its borrows overlaps a place written by:
- its subject, for a borrow argument of a
mutcall; - a block argument of the call, for the subject or a borrow argument;
- an argument written after the borrow argument, while that argument is evaluated.
Unit setFrom(this V, other V) mut { ... }
Unit readAfter(engine Engine, body @concepts$Block) { ... }
Engine readLater(engine Engine, size Int) { ... }
v!setFrom(v); // ILLEGAL: other borrows the subject itself
car!tune(car.engine); // ILLEGAL: the argument is part of the subject
names!push(names[Int(1)]); // ILLEGAL: push may grow names and move the element
readAfter(engines[Int(1)], {
engines = freshEngines(); // ILLEGAL: the block destroys the borrowed element
});
readLater(engines[Int(1)], engines!grow()); // ILLEGAL: the later argument writes engines
first String = names[Int(1)];
names!push(first); // legal: first is a place of its own
same &ListRef<String> = names;
same!push(names[Int(1)]); // ILLEGAL: same stands for names
other &ListRef<String> = spares;
names!push(other[Int(1)]); // legal: other stands for spares
this.left!push(this.right[Int(1)]); // ILLEGAL: two & fields may name one list
An &T parameter moves the check to the call, where the argument’s place is visible:
Unit append(this ListRef<String>, from &ListRef<String>) mut {
this!push(from[Int(1)]); // legal: from is apart from this; the signature records it
return Unit();
}
Unit relay(this ListRef<String>, via &ListRef<String>) mut {
this!append(via); // legal: relay records that via is apart from this
return Unit();
}
names!append(spares); // legal
names!append(names); // ILLEGAL: from names the subject
names!relay(names); // ILLEGAL: via names the subject
A value-type argument is held to the same rule. A value parameter borrows the caller’s slot, and passing a copy is an optimization the compiler makes only where no program can tell the difference (§2.9), so a block that writes a condition its own if borrows is refused like any other:
if(dirty) {
dirty = Bool(false); // ILLEGAL: the block writes the borrowed condition
}
wasDirty Bool = dirty;
if(wasDirty) {
dirty = Bool(false); // legal: the call borrows wasDirty
}
An &T argument is not a borrow. It names a settled owner, which an overwrite replaces in place (§2.2), so a write elsewhere in the call is one the reference observes. A subject is located only after the call’s arguments are evaluated (§2.12), so an argument that writes the subject’s place is not a conflict either.
Story:
stories/memory.md— “A call may not write what it borrows”. Story:stories/memory.md— “Overlap through a reference is checked where the reference is known”.
2.10 Value-downstream enforcement (transitive value-only field restriction)
Value types form a closed world of plain value storage. A value-type field may contain only value types, value-type storage primitives among them (see syntax.md §2.6), and it MUST NOT contain a reference type (a #-marked type or a reference-type intrinsic such as @primitives$ListRef<T>) or an &. This rule applies transitively: a value type containing another value type that eventually contains a reference-type or & field is also illegal.
Here, downstream means “through nested value-type fields.” The restriction is checked recursively through the full value graph.
The rule is about copying, and both banned field kinds fail it the same way. An existing value is copied whole whenever a place expression is bound into a different slot (§2.3). A reference type is the opposite by construction: it exists in order not to be copied. It has exactly one owner at a time (§2.1), an identity that references name (§4), and it reaches a new place by being moved rather than duplicated (see lifetimes.md §1.2). Copying a value that contained one would have to take one of two courses, and each defeats the reference type’s single owner and identity:
- Duplicate the object, minting a second instance with its own identity. References to the original would not follow the copy, and “exactly one owner” would describe nothing.
- Share the object, so two values reach one owner. Ownership would no longer be single, and a value would have become an aliasing mechanism.
An & field fails on the second alternative directly: copying it would duplicate a reference inside a copied value, putting aliasing inside the one world that is defined by having none. ListRef is a reference type and is covered by this restriction. String and List, with their primitives, are value types with owned backing stores; variable payload size does not itself imply reference semantics (types.md §2.7, generics.md §8.3).
What this closure does not bar is recursion, and the reason is that a boxed member (§3.3) is not a reference-type field: it is out-of-line placement of the member’s own declared type, and placement is not language-visible (§3.5). Nothing this rule forbids has entered the value. The recursion rule itself lives in adt.md §4, and the copy that keeps such a value alias-free in §2.3.
type Vec2 = struct {
x Float;
y Float;
}
type Rect = struct {
pos Vec2;
size Vec2;
}
type Countdown = variant {
done Unit;
more Countdown; // legal: a boxed member, deep-copied with the value
}
type BadOwner = struct {
engine Engine; // ILLEGAL: reference-type field inside a value type
}
type BadRef = struct {
target &Engine; // ILLEGAL: `&` field inside a value type
}
Downstream enforcement keeps ownership and reference bookkeeping confined to reference types, and — because nothing reachable from a value can be aliased, whether it is stored inline, in a backing store, or behind a box — is what lets a value be shared by snapshot and mutated concurrently under concurrency.md §4.
Story:
stories/memory.md— “The value world stays closed, and placement stays the compiler’s”. Story:stories/memory.md— “What a copy is for, and the ban that survived it”.
2.11 Symbols require direct initialization
Every symbol declaration MUST provide its initial value in the declaration itself. Zane does not permit bare symbol declarations followed by conditional or delayed first assignment.
text String; // ILLEGAL: symbols require direct initialization
text String(""); // LEGAL: directly initialized
if(runtimeBool()) {
text = String("hi");
}
2.12 A destination is located after its value
A store and a ! call each name a place they write: a store its destination, a ! call its subject. Evaluating the other side can move that place. An element store’s right-hand side may grow the list, and a ! call’s argument may do the same to the list its subject is an element of. So the place is located last.
A store evaluates the operands of its destination in written order, such as the index in list[i], then its right-hand side, and only then locates the destination. A ! call evaluates the operands of its subject, then its arguments in written order, and only then locates the subject. The write lands where the place is at that moment.
Int growBy(this List<Int>, count Int) mut { ... } // appends count elements
Unit bump(this Int, by Int) mut { ... }
ints[Int(1)] = ints!growBy(Int(1000)); // the store lands in ints's block after it grew
ints[Int(2)]!bump(ints!growBy(Int(1000))); // and so does bump's write
The right-hand side still reads the destination’s pre-overwrite state (§2.3): locating a place reads nothing from it.
Story:
stories/memory.md— “A destination is located after its value”.
3. Memory Layout
3.1 Scope arenas and offsets from the program image
Each lexical scope has an arena made from two independent allocation regions:
- The fixed-size region stores materialized value-type slots, statically sized owners, and the fixed-size handles — of dynamically-sized types of either kind and of boxed members alike — that are materialized in scope-level slots.
- The dynamic region stores the payloads behind those handles: the resizable backing stores of types such as
ListandString, and the payloads of boxed members (§3.6). A handle that sits inside a dynamic payload rather than in a scope slot — a boxed node’s own boxed members, an element’s owned storage — is part of that payload’s block and is not separately placed.
The fixed-size regions of nested scopes share one contiguous range of native addresses for the program’s own execution, and a separate range for each spawned call and each running worker-pool step (§3.7). Each scope’s statically sized slots form a frame within that range. Opening a scope advances the frontier by its frame’s size; draining it restores the previous frontier. Fixed-size slots and dynamic backing stores occupy separate storage.
The dynamic region is a chain of 1 MiB chunks mapped from the OS on demand. It maps no chunk until its first allocation. When its current chunk cannot satisfy an allocation, the runtime maps another chunk for that region and makes it current. An ordinary dynamic allocation never straddles a chunk boundary. A dynamic block of at most 1 MiB is wholly contained in one dynamic chunk; if the remaining bytes cannot hold it, allocation continues in a fresh chunk.
A dynamic block larger than 1 MiB is an oversized span: a dedicated contiguous OS mapping of ceil(block_size / 1 MiB) dynamic chunks, all belonging exclusively to that block. A list’s backing store that grows past 1 MiB is placed in a span at the start of a larger reservation of addresses, which holds nothing else, so that later growth extends the span in place (§3.6). A reservation costs addresses, not memory: only the chunks the span covers are backed. Where the system will not reserve more addresses than the span itself, the reservation is the span alone, and the list keeps growing by relocating at each doubling. Its handle stores the span’s native base address and its capacity (§3.6). Elements are addressed by adding their byte offset to that base. Returning an oversized span pushes its base address onto the exact-size stack; the complete span, with its reservation and the pages it has used, remains mapped for reuse until the scope drains, and an oversized block the span’s reservation can hold may take it (§3.2). A span a list’s growth relocates out of is not returned that way: its memory goes back to the system as the elements leave it, and only its addresses stay with the scope until it drains (§3.6).
A reference (§4.1) and a boxed member’s handle (§3.6) each store a signed 32-bit offset from the start of the program’s image, the program’s code and package constants as loaded: a reference counts bytes, and a boxed member’s handle counts 8-byte units. So every fixed-size region lies within 2 GiB of the image, and every dynamic chunk within 16 GiB of it. Decoding either adds it to the image’s address; nothing is looked up. A list’s or string’s handle (§3.6) and a size-stack entry (§3.2) store a native pointer, so an oversized span may lie anywhere.
The addresses within reach of the image bound what a program holds there at once: the fixed-size regions of all its threads of execution (§3.7) and its dynamic chunks. A program that needs more there stops with an error, as a reservation that cannot be made does. Elsewhere, available address space, system reservation limits, and available memory bound allocation.
Scopes nest last-in-first-out, and their arenas nest with them. The one exception is the top-level scope of an object a host holds (packages.md §8.5), which nests in no other scope and drains when its object’s owner has left it, in whatever order the host gives objects back. When a scope drains, its fixed-size frame becomes reusable and its dynamic region is released in bulk (§3.2, lifetimes.md §2.1). The shared fixed-size reservation may remain for later scopes. Arena granularity is an implementation choice, like boolean packing (§3.4) and placement (§3.5) — the compiler may fold several lexical scopes into one arena. What the language fixes is the observable behavior: a scope’s storage ceases to be live together when that scope drains, and no reference ever resolves into released storage. A value that escapes is promoted out of the draining scope first (§3.5); only a roaming owner or a value escapes, and nothing references either.
Story:
stories/memory.md— “Native addresses replace segmented offsets”. Story:stories/memory.md— “References and boxes become 32-bit offsets from the program image”.
3.2 Allocation, reuse, and teardown
The fixed-size region is a pure bump allocator: no size classes, no free list, no coalescing. An owner has a fixed-size storage slot, so an overwrite consumes no new space in that region. Reference-type overwrite and move ordering follow §2.2 and §3.5. A materialized value slot follows the replacement rule of §2.3: the right-hand side observes the pre-overwrite occupant, and any overlapping replacement is completed before the old value ends. Only then are the old value’s owned dynamic blocks — backing stores and boxed payloads alike, recursively — returned to their exact-size stacks and the replacement installed in the same slot. If the compiler proves the replacement does not depend on the current occupant, it may destroy the old value and construct a non-place result directly in that slot. Nothing in the fixed-size region is reclaimed individually — bytes in a slot that cease to be live before the scope drains remain dead space until teardown.
The dynamic region adds exact-size reuse on top of its bump frontier. Each scope maintains one LIFO size stack for every (byte size, alignment) pair that has become reusable. To allocate a dynamic block of size S and alignment A, the runtime first pops size_stack[S, A]; only when that stack is empty does it bump the dynamic frontier, rounding it up to A first. Keying on alignment as well as size is what keeps reuse sound now that blocks no longer share one alignment: a block returned by a type needing 8-byte alignment must not be handed to a type needing 16. It never coalesces neighbouring blocks, and only an oversized block ever takes a block from another size’s stack (§3.2, below).
A block’s size comes from its contents, and the two kinds of block differ in their size requirements. A growable backing store doubles from its first block (§3.6), so its sizes are that block’s size times a power of two; those sizes are a consequence of growth, not a classification imposed on the region. A boxed payload (§3.3) never grows, so it requests exactly the size of the one instance it holds — value type or reference type alike — and is aligned to that type’s alignment requirement, and to at least 8 bytes, the unit its handle counts in (§3.6). There is no size class to round up to and no floor: a twelve-byte node occupies twelve bytes.
Returning a dynamic block pushes its native base address onto the stack for its own size and alignment. The stacks are shared by all dynamic payloads in the scope, whatever produced them: a 128-byte block previously used by a List<Int64> may later hold string bytes, another list’s elements, or a boxed node that happens to match it on both keys. Reuse within a chunk is therefore exact and never approximate — a freed block of at most 1 MiB serves only a request for the same number of bytes. This suits boxed payloads particularly well, because every instance of one type is the same size (a variant has room for its widest case, whichever case is live), so the block a destroyed node returns is precisely what the next node of that type needs. An oversized span participates in the same policy, and its reservation widens it. An oversized block other than a boxed payload takes a returned span whose reservation holds as many addresses as a fresh one’s would: from the stack for its own size first, else from the top of the smallest oversized stack that holds such a span, and only then maps a fresh span. Only a returned span made at the start of a reservation is taken this way; the chunks a boxed payload above 1 MiB returns serve only another boxed payload of their size (§3.6). A span taken this way keeps its reservation, so the block grows in place within it as any list’s span does. A list growing past 1 MiB extends its own reserved span before any of this (§3.6). A boxed payload above 1 MiB keeps to chunks within the image’s reach and never takes an oversized span (§3.6).
When a scope drains — after all its spawned work completes and every worker of a pool it owns has finished (concurrency.md §4.1) — the runtime restores the fixed-size frontier and releases the dynamic region in bulk, with no per-object teardown pass threaded through the exit. Logical destruction timing is independent of this: a value dies when its owner, container, or scope does (lifetimes.md §2.1); it is the memory that is reclaimed together at drain.
Story:
stories/memory.md— “When the free stacks fragment, and the arena takes the scope”. Story:stories/memory.md— “Growth past 1 MiB takes a span an overwrite returned”.
3.3 Instances are stored inline, their fields in an order the compiler chooses
A value-type instance is stored inline, except for owned backing stores (§3.6) and any members the compiler boxes (below). A statically sized reference-type instance is also stored inline in a fixed-size owner slot, so value-type slots and owner slots may sit directly beside each other in the fixed-size region. Reference types differ by identity and ownership semantics, not by requiring a separate indirect allocation.
The compiler chooses the order in which a type’s fields are laid out, once for each type, so every instance of the type has one layout and uniform stride holds (see generics.md §7). Every object that lays the type out uses the same order, as objects a compiler links must match on every layout (dependencies.md §14). The order is placement (§3.5), as boxing is: no operator exposes a field’s offset, and a C-compatible value crosses into C as a copy laid out as its C struct, its fields in declaration order (ffi.md §3.3).
Story:
stories/memory.md— “The compiler orders a struct’s fields, and C receives a copy in its own order”.
A reference-type instance carries no metadata of its own: a reference names it by its address (§4.1). A dynamically-sized reference type such as ListRef occupies a fixed-size handle inline in the same region; only the backing store named by that handle occupies the dynamic region (§3.6).
A boxed member is laid out the same way: a fixed-size handle inline, with the instance it names placed in the dynamic region (§3.6). Which members are boxed is adt.md §4’s rule — required for a variant’s payload on a cycle of owning edges that runs through no list’s elements, where no finite inline layout exists, and for a field on a cycle that passes through no payload; permitted elsewhere for a variant’s payload. §3.5 makes the choice unobservable.
Boxing is available on both sides of the # axis. Two separate questions determine the meaning of a boxed member, and different types answer them:
- The payload’s nature follows the member’s own declared type, never the enclosing type. A reference-typed payload is an ordinary reference-type instance with identity: a boxed field takes its root’s state as any field does (§2.8.1), and a boxed variant payload is roaming like any payload — being boxed neither grants nor withholds a reference. A value-typed payload is an ordinary value: no identity and nothing to reference. Because boxing is permitted off a cycle (§3.3,
adt.md§4), a reference-type variant may box a value-typed payload; that stores a plain value out of line and does not give it identity. - The payload’s fate when the enclosing instance moves, is copied, or dies follows whether the enclosing type is a value type or a reference type. A reference type owns what it boxes and moves it: it destroys the payload when it dies, and moving it while it roams carries the payload with it (§3.5). A value type owns what it boxes and copies it: the payload is copied into fresh storage whenever the value is copied (§2.3) and returned when the value dies (§3.2).
Either way the member’s declared type is unchanged by being boxed, and the box is placement rather than an extra level of type.
A variant with two cases, one whose payload is Unit and one whose payload is a boxed member or an &, is stored as that handle or reference alone, with zero for the Unit case. Neither a handle nor a reference is ever zero, because nothing either one names starts at the image itself (§3.1).
3.4 Booleans may be packed
The compiler may pack booleans in structs and arena frames when doing so does not change language semantics.
3.5 Statically sized storage uses the fixed-size region
Placement is an implementation decision, not a language-visible property. The arena model places every materialized, statically sized scope slot — value-type storage, an owner, a dynamic type’s fixed-size handle, or a boxed member’s handle — inline in that scope’s fixed-size region. The compiler may keep an unobservable value in registers or otherwise optimize its physical placement, but reference types do not require a separate heap allocation merely because they carry identity. A recursive member is boxed for the opposite reason: not because of which side of the # axis its type sits on, but because a finite inline layout does not exist for it (§3.3).
A move transfers a roaming owner into a destination owner of the same type (lifetimes.md §1). Both have the same statically known size, so a move copies the owner’s inline bytes, its handles among them, into the destination’s fixed-size slot. A destination that already holds an object follows §2.2: a settled destination is overwritten in place, and any other destination’s occupant is destroyed first. The source slot is spent (lifetimes.md §1.6). Nothing inside a roaming owner is referenced, so a move updates nothing else.
The dynamic blocks the owner owns — the backing store behind a ListRef, List, or String handle, and the payload of a boxed member — keep their addresses. A move copies their handles and never their contents, while the scope that holds the blocks outlives the destination owner. A move whose destination outlives that scope is an escape: a return out of the scope that allocated the blocks, or a store into an owner declared above it. Before that scope drains, every block the escaping owner owns MUST reside in a scope that lives as long as the destination, so that no handle ever names released memory. The implementation either relocates each block — allocating an equal-size block or oversized span in such a scope’s dynamic region, moving the live contents into it under their ordinary move rules, updating the handle, and returning the old block to its exact-size stack — or allocates the block there in the first place.
Relocation is recursive, because a relocated block may itself own dynamic blocks: a boxed payload holds its own boxed members, and a backing store holds its elements’ owned storage. Relocating the root of a roaming recursive structure therefore relocates the whole structure, at a cost proportional to the number of boxed nodes it contains rather than to the root alone.
When a value itself is copied into a slot owned by another scope, it reaches that scope by copying rather than moving, and the same recursion applies to its blocks: the copy allocates each backing store and boxed payload afresh in the destination scope’s dynamic region and copies into it, so the copy owns storage in the scope that holds it and the source keeps its own (§2.3). This governs the value being copied. A value-typed payload that a reference-type owner holds through a boxed member travels with that owner under the relocation rule above, because whether the enclosing type is a value type or a reference type determines the fate of a boxed payload (§3.3).
Placement never changes observable semantics: destruction stays deterministic (see lifetimes.md §2), and a reference names a settled owner, which no placement decision moves once it has settled (§4).
Story:
stories/memory.md— “The value world stays closed, and placement stays the compiler’s”. Story:stories/memory.md— “The region takes the boxes, and a box requests its own size”. Story:stories/memory.md— “Settled overwrites stay in place, and only an escape relocates”.
3.6 A handle has a fixed footprint; its payload lives in the dynamic region
Dynamically-sized types such as the reference type ListRef and the value types List and String are represented as fixed-size handles. A handle records the payload’s native address and the metadata needed by the type. The handle occupies a statically known footprint inline in the fixed-size region; its resizable backing store is a separate allocation in the dynamic region.
A List or ListRef handle is 16 bytes: the backing store’s native address, a 32-bit element count, and a 32-bit capacity counted in elements. A list therefore holds at most 232 − 1 elements, and growth past that stops the program with an error. A String handle records its bytes’ native address, their length, and its block’s size (types.md §2.7).
A type that contains a handle-typed field therefore stays statically sized:
type Inventory = #struct {
items List<Item>; // fixed-size handle inline; elements in the dynamic region
count Int;
}
A new list’s first block is the bytes of four elements, rounded up to a whole number of 8-byte words and then kept between 32 and 128 bytes, or more when the list’s first push needs more; it holds as many whole elements as fit, four whenever an element is at most 32 bytes. A small list therefore takes about what its elements need. Its block comes from the same exact-size stacks as every other block of its size, so a block is reused across lists with different element types, across other dynamically-sized types, and by a boxed payload whose exact size and alignment match it.
A list grows according to the following rules:
- When its capacity is exhausted, the requested block size is exactly twice its current block size.
- A doubled size larger than 1 MiB extends the current backing store in place when it is an oversized span whose reservation holds the doubled size (§3.1). Otherwise, the live elements are relocated into an oversized span taken as §3.2 says: a returned span whose reservation holds as many addresses as a fresh one’s would, else a fresh one at the start of a reservation of its own.
- A doubled size of at most 1 MiB first checks the size stack for that size. If a block is available, it is popped and the live elements are relocated into it.
- If that stack is empty, the current backing store is the dynamic frontier allocation, and the additional bytes fit before the current chunk boundary, the frontier is bumped by the additional bytes and the store grows in place.
- Otherwise, the doubled block is bump-allocated wholly inside one dynamic chunk, and the live elements are relocated into it.
- After relocation, the handle’s backing-store address and capacity are updated and the old block’s base address is pushed onto the stack for its exact old byte size. An old oversized span is never pushed: its memory is given back to the system as the elements are relocated out of it, so the list never holds its old and new elements’ bytes at once, and only its addresses remain until the scope drains.
A block of at most 1 MiB grows in place only within its chunk. An oversized span grows in place to the end of its reservation, however many other blocks the scope allocates meanwhile, because nothing else is ever placed there; growth that would pass that end relocates into a span taken by the rule of §3.2, a returned one or else a fresh one with a reservation of its own. An oversized span a copy or an escape relocation (§2.3, §3.5) maps fresh has no reservation beyond itself, so its next doubling relocates once; one that takes a returned span (§3.2) keeps that span’s reservation. Relocation moves or copies elements according to their type’s ordinary move rules; an old block below 1 MiB becomes reusable only after its previous occupants are no longer live, and an old oversized span gives its memory back as they leave it (rule 6). References to a ListRef remain valid because they reach its owner, whose fixed-size handle now names the current backing store.
A boxed member (§3.3) uses the same two-part representation with a payload that never grows. Its handle is the payload’s 32-bit offset from the program’s image, in 8-byte units (§3.1); the payload is one instance of the member’s declared type, sized and aligned as §3.2 specifies, and is returned to its size stack when the member’s enclosing instance is destroyed. Overwriting the member writes the replacement into the same block, which always fits because both are instances of the member’s type, and the replacement’s own boxed members are written into the blocks the occupant already holds, recursively (§2.2). A payload larger than 1 MiB is not an oversized span, which may lie anywhere (§3.1): it takes whole dynamic chunks of its own, within the image’s reach like every chunk, only another boxed payload of its size reuses them, and it reuses no oversized span, which may lie out of its handle’s reach. None of the growth rules above apply to it: a boxed payload is allocated once and is thereafter only relocated when its roaming owner escapes, or allocated afresh by a deep value copy (§2.3, §3.5).
Dynamic chunks and oversized spans begin at cache-line-aligned addresses. A block within a chunk takes its own type’s alignment, and a growable backing store or a boxed payload is aligned to at least 8 bytes, which §3.2 applies to reuse and to the frontier alike, so frontier allocations, reused blocks, and dedicated spans all keep their alignment without mixing payloads into fixed-size chunks.
Story:
stories/memory.md— “A free zero sentinel, and cache-line-aligned buffers”. Story:stories/memory.md— “The region takes the boxes, and a box requests its own size”. Story:stories/memory.md— “A list’s oversized span grows in place within a reservation”. Story:stories/memory.md— “An outgrown span gives its memory back as the list leaves it”. Story:stories/memory.md— “Growth past 1 MiB takes a span an overwrite returned”. Story:stories/memory.md— “One box per cycle, and a first block of four elements”. Story:stories/memory.md— “References and boxes become 32-bit offsets from the program image”.
3.7 Nested scopes share a bounded fixed-size region
Scopes open at once in one thread of execution nest last-in-first-out: the program’s main and every call it makes, a host’s call of an exported lambda and every call it makes (packages.md §8.2), or one spawned call and every call it makes (concurrency.md §3). Their fixed-size regions together hold at most a fixed number of bytes. The root package’s manifest sets the bound: fixed-region for the program’s own thread of execution and for each call a host makes of an exported lambda, since each is an entry point as main is, and spawned-fixed-region for each spawned call and for each running step of a worker pool (dependencies.md §2.1). A spawned call’s bound is its own, and holds while the call runs. A worker pool’s step holds a region from when its call begins until it returns, so the regions open at once follow the steps running, not the workers in the pool (concurrency.md §6.4). The bound sizes its contiguous address-space reservation (§3.1). Reserving the range does not commit physical memory for every byte; pages become usable as needed. Every fixed-size region lies within a reference’s reach of the program’s image (§3.1), so the regions of the program’s own thread, of every host call of an exported lambda and of every spawned call open at once share 2 GiB of addresses. The fields have no maximum of their own: that reach and the system’s per-process reservation limits determine which reservations can succeed. A reservation that cannot be made stops the program with an error.
A program whose open scopes need more than the bound stops with an error that names the field. Calls also nest on the system’s machine stack, whose size the language does not set, and a program that fills it stops with an error the same way. Neither is an abort (error-handling.md): no handler sees it, and the program ends.
Int depth(n Int) {
cells ArrayRef.fill(65536, Int(i Int) => i); // 512 KiB of fixed-size region
return depth(n + 1); // stops once 512 KiB per call fills the bound
}
Story:
stories/memory.md— “The fixed-size region gets a bound the root manifest sets”. Story:stories/memory.md— “References and boxes become 32-bit offsets from the program image”.
4. References
4.1 A reference is a settled owner’s offset from the program image
A reference stores the settled owner’s signed 32-bit byte offset from the start of the program’s image (§3.1). A settled owner is a slot of a fixed-size region or a package constant, or lies inline inside one (§2.8.1, §3.3), and both lie within 2 GiB of the image, so every reference fits. An explicitly declared &T slot contains only this offset. Dereferencing it adds the offset to the image’s address and accesses the owner directly.
A reference minted from a field path such as car.engine stores the offset of engine inside car. A reference copied from another reference copies its offset (§2.6). Nothing is allocated to mint a reference, and nothing is recorded in the owner.
dps Float = mainWeapon.dps; // mainWeapon's pointer → owner → dps
Story:
stories/memory.md— “Guests without anchors”. Story:stories/memory.md— “Native addresses replace segmented offsets”. Story:stories/memory.md— “References and boxes become 32-bit offsets from the program image”.
4.2 Why a reference never dangles
A reference could dangle only if the owner it names moved or died while the reference lived. A settled owner never moves (§2.1): it is overwritten in place (§2.2), and nothing inside dynamic storage is referenced (§2.8.1). It dies when its scope drains, and the store rule ensures every reference to it is in that scope or a nested one (lifetimes.md §1.1), so the reference dies no later. A scope with spawned work drains only after that work finishes (concurrency.md §4.1).
An overwrite destroys the old occupant while references to the slot remain. They name the slot, not the occupant, and observe the replacement (§2.2). This is the only change of object a reference ever sees.
Story:
stories/memory.md— “Guests without anchors”.
5. Language Comparisons
5.1 Ownership and references
| Feature | Zane | C++ unique_ptr |
C++ shared_ptr |
Rust |
|---|---|---|---|---|
| Single owner by default | ✅ | ❌ | ❌ | ✅ |
| Non-owning references as explicit opt-in | ✅ | ⚠️ Raw pointers | ⚠️ weak_ptr |
✅ |
| Lifetime annotations required | ❌ | ❌ | ❌ | ✅ |
| Reference counting required | ❌ | ❌ | ✅ | ⚠️ Rc/Arc only |
| Referenced objects never move | ✅ settled owners | ❌ | ❌ | ⚠️ only while borrowed |
| Owner overwrite keeps existing references valid | ✅ in place | ❌ | ❌ | ⚠️ heavily restricted by borrow checking |
5.2 Allocation
| Property | Zane | GC languages | Rust | C/C++ |
|---|---|---|---|---|
| Allocation strategy | per-scope fixed/dynamic arenas | runtime-managed | allocator-dependent | allocator-dependent |
See also:
lifetimes.md§3 for the lifetime and destruction behavior comparison.
6. Summary
| Concept | Rule |
|---|---|
| Settled owner | May be referenced; never moves; a bare reference-type symbol, or a field of a settled root |
| Roaming owner | May move; nothing references it or anything inside it; written ^T, and every ListRef element and variant payload |
| Settling | A roaming owner settles by moving into a settled place; a settled owner never roams again |
| Owning storage | Reference-typed symbols, fields, and container elements are directly initialized and may later be overwritten |
| Settled overwrite | Destroys the old occupant and writes the replacement at the same address, reusing the block of every boxed member reached through struct fields and ArrayRef elements; references to the slot or any of its fields observe the replacement |
| Value type | Mutable in place through a borrowed mut subject; storage may also be overwritten freely |
| Value construction | A non-place value expression constructs directly in its eventual destination, recursively through nested fresh results; only an existing place is copied |
| Value overwrite | The right-hand side observes the pre-overwrite value; an overlapping replacement is completed before the old value and its owned blocks are destroyed |
| Value copy | Copies the whole existing value: inline bytes, plus a fresh allocation and recursive copy of every backing store and boxed payload the value owns, so two values never share storage; a copy from a place never read again may be a move |
& (reference) |
Reference-only non-owning storage naming a settled owner; may be repointed, copied by value, and returned, but can never own a T |
| Spent owner slot | After a roaming owner’s value moves out, the slot is spent and keeps enough storage for a store to refill it |
| Place expression | Existing storage: a named symbol, a field access of a place, a place-projection subscript of a place, or an & parameter |
New & value |
May be minted only from a settled place: a bare settled symbol, or a path from a settled root or an &T parameter through struct fields and ArrayRef elements only; roaming owners, ListRef elements, variant payloads, and temporaries are rejected |
ArrayRef element |
Fixed storage: takes its root’s state, may be referenced under a settled root, is overwritten in place, and is never moved out |
| Field of a roaming root | May be moved out; the root is partly spent until refilled; a field is never declared roaming |
| Borrow | Non-owning, non-escaping access to a caller’s storage for the duration of a call; not storable, not returnable, not a reference source, not a move-source |
| Value-type parameter | Always a read-only borrow; copied only when the parameter is itself bound into a fresh slot |
| Reference-type parameter | T borrows; ^T takes a roaming owner or temporary, spending the caller’s symbol, and is never filled with an & type; &T takes a reference and leaves the caller a full owner |
| Subject | Always a borrow, mutable under mut; never moved, stored, or returned as &T |
| Borrow overlap | Nothing else in a call writes a place the call borrows: not a mut subject a borrow argument overlaps, not a block argument, and not a later argument’s evaluation; an &T argument is not a borrow, but its object is kept apart from the subject and block arguments where the callee’s signature records it |
| Value-downstream enforcement | Value types may contain only value types, value-type primitives among them, transitively — never a reference-type or & field, because a reference type is made to be moved rather than copied; recursion is not barred, since a boxed member is placement rather than a reference-type field |
& targets reference types |
An &T requires T to be a reference type; a value is shared by copy or borrow, never by a stored & |
| Symbol declaration | Must be directly initialized |
| Locating a destination | A store’s destination and a ! call’s subject are located after the right-hand side and the arguments are evaluated |
| Reference-type placement | Inline storage is bump-allocated in the creating scope’s fixed-size region; moving a roaming owner copies its inline bytes and handles, and its dynamic blocks stay put unless it escapes the scope holding them, which must first leave every block in a scope that lives as long as the destination |
| Field order | The compiler lays out a type’s fields in an order of its own choice, the same for every instance; it is placement, unobservable, and a C-compatible value crosses into C as a copy in its C struct’s order |
| Boxed member | A member stored as a fixed-size handle inline with its enclosing instance, a 32-bit offset from the program image, while the instance the handle names lives in the dynamic region; required for a variant payload on a containment cycle and for a field on a cycle with no payload, permitted elsewhere for a payload, and nothing marks it in the source. In a reference type it moves with its enclosing instance; in a value type it is deep-copied with it. An overwrite reuses its block |
& representation |
A reference is the settled owner’s signed 32-bit byte offset from the program’s image |
| Addressing | References and boxed-member handles are 32-bit offsets from the program image, so fixed-size regions lie within 2 GiB of it and dynamic chunks within 16 GiB; list and string handles and size-stack entries use native pointers, so oversized spans lie anywhere |
[Unit; box] variant |
A two-case variant whose other case holds a boxed member or an & is that handle or reference alone, zero for the Unit case |
| List handle | 16 bytes: a native pointer, a 32-bit count, and a 32-bit capacity in elements; a list holds at most 232 − 1 elements |
| Dynamic allocation | Exact-size stack first, frontier second; a list’s first block holds four elements, between 32 and 128 bytes, and doubles from there, while a boxed payload requests exactly its type’s size and has no class; blocks above 1 MiB use dedicated contiguous oversized spans, except a boxed payload, which takes whole chunks within the image’s reach and reuses no span, and a list growing past 1 MiB extends its own reserved span in place, else takes a returned span whose reservation holds as many addresses as a fresh one’s would, else a fresh one with a reservation of its own, giving the span it leaves back to the system as it relocates (§3.2, §3.6) |
| Nesting bound | The fixed-size regions of the scopes open at once in one thread of execution hold at most the root manifest’s fixed-region, which also bounds each host call of an exported lambda, or spawned-fixed-region in a spawned call or a running worker-pool step; all threads’ regions share 2 GiB within reach of the program image, and reservations are limited by the system, and a program that needs more, or fills the machine stack, stops with an error that no handler sees |
| Dynamic-block alignment | Chunks and oversized spans are cache-line aligned; a block takes its type’s alignment, and a growable backing store or a boxed payload is aligned to at least 8 bytes; the frontier is rounded up before it is bumped (§3.6) |
See also:
lifetimes.md§4 for the summary of scope, move, and destruction rules.