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

@@ -0,0 +1,17 @@
// Architecture: a borrowed variable gets frame storage (local.alloc in the entry block, local.store).
// expect: run
// result: 7
// phase: 6
// spec: docs/spec/15-source-language.md
module demo.addressable
fn main() -> u64
effects []
{
var x: u64 = 5;
{
mut r = x;
*r = 7;
}
return x;
}
@@ -0,0 +1,17 @@
module demo.addressable
air 0
lang 0.1
features [effects, ownership]
fn @main() -> u64
effects []
entry:
%x_1 = local.alloc u64
%c_2 = const.u64 5
local.store %x_1, %c_2
%x_3 = borrow.mut %x_1
%c_4 = const.u64 7
store %x_3, %c_4
borrow.end %x_3
%x_5 = local.load %x_1
ret %x_5
@@ -0,0 +1,20 @@
// Element writes and reads on an array variable: the frontend gives it frame storage.
// expect: run
// result: 140
// phase: 6
// spec: docs/spec/15-source-language.md
module demo.arraysum
fn main() -> u64
effects []
{
var buf: [u64; 8] = zero;
for i in 0..buf.len {
buf[i] = i as! u64 * 5;
}
var total: u64 = 0;
for i in 0..8usize {
total = total + buf[i];
}
return total;
}
@@ -0,0 +1,23 @@
// [e; N] evaluates e once and copies it; an array that is only passed around stays an SSA value.
// expect: run
// result: 28
// phase: 6
// spec: docs/spec/15-source-language.md
module demo.arrayvalue
fn total(a: ref<[u8; 4]>) -> u64
effects []
{
return 0;
}
fn main() -> u64
effects []
{
var a: [u8; 4] = [7u8; 4];
var sum: u64 = 0;
for i in 0..4usize {
sum = sum + a[i] as u64;
}
return sum;
}
@@ -0,0 +1,35 @@
// Functions take borrows; the caller lends them for the call (temporaries end after it).
// expect: run
// result: 12
// phase: 6
// spec: docs/spec/15-source-language.md
module demo.params
fn sum(s: slice<u64>) -> u64
effects []
{
var t: u64 = 0;
for i in 0..s.len {
t = t + s[i];
}
return t;
}
fn bump(c: mut<u64>) -> void
effects []
{
*c = *c + 1;
}
fn main() -> u64
effects []
{
var counter: u64 = 0;
bump(mut counter);
bump(mut counter);
var data: [u64; 3] = zero;
data[0] = 3;
data[1] = 3;
data[2] = 4;
return sum(ref data) + counter;
}
@@ -0,0 +1,14 @@
// A checked index out of range traps T0008 at run time, with the source location.
// expect: run
// trap: T0008
// phase: 6
// spec: docs/spec/15-source-language.md
module demo.bounds
fn main() -> u64
effects []
{
var d: [u64; 2] = zero;
let i: usize = 2;
return d[i];
}
@@ -0,0 +1,19 @@
// A byte string is an array in frame storage; escapes \n and \x41.
// expect: run
// result: 3
// phase: 6
// spec: docs/spec/15-source-language.md
module demo.bytes
fn main() -> u64
effects []
{
let msg = b"hi\nA\x41";
var count: u64 = 0;
for i in 0..msg.len {
if msg[i] == 65 or msg[i] == 10 {
count = count + 1;
}
}
return count;
}
@@ -0,0 +1,23 @@
// Architecture: under `if i < s.len` with i and s unchanged the index is borrow.index.proven.
// expect: run
// result: 9
// phase: 6
// spec: docs/spec/15-source-language.md
module demo.proven
fn get(s: slice<u64>, i: usize) -> u64
effects []
{
if i < s.len {
return s[i];
}
return 0;
}
fn main() -> u64
effects []
{
var d: [u64; 2] = zero;
d[1] = 9;
return get(ref d, 1) + get(ref d, 5);
}
@@ -0,0 +1,48 @@
module demo.proven
air 0
lang 0.1
features [arrays, effects, ownership]
fn @get(%s_1: slice<u64>, %i_2: usize) -> u64
effects []
entry:
%len_3 = slice.len %s_1
%cmp_4 = lt.usize %i_2, %len_3
cbr %cmp_4, @then_5(), @else_6()
then_5:
%e_7 = borrow.index.proven %s_1, %i_2
%v_8 = load %e_7
borrow.end %e_7
ret %v_8
else_6:
br @endif_9()
endif_9:
%c_10 = const.u64 0
ret %c_10
fn @main() -> u64
effects []
entry:
%d_1 = local.alloc array<u64, 2>
local.zero %d_1
%c_2 = const.u64 9
%d_3 = borrow.mut %d_1
%c_4 = const.usize 1
%e_5 = borrow.index %d_3, %c_4
store %e_5, %c_2
borrow.end %e_5
borrow.end %d_3
%d_6 = borrow.ref %d_1
%s_7 = slice.make %d_6
%c_8 = const.usize 1
%get_9 = call @get(%s_7, %c_8)
borrow.end %s_7
borrow.end %d_6
%d_10 = borrow.ref %d_1
%s_11 = slice.make %d_10
%c_12 = const.usize 5
%get_13 = call @get(%s_11, %c_12)
borrow.end %s_11
borrow.end %d_10
%t_14 = add.checked.u64 %get_9, %get_13
ret %t_14
@@ -0,0 +1,14 @@
// The frontend does not check aliasing: the verifier reports E2101 at the second borrow.
// expect: reject E2101
// phase: 6
// spec: docs/spec/15-source-language.md
module demo.conflict
fn main() -> u64
effects []
{
var buf: [u64; 2] = zero;
mut a = buf;
ref b = buf;
return 0;
}
@@ -0,0 +1,13 @@
// One spelling: borrows are bound with `ref r = …;` / `mut r = …;`.
// expect: reject E7006
// phase: 6
// spec: docs/spec/15-source-language.md
module demo.letborrow
fn main() -> u64
effects []
{
var x: u64 = 1;
let r = mut x;
return 0;
}
@@ -0,0 +1,17 @@
// `*` is for scalar and struct referents; slices use s[i].
// expect: reject E7006
// phase: 6
// spec: docs/spec/15-source-language.md
module demo.derefslice
fn first(s: slice<u64>) -> u64
effects []
{
return *s;
}
fn main() -> u64
effects []
{
return 0;
}
@@ -0,0 +1,11 @@
// expect: reject E7005
// phase: 6
// spec: docs/spec/15-source-language.md
module demo.indexscalar
fn main() -> u64
effects []
{
var x: u64 = 1;
return x[0];
}
@@ -0,0 +1,15 @@
// An active borrow cannot be shadowed by the same name in the same scope.
// expect: reject E7006
// phase: 6
// spec: docs/spec/15-source-language.md
module demo.shadow
fn main() -> u64
effects []
{
var x: u64 = 1;
var y: u64 = 2;
ref r = x;
ref r = y;
return *r;
}
@@ -0,0 +1,16 @@
// After split_at the parent slice cannot be used while the halves live: E2101 from the verifier.
// expect: reject E2101
// phase: 6
// spec: docs/spec/15-source-language.md
module demo.splitparent
fn main() -> u64
effects []
{
var buf: [u64; 4] = zero;
mut s = buf;
let (l, r) = s.split_at(2);
s[0] = 1;
l[0] = 2;
return 0;
}
@@ -0,0 +1,27 @@
// mut binding, proven indexing under `if i < s.len`, split_at into disjoint halves, ranges and slice.copy.
// expect: run
// result: 4321
// phase: 6
// spec: docs/spec/15-source-language.md
module demo.slices
fn main() -> u64
effects []
{
var buf: [u64; 4] = zero;
{
mut s = buf;
for i in 0..4usize {
if i < s.len {
s[i] = i as! u64 + 1;
}
}
let (left, right) = s.split_at(2);
left[0] = left[0] * 1;
right[1] = right[1] * 1;
}
var out: [u64; 4] = zero;
slice.copy(mut out[0..2], ref buf[2..4]);
slice.copy(mut out[2..4], ref buf[0..2]);
return out[1] * 1000 + out[0] * 100 + out[3] * 10 + out[2];
}
@@ -0,0 +1,28 @@
// Writes through a mut borrow of a struct, a field borrow, reads through a shared borrow.
// expect: run
// result: 57
// phase: 6
// spec: docs/spec/15-source-language.md
module demo.structborrow
struct Pair {
a: u64,
b: u64,
}
fn main() -> u64
effects []
{
var p: Pair = Pair { a: 1, b: 2 };
{
mut r = p;
r.a = 5;
r.b = r.a + 2;
}
{
mut f = p.b;
*f = *f * 7;
}
ref q = p;
return q.a + q.b + 3;
}
@@ -0,0 +1,15 @@
// Architecture: a variable used only by value stays SSA (the golden AIR has no local.alloc).
// expect: run
// result: 10
// phase: 6
// spec: docs/spec/15-source-language.md
module demo.valueonly
fn main() -> u64
effects []
{
let x = 5u64;
var y: u64 = x;
y = y + x;
return y;
}
@@ -0,0 +1,11 @@
module demo.valueonly
air 0
lang 0.1
features [effects]
fn @main() -> u64
effects []
entry:
%c_1 = const.u64 5
%t_2 = add.checked.u64 %c_1, %c_1
ret %t_2