cli: add explicit built-in error values
Add the error_values feature and error.const through specification, AIR, verification, interpretation and source lowering. Error values are Copy data with no allocation, effects or authority. Keep enum.make semantics frozen. Cover all registered tags, feature negotiation, payload/type errors and authority rejection in AIR/source; verify canonical and hosted CLI behavior. Record reference-bundle costs and the map preflight-validation prerequisite.
This commit is contained in:
1 parent
89c63c306b
commit
1eaf968f4f
56 files changed
+823
-4
No files matched your search
@@ -22,6 +22,7 @@ python3 tests/hosted/source_linear.py # whole-value owners, canonical AIR and CL
|
||||
python3 tests/hosted/source_buffers.py # initialized storage, owning vectors and CLI contracts
|
||||
python3 tests/hosted/source_deque.py # ring order, returned owners and CLI contracts
|
||||
python3 tests/hosted/source_hash.py # pure hashes, chunks and source/package/hosted contracts
|
||||
python3 tests/hosted/source_errors.py # pure built-in error construction and authority separation
|
||||
(cd tools/eval && python3 -m unittest discover tests) # evaluation harness self-tests
|
||||
zig fmt --check build.zig src tools
|
||||
```
|
||||
|
||||
@@ -113,3 +113,5 @@ RFC 0024 initialized buffers and safe Vec (cl100k, relative to linear aggregates
|
||||
RFC 0025 safe owning Deque (cl100k, relative to buffer/Vec): Quick Reference 7,667 → 7,747 (+1.0%); other source docs 44,717 → 46,246 (+3.4%); full source excluding AIR 52,384 → 53,993 (+3.1%). library/std.md is 10,961 tokens; AIR remains 175,736 bytes. Eleven generic safe functions reuse buffer/Option/exchange and existing ownership/borrow rules without semantic or verifier-state growth. Full ring invariants and accounting stay on demand; cumulative full-source growth is +58.9%, with the RFC's owner-review justification and no empirical MCC claim.
|
||||
|
||||
RFC 0026 std.hash (cl100k, relative to Deque): Quick Reference 7,747 → 7,806 (+0.8%); other source docs 46,246 → 47,475; full source excluding AIR 53,993 → 55,281 (+2.4%). library/std.md is 12,190 tokens; AIR remains 175,736 bytes. Four pure functions expose exact FNV/CRC continuation with no semantic, verifier or authority growth. Cumulative full-source growth is +62.7%, justified in the RFC for owner review without an empirical MCC claim. The proposed map contract stays outside the bundles.
|
||||
|
||||
RFC 0027 explicit error values (cl100k, relative to hashes): Quick Reference 7,806 → 7,863; other source docs 47,475 → 48,061; full source excluding AIR 55,281 → 55,924 (+1.2%). AIR is 179,282 bytes / 51 sections, and its source-bundle copy is 46,300 tokens. The measurement excludes the developing map contract. Existing variant syntax gains pure built-in error values; no authority, resource or ownership state is added. Cumulative source growth is +64.6%; the RFC records the complexity justification, without claiming MCC improvement.
|
||||
@@ -59,6 +59,7 @@ docs/spec/05-effects-and-capabilities.md :: ## 9. Effects in AIR
|
||||
docs/spec/05-effects-and-capabilities.md :: ## 10. Capabilities in AIR
|
||||
docs/spec/05-effects-and-capabilities.md :: ## 11. Intrinsic calls
|
||||
docs/spec/05-effects-and-capabilities.md :: ## 12. Runtime
|
||||
docs/spec/05-effects-and-capabilities.md :: ## 14. Explicit error values
|
||||
|
||||
# Resources and I/O errors
|
||||
docs/spec/06-resources-and-errors.md :: ## 6. Resources in AIR
|
||||
|
||||
@@ -149,6 +149,8 @@ region.reset(r);
|
||||
|
||||
`err(e)` binds a real value: `match e { alloc_error::budget_exceeded => …, alloc_error::out_of_memory => … }`.
|
||||
|
||||
Built-in errors are Copy data: `let e = alloc_error::budget_exceeded;` constructs a payload-free value without effects, allocation or authority. All registered `alloc_error`, `cap_error` and `io_error` variants work this way (spec 05 §14).
|
||||
|
||||
- **Narrowing:** `cap.narrow.prefix(c, b"/data")` and `cap.narrow.budget(heap, 4096)`, each giving `ok(c2) / err(e)`.
|
||||
- Resource operands (`f`, `s`, `l`, endpoints) are borrowed automatically for an operation; a region is not (bind `ref arena = r;`); data borrows (`mut buf`, `ref bytes`) are always written.
|
||||
- Memory budgets count live bytes, so freeing returns them. In the deterministic profile `mem.alloc` needs a grant with a budget: `--grant heap=mem.alloc:4096`.
|
||||
|
||||
@@ -72,6 +72,7 @@ docs/spec/05-effects-and-capabilities.md :: ## 4. Narrowing
|
||||
docs/spec/05-effects-and-capabilities.md :: ## 5. Capability transfer
|
||||
docs/spec/05-effects-and-capabilities.md :: ## 9. Effects in AIR
|
||||
docs/spec/05-effects-and-capabilities.md :: ## 10. Capabilities in AIR
|
||||
docs/spec/05-effects-and-capabilities.md :: ## 14. Explicit error values
|
||||
docs/reference/effects.md
|
||||
docs/reference/capabilities.md
|
||||
|
||||
|
||||
@@ -30,6 +30,7 @@ Status values: `open`, `resolved`.
|
||||
| Q-025 | resolved | STD-5 | Array construction from complete element values |
|
||||
| Q-035 | resolved | STD-5 | Whole-value linear aggregates |
|
||||
| Q-036 | resolved | STD-6a | Dynamic initialized linear buffer storage and drain |
|
||||
| Q-038 | resolved | STD-6d.2 | Pure construction of built-in errors |
|
||||
|
||||
## Q-001 — Text AIR grammar
|
||||
|
||||
@@ -274,3 +275,9 @@ STD-5 intentionally enables only complete fixed-size values and individually own
|
||||
HashMap<K,V> needs a stable hash/equality pair: equal keys must hash equally, while hash collisions must still compare actual keys. Current Copy/Send/Share/Freeze/Local constraints supply no user operations; type parameters cannot carry callbacks or values (spec 15 §15). Struct/byte-key equality, key ownership and effect-visible callable policies have no general source contract. A hash digest alone cannot represent original-key identity.
|
||||
|
||||
**Open for general keys.** The [owning map proposal](hash-map.md) starts with an honest U64Map<V> specialization, exact numeric equality and existing safe primitives. This does not complete the HashMap<Key, Own<Value>> collection gate. General key support must either use an independently specified concrete representation or justify a generally useful vertical language facility; it must not silently infer hashing/equality or introduce map-only compiler magic.
|
||||
|
||||
## Q-038 — Pure construction of built-in errors
|
||||
|
||||
STD-6d.2 needs to return allocation-size validation failure without allocating. Existing built-in errors were obtainable only from fallible runtime operations.
|
||||
|
||||
**Resolved (RFC 0027 branch implementation):** spec 05 §14 adds feature `error_values` and pure `error.const B.variant`; source reuses `B::variant`. Every registered payload-free error variant is ordinary Copy data, never authority. Existing `enum.make` and runtime outcome rules are unchanged.
|
||||
@@ -59,6 +59,7 @@ SQL/database clients, Redis/Kafka, ORMs, application authentication, gRPC/protob
|
||||
| STD-6c | done (branch implementation; RFC 0025 Proposed) | safe std.deque for Copy/owners/files/endpoints | STD-6a/b | bounded ring, both ends, failure-preserving explicit growth, complete resource transfer and empty-only free |
|
||||
| STD-6d | todo (hash foundation done; maps proposed) | HashMap, HashSet, BinaryHeap | STD-6a/b/c | safe collection contracts, including HashMap<Key, Own<Value>>, recoverable failures and explicit draining |
|
||||
| STD-6d.1 | done (branch implementation; RFC 0026 Proposed) | pure std.hash: FNV-1a 64 / CRC32C, fresh/extend | STD-1b | four safe functions, exact byte/chunk conventions, independent public vectors and CLI/profile checks |
|
||||
| STD-6d.2a | done (branch implementation; RFC 0027 Proposed) | pure built-in error construction prerequisite | STD-6d.1 | vertical error_values feature, no authority/effect/resource changes; preflight allocation validation |
|
||||
| STD-6d.2 | proposed; not implemented | U64Map<V> first; general HashMap<K,V> key policy Q-037 | STD-6d.1 | [reviewable contract](../design/hash-map.md), then executable safe map with ownership/failure/collision tests; general-key gate remains open |
|
||||
| STD-7 | todo | concrete I/O adapters, buffered I/O and lexical paths | STD-1b/2a/2b; STD-3 for public types | short reads, EOF, capacity and write failures covered; paths never grant filesystem authority |
|
||||
| STD-8 | todo | filesystem gaps, args/env capability RFC, time/random distinctions and structured log | STD-3/7; vertical runtime feature where needed | CLI/storage milestone examples work in deterministic and hosted profiles; all authority is granted at entry |
|
||||
|
||||
@@ -39,6 +39,7 @@ Declaring `features [capabilities, effects, ownership]` grants no authority. It
|
||||
| `channel_transfer` | reserved | — | `cancellation`, `capabilities`, `channels`, `effects`, `heap`, `ownership`, `resources`, `tasks` | endpoints inside channel elements or mutex contents; unscheduled |
|
||||
| `channels` | published | 3 | `cancellation`, `capabilities`, `effects`, `heap`, `ownership`, `resources`, `tasks` | `sender<T>`, `receiver<T>`, `channel.*`, `select` (spec 04 §38–§39) |
|
||||
| `effects` | published | 2 | — | flat effect checking of `effects [...]` for `call` and `intrinsic` (E3101, spec 05 §9) |
|
||||
| `error_values` | published | 6 | `capabilities`, `effects`, `heap`, `ownership`, `resources` | pure construction of registered built-in errors with `error.const`; errors confer no authority (spec 05 §14) |
|
||||
| `guard_transfer` | reserved | — | `cancellation`, `effects`, `ownership`, `sync`, `tasks` | `guard<T>` as parameter, return type or aggregate member; unscheduled |
|
||||
| `heap` | published | 2 | `capabilities`, `effects`, `ownership`, `resources` | `own<T>`, `own_array<T>`, `own.*`, built-in `alloc_error`, `mem.alloc` budgets and `budget` narrowing (spec 03 §18–§19) |
|
||||
| `islands` | published | 4 | `capabilities`, `effects`, `heap`, `ownership`, `resources` | `island<T>`, `frozen<T>`, `iref<T>`, `island.*`, `frozen.*`, `iref.eq`, `IslandSafe`, `T0010` (spec 03 §22–§23) |
|
||||
|
||||
@@ -57,6 +57,7 @@ instruction = [ local { "," local } "=" ] operation ;
|
||||
|
||||
operation = "const." int_type int_lit
|
||||
| "const.bool" ( "true" | "false" )
|
||||
| "error.const" ( "alloc_error" | "cap_error" | "io_error" ) "." ident (* feature error_values; no payload *)
|
||||
| "copy" local
|
||||
| arith_op local "," local
|
||||
| "assert" local
|
||||
|
||||
@@ -0,0 +1,82 @@
|
||||
# RFC 0027: Explicit built-in error values
|
||||
|
||||
Status: Proposed
|
||||
Scope: STD-6d.2 prerequisite; vertical Phase 6, additive AIR format 0
|
||||
Language version: 0.1
|
||||
|
||||
## Motivation
|
||||
|
||||
A library must reject overflowing allocation sizes before allocation. Built-in errors could previously only arrive from runtime operations. Artificially allocating to obtain an error would obscure authority and failure behavior. Explicit construction makes pure validation implementable and preserves a single error vocabulary.
|
||||
|
||||
## Precise invariant
|
||||
|
||||
An error value is Copy data. Creating, copying, returning or matching it never confers authority, consumes a resource or proves that an operation happened. Runtime operations keep their existing outcome contracts.
|
||||
|
||||
## Normative rule changes
|
||||
|
||||
Spec 05 §14 specifies pure construction of every frozen registered variant of alloc_error, cap_error and io_error. Spec 07a and the AIR grammar add `error.const B.variant`. Spec 15 §6 extends the existing source variant expression to those built-in families. The registry adds `error_values`, depending on the full feature closure defining all three families.
|
||||
|
||||
## Verifier implications
|
||||
|
||||
Validate static family/tag and result uses; define one available Copy value. No new abstract state, ownership, alias, resource or concurrency rule. Validation is constant time over the fixed variant registry.
|
||||
|
||||
## Interpreter / runtime implications
|
||||
|
||||
Bind a built-in family/tag deterministically, consuming ordinary fuel, without program allocation, I/O or capability acquisition. Error data already has a runtime representation. Real allocation failure remains separately specified.
|
||||
|
||||
## AIR changes
|
||||
|
||||
`error.const` takes a registered error family and variant as static operands, no SSA inputs or payloads, and has one result of that family's type. It has no ownership, borrow, memory or concurrency effects, requires no effects/capabilities and has no instruction-specific traps. The feature declaration is mandatory. This is not syntax sugar: no existing pure instruction constructs a built-in error. `enum.make` retains its declared-enum contract.
|
||||
|
||||
## Valid example
|
||||
|
||||
```aji
|
||||
fn overflow() -> alloc_error effects [] {
|
||||
return alloc_error::budget_exceeded;
|
||||
}
|
||||
```
|
||||
|
||||
## Rejected example
|
||||
|
||||
```aji
|
||||
fn forge() -> cap<mem.alloc> effects [] {
|
||||
return alloc_error::budget_exceeded;
|
||||
}
|
||||
```
|
||||
|
||||
Expected diagnostic: E7005; equivalent AIR is E1001. Missing feature is E0006, unknown AIR family/tag E0004, payload syntax E0001. Unknown source variant is E7002, payload arity E7005. No new diagnostic codes.
|
||||
|
||||
## Spec integration
|
||||
|
||||
Spec 05 §14, 07 §8.9, 07a Values, 08 §3, 15 §6; reference AIR grammar and feature registry; both agent bundle manifests and Quick Reference. Q-038 is resolved by these contracts.
|
||||
|
||||
## Compatibility and migration
|
||||
|
||||
Additive feature in format 0. Existing type/variant meanings, enum.make, fallible operations and authority rules remain frozen. Older implementations reject modules declaring error_values through normal feature negotiation. No migration for existing source or AIR.
|
||||
|
||||
## Agent complexity impact
|
||||
|
||||
One coherent semantic block: constructing ordinary error data. One feature and AIR instruction, no new source syntax form, diagnostics, verifier state or ownership concepts. Existing Enum::variant syntax and exhaustive matching suffice. Explicit preflight failures reduce the need for ad hoc error enums or artificial runtime calls. No empirical MCC benefit is claimed. Dependencies define the error data vocabulary, not ambient authority. Measured Quick Reference, source and AIR bundle growth and validation are recorded below when checks finish.
|
||||
|
||||
## Alternatives considered
|
||||
|
||||
Changing enum.make would alter frozen baseline semantics. A new library-specific overflow enum would force different allocation error handling. Harvesting runtime errors through fake allocations changes failure/authority behavior. A payload-free constant is the smallest explicit primitive covering existing error families.
|
||||
|
||||
## Unresolved questions
|
||||
|
||||
None for this feature. General map key hashing/equality remains Q-037.
|
||||
|
||||
## Validation and measured documentation budget
|
||||
|
||||
`zig build`, `zig build test` and formatting pass. All 32 error-value conformance cases pass normally and with collection before every instruction; all 17 registered tags are exercised directly in AIR and through exhaustive source matches. `tests/hosted/source_errors.py` passes 26 CLI checks, including canonical lowering, matching source/AIR run records, hosted execution and zero-allocation/zero-entry-authority audit reports. Source coverage is 96/96. The full working-tree regression also includes the developing map consumer.
|
||||
|
||||
Relative to RFC 0026, excluding the uncommitted map library contract from the generated source library page:
|
||||
|
||||
| Group | cl100k | o200k | Qwen2.5 |
|
||||
|---|---|---|---|
|
||||
| Quick Reference | 7,806 → 7,863 | 7,837 → 7,894 | 8,043 → 8,102 |
|
||||
| Other source docs | 47,475 → 48,061 | 47,616 → 48,204 | 48,802 → 49,419 |
|
||||
| Full source excluding AIR | 55,281 → 55,924 | 55,453 → 56,098 | 56,845 → 57,521 |
|
||||
| AIR inside source bundle | 45,520 → 46,300 | 45,667 → 46,451 | 46,981 → 47,800 |
|
||||
|
||||
Standalone AIR grows from 175,736 to 179,282 bytes, 19 files / 51 sections. Incremental full source growth is 1.2%; cumulative growth since the pre-stdlib cl100k baseline 33,981 is 64.6%, above the existing review line. The cost is justified by a necessary pure validation primitive with no new source form, authority or ownership concepts. Detailed contracts remain on demand; documentation growth is not evidence of MCC improvement.
|
||||
@@ -289,3 +289,15 @@ atomic.new, atomic.take → no effect
|
||||
For compare-exchange, the operation is non-`seq_cst` if either ordering is. A missing atom is `E3101`.
|
||||
|
||||
If `atomic.weak` is not among the transitive effects of `@main`, every atomic access is `seq_cst`, and every execution of the safe program is explainable as an SC interleaving (spec 04 §22).
|
||||
|
||||
## 14. Explicit error values (feature `error_values`)
|
||||
|
||||
An error is Copy data, not authority or proof that an operation occurred. Safe code MAY create any registered payload-free variant of `alloc_error` (spec 03 §18), `cap_error` (§10) or `io_error` (spec 06 §7). Creating an error MUST NOT allocate, perform I/O, narrow a capability, change resource state or grant authority. Actual fallible operations retain their specified outcome rules.
|
||||
|
||||
In AIR, `%e = error.const B.variant` has two static operands: a built-in error family `B` and one of its registered variant names. It takes no SSA operands or payloads and produces exactly one Copy value of type `B`. The module MUST declare `error_values` and its full dependency closure (`capabilities`, `effects`, `heap`, `ownership`, `resources`). These dependencies make all three existing error families available; they grant no capabilities.
|
||||
|
||||
The verifier MUST validate the family and variant (`E0004` for an unknown name in text), result uses (`E1001` for a type mismatch) and feature declaration (`E0006`). Its transfer function introduces one available Copy value; it changes no ownership, borrow, memory, concurrency or authority state. Effects and capabilities required: none. Instruction-specific traps: none. The interpreter deterministically binds the selected family/tag as ordinary built-in error data; the instruction consumes normal fuel. The payload syntax is not permitted (`E0001`).
|
||||
|
||||
Source uses the existing variant expression `alloc_error::budget_exceeded`, `cap_error::not_subset` or `io_error::invalid_input`, lowering to `error.const`; the frontend MUST include the feature closure. Every registered variant is constructible; errors cannot be converted into capabilities. Source rejects unknown variants with `E7002`, payloads with `E7005`, and wrong result types with `E7005`.
|
||||
|
||||
Valid: a pure function returning `alloc_error::budget_exceeded` before overflowing an allocation size. Rejected: returning that value from a function returning `cap<mem.alloc>` (`E7005` / AIR `E1001`). Existing `enum.make` remains restricted to declared enums. This is an additive format-0 feature; no existing instruction, error tag, fallible outcome or authority rule changes.
|
||||
@@ -258,6 +258,8 @@ Any other change to format 0 — a different meaning, typing rule, trap behavior
|
||||
|
||||
`cap<K>` (feature `capabilities`) is an opaque, copyable, authority-bearing value type; `K` is a registered capability kind ([`../reference/capabilities.md`](../reference/capabilities.md)). Built-in enums such as `cap_error` and `io_error` are spelled without `@`, have registered variants and are used with `enum.switch <builtin> %v, …` like declared enums. Rules: spec 05 §9–§12.
|
||||
|
||||
Feature `error_values`: `%e = error.const alloc_error.budget_exceeded` creates ordinary Copy error data without authority or effects (spec 05 §14); `enum.make` remains a declared-enum instruction.
|
||||
|
||||
Feature `resources`: `file<read>`, `file<write>`, `file<append>`, `listener`, `stream` are linear, authority-bearing resource types (spec 06 §6).
|
||||
|
||||
Feature `heap`: `own<T>` and `own_array<T>` (linear owners, `T` a value type) and the built-in enum `alloc_error`. Feature `regions`: `region` (linear). Rules: spec 03 §18–§21. Linear values — resources, owners and regions — follow the consumption rules of spec 06 §6.2–§6.3.
|
||||
|
||||
@@ -22,6 +22,15 @@ Notation: `T` is the type suffix of the opcode; `I` ranges over integer types; `
|
||||
|
||||
## Values
|
||||
|
||||
### `error.const B.variant` (feature `error_values`)
|
||||
|
||||
- Static operands: built-in error family `B` and its registered payload-free variant; no SSA operands.
|
||||
- Result: one Copy value of type `B`.
|
||||
- Ownership/borrow/memory/concurrency transitions: none; effects and capabilities required: none; traps: none.
|
||||
- Verifier transfer: define one available Copy value. Unknown family/variant: `E0004`; undeclared feature: `E0006`; incompatible result use: `E1001`. Payload syntax: `E0001`.
|
||||
- Interpreter: bind the built-in family/tag deterministically, using normal fuel; no program allocation or authority acquisition.
|
||||
- Complete contract and source syntax: spec 05 §14. `enum.make` is unchanged.
|
||||
|
||||
### `const.T lit`
|
||||
|
||||
- Operands: an integer literal (`T` ∈ `I`) or `true`/`false` (`T` = `bool`).
|
||||
|
||||
@@ -47,6 +47,8 @@ resource state
|
||||
|
||||
## 4. Ownership state machine
|
||||
|
||||
For `error.const` (feature `error_values`, spec 05 §14), verification introduces one available Copy value of the selected built-in error type. It MUST NOT alter authority, resource, memory, borrow or concurrency state. The static family/tag and ordinary result-use typing MUST be checked.
|
||||
|
||||
Representative states:
|
||||
|
||||
```text
|
||||
|
||||
@@ -235,6 +235,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.
|
||||
- **Error construction.** Every registered built-in error variant is also a payload-free expression, e.g. `let e = alloc_error::budget_exceeded;`. It lowers to `error.const` with feature `error_values` and its dependency closure (spec 05 §14), requires no effects or capabilities and changes no resource state. Unknown variants are `E7002`, payload arguments `E7005`; errors are Copy data and cannot confer authority.
|
||||
- **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`.
|
||||
|
||||
@@ -31,6 +31,7 @@ pub const registry = [_]Feature{
|
||||
.{ .name = "channel_transfer", .depends = &.{ "cancellation", "capabilities", "channels", "effects", "heap", "ownership", "resources", "tasks" }, .status = .reserved, .phase = null },
|
||||
.{ .name = "channels", .depends = &.{ "cancellation", "capabilities", "effects", "heap", "ownership", "resources", "tasks" }, .status = .published, .phase = 3 },
|
||||
.{ .name = "effects", .depends = &.{}, .status = .published, .phase = 2 },
|
||||
.{ .name = "error_values", .depends = &.{ "capabilities", "effects", "heap", "ownership", "resources" }, .status = .published, .phase = 6 },
|
||||
.{ .name = "guard_transfer", .depends = &.{ "cancellation", "effects", "ownership", "sync", "tasks" }, .status = .reserved, .phase = null },
|
||||
.{ .name = "heap", .depends = &.{ "capabilities", "effects", "ownership", "resources" }, .status = .published, .phase = 2 },
|
||||
.{ .name = "islands", .depends = &.{ "capabilities", "effects", "heap", "ownership", "resources" }, .status = .published, .phase = 4 },
|
||||
@@ -50,7 +51,7 @@ pub const registry = [_]Feature{
|
||||
};
|
||||
|
||||
/// Features implemented by this toolchain, sorted.
|
||||
pub const supported = [_][]const u8{ "arc", "arrays", "atomics", "cancellation", "capabilities", "channels", "effects", "heap", "islands", "linear_aggregates", "linear_buffers", "ownership", "regions", "resources", "sync", "tasks", "unsafe" };
|
||||
pub const supported = [_][]const u8{ "arc", "arrays", "atomics", "cancellation", "capabilities", "channels", "effects", "error_values", "heap", "islands", "linear_aggregates", "linear_buffers", "ownership", "regions", "resources", "sync", "tasks", "unsafe" };
|
||||
|
||||
pub const Reason = enum {
|
||||
unknown_feature,
|
||||
|
||||
+3
-1
@@ -306,6 +306,8 @@ pub const Op = union(enum) {
|
||||
assume: ValueId,
|
||||
|
||||
@"const": struct { ty: Type, bits: u128 },
|
||||
/// Pure built-in error data (feature error_values, spec 05 §14).
|
||||
error_const: struct { which: authority.Builtin, variant: u32 },
|
||||
copy: ValueId,
|
||||
arith: struct { op: ArithOp, mode: ArithMode, ty: IntType, a: ValueId, b: ValueId },
|
||||
bit: struct { op: BitOp, ty: Type, a: ValueId, b: ValueId },
|
||||
@@ -559,7 +561,7 @@ pub fn forEachUse(op: Op, ctx: anytype, comptime f: fn (@TypeOf(ctx), ValueId) v
|
||||
},
|
||||
inline .buffer_pop, .buffer_free => |x| f(ctx, x.value),
|
||||
.buffer_len, .buffer_capacity, .buffer_view => |v| f(ctx, v),
|
||||
.@"const", .trap => {},
|
||||
.@"const", .error_const, .trap => {},
|
||||
.copy, .assert => |v| f(ctx, v),
|
||||
.arith => |x| {
|
||||
f(ctx, x.a);
|
||||
|
||||
+15
-1
@@ -873,6 +873,20 @@ const Parser = struct {
|
||||
const head = parts[0];
|
||||
|
||||
const op: air.Op = op: {
|
||||
if (eql(op_tok.text, "error.const")) {
|
||||
try p.requireFeature("error_values", op_tok.span, op_tok.text);
|
||||
const t = try p.expect(.word, "built-in error family and variant");
|
||||
const dot = std.mem.indexOfScalar(u8, t.text, '.') orelse return p.fail(t, "`B.variant`");
|
||||
const b = std.meta.stringToEnum(authority.Builtin, t.text[0..dot]) orelse {
|
||||
try p.report("E0004", t.span, "unknown built-in error family", &.{.{ .name = "name", .value = .{ .str = t.text[0..dot] } }});
|
||||
return error.SyntaxError;
|
||||
};
|
||||
const variant = b.variantIndex(t.text[dot + 1 ..]) orelse {
|
||||
try p.report("E0004", t.span, "unknown built-in error variant", &.{.{ .name = "name", .value = .{ .str = t.text[dot + 1 ..] } }});
|
||||
return error.SyntaxError;
|
||||
};
|
||||
break :op .{ .error_const = .{ .which = b, .variant = variant } };
|
||||
}
|
||||
if (eql(head, "const") and parts.len == 2) {
|
||||
if (eql(parts[1], "bool")) {
|
||||
const lit = try p.expect(.word, "`true` or `false`");
|
||||
@@ -1110,7 +1124,7 @@ const Parser = struct {
|
||||
.task_shield_begin, .channel_dup, .mutex_make, .mutex_take, .mutex_borrow => true,
|
||||
.island_root, .island_borrow, .island_freeze, .frozen_root, .frozen_borrow, .arc_from_frozen, .arc_clone, .arc_root, .arc_borrow, .iref_eq => true,
|
||||
.atomic_new, .atomic_take, .atomic_load, .atomic_rmw, .atomic_cmpxchg => true,
|
||||
.@"const", .copy, .arith, .bit, .cmp, .cast, .struct_make, .struct_get, .enum_make => true,
|
||||
.@"const", .error_const, .copy, .arith, .bit, .cmp, .cast, .struct_make, .struct_get, .enum_make => true,
|
||||
.local_alloc, .local_load, .borrow_ref, .borrow_mut, .borrow_field, .load => true,
|
||||
.buffer_len, .buffer_capacity, .buffer_view, .array_splat, .array_make, .value_swap, .borrow_index, .slice_make, .slice_len, .slice_sub, .slice_split => true,
|
||||
else => false,
|
||||
|
||||
@@ -179,6 +179,7 @@ fn printOp(m: air.Module, f: air.Function, op: air.Op, w: *Writer) Writer.Error!
|
||||
},
|
||||
else => unreachable,
|
||||
},
|
||||
.error_const => |x| try w.print("error.const {s}.{s}", .{ @tagName(x.which), x.which.variants()[x.variant] }),
|
||||
.copy => |v| {
|
||||
try w.writeAll("copy ");
|
||||
try value(f, v, w);
|
||||
@@ -717,6 +718,22 @@ test "round-trip whole-value linear operations" {
|
||||
);
|
||||
}
|
||||
|
||||
test "round-trip explicit error values" {
|
||||
try roundTrip(
|
||||
\\module test.errors
|
||||
\\air 0
|
||||
\\lang 0.1
|
||||
\\features [resources, ownership, heap, error_values, effects, capabilities]
|
||||
\\fn @main() -> alloc_error
|
||||
\\effects []
|
||||
\\entry:
|
||||
\\ %io = error.const io_error.invalid_input
|
||||
\\ %cap = error.const cap_error.not_subset
|
||||
\\ %e = error.const alloc_error.budget_exceeded
|
||||
\\ ret %e
|
||||
);
|
||||
}
|
||||
|
||||
test "round-trip initialized buffers" {
|
||||
try roundTrip(
|
||||
\\module test.buffers
|
||||
|
||||
@@ -581,6 +581,7 @@ const Lowerer = struct {
|
||||
.{ "arrays", &.{ "array<", "slice", "array.splat", "borrow.index" } },
|
||||
.{ "ownership", &.{ "local.", "borrow.", " load ", "= load ", "store ", "ref<", "mut<" } },
|
||||
.{ "linear_buffers", &.{ "buffer<", "buffer." } },
|
||||
.{ "error_values", &.{"error.const "} },
|
||||
.{ "linear_aggregates", &.{ "array.make ", "struct.unpack ", "array.unpack ", "enum.take ", "value.swap ", "own.pack " } },
|
||||
.{ "heap", &.{ "own.", "own<", "own_array<", "alloc_error" } },
|
||||
.{ "regions", &.{"region"} },
|
||||
@@ -1401,7 +1402,7 @@ const Lowerer = struct {
|
||||
|
||||
fn typeOfNoFail(l: *Lowerer, t: ast.Type) ?Ty {
|
||||
return switch (t.kind) {
|
||||
.name => |n| if (isIntName(n)) Ty{ .int = n } else if (std.mem.eql(u8, n, "bool")) .bool else l.enumOrStruct(n, t.span),
|
||||
.name => |n| if (isIntName(n)) Ty{ .int = n } else if (std.mem.eql(u8, n, "bool")) .bool else if (std.meta.stringToEnum(authority.Builtin, n)) |b| Ty{ .builtin = b } else l.enumOrStruct(n, t.span),
|
||||
else => null,
|
||||
};
|
||||
}
|
||||
@@ -1413,6 +1414,7 @@ const Lowerer = struct {
|
||||
}
|
||||
|
||||
fn enumType(l: *Lowerer, n: []const u8, span: Span) ?Ty {
|
||||
if (std.meta.stringToEnum(authority.Builtin, n)) |b| return .{ .builtin = b };
|
||||
const t = l.enumOrStruct(n, span) orelse return null;
|
||||
return if (l.m.types[t.named].kind == .@"enum") t else null;
|
||||
}
|
||||
@@ -1751,6 +1753,11 @@ const Lowerer = struct {
|
||||
|
||||
fn variantValue(l: *Lowerer, e: ast.Expr, enum_name: []const u8, variant: []const u8, args: []const ast.Expr) Error!Value {
|
||||
const t = l.enumType(enum_name, e.span) orelse return l.fail("E7002", e.span, "unknown enum", &.{.{ .name = "name", .value = enum_name }});
|
||||
if (t == .builtin) {
|
||||
_ = t.builtin.variantIndex(variant) orelse return l.fail("E7002", e.span, "no such variant", &.{ .{ .name = "enum", .value = enum_name }, .{ .name = "variant", .value = variant } });
|
||||
if (args.len != 0) return l.fail("E7005", e.span, "built-in error variants have no payload", &.{.{ .name = "variant", .value = variant }});
|
||||
return l.def("e", t, try l.fmt("error.const {s}.{s}", .{ enum_name, variant }), e.span);
|
||||
}
|
||||
const vs = l.m.types[t.named].kind.@"enum";
|
||||
const v = for (vs) |x| {
|
||||
if (std.mem.eql(u8, x.name, variant)) break x;
|
||||
|
||||
@@ -846,6 +846,7 @@ const Machine = struct {
|
||||
.bool => .{ .boolean = c.bits != 0 },
|
||||
else => unreachable,
|
||||
}),
|
||||
.error_const => |x| return bind(vals, inst.result, .{ .builtin = .{ .which = x.which, .variant = x.variant } }),
|
||||
.copy => |v| return bind(vals, inst.result, vals[v]),
|
||||
.arith => |x| {
|
||||
const bits = arith(x.op, x.mode, x.ty, vals[x.a].int.bits, vals[x.b].int.bits) catch |e| return .{ .trap = kindOf(e) };
|
||||
|
||||
@@ -258,6 +258,7 @@ const FnCtx = struct {
|
||||
fn resultType(m: air.Module, op: air.Op) ?Type {
|
||||
return switch (op) {
|
||||
.@"const" => |x| x.ty,
|
||||
.error_const => |x| .{ .builtin = x.which },
|
||||
.arith => |x| .{ .int = x.ty },
|
||||
.bit => |x| x.ty,
|
||||
.cmp => .bool,
|
||||
@@ -794,6 +795,7 @@ fn checkInst(fc: *FnCtx, inst: air.Inst) !void {
|
||||
const span = inst.span;
|
||||
switch (inst.op) {
|
||||
.@"const", .trap => {},
|
||||
.error_const => |x| if (x.variant >= x.which.variants().len) try fc.c.report("E1001", span, "invalid built-in error variant", &.{}),
|
||||
.copy => |v| if (!fc.typeOf(v).isValue() or m.isLinearType(fc.typeOf(v))) try fc.c.report("E1001", span, "only value types can be copied", &.{
|
||||
.{ .name = "function", .value = .{ .str = f.name } },
|
||||
.{ .name = "found", .value = .{ .str = try fc.c.typeName(fc.typeOf(v)) } },
|
||||
|
||||
@@ -0,0 +1,14 @@
|
||||
; expect: run
|
||||
; result: {"variant":"budget_exceeded","payload":[]}
|
||||
; phase: 6
|
||||
; spec: docs/spec/05-effects-and-capabilities.md §14
|
||||
module test.errors
|
||||
air 0
|
||||
lang 0.1
|
||||
features [capabilities, effects, error_values, heap, ownership, resources]
|
||||
fn @main() -> alloc_error
|
||||
effects []
|
||||
entry:
|
||||
%e = error.const alloc_error.budget_exceeded
|
||||
%copy = copy %e
|
||||
ret %copy
|
||||
@@ -0,0 +1,14 @@
|
||||
; expect: run
|
||||
; result: {"variant":"out_of_memory","payload":[]}
|
||||
; phase: 6
|
||||
; spec: docs/spec/05-effects-and-capabilities.md §14
|
||||
module test.errors
|
||||
air 0
|
||||
lang 0.1
|
||||
features [capabilities, effects, error_values, heap, ownership, resources]
|
||||
fn @main() -> alloc_error
|
||||
effects []
|
||||
entry:
|
||||
%e = error.const alloc_error.out_of_memory
|
||||
%copy = copy %e
|
||||
ret %copy
|
||||
@@ -0,0 +1,14 @@
|
||||
; expect: run
|
||||
; result: {"variant":"invalid_restriction","payload":[]}
|
||||
; phase: 6
|
||||
; spec: docs/spec/05-effects-and-capabilities.md §14
|
||||
module test.errors
|
||||
air 0
|
||||
lang 0.1
|
||||
features [capabilities, effects, error_values, heap, ownership, resources]
|
||||
fn @main() -> cap_error
|
||||
effects []
|
||||
entry:
|
||||
%e = error.const cap_error.invalid_restriction
|
||||
%copy = copy %e
|
||||
ret %copy
|
||||
@@ -0,0 +1,14 @@
|
||||
; expect: run
|
||||
; result: {"variant":"not_subset","payload":[]}
|
||||
; phase: 6
|
||||
; spec: docs/spec/05-effects-and-capabilities.md §14
|
||||
module test.errors
|
||||
air 0
|
||||
lang 0.1
|
||||
features [capabilities, effects, error_values, heap, ownership, resources]
|
||||
fn @main() -> cap_error
|
||||
effects []
|
||||
entry:
|
||||
%e = error.const cap_error.not_subset
|
||||
%copy = copy %e
|
||||
ret %copy
|
||||
@@ -0,0 +1,14 @@
|
||||
; expect: run
|
||||
; result: {"variant":"unsupported_restriction","payload":[]}
|
||||
; phase: 6
|
||||
; spec: docs/spec/05-effects-and-capabilities.md §14
|
||||
module test.errors
|
||||
air 0
|
||||
lang 0.1
|
||||
features [capabilities, effects, error_values, heap, ownership, resources]
|
||||
fn @main() -> cap_error
|
||||
effects []
|
||||
entry:
|
||||
%e = error.const cap_error.unsupported_restriction
|
||||
%copy = copy %e
|
||||
ret %copy
|
||||
@@ -0,0 +1,12 @@
|
||||
; expect: reject E0006
|
||||
; phase: 6
|
||||
; spec: docs/spec/05-effects-and-capabilities.md §14
|
||||
module test.errors
|
||||
air 0
|
||||
lang 0.1
|
||||
features [error_values]
|
||||
fn @main() -> u64
|
||||
effects []
|
||||
entry:
|
||||
%x = const.u64 42
|
||||
ret %x
|
||||
@@ -0,0 +1,12 @@
|
||||
; expect: reject E0001
|
||||
; phase: 6
|
||||
; spec: docs/spec/05-effects-and-capabilities.md §14
|
||||
module test.errors
|
||||
air 0
|
||||
lang 0.1
|
||||
features [capabilities, effects, error_values, heap, ownership, resources]
|
||||
fn @main() -> alloc_error
|
||||
effects []
|
||||
entry:
|
||||
%e = enum.make alloc_error.budget_exceeded()
|
||||
ret %e
|
||||
@@ -0,0 +1,14 @@
|
||||
; expect: run
|
||||
; result: {"variant":"address_in_use","payload":[]}
|
||||
; phase: 6
|
||||
; spec: docs/spec/05-effects-and-capabilities.md §14
|
||||
module test.errors
|
||||
air 0
|
||||
lang 0.1
|
||||
features [capabilities, effects, error_values, heap, ownership, resources]
|
||||
fn @main() -> io_error
|
||||
effects []
|
||||
entry:
|
||||
%e = error.const io_error.address_in_use
|
||||
%copy = copy %e
|
||||
ret %copy
|
||||
@@ -0,0 +1,14 @@
|
||||
; expect: run
|
||||
; result: {"variant":"address_unavailable","payload":[]}
|
||||
; phase: 6
|
||||
; spec: docs/spec/05-effects-and-capabilities.md §14
|
||||
module test.errors
|
||||
air 0
|
||||
lang 0.1
|
||||
features [capabilities, effects, error_values, heap, ownership, resources]
|
||||
fn @main() -> io_error
|
||||
effects []
|
||||
entry:
|
||||
%e = error.const io_error.address_unavailable
|
||||
%copy = copy %e
|
||||
ret %copy
|
||||
@@ -0,0 +1,14 @@
|
||||
; expect: run
|
||||
; result: {"variant":"already_exists","payload":[]}
|
||||
; phase: 6
|
||||
; spec: docs/spec/05-effects-and-capabilities.md §14
|
||||
module test.errors
|
||||
air 0
|
||||
lang 0.1
|
||||
features [capabilities, effects, error_values, heap, ownership, resources]
|
||||
fn @main() -> io_error
|
||||
effects []
|
||||
entry:
|
||||
%e = error.const io_error.already_exists
|
||||
%copy = copy %e
|
||||
ret %copy
|
||||
@@ -0,0 +1,14 @@
|
||||
; expect: run
|
||||
; result: {"variant":"broken_pipe","payload":[]}
|
||||
; phase: 6
|
||||
; spec: docs/spec/05-effects-and-capabilities.md §14
|
||||
module test.errors
|
||||
air 0
|
||||
lang 0.1
|
||||
features [capabilities, effects, error_values, heap, ownership, resources]
|
||||
fn @main() -> io_error
|
||||
effects []
|
||||
entry:
|
||||
%e = error.const io_error.broken_pipe
|
||||
%copy = copy %e
|
||||
ret %copy
|
||||
@@ -0,0 +1,14 @@
|
||||
; expect: run
|
||||
; result: {"variant":"connection_refused","payload":[]}
|
||||
; phase: 6
|
||||
; spec: docs/spec/05-effects-and-capabilities.md §14
|
||||
module test.errors
|
||||
air 0
|
||||
lang 0.1
|
||||
features [capabilities, effects, error_values, heap, ownership, resources]
|
||||
fn @main() -> io_error
|
||||
effects []
|
||||
entry:
|
||||
%e = error.const io_error.connection_refused
|
||||
%copy = copy %e
|
||||
ret %copy
|
||||
@@ -0,0 +1,14 @@
|
||||
; expect: run
|
||||
; result: {"variant":"connection_reset","payload":[]}
|
||||
; phase: 6
|
||||
; spec: docs/spec/05-effects-and-capabilities.md §14
|
||||
module test.errors
|
||||
air 0
|
||||
lang 0.1
|
||||
features [capabilities, effects, error_values, heap, ownership, resources]
|
||||
fn @main() -> io_error
|
||||
effects []
|
||||
entry:
|
||||
%e = error.const io_error.connection_reset
|
||||
%copy = copy %e
|
||||
ret %copy
|
||||
@@ -0,0 +1,14 @@
|
||||
; expect: run
|
||||
; result: {"variant":"invalid_input","payload":[]}
|
||||
; phase: 6
|
||||
; spec: docs/spec/05-effects-and-capabilities.md §14
|
||||
module test.errors
|
||||
air 0
|
||||
lang 0.1
|
||||
features [capabilities, effects, error_values, heap, ownership, resources]
|
||||
fn @main() -> io_error
|
||||
effects []
|
||||
entry:
|
||||
%e = error.const io_error.invalid_input
|
||||
%copy = copy %e
|
||||
ret %copy
|
||||
@@ -0,0 +1,14 @@
|
||||
; expect: run
|
||||
; result: {"variant":"no_space","payload":[]}
|
||||
; phase: 6
|
||||
; spec: docs/spec/05-effects-and-capabilities.md §14
|
||||
module test.errors
|
||||
air 0
|
||||
lang 0.1
|
||||
features [capabilities, effects, error_values, heap, ownership, resources]
|
||||
fn @main() -> io_error
|
||||
effects []
|
||||
entry:
|
||||
%e = error.const io_error.no_space
|
||||
%copy = copy %e
|
||||
ret %copy
|
||||
@@ -0,0 +1,14 @@
|
||||
; expect: run
|
||||
; result: {"variant":"not_found","payload":[]}
|
||||
; phase: 6
|
||||
; spec: docs/spec/05-effects-and-capabilities.md §14
|
||||
module test.errors
|
||||
air 0
|
||||
lang 0.1
|
||||
features [capabilities, effects, error_values, heap, ownership, resources]
|
||||
fn @main() -> io_error
|
||||
effects []
|
||||
entry:
|
||||
%e = error.const io_error.not_found
|
||||
%copy = copy %e
|
||||
ret %copy
|
||||
@@ -0,0 +1,14 @@
|
||||
; expect: run
|
||||
; result: {"variant":"other","payload":[]}
|
||||
; phase: 6
|
||||
; spec: docs/spec/05-effects-and-capabilities.md §14
|
||||
module test.errors
|
||||
air 0
|
||||
lang 0.1
|
||||
features [capabilities, effects, error_values, heap, ownership, resources]
|
||||
fn @main() -> io_error
|
||||
effects []
|
||||
entry:
|
||||
%e = error.const io_error.other
|
||||
%copy = copy %e
|
||||
ret %copy
|
||||
@@ -0,0 +1,14 @@
|
||||
; expect: run
|
||||
; result: {"variant":"permission_denied","payload":[]}
|
||||
; phase: 6
|
||||
; spec: docs/spec/05-effects-and-capabilities.md §14
|
||||
module test.errors
|
||||
air 0
|
||||
lang 0.1
|
||||
features [capabilities, effects, error_values, heap, ownership, resources]
|
||||
fn @main() -> io_error
|
||||
effects []
|
||||
entry:
|
||||
%e = error.const io_error.permission_denied
|
||||
%copy = copy %e
|
||||
ret %copy
|
||||
@@ -0,0 +1,14 @@
|
||||
; expect: run
|
||||
; result: {"variant":"timed_out","payload":[]}
|
||||
; phase: 6
|
||||
; spec: docs/spec/05-effects-and-capabilities.md §14
|
||||
module test.errors
|
||||
air 0
|
||||
lang 0.1
|
||||
features [capabilities, effects, error_values, heap, ownership, resources]
|
||||
fn @main() -> io_error
|
||||
effects []
|
||||
entry:
|
||||
%e = error.const io_error.timed_out
|
||||
%copy = copy %e
|
||||
ret %copy
|
||||
@@ -0,0 +1,12 @@
|
||||
; expect: reject E1001
|
||||
; phase: 6
|
||||
; spec: docs/spec/05-effects-and-capabilities.md §14
|
||||
module test.errors
|
||||
air 0
|
||||
lang 0.1
|
||||
features [capabilities, effects, error_values, heap, ownership, resources]
|
||||
fn @main() -> cap<mem.alloc>
|
||||
effects []
|
||||
entry:
|
||||
%e = error.const alloc_error.budget_exceeded
|
||||
ret %e
|
||||
@@ -0,0 +1,12 @@
|
||||
; expect: reject E0001
|
||||
; phase: 6
|
||||
; spec: docs/spec/05-effects-and-capabilities.md §14
|
||||
module test.errors
|
||||
air 0
|
||||
lang 0.1
|
||||
features [capabilities, effects, error_values, heap, ownership, resources]
|
||||
fn @main() -> alloc_error
|
||||
effects []
|
||||
entry:
|
||||
%e = error.const alloc_error.budget_exceeded()
|
||||
ret %e
|
||||
@@ -0,0 +1,12 @@
|
||||
; expect: reject E0006
|
||||
; phase: 6
|
||||
; spec: docs/spec/05-effects-and-capabilities.md §14
|
||||
module test.errors
|
||||
air 0
|
||||
lang 0.1
|
||||
features [capabilities, effects, heap, ownership, resources]
|
||||
fn @main() -> alloc_error
|
||||
effects []
|
||||
entry:
|
||||
%e = error.const alloc_error.budget_exceeded
|
||||
ret %e
|
||||
@@ -0,0 +1,12 @@
|
||||
; expect: reject E0004
|
||||
; phase: 6
|
||||
; spec: docs/spec/05-effects-and-capabilities.md §14
|
||||
module test.errors
|
||||
air 0
|
||||
lang 0.1
|
||||
features [capabilities, effects, error_values, heap, ownership, resources]
|
||||
fn @main() -> void
|
||||
effects []
|
||||
entry:
|
||||
%e = error.const user_error.invalid
|
||||
ret
|
||||
@@ -0,0 +1,12 @@
|
||||
; expect: reject E0004
|
||||
; phase: 6
|
||||
; spec: docs/spec/05-effects-and-capabilities.md §14
|
||||
module test.errors
|
||||
air 0
|
||||
lang 0.1
|
||||
features [capabilities, effects, error_values, heap, ownership, resources]
|
||||
fn @main() -> alloc_error
|
||||
effects []
|
||||
entry:
|
||||
%e = error.const alloc_error.invalid
|
||||
ret %e
|
||||
@@ -0,0 +1,12 @@
|
||||
; expect: reject E1001
|
||||
; phase: 6
|
||||
; spec: docs/spec/05-effects-and-capabilities.md §14
|
||||
module test.errors
|
||||
air 0
|
||||
lang 0.1
|
||||
features [capabilities, effects, error_values, heap, ownership, resources]
|
||||
fn @main() -> io_error
|
||||
effects []
|
||||
entry:
|
||||
%e = error.const alloc_error.budget_exceeded
|
||||
ret %e
|
||||
@@ -0,0 +1,20 @@
|
||||
// expect: run
|
||||
// result: 42
|
||||
// phase: 6
|
||||
// spec: docs/spec/05-effects-and-capabilities.md §14
|
||||
module test.errors
|
||||
fn v0() -> alloc_error effects [] { return alloc_error::budget_exceeded; }
|
||||
fn v1() -> alloc_error effects [] { return alloc_error::out_of_memory; }
|
||||
fn main() -> u64 effects [] {
|
||||
let e0 = v0();
|
||||
match e0 {
|
||||
alloc_error::budget_exceeded => {}
|
||||
alloc_error::out_of_memory => { trap; }
|
||||
}
|
||||
let e1 = v1();
|
||||
match e1 {
|
||||
alloc_error::budget_exceeded => { trap; }
|
||||
alloc_error::out_of_memory => {}
|
||||
}
|
||||
return 42u64;
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
// expect: run
|
||||
// result: 42
|
||||
// phase: 6
|
||||
// spec: docs/spec/05-effects-and-capabilities.md §14
|
||||
module test.errors
|
||||
fn v0() -> cap_error effects [] { return cap_error::invalid_restriction; }
|
||||
fn v1() -> cap_error effects [] { return cap_error::not_subset; }
|
||||
fn v2() -> cap_error effects [] { return cap_error::unsupported_restriction; }
|
||||
fn main() -> u64 effects [] {
|
||||
let e0 = v0();
|
||||
match e0 {
|
||||
cap_error::invalid_restriction => {}
|
||||
cap_error::not_subset => { trap; }
|
||||
cap_error::unsupported_restriction => { trap; }
|
||||
}
|
||||
let e1 = v1();
|
||||
match e1 {
|
||||
cap_error::invalid_restriction => { trap; }
|
||||
cap_error::not_subset => {}
|
||||
cap_error::unsupported_restriction => { trap; }
|
||||
}
|
||||
let e2 = v2();
|
||||
match e2 {
|
||||
cap_error::invalid_restriction => { trap; }
|
||||
cap_error::not_subset => { trap; }
|
||||
cap_error::unsupported_restriction => {}
|
||||
}
|
||||
return 42u64;
|
||||
}
|
||||
@@ -0,0 +1,200 @@
|
||||
// expect: run
|
||||
// result: 42
|
||||
// phase: 6
|
||||
// spec: docs/spec/05-effects-and-capabilities.md §14
|
||||
module test.errors
|
||||
fn v0() -> io_error effects [] { return io_error::not_found; }
|
||||
fn v1() -> io_error effects [] { return io_error::permission_denied; }
|
||||
fn v2() -> io_error effects [] { return io_error::already_exists; }
|
||||
fn v3() -> io_error effects [] { return io_error::invalid_input; }
|
||||
fn v4() -> io_error effects [] { return io_error::no_space; }
|
||||
fn v5() -> io_error effects [] { return io_error::broken_pipe; }
|
||||
fn v6() -> io_error effects [] { return io_error::connection_refused; }
|
||||
fn v7() -> io_error effects [] { return io_error::connection_reset; }
|
||||
fn v8() -> io_error effects [] { return io_error::address_in_use; }
|
||||
fn v9() -> io_error effects [] { return io_error::address_unavailable; }
|
||||
fn v10() -> io_error effects [] { return io_error::timed_out; }
|
||||
fn v11() -> io_error effects [] { return io_error::other; }
|
||||
fn main() -> u64 effects [] {
|
||||
let e0 = v0();
|
||||
match e0 {
|
||||
io_error::not_found => {}
|
||||
io_error::permission_denied => { trap; }
|
||||
io_error::already_exists => { trap; }
|
||||
io_error::invalid_input => { trap; }
|
||||
io_error::no_space => { trap; }
|
||||
io_error::broken_pipe => { trap; }
|
||||
io_error::connection_refused => { trap; }
|
||||
io_error::connection_reset => { trap; }
|
||||
io_error::address_in_use => { trap; }
|
||||
io_error::address_unavailable => { trap; }
|
||||
io_error::timed_out => { trap; }
|
||||
io_error::other => { trap; }
|
||||
}
|
||||
let e1 = v1();
|
||||
match e1 {
|
||||
io_error::not_found => { trap; }
|
||||
io_error::permission_denied => {}
|
||||
io_error::already_exists => { trap; }
|
||||
io_error::invalid_input => { trap; }
|
||||
io_error::no_space => { trap; }
|
||||
io_error::broken_pipe => { trap; }
|
||||
io_error::connection_refused => { trap; }
|
||||
io_error::connection_reset => { trap; }
|
||||
io_error::address_in_use => { trap; }
|
||||
io_error::address_unavailable => { trap; }
|
||||
io_error::timed_out => { trap; }
|
||||
io_error::other => { trap; }
|
||||
}
|
||||
let e2 = v2();
|
||||
match e2 {
|
||||
io_error::not_found => { trap; }
|
||||
io_error::permission_denied => { trap; }
|
||||
io_error::already_exists => {}
|
||||
io_error::invalid_input => { trap; }
|
||||
io_error::no_space => { trap; }
|
||||
io_error::broken_pipe => { trap; }
|
||||
io_error::connection_refused => { trap; }
|
||||
io_error::connection_reset => { trap; }
|
||||
io_error::address_in_use => { trap; }
|
||||
io_error::address_unavailable => { trap; }
|
||||
io_error::timed_out => { trap; }
|
||||
io_error::other => { trap; }
|
||||
}
|
||||
let e3 = v3();
|
||||
match e3 {
|
||||
io_error::not_found => { trap; }
|
||||
io_error::permission_denied => { trap; }
|
||||
io_error::already_exists => { trap; }
|
||||
io_error::invalid_input => {}
|
||||
io_error::no_space => { trap; }
|
||||
io_error::broken_pipe => { trap; }
|
||||
io_error::connection_refused => { trap; }
|
||||
io_error::connection_reset => { trap; }
|
||||
io_error::address_in_use => { trap; }
|
||||
io_error::address_unavailable => { trap; }
|
||||
io_error::timed_out => { trap; }
|
||||
io_error::other => { trap; }
|
||||
}
|
||||
let e4 = v4();
|
||||
match e4 {
|
||||
io_error::not_found => { trap; }
|
||||
io_error::permission_denied => { trap; }
|
||||
io_error::already_exists => { trap; }
|
||||
io_error::invalid_input => { trap; }
|
||||
io_error::no_space => {}
|
||||
io_error::broken_pipe => { trap; }
|
||||
io_error::connection_refused => { trap; }
|
||||
io_error::connection_reset => { trap; }
|
||||
io_error::address_in_use => { trap; }
|
||||
io_error::address_unavailable => { trap; }
|
||||
io_error::timed_out => { trap; }
|
||||
io_error::other => { trap; }
|
||||
}
|
||||
let e5 = v5();
|
||||
match e5 {
|
||||
io_error::not_found => { trap; }
|
||||
io_error::permission_denied => { trap; }
|
||||
io_error::already_exists => { trap; }
|
||||
io_error::invalid_input => { trap; }
|
||||
io_error::no_space => { trap; }
|
||||
io_error::broken_pipe => {}
|
||||
io_error::connection_refused => { trap; }
|
||||
io_error::connection_reset => { trap; }
|
||||
io_error::address_in_use => { trap; }
|
||||
io_error::address_unavailable => { trap; }
|
||||
io_error::timed_out => { trap; }
|
||||
io_error::other => { trap; }
|
||||
}
|
||||
let e6 = v6();
|
||||
match e6 {
|
||||
io_error::not_found => { trap; }
|
||||
io_error::permission_denied => { trap; }
|
||||
io_error::already_exists => { trap; }
|
||||
io_error::invalid_input => { trap; }
|
||||
io_error::no_space => { trap; }
|
||||
io_error::broken_pipe => { trap; }
|
||||
io_error::connection_refused => {}
|
||||
io_error::connection_reset => { trap; }
|
||||
io_error::address_in_use => { trap; }
|
||||
io_error::address_unavailable => { trap; }
|
||||
io_error::timed_out => { trap; }
|
||||
io_error::other => { trap; }
|
||||
}
|
||||
let e7 = v7();
|
||||
match e7 {
|
||||
io_error::not_found => { trap; }
|
||||
io_error::permission_denied => { trap; }
|
||||
io_error::already_exists => { trap; }
|
||||
io_error::invalid_input => { trap; }
|
||||
io_error::no_space => { trap; }
|
||||
io_error::broken_pipe => { trap; }
|
||||
io_error::connection_refused => { trap; }
|
||||
io_error::connection_reset => {}
|
||||
io_error::address_in_use => { trap; }
|
||||
io_error::address_unavailable => { trap; }
|
||||
io_error::timed_out => { trap; }
|
||||
io_error::other => { trap; }
|
||||
}
|
||||
let e8 = v8();
|
||||
match e8 {
|
||||
io_error::not_found => { trap; }
|
||||
io_error::permission_denied => { trap; }
|
||||
io_error::already_exists => { trap; }
|
||||
io_error::invalid_input => { trap; }
|
||||
io_error::no_space => { trap; }
|
||||
io_error::broken_pipe => { trap; }
|
||||
io_error::connection_refused => { trap; }
|
||||
io_error::connection_reset => { trap; }
|
||||
io_error::address_in_use => {}
|
||||
io_error::address_unavailable => { trap; }
|
||||
io_error::timed_out => { trap; }
|
||||
io_error::other => { trap; }
|
||||
}
|
||||
let e9 = v9();
|
||||
match e9 {
|
||||
io_error::not_found => { trap; }
|
||||
io_error::permission_denied => { trap; }
|
||||
io_error::already_exists => { trap; }
|
||||
io_error::invalid_input => { trap; }
|
||||
io_error::no_space => { trap; }
|
||||
io_error::broken_pipe => { trap; }
|
||||
io_error::connection_refused => { trap; }
|
||||
io_error::connection_reset => { trap; }
|
||||
io_error::address_in_use => { trap; }
|
||||
io_error::address_unavailable => {}
|
||||
io_error::timed_out => { trap; }
|
||||
io_error::other => { trap; }
|
||||
}
|
||||
let e10 = v10();
|
||||
match e10 {
|
||||
io_error::not_found => { trap; }
|
||||
io_error::permission_denied => { trap; }
|
||||
io_error::already_exists => { trap; }
|
||||
io_error::invalid_input => { trap; }
|
||||
io_error::no_space => { trap; }
|
||||
io_error::broken_pipe => { trap; }
|
||||
io_error::connection_refused => { trap; }
|
||||
io_error::connection_reset => { trap; }
|
||||
io_error::address_in_use => { trap; }
|
||||
io_error::address_unavailable => { trap; }
|
||||
io_error::timed_out => {}
|
||||
io_error::other => { trap; }
|
||||
}
|
||||
let e11 = v11();
|
||||
match e11 {
|
||||
io_error::not_found => { trap; }
|
||||
io_error::permission_denied => { trap; }
|
||||
io_error::already_exists => { trap; }
|
||||
io_error::invalid_input => { trap; }
|
||||
io_error::no_space => { trap; }
|
||||
io_error::broken_pipe => { trap; }
|
||||
io_error::connection_refused => { trap; }
|
||||
io_error::connection_reset => { trap; }
|
||||
io_error::address_in_use => { trap; }
|
||||
io_error::address_unavailable => { trap; }
|
||||
io_error::timed_out => { trap; }
|
||||
io_error::other => {}
|
||||
}
|
||||
return 42u64;
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
// expect: reject E7005
|
||||
// phase: 6
|
||||
// spec: docs/spec/05-effects-and-capabilities.md §14
|
||||
module test.errors
|
||||
fn main() -> cap<mem.alloc> effects [] { return alloc_error::budget_exceeded; }
|
||||
@@ -0,0 +1,5 @@
|
||||
// expect: reject E7005
|
||||
// phase: 6
|
||||
// spec: docs/spec/05-effects-and-capabilities.md §14
|
||||
module test.errors
|
||||
fn main() -> alloc_error effects [] { return alloc_error::budget_exceeded(1u64); }
|
||||
@@ -0,0 +1,5 @@
|
||||
// expect: reject E7002
|
||||
// phase: 6
|
||||
// spec: docs/spec/05-effects-and-capabilities.md §14
|
||||
module test.errors
|
||||
fn main() -> alloc_error effects [] { return alloc_error::invalid; }
|
||||
@@ -0,0 +1,5 @@
|
||||
// expect: reject E7005
|
||||
// phase: 6
|
||||
// spec: docs/spec/05-effects-and-capabilities.md §14
|
||||
module test.errors
|
||||
fn main() -> io_error effects [] { return alloc_error::budget_exceeded; }
|
||||
@@ -0,0 +1,46 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Explicit error values: canonical AIR, pure audit and hosted execution."""
|
||||
import json
|
||||
from pathlib import Path
|
||||
import subprocess
|
||||
import tempfile
|
||||
|
||||
REPO = Path(__file__).resolve().parents[2]
|
||||
AJI = REPO / "zig-out/bin/aji"
|
||||
FIXTURES = REPO / "tests/conformance/source/error_values"
|
||||
checks = 0
|
||||
|
||||
|
||||
def cli(*args, code=0):
|
||||
global checks
|
||||
result = subprocess.run([str(AJI), *map(str, args)], capture_output=True, text=True)
|
||||
assert result.returncode == code, (args, result.returncode, result.stdout, result.stderr)
|
||||
checks += 1
|
||||
return result.stdout
|
||||
|
||||
|
||||
features = json.loads(cli("features"))
|
||||
assert "error_values" in features["features"], features
|
||||
with tempfile.TemporaryDirectory(prefix="aji-errors-") as directory:
|
||||
root = Path(directory)
|
||||
air = root / "result.air"
|
||||
for family in ("alloc_error", "cap_error", "io_error"):
|
||||
source = FIXTURES / f"{family}.aji"
|
||||
canonical = cli("lower", source)
|
||||
assert cli("lower", source) == canonical
|
||||
assert "error_values" in canonical and f"error.const {family}." in canonical
|
||||
air.write_text(canonical)
|
||||
cli("check", air)
|
||||
assert cli("run", source) == cli("run", air)
|
||||
report = json.loads(cli("report", source))
|
||||
assert report["entry"]["capabilities"] == [], report
|
||||
assert all(function["allocations"] == 0 for function in report["functions"]), report
|
||||
result_file = root / "hosted.json"
|
||||
assert cli("run", "--profile", "hosted", "--result-file", result_file, source) == ""
|
||||
record = json.loads(result_file.read_text())
|
||||
assert record["status"] == "returned" and record["value"] == 42, record
|
||||
for name, code in (("no_authority", "E7005"), ("payload", "E7005"),
|
||||
("unknown_variant", "E7002"), ("wrong_family", "E7005")):
|
||||
diagnostics = cli("check", FIXTURES / f"{name}.aji", code=1)
|
||||
assert [json.loads(line)["code"] for line in diagnostics.splitlines()] == [code]
|
||||
print(f"source errors: {checks} checks passed")
|
||||
@@ -20,6 +20,7 @@ TESTS = os.path.join(ROOT, "tests", "conformance", "source")
|
||||
|
||||
# (area, construct, regex, needs a reject test)
|
||||
CHECKLIST = [
|
||||
("failure", "explicit built-in error value", r"(?:return|=) (alloc_error|io_error|cap_error)::", True),
|
||||
("hash", "pure FNV/CRC byte hashing", r"std\.hash\.(fnv1a64|crc32c)\(", True),
|
||||
("hash", "continuation from explicit state", r"std\.hash\.(fnv1a64_extend|crc32c_extend)\(", True),
|
||||
("deque", "bounded owning ring ends", r"std\.deque\.(push_front|push_back|pop_front|pop_back)", True),
|
||||
|
||||
Reference in new issue
Block a user