Phase 6A: .aji layers 3-4 (addressable values, lexical borrows, slices)

Owner rules: addressability is decided by lowering (a variable that is
borrowed, indexed or written through a place gets frame storage in the
entry block; otherwise SSA; by name, deterministic); borrows are bound
only by 'ref r = place;' / 'mut r = place;' and live to the end of the
block (borrow.end at every normal exit, including break/continue/return);
no shadowing of an active borrow in the same scope; ref/mut as call
arguments are temporaries ended after the call; *r only for scalar and
struct referents, slices via s[i], s[a..b], s.len; writes *r = v,
r.f = v, s[i] = v with the value evaluated first; split_at as the only
multi-result binding; slice.copy(mut dst, ref src) as the only copy;
[e; N] evaluates e once; byte strings as [u8; N] in frame storage with
a fixed escape set; borrow.index.proven only for the verifier's own
fact form (if i < s.len, same SSA values), otherwise checked.

Diagnostics on lowered AIR are deduplicated per code and source
location. aji lower --stats counts code only (no comment lines).

Tests: 10 run (architecture goldens: value-only has no local.alloc,
borrowed has local.alloc, proven indexing), 6 rejects incl.
verifier-caught E2101 (aliasing; parent used after split_at).
Expansion on these programs: 4-9x bytes, 2.2-6.6x lines.
This commit is contained in:
dev committed 2026-10-05 11:51:04 +03:00
1 parent 0fdd04179b
commit eca30f817a
28 files changed
+1157 -73

No files matched your search

+9 -8
View File
@@ -79,19 +79,20 @@ There is one spelling per construct: no `+=`, no alternative syntax.
```aji
var data: [u64; 8] = zero;
let s = mut data; // exclusive borrow (here: slice_mut<u64>)
let e = mut s[i]; // element borrow; out of range traps T0008
*e = 7; // store through mut
let v = *e; // load
data[0] = 7; // element write (array variables get frame storage automatically)
mut s = data; // bind an exclusive borrow (an array is borrowed as slice_mut<u64>)
s[1] = s[0] + 1; // checked index; out of range traps T0008
let n = s.len;
let (l, r) = s.split_at(4); // two disjoint halves
copy(dst, ref src); // element-wise copy, same element type
let first = ref p.a; // field borrow; r.f reads a field through a borrow
if i < s.len { s[i] = 0; } // no runtime check here: the verifier proves i < s.len
let (l, r) = s.split_at(4); // two disjoint halves of s
slice.copy(mut out[0..4], ref data[4..8]); // borrows as arguments end after the call
ref q = p; // shared borrow of a struct; q.a reads a field
*c = *c + 1; // through mut<u64>
```
Rules (checked by the verifier):
- **A borrow lives until the end of the block where it was `let`-bound.** A borrow written directly as an argument lives until the end of the statement. To end one early, put it in an inner `{ … }` block.
- **A borrow lives until the end of the block where it was bound** (`ref r = …;` / `mut r = …;`). A borrow written as an argument (`f(mut buf)`) lives for that call. To end one early, put it in an inner `{ … }` block.
- **Exclusive XOR shared.** While a `mut` borrow of a place is live, no other borrow of the same place, and no direct use of it, is allowed (`E2101`). Disjoint fields and `split_at` halves are independent.
- A borrow cannot outlive what it borrows from (`E2102`). A function returns a borrow only with `-> ref<T> from param`.
- **Moves are explicit:** a named linear value is consumed only as `move x` (otherwise `E7003`). A fresh temporary needs no `move`: `consume(make())`. Copyable values are passed plainly; `move` on them is `E7006`.
+1 -2
View File
@@ -40,8 +40,7 @@ Lowering layers (P6A-3/4), each with accepted (golden AIR), rejected (`E7xxx`) a
|---|---|---|
| 1 | scalars, expressions, functions, control flow, variables carried across joins, short-circuit `and`/`or`, casts | done |
| 2 | structs (named construction, declaration-order lowering), enums (`Enum::Variant`), exhaustive `match` over enums (no `_`) and integers (`_` required), `match` as an expression; outcome matches and `or trap` move to layer 5 with the fallible operations | done |
| 3 | arrays, slices, byte strings | todo |
| 4 | lexical borrows, frame storage, `move`, places | todo |
| 3–4 | addressable values (frame storage chosen by lowering), lexical borrows (`ref r = …;`/`mut r = …;`), places and writes, arrays, slices, `split_at`, `slice.copy`, byte strings, proven indexing for the verifier's fact form; `move` comes with linear values in layer 5 | done |
| 5 | `own`, regions, resources, unsafe | todo |
| 6 | capabilities, intrinsics, I/O | todo |
| 7 | tasks, channels | todo |
+33 -11
View File
@@ -158,16 +158,37 @@ Lowering: structured control flow becomes blocks and terminators. A variable ass
| Source | Meaning |
|---|---|
| `ref place` | shared borrow (`borrow.ref`, `borrow.field`, `borrow.index`, `slice.make`, `slice.sub` as the place requires) |
| `mut place` | exclusive borrow |
| `*r` | load through `ref<T>`/`mut<T>` |
| `r.f` | load of a field through a borrow of a struct |
| `s.len` | `slice.len` of a slice, or the static length of an array |
| `s.split_at(k)` | `slice.split`; bound with `let (left, right) = …;` |
| `copy(dst, src)` | `slice.copy` |
| `ref r = place;` / `mut r = place;` | **the only way to bind a borrow**: a statement, valid to the end of the enclosing block. `let r = mut x;` is `E7006`. An active borrow cannot be shadowed by the same name in the same scope (`E7006`). Borrowing a whole array binds a slice (`slice<T>` / `slice_mut<T>`). Borrowing a borrow binding reborrows it. |
| `ref place` / `mut place` as an argument | a temporary borrow for that call (ended right after it); allowed only as an argument |
| `*r` | load through `ref<T>`/`mut<T>` of a scalar or struct; slices and arrays use `s[i]` (`E7006`) |
| `r.f` | field through a borrow of a struct (borrow, load, end) |
| `s[i]` | element value (borrow, load, end) |
| `s.len` | `slice.len`, or the static length of an array |
| `s[a..b]` | a sub-slice (`slice.sub` with start `a`, length `b - a`); a borrow, so it is bound or passed, never a value |
| `let (left, right) = s.split_at(k);` | `slice.split`: two disjoint borrows of `s`; the only multi-result binding. There is no general tuple destructuring. |
| `slice.copy(mut dst, ref src)` | `slice.copy`, the only copy form |
- **Indexing.** `s[i]` is a checked index (`T0008`). Inside the true branch of `if i < s.len`, with `i` and `s` unchanged, the frontend emits `borrow.index.proven`. The verifier confirms the fact; otherwise the index stays checked.
- **Unsafe indexing.** `ref unchecked s[i]` and `mut unchecked s[i]` are `borrow.index.unchecked`, and `assume(c)` is `assume`. Both are allowed only under spec 13 §6.
**Writes.**
- `*r = v;` for a scalar or struct referent;
- `r.f = v;`, `x.f = v;`;
- `s[i] = v;`, `x[i] = v;`.
The value is evaluated first, then the place is borrowed for the store and the borrow ends. So `s[i] = s[i] + 1;` reads before it writes.
**Bounds.** These follow AIR exactly:
- `s[i]` is a checked `borrow.index` (`T0008`);
- inside the true branch of `if i < s.len`, with `i` and `s` the unchanged SSA values, the frontend emits `borrow.index.proven`. This is the fact the verifier proves (spec 08 §12, "fact on the same slice");
- in every other case the index stays checked. The frontend never knows more about bounds than the verifier.
**Unsafe indexing.** `ref unchecked s[i]` and `mut unchecked s[i]` are `borrow.index.unchecked`, and `assume(c)` is `assume`. Both are allowed only under spec 13 §6.
**Arrays and byte strings.**
- `[e; N]` evaluates `e` once and copies the value N times (`array.splat`). The element must be copyable; a linear element is `E7006`.
- `b"…"` is an immutable `[u8; N]` value held in frame storage, written there byte by byte, with no heap. The escapes are exactly `\n \r \t \\ \" \0 \xNN`.
- Used where a `slice<u8>` is expected, it is borrowed for the operation.
**Moves.**
@@ -286,8 +307,8 @@ A byte string or array used where a `slice<u8>` is expected is materialized in f
## 9. Frame storage, borrow scopes and features
- **Frame storage.** A variable whose place is borrowed lowers to `local<T>` storage allocated in the entry block (spec 08 §11.5). Every other variable is an SSA value. The frame-size limit applies to the lowered AIR. There is no implicit heap allocation.
- **Borrow scopes.** A borrow bound by `let` ends at the end of the block that contains the `let`. A temporary borrow, used directly as an operand, ends at the end of its statement. Both end at every normal exit from their scope. The frontend emits `borrow.end` in reverse order of creation. A borrow that must end earlier is placed in an inner block. Borrows lent to tasks end at the join (spec 04 §34).
- **Frame storage.** Addressability is a property of lowering, not of source types. A variable whose name is borrowed, indexed, or written through a field or element anywhere in the function lowers to `local<T>` storage, allocated in the entry block (spec 08 §11.5). Every other variable is an SSA value. The decision is by name, so the same source always gives the same placement. The frame-size limit applies to the lowered AIR. There is no implicit heap allocation.
- **Borrow scopes.** A borrow bound by `ref`/`mut` ends at the end of the block that contains the binding. Because a borrow's name is not visible after its scope, a use after the borrow ended (`E2106`) cannot be written in source. A temporary borrow, used directly as an operand, ends at the end of its statement. Both end at every normal exit from their scope. The frontend emits `borrow.end` in reverse order of creation. A borrow that must end earlier is placed in an inner block. Borrows lent to tasks end at the join (spec 04 §34).
- **Features.** The frontend emits the `features [...]` header as the dependency closure of the constructs used, always including `effects`.
- **Effects.** They are declared by the programmer. `E3101` names the missing effect and suggests the complete effect list (`suggested_effects`).
@@ -297,6 +318,7 @@ A byte string or array used where a `slice<u8>` is expected is materialized in f
|---|---|
| found by the verifier on the lowered AIR | the AIR code; the diagnostic's `file`, `line` and `col` are the source location of the construct the instruction came from, and `air_line`/`air_col` locate it in the lowered AIR (`aji lower`); the verifier's own fields (`function`, `block`, `inst`) are kept |
| syntax, name resolution, lowering and invalid source constructs | `E7xxx`, below |
| several AIR instructions of one construct reporting the same code | one diagnostic (the first) per code and source location |
| trap at run time | the trap record also carries `source_location` (`file`, `line`, `col`) |
| Code | Kind | Meaning |