Files
aji/tests/conformance/source/layer7/drain_linear.aji
T
dev 179dee3c3a Phase 6A: .aji layer 7 (task scopes, spawn/join, channels)
- scope s { … }: task.scope.begin, task.scope.end at every normal exit
  (scope ends generalized: borrow.end or task.scope.end, innermost first)
- task.spawn(s, f, args): call-like; named linear values moved (E7003);
  lent borrows are bound first (inline borrow E7006); scope operand E7005
- task.join/cancel/cancelled; task<T>, sender<T>, receiver<T>, scope types
- channels: make<T>, dup, send, recv, close_sender, close_receiver; send,
  recv and close_receiver are outcome matches; endpoints by name, borrowed
  for the operation and ended on every edge (written `mut tx`: E7006)
- outcome rebinding: a value coming back under the name of a variable of
  its type becomes the variable's value (pending(r))
- joins carry live variables only (pruned SSA): a linear value consumed
  before a join does not travel to it; ordered after-walk over the
  enclosing statement or tail, sibling arms not counted
- parser: a keyword after `.` is a member name (task.cancelled)
- conformance equivalence ignores schedule.choices (quantum counts AIR
  instructions)
- tests: 10 run tests with AIR equivalence (Phase 3 programs), 12 rejects
  (E4301, E4101 x2, E4002, E2001, E3101, E3002, E7006 x2, E7003, E0006,
  E7005); Quick Reference tasks/channels programs now pass
- spec 15 §4/§6/§7, grammar, quick reference, bundle notes, plan
2026-10-05 17:55:30 +03:00

52 lines
1.9 KiB
Rust

// Closing a receiver with buffered owned values hands it back on `pending`; a send
// after that ends `closed` and returns the value; the receiver drains everything
// before the close succeeds. No owned value is dropped.
// expect: run
// grant: heap=mem.alloc:64
// result: 13
// equivalent: phase3/run/channels_drain_linear.air
// phase: 6
// spec: docs/spec/15-source-language.md
module demo.drainlinear
fn main(heap: cap<mem.alloc>) -> u64
effects [mem.alloc, task.wait]
{
match channel.make<own<u64>>(4, heap) {
ok(tx, rx) => {
let a = own.new(1u64, heap) or trap;
channel.send(tx, move a) or trap;
let b = own.new(2u64, heap) or trap;
channel.send(tx, move b) or trap;
var acc: u64 = 0;
match channel.close_receiver(move rx) {
ok => { trap; }
pending(r) => {
let c = own.new(10u64, heap) or trap;
match channel.send(tx, move c) {
ok => { trap; }
closed(back) => { acc = own.take(move back); }
cancelled(back) => { acc = own.take(move back); }
}
var rr: receiver<own<u64>> = move r;
loop {
match channel.recv(rr) {
ok(o) => { acc = acc + own.take(move o); }
closed => {
match channel.close_receiver(move rr) {
ok => { break; }
pending(rr) => {}
}
}
cancelled => {}
}
}
}
}
channel.close_sender(move tx);
acc
}
err(e) => { trap; }
}
}