Phase 6A: .aji layer 6 (intrinsics, resources, narrowing, unsafe operations)

- intrinsics from the registry as plain calls: fallible ones are outcome
  matches or `or trap` (E7006 otherwise), infallible ones plain calls
- resource operands by name, borrowed mut for the one operation and ended
  on every edge; a mut binding/parameter passes as is; `mut f` is E7006
- closes consume with `move` (E7003); no inserted closes (E3002/E3001)
- data operands explicit (ref/mut/byte string, E7005 otherwise)
- file<…>, stream, listener types; cap.narrow.prefix with cap_error edges
- ref/mut unchecked s[i] (also as a binding) and assume(c) under
  effects [unsafe]; boundary left to the verifier (E6001)
- trusted is reserved and rejected (E7006)
- parameter names preserved; generated names avoid them (normative)
- parser: >> closes two type arguments
- conformance: `equivalent:` compares the run record with a reference
  AIR program (hello, file_copy, tcp_echo, narrow ok/not_subset/invalid,
  random)
- spec 15, grammar, quick reference (io_error match fixed), plan, examples
This commit is contained in:
dev committed 2026-10-05 12:16:54 +03:00
1 parent bdcf871e5d
commit c18fb7acc1
39 files changed
+1309 -69

No files matched your search

+3 -2
View File
@@ -111,18 +111,19 @@ let n = net.recv(conn, mut buf) or trap; // any non-ok outcome traps
- Arms are named after the outcomes and the `match` must cover all of them (`E7004`).
- `or trap` is the only shorthand: it takes the `ok` value and traps on anything else.
- Matching an `err(e)` of type `io_error`: `match e { not_found => …, _ => … }`.
- `err(e)` binds a real `io_error`. Match it like any enum, every variant and no `_`: `match e { io_error::not_found => …, io_error::permission_denied => …, … }`, or just `trap`.
- Closing consumes the resource on **both** outcomes.
## 6. Authority, allocation, I/O
Capabilities are only ever received (as `main` parameters, passed down as arguments) or narrowed. They are copyable.
Capabilities are only ever received (as `main` parameters, passed down as arguments) or narrowed. They are copyable. A capability is needed only to acquire: the file or stream you get back carries the authority, so `fs.read(f, mut buf)` takes no capability. Pass a resource **by name** (the call borrows it); pass data with explicit `ref`/`mut`. Paths are byte strings: `b"/in/data.txt"`.
| Kind (`cap<K>`) | Effect | Operations (all fallible unless noted) |
|---|---|---|
| `io.read` / `io.write` | same | `io.stdin_read(c, mut buf) → ok(n)`; `io.stdout_write(c, ref bytes)`, `io.stderr_write(c, ref bytes)` |
| `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)` |
| narrowing | — | `cap.narrow.prefix(c, b"/data/logs") → ok(c2) / err(e: cap_error)` (`fs.read`, `fs.write`) |
| `clock.read`, `random.read` | same | `clock.monotonic(c)` (infallible, `u64`); `random.fill(c, mut buf)` (infallible) |
| `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)` |
+1 -1
View File
@@ -42,7 +42,7 @@ Lowering layers (P6A-3/4), each with accepted (golden AIR), rejected (`E7xxx`) a
| 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 | 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 |
| 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 |
| 8 | `lock`, `select`, cancellation, shields | todo |
+3 -2
View File
@@ -24,8 +24,7 @@ variant = ident [ "(" type { "," type } ")" ] ;
fn_decl = "fn" ident "(" [ param { "," param } ] ")" "->" type [ "from" ident ]
"effects" "[" [ path { "," path } ] "]"
[ "trusted" string ]
block ;
block ; (* `trusted` is reserved and rejected, E7006 *)
param = ident ":" type ;
type = int_type | "bool" | "void" | "never" | ident
@@ -33,6 +32,7 @@ type = int_type | "bool" | "void" | "never" | ident
| wrapper "<" type ">"
| "cap" "<" path ">"
| "file" "<" ( "read" | "write" | "append" ) ">" ;
(* `>>` closes two type arguments: mut<file<write>> *)
wrapper = "ref" | "mut" | "slice" | "slice_mut" | "own" | "own_array"
| "task" | "sender" | "receiver" | "mutex" ;
int_type = "u8" | "u16" | "u32" | "u64" | "u128" | "usize"
@@ -45,6 +45,7 @@ int_type = "u8" | "u16" | "u32" | "u64" | "u128" | "usize"
block = "{" { statement } [ expr ] "}" ; (* optional tail expression: the block's value *)
statement = "let" binding [ ":" type ] "=" expr ";"
| "var" ident ":" type "=" ( expr | "zero" ) ";"
| ( "ref" | "mut" ) [ "unchecked" ] ident "=" place ";" (* borrow binding *)
| place "=" expr ";"
| "break" ";" | "continue" ";" | "return" [ expr ] ";" | "trap" ";"
| expr ";"
+20 -5
View File
@@ -60,8 +60,13 @@ effects [io.read, io.write]
- A file holds one module: `module <dotted.path>`, then type and function declarations in any order.
- `//` starts a line comment.
- Function `f` lowers to `@f`; type `T` to `@T`.
- **Signature.** `fn name(params) -> type`, then optionally `from p` for a borrow return (spec 08 §11.3), then the line `effects [...]`, then optionally `trusted "<obligation>"` (spec 13 §6), then the body block.
- **The entry point** is `main`. Its parameters are capabilities bound by name (spec 05 §10.2).
- **Signature.** `fn name(params) -> type`, then optionally `from p` for a borrow return (spec 08 §11.3), then the line `effects [...]`, then the body block.
- **Parameter names are preserved.** A parameter `p` lowers to `%p`. Names the compiler generates are chosen so that they never equal a parameter name of the function. The names of `@main`'s parameters are part of the instantiation contract.
- **The entry point** is `main`. Its parameters are capabilities `cap<K>`, bound by name (spec 05 §10.2). Startup values of other types (command-line arguments, for example) are not part of `.aji` 0.1.
- **Grants are not source semantics.** A program only declares `main`'s capability parameters. Granting them is instantiation after the module is lowered and verified (`aji run --grant …`, `aji.toml`, spec 12 §1). A missing grant is `not_instantiated` at run time, never a source or verifier diagnostic.
- **One source for every profile.** The frontend does not know the execution profile. The same `.aji` runs in the deterministic and the hosted profile (spec 11).
- **`trusted` is not available.** It is the boundary of runtime, std and toolchain modules (spec 13 §6.3), not something a program declares about its own code. The word is reserved and a `trusted` clause is `E7006`.
- **Unsafe operations** are the format-0 ones and nothing else: `ref unchecked s[i]` / `mut unchecked s[i]` (also bound: `ref unchecked e = s[i];`) lower to `borrow.index.unchecked`, and `assume(c)` to `assume`. Their boundary is the AIR one: the function declares `effects [unsafe]`, which propagates to its callers. Outside it the verifier reports `E6001`. There is no `unsafe { … }` block, because AIR has no block-level unsafe boundary. There is no FFI, raw address or syscall surface in format 0, so none exists in source.
## 3. Types
@@ -289,7 +294,7 @@ Arms bind the send values not sent on their edge, in the order of first appearan
Step 4 happens at **every normal exit** from the block: fall-through, and every `return`, `break` and `continue` that leaves it. The `cancelled { … }` block is the lock's cancelled edge. On a trap nothing is unlocked: the execution terminates (spec 04 §33.2). The guard is never a source value. The critical-section rule (spec 04 §41) applies to the block's contents.
**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:
**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 (§8):
- `file<…>`, `stream`, `listener`;
- `sender<T>`, `receiver<T>`;
@@ -306,9 +311,19 @@ Allocation and resource operations keep their AIR names and operand order:
- `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)`: `ok(c2)` binds a `cap<K>`, `err(e)` a `cap_error`; `cap.narrow.prefix(c, path)` (layer 6);
- `cap.narrow.budget(c, n)` and `cap.narrow.prefix(c, path)`: `ok(c2)` binds a `cap<K>`, `err(e)` a `cap_error` (`invalid_restriction`, `not_subset`, `unsupported_restriction`). Narrowing is always fallible and never yields an empty capability;
- 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")`.
- intrinsics under their registered names and operand order (`reference/intrinsics.md`), as plain calls: `io.stdout_write(out, ref buf[0..n])`, `fs.open_read(c, b"/in/data.txt")`, `fs.close(move f)`, `net.listen(c, b"127.0.0.1:7")`. There are no methods, no `File.open()` and no implicit I/O.
**Intrinsics.**
- **Failure.** A fallible intrinsic is an outcome match (`ok(results…)`, `err(e)` with `e: io_error`) or `op(…) or trap` (§6). Used as a plain value or statement it is `E7006`. An infallible one (`clock.monotonic`, `random.fill`) is a plain call; matching it is `E7006`.
- **Authority.** A capability is needed only to acquire authority: `fs.open_read(c, path)` returns a `file<read>` that carries it, and `fs.read(f, mut buf)` takes no capability. The capability kind is checked by the verifier (`E3201`), and the effect too (`E3101`).
- **Resource operands.** An operand the registry marks as borrowed is the resource **by name**. The frontend borrows it `mut` for that one operation and ends the borrow on every outcome edge. A `mut` borrow binding or parameter of the resource (`f: mut<file<write>>`) is passed as is. Writing `mut f` there is `E7006` (one spelling); a resource of the wrong type or mode is `E7005`.
- **Consumed operands** (`fs.close`, `net.close_stream`, `net.close_listener`) take `move f` (`E7003` without it). The frontend never inserts a close: an unclosed resource is the verifier's `E3002`, use after close `E3001`.
- **Data operands** are always written: `ref bytes` / `ref buf[a..b]` / a byte string for read bytes, `mut buf` / `mut buf[a..b]` for a buffer the operation fills (`E7005` otherwise).
- **Paths and addresses** are byte strings or borrowed bytes, passed as they are. There is no `Path` type and the frontend does not normalize. Whether a path is absolute, free of `.`/`..` and inside the capability's scope is decided by the intrinsic at run time (`invalid_input`, `permission_denied`; for narrowing, `invalid_restriction`, `not_subset`).
- **Source map.** The intrinsic's terminator maps to the call. The borrow ends on each edge map to the call, and each arm's code to its arm. A runtime trap under `or trap` points at the call (`source_location` in the run record). A verifier diagnostic on an operand points at that operand's construct.
**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: