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:
1 parent
0fdd04179b
commit
eca30f817a
28 files changed
+1157
-73
No files matched your search
@@ -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`.
|
||||
|
||||
Reference in new issue
Block a user