P6A-8: Source Reference Bundle and harness profile aji_source
- source bundle: a /docs directory with QUICK.md (Quick Reference plus a where-to-read table), language/*.md, diagnostics/errors.md and the AIR bundle only under air/; built from docs/agent/source-bundle.txt (> output files, @manifest includes) by zig build agent-reference - Quick Reference examples are checked: every aji block must be an excerpt of a conformance program in tests/conformance/source/quickref (zig build test fails otherwise); QR fixes found by the check (mut d = data, region not auto-borrowed, statement/borrow blocks that could not verify, io_error match) - frontend: task.*/channel.*/mutex.* are not-yet-implemented, not E7006 - aji lower --stats: mechanical_air_eliminated_raw and _normalized (each byte-string materialization counted as one constant; spec 15 §11) - harness: profile aji_source (doc_groups), per-file and per-group docs metrics on distinctive lines, profile columns (tool calls, lowered AIR raw/normalized, docs files read, IR-form and type-mismatch counts), dev-echo aji_source variant, scripted run and test, source token report - plan: P6A-8 done, P6A-9 criteria; Q-021/Q-022 resolved; unsafe block deferred
This commit is contained in:
1 parent
61be766634
commit
3e2d9b63a7
35 files changed
+1170
-106
No files matched your search
@@ -0,0 +1,46 @@
|
||||
// Quick Reference §4: borrows and moves.
|
||||
// expect: run
|
||||
// result: 36
|
||||
// phase: 6
|
||||
// spec: docs/agent/quick-reference.md
|
||||
module demo.borrows
|
||||
|
||||
struct P {
|
||||
a: u64,
|
||||
b: u64,
|
||||
}
|
||||
|
||||
fn main() -> u64
|
||||
effects []
|
||||
{
|
||||
let i = 2usize;
|
||||
var out: [u64; 4] = zero;
|
||||
var total: u64 = 0;
|
||||
var data: [u64; 8] = zero;
|
||||
data[0] = 7; // element write (array variables get frame storage automatically)
|
||||
{
|
||||
mut s = data; // exclusive borrow to the end of this block
|
||||
s[1] = s[0] + 1; // checked index; out of range traps T0008
|
||||
let n = s.len;
|
||||
if i < s.len { s[i] = 0; } // no runtime check here: the verifier proves i < s.len
|
||||
let (l, r) = s.split_at(4); // two disjoint halves of s
|
||||
l[3] = n as! u64;
|
||||
r[0] = 9;
|
||||
}
|
||||
slice.copy(mut out[0..4], ref data[4..8]); // borrows as arguments end after the call
|
||||
total = total + out[0];
|
||||
var p: P = P { a: 1, b: 2 };
|
||||
{
|
||||
ref q = p; // shared borrow of a struct; q.a reads a field
|
||||
total = total + q.a + q.b;
|
||||
}
|
||||
var count: u64 = 0;
|
||||
bump(mut count);
|
||||
total + count + data[0] + data[1] + data[3]
|
||||
}
|
||||
|
||||
fn bump(c: mut<u64>) -> void
|
||||
effects []
|
||||
{
|
||||
*c = *c + 1; // through c: mut<u64>
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
// Quick Reference §7: a bounded channel, every outcome handled (layer 7).
|
||||
// expect: run
|
||||
// grant: heap=mem.alloc:1024
|
||||
// result: 5
|
||||
// phase: 6
|
||||
// spec: docs/agent/quick-reference.md
|
||||
module demo.qrchannels
|
||||
|
||||
fn main(heap: cap<mem.alloc>) -> u64
|
||||
effects [mem.alloc, task.wait]
|
||||
{
|
||||
let v = 5u64;
|
||||
var got: u64 = 0;
|
||||
match channel.make<u64>(2, heap) { // effect mem.alloc
|
||||
ok(tx, rx) => {
|
||||
let tx2 = channel.dup(tx);
|
||||
channel.close_sender(move tx2);
|
||||
match channel.send(tx, v) { ok => {} closed(v) => { trap; } cancelled(v) => { trap; } } // a linear v: `move v`
|
||||
match channel.recv(rx) { ok(v) => { got = v; } closed => { trap; } cancelled => { trap; } }
|
||||
channel.close_sender(move tx);
|
||||
match channel.close_receiver(move rx) { ok => {} pending(rx) => { trap; } } // drain, then close again
|
||||
}
|
||||
err(e) => { trap; }
|
||||
}
|
||||
got
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
// Quick Reference §1: the skeleton program.
|
||||
// expect: run
|
||||
// grant: input=io.read
|
||||
// grant: out=io.write
|
||||
// stdin: hi
|
||||
// result: null
|
||||
// stdout: hi
|
||||
// phase: 6
|
||||
// spec: docs/agent/quick-reference.md
|
||||
module demo.echo
|
||||
|
||||
fn main(input: cap<io.read>, out: cap<io.write>) -> void
|
||||
effects [io.read, io.write]
|
||||
{
|
||||
var buf: [u8; 64] = zero;
|
||||
loop {
|
||||
let n = io.stdin_read(input, mut buf) or trap;
|
||||
if n == 0 { return; }
|
||||
io.stdout_write(out, ref buf[0..n]) or trap;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
// Quick Reference §5: every outcome is a match arm.
|
||||
// expect: run
|
||||
// grant: c=fs.read:/data
|
||||
// grant: listen=net.listen
|
||||
// grant: connect=net.connect
|
||||
// vfs: /data/in.txt=abc
|
||||
// result: 3
|
||||
// phase: 6
|
||||
// spec: docs/agent/quick-reference.md
|
||||
module demo.failure
|
||||
|
||||
fn main(c: cap<fs.read>, listen: cap<net.listen>, connect: cap<net.connect>) -> u8
|
||||
effects [fs.read, net.listen, net.connect, net.read, net.write]
|
||||
{
|
||||
var buf: [u8; 8] = zero;
|
||||
var got: usize = 0;
|
||||
match fs.open_read(c, b"/data/in.txt") {
|
||||
ok(f) => { got = fs.read(f, mut buf) or trap; fs.close(move f) or trap; }
|
||||
err(e) => { return 1u8; }
|
||||
}
|
||||
let l = net.listen(listen, b"127.0.0.1:7") or trap;
|
||||
let peer = net.connect(connect, b"127.0.0.1:7") or trap;
|
||||
let conn = net.accept(l) or trap;
|
||||
net.send_all(peer, ref buf[0..got]) or trap;
|
||||
let n = net.recv(conn, mut buf) or trap; // any non-ok outcome traps
|
||||
net.close_stream(move conn) or trap;
|
||||
net.close_stream(move peer) or trap;
|
||||
net.close_listener(move l) or trap;
|
||||
n as! u8
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
// Quick Reference §7: a mutex, a lock block and mutex.take (layer 8).
|
||||
// expect: run
|
||||
// result: 1
|
||||
// phase: 6
|
||||
// spec: docs/agent/quick-reference.md
|
||||
module demo.qrmutex
|
||||
|
||||
fn main() -> u64
|
||||
effects [task.wait]
|
||||
{
|
||||
let m = mutex.make(0u64);
|
||||
lock ref m as count { // count: mut<u64>; unlocked at every exit of the block
|
||||
*count = *count + 1;
|
||||
} cancelled { trap; }
|
||||
let total = mutex.take(move m); // needs no live borrow of m
|
||||
total
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
// Quick Reference §6: allocation in a region with an explicit region borrow.
|
||||
// expect: run
|
||||
// grant: heap=mem.alloc:256
|
||||
// result: 2
|
||||
// phase: 6
|
||||
// spec: docs/agent/quick-reference.md
|
||||
module demo.qrregion
|
||||
|
||||
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; // item: mut<u64>, ends with this block
|
||||
*item = *item + 1;
|
||||
v = *item;
|
||||
}
|
||||
region.reset(r);
|
||||
region.destroy(move r);
|
||||
v
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
// Quick Reference §7: select over a receive and a send (layer 8).
|
||||
// expect: run
|
||||
// grant: heap=mem.alloc:1024
|
||||
// result: 1
|
||||
// phase: 6
|
||||
// spec: docs/agent/quick-reference.md
|
||||
module demo.qrselect
|
||||
|
||||
fn main(heap: cap<mem.alloc>) -> u64
|
||||
effects [mem.alloc, task.wait]
|
||||
{
|
||||
let job = 9u64;
|
||||
var which: u64 = 0;
|
||||
match channel.make<u64>(1, heap) {
|
||||
ok(tx_a, rx_a) => {
|
||||
match channel.make<u64>(1, heap) {
|
||||
ok(tx_b, rx_b) => {
|
||||
channel.close_sender(move tx_a);
|
||||
select {
|
||||
recv(rx_a) { ok(x) => { which = 2; } closed => { which = 1; } }
|
||||
send(tx_b, move job) { ok => { which = 1; } closed(job) => { which = 3; } }
|
||||
cancelled(job) => { trap; }
|
||||
}
|
||||
channel.close_sender(move tx_b);
|
||||
match channel.close_receiver(move rx_a) { ok => {} pending(rx_a) => { trap; } }
|
||||
match channel.recv(rx_b) { ok(v) => {} closed => {} cancelled => { trap; } }
|
||||
match channel.close_receiver(move rx_b) { ok => {} pending(rx_b) => { trap; } }
|
||||
}
|
||||
err(e) => { trap; }
|
||||
}
|
||||
}
|
||||
err(e) => { trap; }
|
||||
}
|
||||
which
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
// Quick Reference §3: statements and expressions.
|
||||
// expect: run
|
||||
// result: 50
|
||||
// phase: 6
|
||||
// spec: docs/agent/quick-reference.md
|
||||
module demo.statements
|
||||
|
||||
enum Message {
|
||||
Stop,
|
||||
Value(u64),
|
||||
}
|
||||
|
||||
fn main() -> u64
|
||||
effects []
|
||||
{
|
||||
let a = 20u64;
|
||||
let b = 22u64;
|
||||
let c = a < b;
|
||||
let d = false;
|
||||
let t = 1u8;
|
||||
let n = 4usize;
|
||||
let m = Message::Value(7);
|
||||
var acc: u64 = 0;
|
||||
let x = a + b; // immutable
|
||||
var i: usize = 0; // variable; reassign with i = i + 1;
|
||||
var buf: [u8; 16] = zero; // zero-initialized storage
|
||||
if c { acc = acc + 1; } else if d { acc = acc + 2; } else { acc = acc + 3; }
|
||||
while i < n { i = i + 1; buf[0] = 1; }
|
||||
for k in 0..n { acc = acc + k as! u64; }
|
||||
loop {
|
||||
i = i + 1;
|
||||
if i > 6 { break; }
|
||||
if i == 5 { continue; }
|
||||
acc = acc + 1;
|
||||
}
|
||||
let y = if c { 1 } else { 2 };
|
||||
let w: u64 = match m {
|
||||
Message::Stop => 0,
|
||||
Message::Value(v) => v,
|
||||
};
|
||||
match t { 0 => { acc = acc + 100; } 1 => { assert(acc + buf[0] as u64 == 9); } _ => { trap; } }
|
||||
assert(c);
|
||||
return x + y + w;
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
// Quick Reference §7: a scope with two tasks over disjoint halves (layer 7).
|
||||
// expect: run
|
||||
// result: 30
|
||||
// phase: 6
|
||||
// spec: docs/agent/quick-reference.md
|
||||
module demo.qrtasks
|
||||
|
||||
fn square(s: slice_mut<u64>) -> u64
|
||||
effects []
|
||||
{
|
||||
var sum: u64 = 0;
|
||||
for i in 0..s.len {
|
||||
s[i] = s[i] * s[i];
|
||||
sum = sum + s[i];
|
||||
}
|
||||
sum
|
||||
}
|
||||
|
||||
fn main() -> u64
|
||||
effects [task.spawn, task.wait]
|
||||
{
|
||||
var data: [u64; 8] = [1; 8];
|
||||
data[4] = 2;
|
||||
data[5] = 2;
|
||||
data[6] = 2;
|
||||
data[7] = 4;
|
||||
var total: u64 = 0;
|
||||
{
|
||||
mut d = data; // data: [u64; 8]
|
||||
scope s { // every task spawned in s must be joined before the block ends (E4301)
|
||||
let (l, r) = d.split_at(4);
|
||||
let a = task.spawn(s, square, l); // borrows are lent to the task until its join
|
||||
let b = task.spawn(s, square, r);
|
||||
let x = task.join(move a);
|
||||
let y = task.join(move b);
|
||||
total = x + y;
|
||||
}
|
||||
}
|
||||
total
|
||||
}
|
||||
Reference in new issue
Block a user