- spec 08 §7.1: platform-independent accounted size table shared by max_value_size and max_local_frame; enum tag is the smallest of 1/2/4/8 bytes; borrows 8, slices 16; checked arithmetic, overflow is E5101 with found = "overflow" - every materializable type in a module must be at most 1 MiB, checked before functions are verified; each oversized type reported once - E5101 fields no longer truncate huge sizes - conformance: frame_too_large, value_too_large, size_overflow - Q-020 resolved; Phase 1 closed
20 KiB
RFC 0005: Frame Storage, Second-Class Borrows and Arrays
Status: Accepted (revision 2, after owner review items 1–5)
Scope: AIR format 0, features ownership and arrays
Language version: 0.1
Erratum (during integration): the array type is spelled array<T, N> in text AIR, because ; starts a comment; revision 2 wrote [T; N]. Source syntax may keep [T; N].
Later change: the accounted-size table moved to spec 08 §7.1 and gained max_value_size and the enum tag rule (DD-015, Q-020).
Integrated into: spec 03 §6, §4, §8.1; spec 07 §8.8; spec 07a; spec 08 §11–§14; reference/air-text.ebnf, reference/air-features.md, reference/error-codes.md
Supersedes: —
Decisions already taken by the language owner (2026-10-04) and recorded here:
- Q-009: borrows are second-class; a function returns a borrow only with an explicit
from %param. - Q-010: region generations are checked statically only (applies when
regionsis specified in Phase 2). - Phase order: Phase 1 has no heap.
Own<T>, heap allocation and regions move to Phase 2, after capabilities.
Motivation
Phase 1 must give AIR mutable memory, references and bounds-checked arrays while keeping the verifier simple enough to be specified exactly. Allocation needs authority (cap<mem.alloc>, spec 05), which does not exist before Phase 2, so Phase 1 uses frame storage only: storage that lives exactly as long as a function activation.
Invariants
For every program accepted with ownership and arrays:
- No dangling borrow, no frame escape. A borrow never outlives the storage it was derived from. A borrow whose provenance leads to a
local.allocnever leaves the call that executed thelocal.alloc: not byret, and not inside an aggregate (borrows cannot be stored in aggregates at all, §2). - Exclusive XOR shared. For overlapping storage, at any program point there is either one live
mutborrow (possibly with live descendants) or any number of liverefborrows, never both. Direct access to a local (local.load,local.store,local.zero) counts as a short access of the whole local. - No uninitialized read. Storage is read or borrowed only after it is definitely initialized on every path.
- No out-of-bounds access. Every element access is either proven in bounds by the spec-defined fact analysis (§6) or checked at runtime and traps with
T0008. - Explicit live ranges. On every normal path, every borrow created in a function ends at a visible point:
borrow.end, orretwhen it is the returned borrow. - Static frames. The frame storage of a function is fixed before execution:
local.allococcurs only in the entry block, and the total accounted size is bounded.
1. Features
| Feature | Depends on | Contents |
|---|---|---|
ownership |
— | local<T>, ref<T>, mut<T>; local.*, borrow.*, load, store; from on returns |
arrays |
ownership |
array<T, N>, slice<T>, slice_mut<T>; array.splat, borrow.index, slice.*, guard |
Registry changes: arrays gains the dependency ownership; regions moves to Phase 2 and depends on capabilities, effects, ownership; a new reserved feature heap (Phase 2: Own<T>, own.alloc, own.free) depends on capabilities, effects, ownership; islands lists the new closure. A reserved feature borrow_joins (see §5.4) is added without a phase.
2. Types
| Type | Class | Notes |
|---|---|---|
local<T> |
storage | frame storage holding a T; only an operand of local.* and borrow.* |
ref<T> |
borrow | shared borrow |
mut<T> |
borrow | exclusive borrow |
array<T, N> |
value | array of N values, 1 ≤ N ≤ 2^32; copyable when T is |
slice<T> |
borrow | shared borrow of a contiguous range |
slice_mut<T> |
borrow | exclusive borrow of a contiguous range |
T in these types is a value type: not void, never, a storage type or a borrow type.
Second-class rule. Storage and borrow types MUST NOT appear as struct fields, enum payloads, array elements or the T of another storage or borrow type (E1001). A storage type MUST NOT appear as a function parameter, return type or block parameter (E1001). A borrow type MAY appear as a function parameter and, with from, as a return type (§5.3). As a block parameter type it requires the reserved feature borrow_joins (§5.4).
All value types of format 0 and Phase 1 are copyable. Move-only values arrive with heap in Phase 2.
3. Syntax additions
function = "fn" global "(" [ param_list ] ")" "->" type [ "from" local ] NL …
type = … | "local" "<" type ">" | "ref" "<" type ">" | "mut" "<" type ">"
| "array" "<" type "," int_lit ">" | "slice" "<" type ">" | "slice_mut" "<" type ">"
instruction = [ local { "," local } "=" ] operation ; multi-result, used only by slice.split
Multi-result binding is new syntax for new instructions only; existing instructions keep a single result.
4. Instructions
Common contract unless stated: no effects, no capabilities, no synchronization. "Owned borrow" means a borrow created in the current function (§5.1).
ownership
| Instruction | Operands → result | Verifier | Runtime |
|---|---|---|---|
%s = local.alloc T |
→ local<T> |
only in the entry block (E5002); storage is uninitialized; counts toward the frame limit (§5.5) |
allocates frame storage once per call, freed at return |
local.store %s, %v |
local<T>, T |
no live borrow of %s (E2101); initializes %s |
writes %v |
local.zero %s |
local<T> |
T zeroable: integers, bool, arrays and structs of zeroable types (E1001); no live borrow (E2101); initializes |
writes all-zero value |
%v = local.load %s |
local<T> → T |
initialized (E1010); no live mut borrow of %s (E2101) |
reads |
%r = borrow.ref %x |
local<T>, ref<T>, mut<T> → ref<T>; slice<T>, slice_mut<T> → slice<T> |
§5.2; locals must be initialized (E1010); child of %x unless %x is a local |
shared view of the same place |
%m = borrow.mut %x |
local<T>, mut<T> → mut<T>; slice_mut<T> → slice_mut<T> |
§5.2; locals must be initialized (E1010); child of %x unless %x is a local |
exclusive view of the same place |
%f = borrow.field @S.f, %b |
ref<@S>/mut<@S> → ref<F>/mut<F> |
child of %b; distinct fields are disjoint |
reference to the field |
borrow.end %b |
owned borrow | no live children (E2102); not a parameter (E2107); not already ended (E2106) |
none |
%v = load %b |
ref<T>/mut<T> → T |
%b usable for reading (§5.2) |
reads |
store %b, %v |
mut<T>, T |
%b usable for writing (§5.2) |
writes |
arrays
| Instruction | Operands → result | Verifier | Runtime |
|---|---|---|---|
%a = array.splat array<T, N> %v |
T → array<T, N> |
— | every element is %v |
%e = borrow.index %b, %i |
ref/mut of array<T, N>, or slice/slice_mut of T; usize → ref<T>/mut<T> |
child of %b |
traps T0008 if %i ≥ length |
%e = borrow.index.proven %b, %i |
as above | %i < length proven (§6), otherwise E2103 |
no check |
%s = slice.make %b |
ref<array<T, N>>/mut<array<T, N>> → slice<T>/slice_mut<T> |
child of %b |
whole array |
%n = slice.len %s |
slice<T>/slice_mut<T> → usize |
— | length |
%t = slice.sub %s, %start, %len |
slice, usize, usize → same slice type |
child of %s |
traps T0008 unless start ≤ slice.len(s) and len ≤ slice.len(s) − start (evaluated without overflow); result covers [start, start + len) |
%l, %r = slice.split %s, %at |
slice, usize → two slices of the same type |
two children of %s, disjoint |
traps T0008 unless at ≤ slice.len(s); l covers [0, at), r covers [at, len) |
slice.copy %dst, %src |
slice_mut<T>, slice<T>/slice_mut<T> |
both usable (§5.2); overlapping operands are rejected by the aliasing rules (§5.2), so this is never a memmove |
traps T0008 unless len(dst) ≥ len(src); copies exactly len(src) elements into the start of dst |
guard %c |
bool |
adds a fact (§6) | traps T0002 if false |
New trap code: T0008 trap.out_of_bounds.
slice.make creates a slice only from an existing borrow of an array, covering exactly that array and inheriting its provenance. No instruction creates a slice from an address and a length. An overlap-safe copy, if ever needed, will be a separate instruction (slice.move).
5. Borrow rules
5.1 Owned and lent borrows
- A borrow created in the function (
borrow.*,slice.make,slice.sub,slice.split, or the result of acallto a function withfrom) is owned by the function. - A borrow-typed function parameter is lent: the caller keeps it alive for the whole call. A lent borrow is never ended by the callee (
E2107) and need not be ended. - Path linearity. On every reachable normal path from the creation of an owned borrow to a
ret, the borrow MUST be either ended exactly once or transferred to the caller by a validret … from(§5.3); otherwiseE2105. The rule is per path, not per instruction text: a borrow may have severalborrow.endinstructions in different branches, as long as each path executes exactly one. trapends a path abnormally. Aji has no unwinding, so paths ending intrapcarry no obligation.- Using or ending a borrow on a path where it has already been ended is
E2106.
5.2 Places, overlap and access
Every borrow has a root (a local or a lent parameter), a path from the root (field names, a slice range, or an element), a mode (ref/mut) and a parent (none for borrows taken directly from a local or a parameter).
- Two paths overlap unless they diverge at different fields of the same struct, or they are the two results of one
slice.split(and descendants of each). - Element borrows (
borrow.index) and sub-slices of the same parent all overlap each other. - Creating a borrow conflicts with every live borrow whose path overlaps when either is
mut(E2101). - A borrow with live children is frozen: it may still create further children that do not conflict, but it cannot be read, written or ended while a live child conflicts with that use (
E2101,E2102for ending). - A
refborrow is usable for reading; amutborrow is usable for reading and writing, in both cases only if not frozen by a conflicting child. - There is no implicit conversion between borrow types: a
mut/slice_mutborrow is passed where a shared one is expected only through an explicitborrow.ref(E1001otherwise). call: each borrow argument is lent to the callee for the duration of the call. Arguments are checked as if each were a new borrow of its operand at the call: amutargument conflicts with any other argument that overlaps it (E2101).
5.3 Returning a borrow
fn @f(…, %p: slice<u8>, …) -> ref<u8> from %p declares that the result borrows from the argument passed for %p.
fromnames a borrow-typed parameter; its mode must allow the result mode (mutresult needs amut/slice_mutparameter) (E1001).- A borrow-typed return without
fromisE2102. ret %bin such a function requires%bto be%por a descendant of%p. If%b's provenance leads to alocal.allocof this function, the return isE2108(frame escape); if it leads to another parameter,E2102. Atret, the only live owned borrows may be%band its owned ancestors, which are transferred to the caller with it; any other live owned borrow isE2105.- Escape through aggregates is impossible by construction: borrow types cannot be fields, payloads or array elements (§2), so
struct.make,enum.makeand returned aggregates never contain a borrow. frommay name only a borrow parameter. Later features may add other escapable sources (for example region-derived borrows in Phase 2); frame-derived borrows never escape.- At the call site, the result is a new owned borrow whose parent is the argument passed for
%p; the argument cannot be ended before the result (E2102).
5.4 Joins and loops
The complete borrow state MUST be identical on every edge into a block (E2105, reported at the block). The state of each borrow consists of its identity (SSA value), kind (ref/mut), provenance (root), path, parent borrow, and whether it is live or ended. In particular a borrow created inside a loop body is ended before the back edge.
Only the set of accepted programs is normative. Because states must match exactly at joins, an implementation can verify a function in one pass over a reverse postorder with one comparison per back edge, but it is not required to work that way.
5.5 Frame size
Each function's frame storage is the sum of the accounted sizes of its local.alloc types. It MUST NOT exceed max_local_frame = 1 048 576 bytes (E5101, limit local frame bytes). Accounted size is a normative counting rule, not a memory layout:
| Type | Accounted bytes |
|---|---|
u8/i8/bool |
1 |
u16/i16 |
2 |
u32/i32 |
4 |
u64/i64 |
8 |
u128/i128 |
16 |
usize/isize |
8 (the largest supported pointer width, so the result is target-independent) |
| struct | sum of its fields |
| enum | 4 + the largest sum over a variant's payload |
array<T, N> |
N × accounted size of T |
5.6 Borrow-typed block parameters
Borrow-typed block parameters are reserved for the feature borrow_joins. Without it they are rejected with E0006 reason feature_not_declared. Using a feature gate, rather than a permanent type error, keeps the door open without breaking the format-0 freeze (spec 07 §8.7).
6. Bounds facts
A fact %i < %n is established only by the following table. %i and %n are SSA values of type usize; %c is the comparison result.
Comparison defining %c |
Established by | Fact |
|---|---|---|
%c = lt.usize %i, %n |
true edge of cbr %c, or guard %c |
%i < %n |
%c = gt.usize %n, %i |
true edge of cbr %c, or guard %c |
%i < %n |
%c = ge.usize %i, %n |
false edge of cbr %c |
%i < %n |
%c = le.usize %n, %i |
false edge of cbr %c |
%i < %n |
Nothing else establishes a fact. In particular le.usize %i, %n and ge.usize %n, %i (that is, i ≤ n) do not, since i = n is out of bounds; comparisons of other types do not; and no arithmetic is performed.
Scope of a fact:
- from a
cbredge: the target block and every block it dominates, provided the target block's only predecessor is the block containing thecbr(so the edge is the only way in); - from
guard %c: the instructions after theguardin its block and every block that block dominates.
borrow.index.proven %b, %i is proven exactly when one of these holds at the instruction:
| Case | Condition |
|---|---|
| fact on the same slice | a fact %i < %n holds where %n = slice.len %b, with %i and %b the same SSA values as the operands |
| constant index | %i = const.usize k and %b has a static length N (a borrow of array<T, N>) with k < N |
| fact against a constant | a fact %i < %n holds where %n = const.usize k, %b has a static length N and k ≤ N |
This is the complete normative analysis: an implementation MUST NOT accept more or fewer programs.
7. Runtime and interpreter
- Frame storage is allocated by
local.allocand released at return; no code runs at release. - The interpreter represents a borrow as (storage, path) and a slice as (storage, path, offset, length). It performs no alias or liveness checks at runtime: the verifier guarantees them. Bounds checks run exactly where §4 says.
- Behavior is deterministic; storage addresses are never observable.
8. Diagnostics
| Code | Kind | Meaning |
|---|---|---|
| E1010 | init.read_uninitialized | (existing) local read or borrowed before definite initialization |
| E2101 | borrow.mutable_alias | (existing) conflicting borrow or access |
| E2102 | borrow.outlives_owner | (existing) borrow escapes its storage, parent ended before child, or borrow returned without valid from |
| E2103 | memory.out_of_bounds | (existing) borrow.index.proven without a proof |
| E2105 | borrow.not_ended | owned borrow live at ret or live on only some edges into a block |
| E2106 | borrow.use_after_end | borrow used or ended after it was ended |
| E2107 | borrow.not_owned | lent (parameter) borrow ended by the callee |
| E2108 | borrow.escapes_frame | borrow derived from frame storage returned from its function |
| E5002 | air.invalid_cfg | (existing) local.alloc outside the entry block |
| E5101 | verifier.resource_limit | (existing) frame storage above max_local_frame |
| T0008 | trap.out_of_bounds | runtime bounds check failed |
Structured fields: function, block, inst, borrow (SSA name), root, and for E2101 the conflicting borrow conflicts_with.
Valid example
module example.copy_core
air 0
lang 0.1
features [arrays, ownership]
fn @copy(%dst: slice_mut<u8>, %src: slice<u8>) -> void
effects []
entry:
slice.copy %dst, %src ; traps T0008 on length mismatch
ret
fn @main() -> u8
effects []
entry:
%buf = local.alloc array<u8, 8>
local.zero %buf
%m = borrow.mut %buf
%s = slice.make %m
%four = const.usize 4
%lo, %hi = slice.split %s, %four
%src = borrow.ref %lo ; shared view: no implicit mut -> shared conversion
call @copy(%hi, %src) ; disjoint halves: accepted
borrow.end %src
borrow.end %lo
borrow.end %hi
borrow.end %s
borrow.end %m
%v = local.load %buf
...
Rejected examples
%a = borrow.mut %buf
%b = borrow.ref %buf ; E2101: %a is live and mut
fn @leak() -> ref<u8> ; E2102: borrow return without `from`
fn @escape(%p: ref<u8>) -> ref<u8> from %p
effects []
entry:
%x = local.alloc u8
local.zero %x
%b = borrow.ref %x
ret %b ; E2108: frame storage escapes
fn @f(%n: usize) -> void
effects []
entry:
br @loop()
loop:
%x = local.alloc u8 ; E5002: local.alloc outside the entry block
...
%ok = le.usize %i, %len ; i <= len does not prove i < len
cbr %ok, @use(), @out()
use:
%e = borrow.index.proven %s, %i ; E2103
%x = local.alloc u8
%v = local.load %x ; E1010: not initialized
%e = borrow.index.proven %s, %i ; E2103: no fact %i <u slice.len %s
%r = borrow.ref %buf
ret %v ; E2105: %r not ended
Diagnostics and compatibility
Additive under spec 07 §8.7: new types, instructions, codes and a trap code, all inside the features ownership and arrays. The phase-1 conformance test use_after_move (needs Own<T>) moves to Phase 2.
Spec integration
On acceptance: spec 03 (§3, §4, §6, §9–§11: frame storage, second-class borrows, slices, initialization), spec 07 §8 (syntax, multi-result), spec 07a (instruction contracts), spec 08 (§5–§6 facts; analysis order types → init → borrows → bounds), reference/air-text.ebnf, reference/air-features.md, reference/error-codes.md. Plan: Phase 1 and Phase 2 files.
Alternatives considered
- Explicit lifetime parameters (Rust-style): more expressive (borrows in structs) but a much larger verifier and diagnostics surface. Rejected by Q-009.
- Implicit borrow end at last use (non-lexical lifetimes): fewer instructions, but live ranges become an inference result instead of a visible fact. Explicit
borrow.endmakes the verifier's transfer function local and diagnostics exact. - Pure value semantics (arrays updated functionally, no borrows): aliasing becomes impossible, but conflicts with spec 03's borrow model and makes I/O buffers awkward before ownership-passing I/O exists.
- Runtime generation checks for regions: rejected by Q-010.
- Heap in Phase 1 without capabilities: would grant implicit authority and require an incompatible change later.
Unresolved questions
- Whether
borrow_joinsshould be specified in Phase 2 or later. - Whether
slice.copyshould also exist as a proven variant without the length check (not needed for Phase 1).