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

@@ -1,11 +1,11 @@
; The same feature is declared twice: reasons unsupported_feature (arrays is reserved) and duplicate_feature.
; expect: reject E0006 E0006
; The same feature is declared twice.
; expect: reject E0006
; phase: 0
; spec: docs/spec/07-air.md
module test.duplicate_feature
air 0
lang 0.1
features [arrays, arrays]
features [arrays, arrays, ownership]
fn @f() -> void
effects []
@@ -0,0 +1,13 @@
; arrays depends on ownership, which is not declared.
; expect: reject E0006
; phase: 0
; spec: docs/spec/07-air.md
module test.missing_dependency
air 0
lang 0.1
features [arrays]
fn @f() -> void
effects []
entry:
ret
@@ -1,11 +1,11 @@
; ownership is registered but reserved until Phase 1 publishes it.
; heap is registered but reserved until Phase 2 publishes it.
; expect: reject E0006
; phase: 0
; spec: docs/spec/07-air.md
module test.reserved_feature
air 0
lang 0.1
features [ownership]
features [heap]
fn @f() -> void
effects []
@@ -0,0 +1,26 @@
; Exclusive borrows of two different fields coexist.
; expect: accept
; phase: 1
; spec: docs/spec/08-verifier.md
module test.disjoint_fields
air 0
lang 0.1
features [arrays, ownership]
type @P = struct { a: u8, b: u8 }
fn @f() -> void
effects []
entry:
%x = local.alloc @P
local.zero %x
%m = borrow.mut %x
%a = borrow.field @P.a, %m
%b = borrow.field @P.b, %m
%one = const.u8 1
store %a, %one
store %b, %one
borrow.end %a
borrow.end %b
borrow.end %m
ret
@@ -0,0 +1,25 @@
; A borrow ended in each branch of a cbr: exactly one end executes on every path.
; expect: accept
; phase: 1
; spec: docs/spec/08-verifier.md
module test.path_linear_ends
air 0
lang 0.1
features [arrays, ownership]
fn @f(%c: bool) -> u8
effects []
entry:
%x = local.alloc u8
local.zero %x
%r = borrow.ref %x
cbr %c, @a(), @b()
a:
%v = load %r
borrow.end %r
ret %v
b:
borrow.end %r
%z = const.u8 0
ret %z
@@ -0,0 +1,15 @@
; A borrow returned with `from` derives from the named parameter.
; expect: accept
; phase: 1
; spec: docs/spec/08-verifier.md
module test.return_from_param
air 0
lang 0.1
features [arrays, ownership]
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,21 @@
; Any number of shared borrows and direct reads coexist.
; expect: accept
; phase: 1
; spec: docs/spec/08-verifier.md
module test.shared_borrows
air 0
lang 0.1
features [arrays, ownership]
fn @f() -> u8
effects []
entry:
%x = local.alloc u8
local.zero %x
%a = borrow.ref %x
%b = borrow.ref %x
%v = local.load %x
borrow.end %a
borrow.end %b
ret %v
@@ -0,0 +1,26 @@
; The two halves of slice.split are disjoint, so one can be copied into the other.
; expect: accept
; phase: 1
; spec: docs/spec/08-verifier.md
module test.split_halves
air 0
lang 0.1
features [arrays, ownership]
fn @f() -> void
effects []
entry:
%x = local.alloc array<u8, 8>
local.zero %x
%m = borrow.mut %x
%s = slice.make %m
%k = const.usize 4
%l, %r = slice.split %s, %k
%src = borrow.ref %l
slice.copy %r, %src
borrow.end %src
borrow.end %l
borrow.end %r
borrow.end %s
borrow.end %m
ret
@@ -0,0 +1,18 @@
; local.alloc in a loop body would make the frame dynamic.
; expect: reject E5002
; phase: 1
; spec: docs/spec/08-verifier.md
module test.alloc_outside_entry
air 0
lang 0.1
features [arrays, ownership]
fn @f(%c: bool) -> void
effects []
entry:
br @loop()
loop:
%x = local.alloc u8
cbr %c, @loop(), @out()
out:
ret
@@ -0,0 +1,15 @@
; Borrow-typed block parameters need the reserved feature borrow_joins.
; expect: reject E0006
; phase: 1
; spec: docs/spec/08-verifier.md
module test.borrow_block_parameter
air 0
lang 0.1
features [arrays, ownership]
fn @f(%p: ref<u8>) -> void
effects []
entry:
br @next(%p)
next(%q: ref<u8>):
ret
@@ -0,0 +1,15 @@
; Borrows are second-class and cannot be struct fields.
; expect: reject E1001
; phase: 1
; spec: docs/spec/07-air.md
module test.borrow_in_struct
air 0
lang 0.1
features [arrays, ownership]
type @Holder = struct { r: ref<u8> }
fn @f() -> void
effects []
entry:
ret
@@ -0,0 +1,17 @@
; A borrow is still live at ret.
; expect: reject E2105
; phase: 1
; spec: docs/spec/08-verifier.md
module test.borrow_not_ended
air 0
lang 0.1
features [arrays, ownership]
fn @f() -> void
effects []
entry:
%x = local.alloc u8
local.zero %x
%r = borrow.ref %x
ret
@@ -0,0 +1,15 @@
; A function returns a borrow without naming its source.
; expect: reject E2102
; phase: 1
; spec: docs/spec/08-verifier.md
module test.borrow_return_without_from
air 0
lang 0.1
features [arrays, ownership]
fn @f(%s: slice<u8>) -> ref<u8>
effects []
entry:
%z = const.usize 0
%e = borrow.index %s, %z
ret %e
@@ -0,0 +1,19 @@
; Borrows cannot be copied.
; expect: reject E1001
; phase: 1
; spec: docs/spec/07-air.md
module test.copy_borrow
air 0
lang 0.1
features [arrays, ownership]
fn @f() -> void
effects []
entry:
%x = local.alloc u8
local.zero %x
%r = borrow.ref %x
%r2 = copy %r
borrow.end %r
ret
@@ -0,0 +1,22 @@
; slice.copy with source and destination in the same range is rejected: it is not a memmove.
; expect: reject E2101
; phase: 1
; spec: docs/spec/08-verifier.md
module test.copy_overlapping
air 0
lang 0.1
features [arrays, ownership]
fn @f() -> void
effects []
entry:
%x = local.alloc array<u8, 8>
local.zero %x
%m = borrow.mut %x
%s = slice.make %m
%src = borrow.ref %s
slice.copy %s, %src
borrow.end %src
borrow.end %s
borrow.end %m
ret
@@ -0,0 +1,14 @@
; A callee ends a borrow it was lent.
; expect: reject E2107
; phase: 1
; spec: docs/spec/08-verifier.md
module test.end_lent_parameter
air 0
lang 0.1
features [arrays, ownership]
fn @f(%p: ref<u8>) -> void
effects []
entry:
borrow.end %p
ret
@@ -0,0 +1,20 @@
; The parent borrow is ended while its child is live.
; expect: reject E2102
; phase: 1
; spec: docs/spec/08-verifier.md
module test.end_parent_first
air 0
lang 0.1
features [arrays, ownership]
fn @f() -> void
effects []
entry:
%x = local.alloc u8
local.zero %x
%m = borrow.mut %x
%r = borrow.ref %m
borrow.end %m
borrow.end %r
ret
@@ -0,0 +1,14 @@
; local.alloc belongs to ownership, which the module does not declare.
; expect: reject E0006
; phase: 1
; spec: docs/spec/07-air.md
module test.feature_not_declared
air 0
lang 0.1
features []
fn @f() -> void
effects []
entry:
%x = local.alloc u8
ret
@@ -0,0 +1,17 @@
; A borrow of frame storage is returned.
; expect: reject E2108
; phase: 1
; spec: docs/spec/08-verifier.md
module test.frame_escape
air 0
lang 0.1
features [arrays, ownership]
fn @f(%p: ref<u8>) -> ref<u8> from %p
effects []
entry:
%x = local.alloc u8
local.zero %x
%r = borrow.ref %x
ret %r
@@ -0,0 +1,14 @@
; 2 MiB of frame storage exceeds max_local_frame.
; expect: reject E5101
; phase: 1
; spec: docs/spec/08-verifier.md
module test.frame_too_large
air 0
lang 0.1
features [arrays, ownership]
fn @f() -> void
effects []
entry:
%x = local.alloc array<u8, 2097152>
ret
@@ -0,0 +1,24 @@
; The borrow is ended on one edge into the join but not on the other.
; expect: reject E2105
; phase: 1
; spec: docs/spec/08-verifier.md
module test.join_mismatch
air 0
lang 0.1
features [arrays, ownership]
fn @f(%c: bool) -> void
effects []
entry:
%x = local.alloc u8
local.zero %x
%r = borrow.ref %x
cbr %c, @a(), @b()
a:
borrow.end %r
br @join()
b:
br @join()
join:
trap
@@ -0,0 +1,22 @@
; i <= len(s) does not prove i < len(s).
; expect: reject E2103
; phase: 1
; spec: docs/spec/08-verifier.md
module test.le_is_not_a_fact
air 0
lang 0.1
features [arrays, ownership]
fn @f(%s: slice<u8>, %i: usize) -> u8
effects []
entry:
%n = slice.len %s
%c = le.usize %i, %n
cbr %c, @in(), @out()
in:
%e = borrow.index.proven %s, %i
%v = load %e
borrow.end %e
ret %v
out:
trap
@@ -0,0 +1,20 @@
; A shared borrow is created while an exclusive one is live.
; expect: reject E2101
; phase: 1
; spec: docs/spec/08-verifier.md
module test.mut_and_shared
air 0
lang 0.1
features [arrays, ownership]
fn @f() -> void
effects []
entry:
%x = local.alloc u8
local.zero %x
%m = borrow.mut %x
%r = borrow.ref %x
borrow.end %r
borrow.end %m
ret
@@ -0,0 +1,15 @@
; The local is read before it is initialized.
; expect: reject E1010
; phase: 1
; spec: docs/spec/08-verifier.md
module test.read_uninitialized
air 0
lang 0.1
features [arrays, ownership]
fn @f() -> u8
effects []
entry:
%x = local.alloc u8
%v = local.load %x
ret %v
@@ -0,0 +1,19 @@
; A borrow is used after borrow.end.
; expect: reject E2106
; phase: 1
; spec: docs/spec/08-verifier.md
module test.use_after_end
air 0
lang 0.1
features [arrays, ownership]
fn @f() -> u8
effects []
entry:
%x = local.alloc u8
local.zero %x
%r = borrow.ref %x
borrow.end %r
%v = load %r
ret %v
@@ -0,0 +1,17 @@
; Enums are not zeroable.
; expect: reject E1001
; phase: 1
; spec: docs/spec/07a-air-instructions.md
module test.zero_enum
air 0
lang 0.1
features [arrays, ownership]
type @E = enum { A, B }
fn @f() -> void
effects []
entry:
%x = local.alloc @E
local.zero %x
ret
@@ -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