Phase 6A: .aji layer 5 (linear values, own, regions, outcome matches, or trap)
- explicit move for named linear values; E7003 with suggestion "move x"; move of a copyable value is E7006; moved state left to the verifier - own.new/own.array/region.create/region.new/region.array/cap.narrow.budget as outcome matches or with `or trap`; own.take/own.free/region.reset/ region.destroy as AIR names in AIR operand order - region.new/region.array take an explicit ref/mut region binding (E7005 otherwise); reset/destroy end nothing (verifier E2104) - match kind (enum/outcome/integer) decided by the parser; built-in error enums (alloc_error, cap_error, io_error) matched exhaustively - cap<K> types; linear owners borrowed in place - features header = registry closure of the features used - parameters keep their source names (grants by @main parameter name) - aji lower --stats: mechanical_air_eliminated = 1 - source/AIR bytes - spec 15, grammar, quick reference, plan updated
This commit is contained in:
1 parent
eca30f817a
commit
bdcf871e5d
28 files changed
+882
-78
No files matched your search
@@ -124,7 +124,20 @@ Capabilities are only ever received (as `main` parameters, passed down as argume
|
||||
| `fs.read` / `fs.write` | same | `fs.open_read(c, path) → ok(f)`, `fs.create(c, path)`, `fs.open_append(c, path)`; `fs.read(f, mut buf) → ok(n)`; `fs.write_all(f, ref bytes)`; `fs.sync(f)`; `fs.close(move f)` |
|
||||
| `net.listen` / `net.connect` | same; `net.recv` needs `net.read`, `net.send_all` needs `net.write` | `net.listen(c, b"127.0.0.1:7") → ok(l)`; `net.accept(l) → ok(s)`; `net.connect(c, addr) → ok(s)`; `net.recv(s, mut buf) → ok(n)` (0 = closed); `net.send_all(s, ref bytes)`; `net.close_stream(move s)`; `net.close_listener(move l)` |
|
||||
| `clock.read`, `random.read` | same | `clock.monotonic(c)` (infallible, `u64`); `random.fill(c, mut buf)` (infallible) |
|
||||
| `mem.alloc` | `mem.alloc` | `own.new(heap, v) → ok(o) / err(e)`; `own.array(heap, n, v)`; `own.take(move o)`, `own.free(move o)`; `region.create(heap) → ok(r)`; `region.new(r, v) → ok(p)`; `region.array(r, n, v)`; `region.reset(r)`; `region.destroy(move r)` |
|
||||
| `mem.alloc` | `mem.alloc` | `own.new(v, heap) → ok(o) / err(e)`; `own.array(n, v, heap)`; `own.take(move o)`, `own.free(move o)`; `region.create(heap) → ok(r)`; `ref arena = r;` then `region.new(arena, v) → ok(p)`, `region.array(arena, n, v)`; `region.reset(r)`; `region.destroy(move r)`; `cap.narrow.budget(heap, n) → ok(small) / err(e)` |
|
||||
|
||||
A region allocation needs an explicit borrow of the region that outlives the allocations; `region.reset`/`region.destroy` while one is live is `E2104`, so end it first with a block:
|
||||
|
||||
```aji
|
||||
{
|
||||
ref arena = r;
|
||||
let item = region.new(arena, 1u64) or trap; // item: mut<u64>, ends with this block
|
||||
*item = *item + 1;
|
||||
}
|
||||
region.reset(r);
|
||||
```
|
||||
|
||||
`err(e)` binds a real value: `match e { alloc_error::budget_exceeded => …, alloc_error::out_of_memory => … }`.
|
||||
|
||||
- **Narrowing:** `cap.narrow.prefix(c, b"/data")` and `cap.narrow.budget(heap, 4096)`, each giving `ok(c2) / err(e)`.
|
||||
- Resource operands (`f`, `s`, `l`, `r`, endpoints) are borrowed automatically for an operation; data borrows (`mut buf`, `ref bytes`) are always written.
|
||||
|
||||
@@ -41,8 +41,8 @@ Lowering layers (P6A-3/4), each with accepted (golden AIR), rejected (`E7xxx`) a
|
||||
| 1 | scalars, expressions, functions, control flow, variables carried across joins, short-circuit `and`/`or`, casts | done |
|
||||
| 2 | structs (named construction, declaration-order lowering), enums (`Enum::Variant`), exhaustive `match` over enums (no `_`) and integers (`_` required), `match` as an expression; outcome matches and `or trap` move to layer 5 with the fallible operations | done |
|
||||
| 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 | `own`, regions, resources, unsafe | todo |
|
||||
| 6 | capabilities, intrinsics, I/O | todo |
|
||||
| 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 | host capabilities and grants, intrinsics, I/O, resources (`file`, `stream`, `listener`), `cap.narrow.prefix`, `trusted`/unsafe | todo |
|
||||
| 7 | tasks, channels | todo |
|
||||
| 8 | `lock`, `select`, cancellation, shields | todo |
|
||||
|
||||
|
||||
@@ -39,6 +39,8 @@ int_type = "u8" | "u16" | "u32" | "u64" | "u128" | "usize"
|
||||
| "i8" | "i16" | "i32" | "i64" | "i128" | "isize" ;
|
||||
(* `region`, `listener`, `stream`, `io_error`, `alloc_error`,
|
||||
`cap_error` are built-in type names (ident). *)
|
||||
(* A built-in operation is a call whose callee is a dotted path:
|
||||
`own.new(v, mem)`, `region.new(arena, v)`, `cap.narrow.budget(c, n)`. *)
|
||||
|
||||
block = "{" { statement } [ expr ] "}" ; (* optional tail expression: the block's value *)
|
||||
statement = "let" binding [ ":" type ] "=" expr ";"
|
||||
@@ -92,5 +94,5 @@ field_init = ident ":" expr ;
|
||||
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 \t \\ \" \xHH *)
|
||||
string = 'b"' { char | escape } '"' ; (* byte string; escapes \n \r \t \0 \\ \" \xHH *)
|
||||
ident = ( letter | "_" ) { letter | digit | "_" } ;
|
||||
@@ -192,7 +192,10 @@ The value is evaluated first, then the place is borrowed for the store and the b
|
||||
|
||||
**Moves.**
|
||||
|
||||
- A named linear value is consumed only by `move x`. Using a named linear value as a consuming operand without `move` is `E7003`.
|
||||
- A named linear value (`own<T>`, `own_array<T>`, `region`, and the resources and endpoints of later layers) is consumed only by `move x`. Using a named linear value as a consuming operand without `move` is `E7003`; the diagnostic carries `suggestion: "move x"`. Consuming operands are: call arguments, `let`/`var` initializers, assignments, `return`, the value operand of an allocation, and `own.take`, `own.free`, `region.destroy`.
|
||||
- `move` of a copyable value is `E7006`.
|
||||
- The frontend does not track moved state. Use after move (`E2001`), double consumption (`E2002`), use after consume (`E3001`) and leaks (`E3002`) are found by the verifier on the lowered AIR and reported at the source location.
|
||||
- A linear value used as a borrow place (`ref x`, `mut x`, `ref r = x;`) is borrowed in place: `own<T>` as `ref<T>`/`mut<T>`, `own_array<T>` as `slice<T>`/`slice_mut<T>`, `region` as `ref<region>`. There is no implicit dereference of an owner.
|
||||
- A temporary, that is the result of a call or operation used directly as an operand, needs no `move`: it has no other owner in the source.
|
||||
- Copyable values are copied by plain use.
|
||||
|
||||
@@ -217,6 +220,8 @@ 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)`.
|
||||
- **`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`.
|
||||
@@ -286,23 +291,42 @@ Step 4 happens at **every normal exit** from the block: fall-through, and every
|
||||
|
||||
**Resource and endpoint operands.** An operation whose AIR operand is a borrow of a resource or endpoint accepts the resource by name and borrows it for the duration of the operation:
|
||||
|
||||
- `file<…>`, `stream`, `listener`, `region`;
|
||||
- `file<…>`, `stream`, `listener`;
|
||||
- `sender<T>`, `receiver<T>`;
|
||||
- `task<T>` for `task.cancel`.
|
||||
|
||||
For example `net.accept(l)`, `fs.read(f, mut buf)`, `channel.send(tx, move v)`. Data borrows (`ref`/`mut` of values, arrays and slices) are always written. A mutex is borrowed explicitly: `lock ref m`.
|
||||
For example `net.accept(l)`, `fs.read(f, mut buf)`, `channel.send(tx, move v)`. Data borrows (`ref`/`mut` of values, arrays and slices) are always written. A mutex is borrowed explicitly: `lock ref m`. A region is borrowed explicitly too (§8).
|
||||
|
||||
## 8. Allocation, resources and intrinsics
|
||||
|
||||
Allocation and resource operations keep their AIR names and operand order:
|
||||
|
||||
- `own.new(mem, v)` and `own.array(mem, n, v)` (matches, §6);
|
||||
- `own.take(move o)`, `own.free(move o)`;
|
||||
- `region.create(mem)`, `region.new(r, v)`, `region.array(r, n, v)`;
|
||||
- `own.new(v, mem)` and `own.array(n, v, mem)` (outcome matches, §6): `ok(o)` binds `own<T>` / `own_array<T>`, `err(e)` an `alloc_error`;
|
||||
- `let v = own.take(move o);`, `own.free(move o);` (`own<T>` or `own_array<T>`; there is no take for arrays);
|
||||
- `region.create(mem)`: `ok(r)` binds a `region`;
|
||||
- `region.new(rb, v)`, `region.array(rb, n, v)`: `rb` MUST be a `ref`/`mut` borrow binding of the region; `ok(item)` binds `mut<T>` / `slice_mut<T>`;
|
||||
- `region.reset(r)`, `region.destroy(move r)`;
|
||||
- `cap.narrow.budget(c, n)`, `cap.narrow.prefix(c, path)`;
|
||||
- `cap.narrow.budget(c, n)`: `ok(c2)` binds a `cap<K>`, `err(e)` a `cap_error`; `cap.narrow.prefix(c, path)` (layer 6);
|
||||
- capabilities are values of type `cap<K>`, received as parameters and copied by plain use (no `move`);
|
||||
- intrinsics under their registered names (`reference/intrinsics.md`): `io.stdout_write(out, ref buf[0..n])`, `fs.open_read(c, b"data.txt")`, `fs.close(move f)`, `net.listen(c, b"127.0.0.1:7")`.
|
||||
|
||||
**Region borrows are explicit.** The borrow of a region that `region.new`/`region.array` take is written by the programmer and lexically outlives the allocation borrows derived from it:
|
||||
|
||||
```aji
|
||||
{
|
||||
ref arena = r;
|
||||
match region.new(arena, 1u64) {
|
||||
ok(item) => { *item = *item + 1; }
|
||||
err(e) => { trap; }
|
||||
}
|
||||
}
|
||||
region.reset(r);
|
||||
```
|
||||
|
||||
Passing the region itself is `E7005`; the frontend never creates a temporary region borrow. `region.reset` and `region.destroy` end no borrow: a live allocation borrow at a reset or destroy is the verifier's `E2104`. The programmer narrows the scope.
|
||||
|
||||
**Linear aggregates.** A struct or enum with a linear field (`own<T>`, `region`, a resource) needs the reserved feature `linear_aggregates`. The frontend lowers it as written and the AIR is rejected (`E0006`) until that feature is published.
|
||||
|
||||
A byte string or array used where a `slice<u8>` is expected is materialized in frame storage and borrowed for the operation.
|
||||
|
||||
## 9. Frame storage, borrow scopes and features
|
||||
|
||||
Reference in new issue
Block a user