Phase 6A: .aji layer 5 (linear values, own, regions, outcome matches, or trap)
- explicit move for named linear values; E7003 with suggestion "move x"; move of a copyable value is E7006; moved state left to the verifier - own.new/own.array/region.create/region.new/region.array/cap.narrow.budget as outcome matches or with `or trap`; own.take/own.free/region.reset/ region.destroy as AIR names in AIR operand order - region.new/region.array take an explicit ref/mut region binding (E7005 otherwise); reset/destroy end nothing (verifier E2104) - match kind (enum/outcome/integer) decided by the parser; built-in error enums (alloc_error, cap_error, io_error) matched exhaustively - cap<K> types; linear owners borrowed in place - features header = registry closure of the features used - parameters keep their source names (grants by @main parameter name) - aji lower --stats: mechanical_air_eliminated = 1 - source/AIR bytes - spec 15, grammar, quick reference, plan updated
This commit is contained in:
1 parent
eca30f817a
commit
bdcf871e5d
28 files changed
+882
-78
No files matched your search
@@ -3,22 +3,22 @@ air 0
|
||||
lang 0.1
|
||||
features [arrays, effects, ownership]
|
||||
|
||||
fn @get(%s_1: slice<u64>, %i_2: usize) -> u64
|
||||
fn @get(%s: slice<u64>, %i: 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
|
||||
%len_1 = slice.len %s
|
||||
%cmp_2 = lt.usize %i, %len_1
|
||||
cbr %cmp_2, @then_3(), @else_4()
|
||||
then_3:
|
||||
%e_5 = borrow.index.proven %s, %i
|
||||
%v_6 = load %e_5
|
||||
borrow.end %e_5
|
||||
ret %v_6
|
||||
else_4:
|
||||
br @endif_7()
|
||||
endif_7:
|
||||
%c_8 = const.u64 0
|
||||
ret %c_8
|
||||
|
||||
fn @main() -> u64
|
||||
effects []
|
||||
|
||||
@@ -0,0 +1,22 @@
|
||||
// err(e) binds a real alloc_error; it is matched like any closed enum.
|
||||
// expect: run
|
||||
// grant: heap=mem.alloc:8
|
||||
// result: 2
|
||||
// phase: 6
|
||||
// spec: docs/spec/15-source-language.md
|
||||
module demo.allocerror
|
||||
|
||||
fn main(heap: cap<mem.alloc>) -> u64
|
||||
effects [mem.alloc]
|
||||
{
|
||||
match own.array(64, 0u64, heap) {
|
||||
ok(a) => {
|
||||
own.free(move a);
|
||||
1
|
||||
}
|
||||
err(e) => match e {
|
||||
alloc_error::budget_exceeded => 2,
|
||||
alloc_error::out_of_memory => 3,
|
||||
},
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
module demo.allocerror
|
||||
air 0
|
||||
lang 0.1
|
||||
features [arrays, capabilities, effects, heap, ownership, resources]
|
||||
|
||||
fn @main(%heap: cap<mem.alloc>) -> u64
|
||||
effects [mem.alloc]
|
||||
entry:
|
||||
%c_1 = const.usize 64
|
||||
%c_2 = const.u64 0
|
||||
own.array %c_1, %c_2, %heap, ok -> @ok_3, err -> @err_5
|
||||
ok_3(%ok_4: own_array<u64>):
|
||||
own.free %ok_4
|
||||
%c_7 = const.u64 1
|
||||
br @endmatch_14(%c_7)
|
||||
err_5(%err_6: alloc_error):
|
||||
enum.switch alloc_error %err_6, budget_exceeded -> @case_budget_exceeded_8, out_of_memory -> @case_out_of_memory_9
|
||||
case_budget_exceeded_8:
|
||||
%c_10 = const.u64 2
|
||||
br @endmatch_12(%c_10)
|
||||
case_out_of_memory_9:
|
||||
%c_11 = const.u64 3
|
||||
br @endmatch_12(%c_11)
|
||||
endmatch_12(%match_13: u64):
|
||||
br @endmatch_14(%match_13)
|
||||
endmatch_14(%match_15: u64):
|
||||
ret %match_15
|
||||
@@ -0,0 +1,23 @@
|
||||
// cap.narrow.budget gives a smaller allocation budget; caps are copied plainly.
|
||||
// expect: run
|
||||
// grant: heap=mem.alloc:256
|
||||
// result: 5
|
||||
// phase: 6
|
||||
// spec: docs/spec/15-source-language.md
|
||||
module demo.narrow
|
||||
|
||||
fn make(small: cap<mem.alloc>) -> u64
|
||||
effects [mem.alloc]
|
||||
{
|
||||
let x = own.new(5u64, small) or trap;
|
||||
own.take(move x)
|
||||
}
|
||||
|
||||
fn main(heap: cap<mem.alloc>) -> u64
|
||||
effects [mem.alloc]
|
||||
{
|
||||
match cap.narrow.budget(heap, 16) {
|
||||
ok(small) => make(small),
|
||||
err(e) => 0,
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
// own.new is matched over its outcomes; the owner is borrowed in place, moved explicitly and taken.
|
||||
// expect: run
|
||||
// grant: heap=mem.alloc:64
|
||||
// result: 42
|
||||
// phase: 6
|
||||
// spec: docs/spec/15-source-language.md
|
||||
module demo.ownbasic
|
||||
|
||||
fn bump(c: mut<u64>) -> void
|
||||
effects []
|
||||
{
|
||||
*c = *c + 1;
|
||||
}
|
||||
|
||||
fn main(heap: cap<mem.alloc>) -> u64
|
||||
effects [mem.alloc]
|
||||
{
|
||||
match own.new(41u64, heap) {
|
||||
ok(x) => {
|
||||
bump(mut x);
|
||||
own.take(move x)
|
||||
}
|
||||
err(e) => 0,
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
module demo.ownbasic
|
||||
air 0
|
||||
lang 0.1
|
||||
features [capabilities, effects, heap, ownership, resources]
|
||||
|
||||
fn @bump(%c: mut<u64>) -> void
|
||||
effects []
|
||||
entry:
|
||||
%v_1 = load %c
|
||||
%c_2 = const.u64 1
|
||||
%t_3 = add.checked.u64 %v_1, %c_2
|
||||
store %c, %t_3
|
||||
ret
|
||||
|
||||
fn @main(%heap: cap<mem.alloc>) -> u64
|
||||
effects [mem.alloc]
|
||||
entry:
|
||||
%c_1 = const.u64 41
|
||||
own.new %c_1, %heap, ok -> @ok_2, err -> @err_4
|
||||
ok_2(%ok_3: own<u64>):
|
||||
%x_6 = borrow.mut %ok_3
|
||||
call @bump(%x_6)
|
||||
borrow.end %x_6
|
||||
%v_7 = own.take %ok_3
|
||||
br @endmatch_9(%v_7)
|
||||
err_4(%err_5: alloc_error):
|
||||
%c_8 = const.u64 0
|
||||
br @endmatch_9(%c_8)
|
||||
endmatch_9(%match_10: u64):
|
||||
ret %match_10
|
||||
@@ -0,0 +1,22 @@
|
||||
// `or trap`: ok continues, err traps; own.free consumes the owner.
|
||||
// expect: run
|
||||
// grant: heap=mem.alloc:64
|
||||
// result: 9
|
||||
// phase: 6
|
||||
// spec: docs/spec/15-source-language.md
|
||||
module demo.ortrap
|
||||
|
||||
fn read(c: ref<u64>) -> u64
|
||||
effects []
|
||||
{
|
||||
*c
|
||||
}
|
||||
|
||||
fn main(heap: cap<mem.alloc>) -> u64
|
||||
effects [mem.alloc]
|
||||
{
|
||||
let x = own.new(9u64, heap) or trap;
|
||||
let v = read(ref x);
|
||||
own.free(move x);
|
||||
v
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
// A region borrow is bound explicitly and outlives the allocation borrows derived from it.
|
||||
// expect: run
|
||||
// grant: heap=mem.alloc:256
|
||||
// result: 30
|
||||
// phase: 6
|
||||
// spec: docs/spec/15-source-language.md
|
||||
module demo.region
|
||||
|
||||
fn main(heap: cap<mem.alloc>) -> u64
|
||||
effects [mem.alloc]
|
||||
{
|
||||
let r = region.create(heap) or trap;
|
||||
var total: u64 = 0;
|
||||
{
|
||||
ref arena = r;
|
||||
match region.new(arena, 10u64) {
|
||||
ok(item) => {
|
||||
*item = *item + 10;
|
||||
total = total + *item;
|
||||
}
|
||||
err(e) => {}
|
||||
}
|
||||
let s = region.array(arena, 4, 2u64) or trap;
|
||||
s[0] = 8;
|
||||
total = total + s[0] + s[1];
|
||||
}
|
||||
region.reset(r);
|
||||
region.destroy(move r);
|
||||
total
|
||||
}
|
||||
@@ -0,0 +1,54 @@
|
||||
module demo.region
|
||||
air 0
|
||||
lang 0.1
|
||||
features [arrays, capabilities, effects, heap, ownership, regions, resources]
|
||||
|
||||
fn @main(%heap: cap<mem.alloc>) -> u64
|
||||
effects [mem.alloc]
|
||||
entry:
|
||||
region.create %heap, ok -> @ok_1, err -> @err_3
|
||||
ok_1(%ok_2: region):
|
||||
%c_5 = const.u64 0
|
||||
%r_6 = borrow.ref %ok_2
|
||||
%c_7 = const.u64 10
|
||||
region.new %r_6, %c_7, ok -> @ok_8, err -> @err_10
|
||||
err_3(%err_4: alloc_error):
|
||||
trap
|
||||
ok_8(%ok_9: mut<u64>):
|
||||
%v_12 = load %ok_9
|
||||
%c_13 = const.u64 10
|
||||
%t_14 = add.checked.u64 %v_12, %c_13
|
||||
store %ok_9, %t_14
|
||||
%v_15 = load %ok_9
|
||||
%t_16 = add.checked.u64 %c_5, %v_15
|
||||
borrow.end %ok_9
|
||||
br @endmatch_17(%t_16)
|
||||
err_10(%err_11: alloc_error):
|
||||
br @endmatch_17(%c_5)
|
||||
endmatch_17(%total_18: u64):
|
||||
%c_19 = const.usize 4
|
||||
%c_20 = const.u64 2
|
||||
region.array %r_6, %c_19, %c_20, ok -> @ok_21, err -> @err_23
|
||||
ok_21(%ok_22: slice_mut<u64>):
|
||||
%c_25 = const.u64 8
|
||||
%c_26 = const.usize 0
|
||||
%e_27 = borrow.index %ok_22, %c_26
|
||||
store %e_27, %c_25
|
||||
borrow.end %e_27
|
||||
%c_28 = const.usize 0
|
||||
%e_29 = borrow.index %ok_22, %c_28
|
||||
%v_30 = load %e_29
|
||||
borrow.end %e_29
|
||||
%t_31 = add.checked.u64 %total_18, %v_30
|
||||
%c_32 = const.usize 1
|
||||
%e_33 = borrow.index %ok_22, %c_32
|
||||
%v_34 = load %e_33
|
||||
borrow.end %e_33
|
||||
%t_35 = add.checked.u64 %t_31, %v_34
|
||||
borrow.end %ok_22
|
||||
borrow.end %r_6
|
||||
region.reset %ok_2
|
||||
region.destroy %ok_2
|
||||
ret %t_35
|
||||
err_23(%err_24: alloc_error):
|
||||
trap
|
||||
@@ -0,0 +1,19 @@
|
||||
// Correct source, but the owner is consumed again after its move: the verifier reports E2002.
|
||||
// expect: reject E2002
|
||||
// phase: 6
|
||||
// spec: docs/spec/15-source-language.md
|
||||
module demo.doubleconsume
|
||||
|
||||
fn sink(o: own<u64>) -> void
|
||||
effects []
|
||||
{
|
||||
own.free(move o);
|
||||
}
|
||||
|
||||
fn main(heap: cap<mem.alloc>) -> u64
|
||||
effects [mem.alloc]
|
||||
{
|
||||
let x = own.new(1u64, heap) or trap;
|
||||
sink(move x);
|
||||
own.take(move x)
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
// An owner that is never consumed: the verifier reports the leak.
|
||||
// expect: reject E3002
|
||||
// phase: 6
|
||||
// spec: docs/spec/15-source-language.md
|
||||
module demo.leak
|
||||
|
||||
fn main(heap: cap<mem.alloc>) -> u64
|
||||
effects [mem.alloc]
|
||||
{
|
||||
let x = own.new(1u64, heap) or trap;
|
||||
7
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
// A struct with a linear field needs the unpublished linear_aggregates feature: AIR rejects it.
|
||||
// expect: reject E0006
|
||||
// phase: 6
|
||||
// spec: docs/spec/15-source-language.md
|
||||
module demo.linearfield
|
||||
|
||||
struct Holder { o: own<u64> }
|
||||
|
||||
fn main() -> u64
|
||||
effects []
|
||||
{
|
||||
0
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
// Allocation without the effect: the verifier's E3101 (with suggested_effects).
|
||||
// expect: reject E3101
|
||||
// phase: 6
|
||||
// spec: docs/spec/15-source-language.md
|
||||
module demo.missingeffect
|
||||
|
||||
fn main(heap: cap<mem.alloc>) -> u64
|
||||
effects []
|
||||
{
|
||||
let x = own.new(1u64, heap) or trap;
|
||||
own.take(move x)
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
// A named linear value passed without `move`: the frontend asks for it (hint "move x").
|
||||
// expect: reject E7003
|
||||
// phase: 6
|
||||
// spec: docs/spec/15-source-language.md
|
||||
module demo.missingmove
|
||||
|
||||
fn main(heap: cap<mem.alloc>) -> u64
|
||||
effects [mem.alloc]
|
||||
{
|
||||
let x = own.new(1u64, heap) or trap;
|
||||
own.take(x)
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
// An outcome match handles every outcome of the operation.
|
||||
// expect: reject E7004
|
||||
// phase: 6
|
||||
// spec: docs/spec/15-source-language.md
|
||||
module demo.missingoutcome
|
||||
|
||||
fn main(heap: cap<mem.alloc>) -> u64
|
||||
effects [mem.alloc]
|
||||
{
|
||||
match own.new(1u64, heap) {
|
||||
ok(x) => own.take(move x),
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
// `move` applies only to linear values.
|
||||
// expect: reject E7006
|
||||
// phase: 6
|
||||
// spec: docs/spec/15-source-language.md
|
||||
module demo.movecopy
|
||||
|
||||
fn main() -> u64
|
||||
effects []
|
||||
{
|
||||
let a = 3u64;
|
||||
move a
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
// region.new takes an explicit region borrow binding; the frontend creates no hidden one.
|
||||
// expect: reject E7005
|
||||
// phase: 6
|
||||
// spec: docs/spec/15-source-language.md
|
||||
module demo.regionvalue
|
||||
|
||||
fn main(heap: cap<mem.alloc>) -> u64
|
||||
effects [mem.alloc]
|
||||
{
|
||||
let r = region.create(heap) or trap;
|
||||
let item = region.new(r, 1u64) or trap;
|
||||
region.destroy(move r);
|
||||
0
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
// region.reset while an allocation borrow is live: the frontend ends nothing early; the verifier reports E2104.
|
||||
// expect: reject E2104
|
||||
// phase: 6
|
||||
// spec: docs/spec/15-source-language.md
|
||||
module demo.resetlive
|
||||
|
||||
fn main(heap: cap<mem.alloc>) -> u64
|
||||
effects [mem.alloc]
|
||||
{
|
||||
let r = region.create(heap) or trap;
|
||||
var v: u64 = 0;
|
||||
{
|
||||
ref arena = r;
|
||||
let item = region.new(arena, 1u64) or trap;
|
||||
region.reset(r);
|
||||
v = *item;
|
||||
}
|
||||
region.destroy(move r);
|
||||
v
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
// A fallible operation used as a plain value: handle it with match or `or trap`.
|
||||
// expect: reject E7006
|
||||
// phase: 6
|
||||
// spec: docs/spec/15-source-language.md
|
||||
module demo.unhandled
|
||||
|
||||
fn main(heap: cap<mem.alloc>) -> u64
|
||||
effects [mem.alloc]
|
||||
{
|
||||
let x = own.new(1u64, heap);
|
||||
0
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
// Correct source, but the owner is borrowed after its move: the verifier reports E2001.
|
||||
// expect: reject E2001
|
||||
// phase: 6
|
||||
// spec: docs/spec/15-source-language.md
|
||||
module demo.useaftermove
|
||||
|
||||
fn sink(o: own<u64>) -> void
|
||||
effects []
|
||||
{
|
||||
own.free(move o);
|
||||
}
|
||||
|
||||
fn read(c: ref<u64>) -> u64
|
||||
effects []
|
||||
{
|
||||
*c
|
||||
}
|
||||
|
||||
fn main(heap: cap<mem.alloc>) -> u64
|
||||
effects [mem.alloc]
|
||||
{
|
||||
let x = own.new(1u64, heap) or trap;
|
||||
sink(move x);
|
||||
read(ref x)
|
||||
}
|
||||
Reference in new issue
Block a user