Phase 6A: .aji layer 8 (lock and mutexes, select, shields, cancellation)

- lock ref m as x { … } cancelled { … }: mutex.lock on a ref<mutex<T>>
  (a mutex is borrowed for the lock, a ref binding used as is); ok edge:
  mutex.borrow bound as x; at every normal exit borrow.end x then
  mutex.unlock; the guard is never a source value
- mutex.make/mutex.take; mutex<T> type; ref mr = m lends a mutex
- shield { … }: task.shield.begin/end at every exit (nesting by construction)
- task.check_cancel and task.join_cancel as outcome matches
- select: one borrow per endpoint, send values evaluated first and
  deduplicated, every edge binds its own value then the unsent values;
  bind count E7005, missing outcome E7004
- linear values never get frame storage (borrowed in place)
- liveness: select handled in the read and after walks
- Quick Reference: select edges bind `job` back (ok(x, job), closed(job));
  every Quick Reference example now passes (no PENDING)
- tests: 6 run tests with AIR equivalence (mutex counter, select draw,
  select same value, shield, join_cancel forwarding, cancelled recv), 10
  rejects (E4304 x3, E3001, E2101, E7005 x2, E7004, E3101 x2, E4101)
- spec 15 §7/§9, quick reference, bundle notes, plan
This commit is contained in:
dev committed 2026-10-05 18:02:11 +03:00
1 parent 609b6bf86e
commit 154c39865f
24 files changed
+912 -50

No files matched your search

@@ -0,0 +1,50 @@
// A cancelled parent forwards cancellation to its child explicitly: join_cancel
// hands the child back on `cancelled`; the parent cancels and joins it.
// expect: run
// schedules: 20
// result: 13
// equivalent: phase3/run/cancel_join_forward.air
// phase: 6
// spec: docs/spec/15-source-language.md
module demo.canceljoinforward
fn child() -> u64
effects []
{
loop {
match task.check_cancel() {
ok => {}
cancelled => { break; }
}
}
3
}
fn parent() -> u64
effects [task.spawn, task.wait]
{
var r: u64 = 0;
scope sc {
let c = task.spawn(sc, child);
match task.join_cancel(move c) {
ok(v) => { r = v; }
cancelled(c) => {
task.cancel(c);
r = task.join(move c) + 10;
}
}
}
r
}
fn main() -> u64
effects [task.spawn, task.wait]
{
var r: u64 = 0;
scope sc {
let p = task.spawn(sc, parent);
task.cancel(p);
r = task.join(move p);
}
r
}
@@ -0,0 +1,47 @@
// A shielded receive is not cancelled; after the shield the task observes the
// pending cancellation at task.check_cancel.
// expect: run
// grant: heap=mem.alloc:64
// schedules: 20
// result: 105
// equivalent: phase3/run/cancel_shield.air
// phase: 6
// spec: docs/spec/15-source-language.md
module demo.cancelshield
fn waiter(rx: receiver<u64>) -> u64
effects [task.wait]
{
var v: u64 = 0;
shield {
v = channel.recv(rx) or trap;
}
channel.close_receiver(move rx) or trap;
match task.check_cancel() {
ok => v,
cancelled => v + 100,
}
}
fn main(heap: cap<mem.alloc>) -> u64
effects [mem.alloc, task.spawn, task.wait]
{
match channel.make<u64>(1, heap) {
ok(tx, rx) => {
var r: u64 = 0;
scope sc {
let t = task.spawn(sc, waiter, move rx);
task.cancel(t);
match channel.send(tx, 5u64) {
ok => {}
closed(w) => {}
cancelled(w) => {}
}
r = task.join(move t);
}
channel.close_sender(move tx);
r
}
err(e) => { trap; }
}
}
@@ -0,0 +1,47 @@
// A task waiting in channel.recv observes cancellation on its `cancelled` edge;
// cancellation is a request, so the parent still joins it.
// expect: run
// grant: heap=mem.alloc:64
// schedules: 20
// result: 7
// equivalent: phase3/run/cancel_wait_recv.air
// phase: 6
// spec: docs/spec/15-source-language.md
module demo.cancelwaitrecv
fn waiter(rx: receiver<u64>) -> u8
effects [task.wait]
{
match channel.recv(rx) {
ok(v) => {
channel.close_receiver(move rx) or trap;
0
}
closed => {
channel.close_receiver(move rx) or trap;
0
}
cancelled => {
channel.close_receiver(move rx) or trap;
7
}
}
}
fn main(heap: cap<mem.alloc>) -> u8
effects [mem.alloc, task.spawn, task.wait]
{
match channel.make<u64>(1, heap) {
ok(tx, rx) => {
var r: u8 = 0;
scope sc {
let t = task.spawn(sc, waiter, move rx);
task.cancel(t);
r = task.join(move t);
}
channel.close_sender(move tx);
r
}
err(e) => { trap; }
}
}
@@ -0,0 +1,41 @@
// Two tasks increment a shared counter under a mutex; the lock block unlocks at
// every exit; mutex.take dismantles the mutex once no borrow is live.
// expect: run
// schedules: 30
// quantum: 1
// result: 10
// equivalent: phase3/run/sync_mutex_counter.air
// phase: 6
// spec: docs/spec/15-source-language.md
module demo.mutexcounter
fn bump(mr: ref<mutex<u64>>) -> u8
effects [task.wait]
{
var i: u64 = 0;
while i != 5 {
lock ref mr as count {
*count = *count + 1;
} cancelled {
return 0;
}
i = i + 1;
}
0
}
fn main() -> u64
effects [task.spawn, task.wait]
{
let m = mutex.make(0u64);
{
ref mr = m;
scope sc {
let a = task.spawn(sc, bump, mr);
let b = task.spawn(sc, bump, mr);
let x = task.join(move a);
let y = task.join(move b);
}
}
mutex.take(move m)
}
@@ -0,0 +1,51 @@
module demo.mutexcounter
air 0
lang 0.1
features [cancellation, effects, ownership, sync, tasks]
fn @bump(%mr: ref<mutex<u64>>) -> u8
effects [task.wait]
entry:
%c_1 = const.u64 0
br @while_2(%c_1)
while_2(%i_4: u64):
%c_5 = const.u64 5
%cmp_6 = ne.u64 %i_4, %c_5
cbr %cmp_6, @body_7(), @after_3()
after_3:
%c_20 = const.u8 0
ret %c_20
body_7:
mutex.lock %mr, ok -> @locked_8, cancelled -> @lock_cancelled_10
locked_8(%guard_9: guard<u64>):
%count_11 = mutex.borrow %guard_9
%v_12 = load %count_11
%c_13 = const.u64 1
%t_14 = add.checked.u64 %v_12, %c_13
store %count_11, %t_14
borrow.end %count_11
mutex.unlock %guard_9
br @endlock_16(%i_4)
lock_cancelled_10:
%c_15 = const.u8 0
ret %c_15
endlock_16(%i_17: u64):
%c_18 = const.u64 1
%t_19 = add.checked.u64 %i_17, %c_18
br @while_2(%t_19)
fn @main() -> u64
effects [task.spawn, task.wait]
entry:
%c_1 = const.u64 0
%m_2 = mutex.make %c_1
%m_3 = borrow.ref %m_2
%sc_4 = task.scope.begin
%t_5 = task.spawn %sc_4, @bump(%m_3)
%t_6 = task.spawn %sc_4, @bump(%m_3)
%r_7 = task.join %t_5
%r_8 = task.join %t_6
task.scope.end %sc_4
borrow.end %m_3
%v_9 = mutex.take %m_2
ret %v_9
@@ -0,0 +1,21 @@
// Calling a function whose effects include task.wait inside a `lock` block is E4304.
// expect: reject E4304
// phase: 6
// spec: docs/spec/15-source-language.md
module demo.call_waiting_function
fn pause() -> void
effects [task.wait]
{
}
fn main() -> u64
effects [task.wait]
{
let m = mutex.make(0u64);
lock ref m as c {
pause();
*c = 1;
} cancelled {}
mutex.take(move m)
}
@@ -0,0 +1,33 @@
// On the `cancelled` edge of task.join_cancel the task is not joined: its loans stay, so touching the lent data is E4101.
// expect: reject E4101
// phase: 6
// spec: docs/spec/15-source-language.md
module demo.cancelled_keeps_loans
fn bump(s: slice_mut<u64>) -> u64
effects []
{
s[0] = s[0] + 1;
s[0]
}
fn main() -> u64
effects [task.spawn, task.wait]
{
var buf: [u64; 2] = zero;
var r: u64 = 0;
{
mut s = buf;
scope sc {
let t = task.spawn(sc, bump, s);
match task.join_cancel(move t) {
ok(v) => { r = v; }
cancelled(t) => {
s[0] = 9;
r = task.join(move t);
}
}
}
}
r
}
@@ -0,0 +1,25 @@
// task.join_cancel waits, and so does the join on its cancelled edge: both require task.wait (E3101 at each).
// expect: reject E3101 E3101
// phase: 6
// spec: docs/spec/15-source-language.md
module demo.join_cancel_needs_wait
fn work() -> u64
effects []
{
1
}
fn main() -> u64
effects [task.spawn]
{
var r: u64 = 0;
scope sc {
let t = task.spawn(sc, work);
match task.join_cancel(move t) {
ok(v) => { r = v; }
cancelled(t) => { r = task.join(move t); }
}
}
r
}
@@ -0,0 +1,15 @@
// `lock` needs a mutex.
// expect: reject E7005
// phase: 6
// spec: docs/spec/15-source-language.md
module demo.lock_not_mutex
fn main() -> u64
effects [task.wait]
{
var x: u64 = 1;
lock ref x as c {
*c = 2;
} cancelled {}
x
}
@@ -0,0 +1,18 @@
// A second lock while one is held waits while locked (E4304): lock-order deadlocks cannot be written.
// expect: reject E4304
// phase: 6
// spec: docs/spec/15-source-language.md
module demo.lock_twice
fn main() -> u64
effects [task.wait]
{
let a = mutex.make(0u64);
let b = mutex.make(0u64);
lock ref a as x {
lock ref b as y {
*y = *x;
} cancelled {}
} cancelled {}
mutex.take(move a) + mutex.take(move b)
}
@@ -0,0 +1,24 @@
// Every `select` edge binds its own value and then the values not sent on it: `ok(x)` misses `job`.
// expect: reject E7005
// phase: 6
// spec: docs/spec/15-source-language.md
module demo.select_bind_count
fn main(heap: cap<mem.alloc>) -> u64
effects [mem.alloc, task.wait]
{
match channel.make<own<u64>>(1, heap) {
ok(tx, rx) => {
let job = own.new(1u64, heap) or trap;
select {
recv(rx) { ok(x) => { own.free(move x); } closed(job) => { own.free(move job); } }
send(tx, move job) { ok => {} closed(job) => { own.free(move job); } }
cancelled(job) => { own.free(move job); }
}
channel.close_sender(move tx);
channel.close_receiver(move rx) or trap;
0
}
err(e) => { trap; }
}
}
@@ -0,0 +1,22 @@
// A `select` arm handles both of its outcomes.
// expect: reject E7004
// phase: 6
// spec: docs/spec/15-source-language.md
module demo.select_missing_outcome
fn main(heap: cap<mem.alloc>) -> u64
effects [mem.alloc, task.wait]
{
match channel.make<u64>(1, heap) {
ok(tx, rx) => {
channel.close_sender(move tx);
select {
recv(rx) { ok(x) => {} }
cancelled => {}
}
channel.close_receiver(move rx) or trap;
0
}
err(e) => { trap; }
}
}
@@ -0,0 +1,13 @@
// mutex.take while a borrow of the mutex is live is E2101.
// expect: reject E2101
// phase: 6
// spec: docs/spec/15-source-language.md
module demo.take_while_borrowed
fn main() -> u64
effects []
{
let m = mutex.make(1u64);
ref mr = m;
mutex.take(move m)
}
@@ -0,0 +1,16 @@
// A mutex locked after mutex.take: the verifier reports use after consume (E3001).
// expect: reject E3001
// phase: 6
// spec: docs/spec/15-source-language.md
module demo.use_after_take
fn main() -> u64
effects [task.wait]
{
let m = mutex.make(1u64);
let v = mutex.take(move m);
lock ref m as c {
*c = 2;
} cancelled {}
v
}
@@ -0,0 +1,22 @@
// A waiting operation inside a `lock` block breaks the critical-section rule (E4304).
// expect: reject E4304
// phase: 6
// spec: docs/spec/15-source-language.md
module demo.wait_while_locked
fn main(heap: cap<mem.alloc>) -> u64
effects [mem.alloc, task.wait]
{
let m = mutex.make(0u64);
match channel.make<u64>(1, heap) {
ok(tx, rx) => {
lock ref m as c {
*c = channel.recv(rx) or trap;
} cancelled {}
channel.close_sender(move tx);
channel.close_receiver(move rx) or trap;
}
err(e) => { trap; }
}
mutex.take(move m)
}
@@ -0,0 +1,38 @@
// Two ready receive arms: the scheduler's seeded draw picks one; the other value is
// received afterwards; the sum does not depend on the draw.
// expect: run
// grant: heap=mem.alloc:64
// schedules: 20
// result: 3
// equivalent: phase3/run/select_ready_draw.air
// phase: 6
// spec: docs/spec/15-source-language.md
module demo.selectdraw
fn main(heap: cap<mem.alloc>) -> u64
effects [mem.alloc, task.wait]
{
match channel.make<u64>(1, heap) {
ok(ta, ra) => {
match channel.make<u64>(1, heap) {
ok(tb, rb) => {
channel.send(ta, 1u64) or trap;
channel.send(tb, 2u64) or trap;
channel.close_sender(move ta);
channel.close_sender(move tb);
var sum: u64 = 0;
select {
recv(ra) { ok(x) => { sum = x + (channel.recv(rb) or trap); } closed => { trap; } }
recv(rb) { ok(y) => { sum = y + (channel.recv(ra) or trap); } closed => { trap; } }
cancelled => { trap; }
}
channel.close_receiver(move ra) or trap;
channel.close_receiver(move rb) or trap;
sum
}
err(e) => { trap; }
}
}
err(e) => { trap; }
}
}
@@ -0,0 +1,42 @@
// The same owned value in two send arms: it is consumed only on the edge of the arm
// that runs; every other edge hands it back.
// expect: run
// grant: heap=mem.alloc:64
// schedules: 20
// result: 42
// equivalent: phase3/run/select_same_value.air
// phase: 6
// spec: docs/spec/15-source-language.md
module demo.selectsamevalue
fn main(heap: cap<mem.alloc>) -> u64
effects [mem.alloc, task.wait]
{
match channel.make<own<u64>>(1, heap) {
ok(ta, ra) => {
match channel.make<own<u64>>(1, heap) {
ok(tb, rb) => {
let x = own.new(42u64, heap) or trap;
select {
send(ta, move x) { ok => {} closed(x) => { trap; } }
send(tb, move x) { ok => {} closed(x) => { trap; } }
cancelled(x) => { trap; }
}
var r: u64 = 0;
select {
recv(ra) { ok(o) => { r = own.take(move o); } closed => { trap; } }
recv(rb) { ok(o) => { r = own.take(move o); } closed => { trap; } }
cancelled => { trap; }
}
channel.close_sender(move ta);
channel.close_sender(move tb);
channel.close_receiver(move ra) or trap;
channel.close_receiver(move rb) or trap;
r
}
err(e) => { trap; }
}
}
err(e) => { trap; }
}
}
@@ -0,0 +1,89 @@
module demo.selectsamevalue
air 0
lang 0.1
features [cancellation, capabilities, channels, effects, heap, ownership, resources, tasks]
fn @main(%heap: cap<mem.alloc>) -> u64
effects [mem.alloc, task.wait]
entry:
%c_1 = const.usize 1
channel.make own<u64>, %c_1, %heap, ok -> @ok_2, err -> @err_5
ok_2(%ok_3: sender<own<u64>>, %ok_4: receiver<own<u64>>):
%c_7 = const.usize 1
channel.make own<u64>, %c_7, %heap, ok -> @ok_8, err -> @err_11
err_5(%err_6: alloc_error):
trap
ok_8(%ok_9: sender<own<u64>>, %ok_10: receiver<own<u64>>):
%c_13 = const.u64 42
own.new %c_13, %heap, ok -> @ok_14, err -> @err_16
err_11(%err_12: alloc_error):
trap
ok_14(%ok_15: own<u64>):
%ta_18 = borrow.mut %ok_3
%tb_19 = borrow.mut %ok_9
select [send %ta_18, %ok_15, ok -> @ok_20, closed -> @closed_21], [send %tb_19, %ok_15, ok -> @ok_23, closed -> @closed_24], cancelled -> @cancelled_26
err_16(%err_17: alloc_error):
trap
ok_20:
borrow.end %tb_19
borrow.end %ta_18
br @endselect_28()
closed_21(%closed_22: own<u64>):
borrow.end %tb_19
borrow.end %ta_18
trap
ok_23:
borrow.end %tb_19
borrow.end %ta_18
br @endselect_28()
closed_24(%closed_25: own<u64>):
borrow.end %tb_19
borrow.end %ta_18
trap
cancelled_26(%cancelled_27: own<u64>):
borrow.end %tb_19
borrow.end %ta_18
trap
endselect_28:
%c_29 = const.u64 0
%ra_30 = borrow.mut %ok_4
%rb_31 = borrow.mut %ok_10
select [recv %ra_30, ok -> @ok_32, closed -> @closed_34], [recv %rb_31, ok -> @ok_35, closed -> @closed_37], cancelled -> @cancelled_38
ok_32(%ok_33: own<u64>):
borrow.end %rb_31
borrow.end %ra_30
%v_39 = own.take %ok_33
br @endselect_41(%v_39)
closed_34:
borrow.end %rb_31
borrow.end %ra_30
trap
ok_35(%ok_36: own<u64>):
borrow.end %rb_31
borrow.end %ra_30
%v_40 = own.take %ok_36
br @endselect_41(%v_40)
closed_37:
borrow.end %rb_31
borrow.end %ra_30
trap
cancelled_38:
borrow.end %rb_31
borrow.end %ra_30
trap
endselect_41(%r_42: u64):
channel.close_sender %ok_3
channel.close_sender %ok_9
channel.close_receiver %ok_4, ok -> @ok_43, pending -> @pending_44
ok_43:
channel.close_receiver %ok_10, ok -> @ok_46, pending -> @pending_47
pending_44(%pending_45: receiver<own<u64>>):
trap
ok_46:
br @endmatch_49(%r_42)
pending_47(%pending_48: receiver<own<u64>>):
trap
endmatch_49(%match_50: u64):
br @endmatch_51(%match_50)
endmatch_51(%match_52: u64):
ret %match_52