; Phase 3 exit program: ownership transfer through a bounded channel. The ; producer allocates each message on the heap and moves the owner into the ; channel; the consumer takes it. The budget (64 bytes) only fits the channel ; (2 × 8) plus three live messages (3 × 16): bounded capacity bounds memory. ; Result: 10 + 20 + 30 = 60. ; expect: run ; grant: heap=mem.alloc:64 ; schedules: 30 ; quantum: 2 ; result: 60 ; phase: 3 ; spec: docs/spec/04-concurrency-and-atomics.md module test.program_channel_transfer air 0 lang 0.1 features [cancellation, capabilities, channels, effects, heap, ownership, resources, tasks] type @Message = struct { id: u64, size: u64 } fn @main(%heap: cap) -> u64 effects [mem.alloc, task.spawn, task.wait] entry: %cap = const.usize 2 channel.make own<@Message>, %cap, %heap, ok -> @made, err -> @oom made(%tx: sender>, %rx: receiver>): %sc = task.scope.begin %p = task.spawn %sc, @producer(%tx, %heap) %z = const.u64 0 br @recv_loop(%rx, %z) recv_loop(%r: receiver>, %acc: u64): %rb = borrow.mut %r channel.recv %rb, ok -> @got, closed -> @drained, cancelled -> @drained got(%o: own<@Message>): borrow.end %rb %msg = own.take %o %id = struct.get @Message.id, %msg %acc2 = add.checked.u64 %acc, %id br @recv_loop(%r, %acc2) drained: borrow.end %rb channel.close_receiver %r, ok -> @closed, pending -> @pending pending(%r2: receiver>): br @recv_loop(%r2, %acc) closed: %x = task.join %p task.scope.end %sc ret %acc oom(%e: alloc_error): trap fn @producer(%tx: sender>, %heap: cap) -> u8 effects [mem.alloc, task.wait] entry: %txb = borrow.mut %tx %ten = const.u64 10 br @loop(%ten) loop(%id: u64): %end = const.u64 40 %done = eq.u64 %id, %end cbr %done, @finish(), @build(%id) build(%i: u64): %size = const.u64 16 %msg = struct.make @Message(%i, %size) own.new %msg, %heap, ok -> @owned, err -> @oom owned(%o: own<@Message>): channel.send %txb, %o, ok -> @sent, closed -> @refused, cancelled -> @refused sent: ; %o is gone: the receiver owns the message now %step = const.u64 10 %next = add.checked.u64 %i, %step br @loop(%next) refused(%back: own<@Message>): own.free %back br @finish() finish: borrow.end %txb channel.close_sender %tx %r = const.u8 0 ret %r oom(%e: alloc_error): trap