Phase 1: frame storage, second-class borrows, arrays (P1-3..P1-10)

- air: types local/ref/mut/array/slice/slice_mut, from-returns,
  multi-result binding, 18 new instructions; feature gating reports
  E0006 feature_not_declared; borrow-typed block parameters gated by
  borrow_joins; printer and round-trip tests for all new forms
- verifier: second-class and zeroable type rules (E1001); local.alloc
  only in entry (E5002); frame size accounting (E5101); stages init
  (E1010), borrow (E2101, E2102, E2105-E2108) and bounds facts (E2103);
  value types shared between stages
- interp: frame cells, places, slices, deep copies between storage and
  SSA values, T0008 bounds traps
- features arrays and ownership supported; implemented_phase = 1
- conformance: 5 accept, 18 reject, 11 run tests for Phase 1, including
  the file-copy core exit program; 6000 mutants without a crash
This commit is contained in:
dev committed 2026-10-04 13:24:09 +03:00
1 parent be9dd0e1ed
commit 1ae6b485d8
50 files changed
+2376 -37

No files matched your search

@@ -0,0 +1,32 @@
; A borrow returned with `from` reads storage of the caller.
; expect: run
; result: 5
; phase: 1
; spec: docs/spec/07a-air-instructions.md
module test.borrow_from_call
air 0
lang 0.1
features [arrays, ownership]
fn @main() -> u8
effects []
entry:
%x = local.alloc array<u8, 3>
%five = const.u8 5
%a = array.splat array<u8, 3> %five
local.store %x, %a
%r = borrow.ref %x
%s = slice.make %r
%e = call @first(%s)
%v = load %e
borrow.end %e
borrow.end %s
borrow.end %r
ret %v
fn @first(%s: slice<u8>) -> ref<u8> from %s
effects []
entry:
%z = const.usize 0
%e = borrow.index %s, %z
ret %e
@@ -0,0 +1,30 @@
; slice.copy copies len(src) elements into the start of a longer destination.
; expect: run
; result: [9,9,0,0]
; phase: 1
; spec: docs/spec/07a-air-instructions.md
module test.copy_into_prefix
air 0
lang 0.1
features [arrays, ownership]
fn @main() -> array<u8, 4>
effects []
entry:
%x = local.alloc array<u8, 2>
%y = local.alloc array<u8, 4>
%nine = const.u8 9
%a = array.splat array<u8, 2> %nine
local.store %x, %a
local.zero %y
%rx = borrow.ref %x
%sx = slice.make %rx
%my = borrow.mut %y
%sy = slice.make %my
slice.copy %sy, %sx
borrow.end %sy
borrow.end %my
borrow.end %sx
borrow.end %rx
%out = local.load %y
ret %out
@@ -0,0 +1,27 @@
; slice.copy into a shorter destination traps.
; expect: run
; trap: T0008
; phase: 1
; spec: docs/spec/07a-air-instructions.md
module test.copy_short_destination
air 0
lang 0.1
features [arrays, ownership]
fn @main() -> void
effects []
entry:
%x = local.alloc array<u8, 4>
%y = local.alloc array<u8, 2>
local.zero %x
local.zero %y
%rx = borrow.ref %x
%sx = slice.make %rx
%my = borrow.mut %y
%sy = slice.make %my
slice.copy %sy, %sx
borrow.end %sy
borrow.end %my
borrow.end %sx
borrow.end %rx
ret
@@ -0,0 +1,25 @@
; Writes through field borrows land in the struct.
; expect: run
; result: {"a":0,"b":513}
; phase: 1
; spec: docs/spec/07a-air-instructions.md
module test.field_write
air 0
lang 0.1
features [arrays, ownership]
type @P = struct { a: u8, b: u16 }
fn @main() -> @P
effects []
entry:
%x = local.alloc @P
local.zero %x
%m = borrow.mut %x
%b = borrow.field @P.b, %m
%v = const.u16 513
store %b, %v
borrow.end %b
borrow.end %m
%out = local.load %x
ret %out
@@ -0,0 +1,74 @@
; Phase 1 exit program: fill a local buffer, copy it into another in chunks of 4, return the copy.
; expect: run
; result: [0,1,2,3,4,5,6,7,8,9]
; phase: 1
; spec: docs/spec/07a-air-instructions.md
module test.file_copy_core
air 0
lang 0.1
features [arrays, ownership]
fn @main() -> array<u8, 10>
effects []
entry:
%src = local.alloc array<u8, 10>
%dst = local.alloc array<u8, 10>
local.zero %src
local.zero %dst
%ms = borrow.mut %src
%ss = slice.make %ms
%n = slice.len %ss
%z = const.usize 0
br @fill(%z)
fill(%i: usize):
%c = lt.usize %i, %n
cbr %c, @fill_body(), @fill_done()
fill_body:
%e = borrow.index.proven %ss, %i
%v = cast.checked.u8 %i
store %e, %v
borrow.end %e
%one = const.usize 1
%i2 = add.checked.usize %i, %one
br @fill(%i2)
fill_done:
%rs = borrow.ref %ss
%md = borrow.mut %dst
%sd = slice.make %md
%chunk = const.usize 4
call @copy_chunks(%sd, %rs, %chunk)
borrow.end %sd
borrow.end %md
borrow.end %rs
borrow.end %ss
borrow.end %ms
%out = local.load %dst
ret %out
fn @copy_chunks(%dst: slice_mut<u8>, %src: slice<u8>, %chunk: usize) -> void
effects []
entry:
%n = slice.len %src
%z = const.usize 0
br @loop(%z)
loop(%off: usize):
%done = ge.usize %off, %n
cbr %done, @exit(), @body()
body:
%rest = sub.checked.usize %n, %off
%small = lt.usize %rest, %chunk
cbr %small, @tail(), @full()
tail:
br @copy(%rest)
full:
br @copy(%chunk)
copy(%len: usize):
%s = slice.sub %src, %off, %len
%d = slice.sub %dst, %off, %len
slice.copy %d, %s
borrow.end %d
borrow.end %s
%next = add.checked.usize %off, %len
br @loop(%next)
exit:
ret
@@ -0,0 +1,18 @@
; A false guard traps like an assertion.
; expect: run
; trap: T0002
; phase: 1
; spec: docs/spec/07a-air-instructions.md
module test.guard_failure
air 0
lang 0.1
features [arrays, ownership]
fn @main() -> void
effects []
entry:
%a = const.usize 3
%b = const.usize 2
%c = lt.usize %a, %b
guard %c
ret
@@ -0,0 +1,22 @@
; Checked borrow.index past the end traps.
; expect: run
; trap: T0008
; phase: 1
; spec: docs/spec/07a-air-instructions.md
module test.index_out_of_bounds
air 0
lang 0.1
features [arrays, ownership]
fn @main() -> u8
effects []
entry:
%x = local.alloc array<u8, 4>
local.zero %x
%r = borrow.ref %x
%k = const.usize 4
%e = borrow.index %r, %k
%v = load %e
borrow.end %e
borrow.end %r
ret %v
@@ -0,0 +1,24 @@
; slice.sub checks without overflow: a huge len cannot wrap around.
; expect: run
; trap: T0008
; phase: 1
; spec: docs/spec/07a-air-instructions.md
module test.slice_sub_no_overflow
air 0
lang 0.1
features [arrays, ownership]
fn @main() -> void
effects []
entry:
%x = local.alloc array<u8, 4>
local.zero %x
%r = borrow.ref %x
%s = slice.make %r
%a = const.usize 1
%b = const.usize 18446744073709551615
%t = slice.sub %s, %a, %b
borrow.end %t
borrow.end %s
borrow.end %r
ret
@@ -0,0 +1,24 @@
; slice.sub with start + len beyond the slice traps.
; expect: run
; trap: T0008
; phase: 1
; spec: docs/spec/07a-air-instructions.md
module test.slice_sub_out_of_bounds
air 0
lang 0.1
features [arrays, ownership]
fn @main() -> void
effects []
entry:
%x = local.alloc array<u8, 4>
local.zero %x
%r = borrow.ref %x
%s = slice.make %r
%a = const.usize 2
%b = const.usize 3
%t = slice.sub %s, %a, %b
borrow.end %t
borrow.end %s
borrow.end %r
ret
@@ -0,0 +1,26 @@
; A value loaded from a local is a copy: later stores do not change it.
; expect: run
; result: [0,0]
; phase: 1
; spec: docs/spec/07a-air-instructions.md
module test.values_do_not_alias_storage
air 0
lang 0.1
features [arrays, ownership]
fn @main() -> array<u8, 2>
effects []
entry:
%x = local.alloc array<u8, 2>
local.zero %x
%before = local.load %x
%m = borrow.mut %x
%s = slice.make %m
%z = const.usize 0
%e = borrow.index %s, %z
%seven = const.u8 7
store %e, %seven
borrow.end %e
borrow.end %s
borrow.end %m
ret %before