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
This commit is contained in:
dev committed 2026-10-05 17:55:30 +03:00
1 parent c2c238d9b1
commit 179dee3c3a
32 files changed
+1315 -41

No files matched your search

+1 -1
View File
@@ -154,7 +154,7 @@ region.reset(r);
## 7. Concurrency
Not implemented in the source frontend yet (layers 7–8): `aji check` reports these constructs as not implemented (exit code 3). They are shown as specified.
Tasks and channels work (layer 7). `select`, `lock`/`mutex`, `shield`, `task.check_cancel` and `task.join_cancel` are layer 8, not implemented yet: `aji check` reports them as not implemented (exit code 3).
```aji
mut d = data; // data: [u64; 8]
+1 -1
View File
@@ -23,7 +23,7 @@ You write `.aji`; you never need to write AIR. Diagnostics point at your source
## Concurrency status
The source frontend implements layers 1–6 of spec 15. Layers 7 (tasks, channels) and 8 (`lock`, `select`, cancellation, shields) are not implemented yet: a program that uses `scope`, `task.*`, `channel.*`, `mutex.*`, `lock`, `select` or `shield` is reported as not implemented (exit code 3). The rules below describe the language as specified.
The source frontend implements layers 1–7 of spec 15: task scopes, `task.spawn`/`join`/`cancel`/`cancelled` and channels work. Layer 8 (`lock`/`mutex.*`, `select`, `shield`, `task.check_cancel`, `task.join_cancel`) is not implemented yet: a program that uses it is reported as not implemented (exit code 3). The rules below describe the language as specified.
## The AIR reference
+1 -1
View File
@@ -88,7 +88,7 @@ Lowering layers (P6A-3/4), each with accepted (golden AIR), rejected (`E7xxx`) a
| 3–4 | addressable values (frame storage chosen by lowering), lexical borrows (`ref r = …;`/`mut r = …;`), places and writes, arrays, slices, `split_at`, `slice.copy`, byte strings, proven indexing for the verifier's fact form; `move` comes with linear values in layer 5 | done |
| 5 | explicit `move` for linear values (`E7003` with the `move x` hint), `own`/`own_array`, regions with an explicit region borrow, outcome matches (match kind decided by the parser), `or trap`, built-in error enums, `cap<mem.alloc>` and `cap.narrow.budget`; linear aggregates stay rejected (`E0006`); `aji lower --stats` reports `mechanical_air_eliminated` | done |
| 6 | intrinsics as plain calls (outcome match / `or trap`), resources by name with a per-operation borrow, explicit closes and data borrows, `cap.narrow.prefix`, byte-string paths, unsafe operations under `effects [unsafe]`, `trusted` rejected, preserved parameter names; source/AIR run-record equivalence (`equivalent:`) for hello, file copy, TCP echo, prefix narrowing, random | done |
| 7 | tasks, channels | todo |
| 7 | `scope` blocks, `task.spawn`/`join`/`cancel`/`cancelled`, `task<T>`/`sender<T>`/`receiver<T>`, channels (`make`, `dup`, `send`, `recv`, `close_sender`, `close_receiver`), endpoint operands by name, rebinding of variables on outcome edges, joins carry live variables only; source/AIR equivalence with the Phase 3 task and channel programs | done |
| 8 | `lock`, `select`, cancellation, shields | todo |
## Tasks (full Phase 6)
+3 -1
View File
@@ -83,7 +83,8 @@ unary = ( "-" | "not" | "*" ) unary
| ( "ref" | "mut" ) [ "unchecked" ] place
| "move" ident
| postfix ;
postfix = primary { "." ident | "[" expr [ ".." expr ] "]" | call_args } ;
postfix = primary { "." member | "[" expr [ ".." expr ] "]" | call_args } ;
member = ident | keyword ; (* after `.` a keyword is a name: task.cancelled *)
call_args = [ "<" type ">" ] "(" [ expr { "," expr } ] ")" ;
primary = int_lit | "true" | "false" | string
| ident [ "{" field_init { "," field_init } [ "," ] "}" ] (* struct literal *)
@@ -97,3 +98,4 @@ place = ident { "." ident | "[" expr [ ".." expr ] "]" } | "*" unary ;
int_lit = digit { digit | "_" } [ int_type ] | "0x" hexdigit { hexdigit | "_" } [ int_type ] ;
string = 'b"' { char | escape } '"' ; (* byte string; escapes \n \r \t \0 \\ \" \xHH *)
ident = ( letter | "_" ) { letter | digit | "_" } ;
keyword = ? any keyword listed under Lexical conventions ? ;
+7 -5
View File
@@ -106,6 +106,8 @@ Statements end with `;`. Block constructs (`if`, `while`, `for`, `loop`, `match`
`if` and `match` are expressions when every branch yields a value of the same type through a tail expression (an expression without `;` at the end of a block) or diverges (`return`, `break`, `continue`, `trap`).
**Joins carry live variables only.** A variable is passed to a join (the block after an `if` or `match`, a loop header, a loop exit) only if it may be read after that join: on a later path, or, for a loop header, anywhere in the loop. A variable that is not read again is not passed on. This is ordinary pruned SSA construction, not tracking of moves. A linear value consumed before a join (`channel.close_receiver(move r)` before `break`) does not travel to it. A linear value that is read after it was consumed is still the verifier's `E2001`. The analysis is conservative: any read on a later path counts, and only a whole-variable assignment in straight-line code ends a variable's life.
**Type inference is deliberately small.** It works within one function, it is monomorphic, and it does no overload resolution or search among several admissible types. An expression's type comes from:
- its own form: a literal suffix, a name, a call's declared return type, a cast, a struct or enum constructor;
@@ -227,7 +229,7 @@ match io.stdin_read(input, mut buf) {
- **Match kinds.** The parser decides the kind of a `match` from its arms: `Enum::Variant` arms make an enum match, integer literals and `_` an integer match, and bare outcome names (`ok`, `err`, `closed`, `cancelled`, `pending`) an outcome match. Outcome names are not enum patterns. In an outcome match the scrutinee MUST be a fallible operation (`E7006` otherwise). Each outcome is matched exactly once: a repeated one is `E7006`, an unknown one `E7002`, a wrong number of bound names `E7005`. A fallible operation outside an outcome match or `or trap` is `E7006`.
- **Outcome values.** `err(e)` binds the real error value (`alloc_error`, `cap_error`, `io_error`). These built-in enums are matched like declared ones: `alloc_error::out_of_memory`, exhaustive, no `_`. A borrow bound by an outcome (the `ok(item)` of `region.new`) ends at the end of its arm. A borrow bound by `let item = region.new(arena, v) or trap;` ends at the end of the enclosing block.
- **Rebinding.** A value that comes back on an edge is rebound by the name in its arm: `closed(v)`, `pending(rx)`, `cancelled(t)`.
- **Rebinding.** A value that comes back on an edge is rebound by the name in its arm: `closed(v)`, `pending(rx)`, `cancelled(t)`. If that name is a variable (`var`) of the value's type, the value becomes the variable's new value. A receiver kept in `var r` is closed with `match channel.close_receiver(move r) { ok => …, pending(r) => … }` and received from again.
- **`or trap`.** `op(args) or trap` is the expression of the `ok` value, or of `void`. Every other outcome traps (`T0001`). It is the only shorthand.
- **Enums.** A `match` over a value of an enum type has exactly one arm per variant, each written `Enum::Variant(names…)` (`Enum::Variant` without payload). It must be exhaustive (`E7004` lists the missing variants), and `_` is not allowed (`E7006`), so a new variant cannot slip through silently. Payload names are bound only inside their arm (they are the arm block's parameters in AIR). It lowers to `enum.switch`.
- **Integers.** A `match` over an integer has literal arms and exactly one `_` arm, which is required (`E7004`). It lowers to `switch`.
@@ -246,8 +248,8 @@ scope s {
**`scope s { … }`** is `task.scope.begin` and `task.scope.end`, at every normal exit of the block. Inside it:
- `task.spawn(s, f, args)` spawns; borrow arguments are lent until the join (spec 04 §34);
- `task.join(move t)` joins;
- `task.spawn(s, f, args)` spawns `f` (a function of the module) into the scope `s`. Arguments are passed like call arguments: a named linear value is `move`d (`E7003`). A borrow argument is lent until the join (spec 04 §34), so it is a borrow binding passed by name (`mut part = buf;` … `task.spawn(s, f, part)`). A borrow or byte string written inline is `E7006`, because a temporary would have to end right after the spawn. The first operand must be the scope (`E7005`);
- `task.join(move t)` joins; its value is the task's result (`task<T>` gives `T`);
- a task that is not joined at an exit is `E4301`.
**Task operations elsewhere:**
@@ -261,9 +263,9 @@ scope s {
**Channels:**
- `channel.make<T>(n, mem)`: a match, §6;
- `channel.dup(tx)`;
- `channel.dup(tx)`: `channel.sender.dup` on a shared borrow of `tx`, made for the operation;
- `channel.send(tx, v)` (`move v` for a linear `v`);
- `channel.recv(rx)`;
- `channel.recv(rx)`; for `send` and `recv` the endpoint is borrowed `mut` for the operation and the borrow ends on every edge;
- `channel.close_sender(move tx)`;
- `channel.close_receiver(move rx)`.