cli: validate exclusive source places before AIR

This commit is contained in:
dev committed 2026-10-08 12:37:22 +03:00
1 parent dcd5f1f702
commit 41f3afbf05
54 files changed
+541 -2

No files matched your search

+1
View File
@@ -33,6 +33,7 @@ jobs:
- run: python3 tests/hosted/source_array_literals.py
- run: python3 tests/hosted/source_inferred_content.py
- run: python3 tests/hosted/source_nonvalue_operands.py
- run: python3 tests/hosted/source_store_modes.py
- run: python3 tests/hosted/examples.py --stress 200
- run: python3 tests/hosted/source_static_functions.py
- run: python3 tests/hosted/source_linear.py
+1
View File
@@ -29,6 +29,7 @@ python3 tests/hosted/source_own_array_content.py # Copy content, nominal/generic
python3 tests/hosted/source_array_literals.py # inferred elements, repetitions, explicit moves and packages
python3 tests/hosted/source_inferred_content.py # constructor content, transfers, recovery and package provenance
python3 tests/hosted/source_nonvalue_operands.py # never operands, divergent receivers, traps and package provenance
python3 tests/hosted/source_store_modes.py # exclusive places, source positions, array modes and package provenance
python3 tests/hosted/examples.py --stress 200 # all examples/packages, wire bytes, partial I/O and source/AIR schedules
python3 tests/hosted/source_static_functions.py # explicit operations, effects, nominal policy identity and workers
python3 tests/hosted/source_linear.py # whole-value owners, canonical AIR and CLI errors
+2
View File
@@ -164,3 +164,5 @@ Q-045 inferred array literals (cl100k): Quick Reference stays 8,948; other sourc
Q-046 inferred constructor content (cl100k): Quick Reference stays 8,948; other source docs 66,906 → 67,074 (+168), full source 75,854 → 76,022 (+0.22%). o200k groups are 8,984 / 67,279 / 76,263; Qwen2.5 9,226 / 68,724 / 77,950. AIR remains 184,628 bytes / 47,445 cl100k tokens; library/std.md is unchanged. Existing value/Copy/borrow restrictions now reject at constructor arguments, including generic templates, before malformed AIR or null-block failure. Complete linear packing and recovery remain valid; graph/endpoint feature diagnostics keep their existing precedence. No new concept, syntax, code/reason, feature or verifier/runtime state is added. Accepted canonical AIR is preserved; invalid-source diagnostics may change. No empirical MCC benefit is claimed.
Q-047 non-value operands (cl100k): Quick Reference stays 8,948; other source docs 67,074 → 67,258 (+184), full source 76,022 → 76,206 (+0.24%). o200k groups are 8,984 / 67,461 / 76,445; Qwen2.5 9,226 / 68,912 / 78,138. AIR remains 184,628 bytes / 47,445 cl100k tokens; library/std.md is unchanged. Typed operation inputs reject never before subsequent emission; divergent returns/branches/statements and variable receivers retain their control flow, and frame receivers skip writes. Store values remain required. Invalid-source diagnostic order may change; accepted canonical AIR is preserved. No new concept, syntax, code/reason, feature or verifier/runtime state is added. No empirical MCC benefit is claimed.
Q-048 source access modes (cl100k): Quick Reference stays 8,948; other source docs 67,258 → 67,343 (+85), full source 76,206 → 76,291 (+0.11%). o200k groups are 8,984 / 67,546 / 76,530; Qwen2.5 9,226 / 69,001 / 78,227. AIR remains 184,628 bytes / 47,445 cl100k tokens; library/std.md is unchanged. Shared places reject at source stores and explicit mut bindings/arguments before array shape conversion; valid canonical AIR is preserved. No new concept, syntax, code/reason, feature or verifier/runtime state is added. No empirical MCC benefit is claimed.
+6
View File
@@ -354,3 +354,9 @@ Audit non-value operands in ordinary calls, operators and aggregates against spe
Q-047 review independently reproduced `fn update(p: ref<u64>) -> void effects [] { *p = 1u64; }`: it reaches AIR E1001 (`expected: mut<T>`, `found: ref<u64>`) instead of frontend E7xxx, although the mode is determined by source types (spec 15 §10). Review also observed accepted writes to fields of a `let` aggregate. An immutable binding does not necessarily imply immutable owned content; whether that behavior disagrees with the specification remains unverified and MUST NOT be guessed from the binding's `mutable` flag.
Audit existing place modes and referents against spec 15 §4/§5 and AIR borrow/store rules. Reject fully type-decided incompatibilities at the source, preserving ownership, aliasing/lifetime diagnostics, evaluation order and canonical AIR for valid source. Determine the binding/content distinction from governing text before proposing any semantic restriction.
**Resolved (spec 15 §10):** stores require `mut<T>`; explicit `mut` bindings and arguments require an exclusive place before array-to-slice conversion. Shared places reject E7005 with expected/found borrow types at the place or argument. This also prevents accepted `mut` bindings from silently retaining shared field/index/range modes, or requesting an exclusive slice from a shared array. Dereferenced slices are not scalar store places. Whole-array stores retain E7006; RHS evaluation, valid canonical AIR, ownership/projection/alias/lifetime rules and runtime semantics remain unchanged. Q-049 separates the unresolved Copy-let storage policy.
## Q-049 — Immutable Copy bindings and mutable frame content
Read-only Q-048 review reproduced accepted `let p = Pair { a: 0u64 }; p.a = 42u64; return p.a;`, and exclusive borrows of Copy-let frame storage. Spec 15 §4 calls `let` an immutable binding; assignment by name already rejects. Whether this should also prohibit content writes through fields/indices/borrows is unresolved. Owned content through `let` intentionally remains mutable and is used by public std examples. Do not infer a new restriction from `Binding.mutable`: distinguish Copy frame storage from owners, regions, resources and borrow bindings, and settle the normative policy before changing accepted source. This question is independent of Q-048's existing shared/exclusive borrow modes.
+8
View File
@@ -277,3 +277,11 @@ Q-047/spec 15 §10 distinguishes typed operation inputs from divergent control f
The preserved invariants are that no missing value becomes an operand or store and no write/borrow is emitted after control flow terminates. AIR independently retains its type, ownership, lifetime and effect/capability rules. Runtime cleanup, abnormal-path resource handling, deterministic execution and accepted-source canonical AIR are unchanged. Positive cases cover variable receivers in SSA/frame storage, normal/trapping branch paths, ownership and returned views; rejected cases cover non-value inputs and malformed place targets. Former compiler aborts now produce a diagnostic or valid variable-receiver divergence. Invalid diagnostic order may change, including place RHS errors before unknown targets. No new syntax, diagnostic code/reason, feature, verifier/runtime state or evaluation input changes. Source bundle growth is recorded in docs/agent/README.md. P6-2 remains open; Q-048 is the separately reproduced store-mode gap, and Q-044 remains an implementation-cost audit.
Validation: full conformance passes 1,497/1,497; unit tests and Zig formatting pass. The 75 new fixtures pass with collection before every instruction and the CLI suite passes 635 checks, including exact positions/fields across diagnostic commands, source/AIR/hosted trap/return parity, formatting and imported generic reachability. Existing constructor/literal/owning-array/linear CLI suites pass 302/152/119/49 checks. Final comparison on 1,136 preceding public sources (659 successful old lowerings) has zero changed check or canonical lower outputs. The formatter corpus passes 5,098 checks on the first 1,200 public files (1,192 parsed, eight existing syntax errors, 676 identical AIR outputs); CLI formatting covers all 75 new fixtures, including the eleven later additions. Source coverage remains 121/121.
## P6-2 source place access modes (2026-10-08)
Q-048/spec 15 §10 rejects stores through shared borrows, and checks explicit `mut` bindings/arguments before whole-array-to-slice conversion. Scalar stores require exactly `mut<T>`; dereferenced slices reject. E7005 carries expected/found borrow types at the place (binding/store) or argument. Existing whole-array E7006 and RHS evaluation order remain. Valid exclusive stores, shared reborrows, projections, array/slice views and owned content use unchanged AIR. Previously accepted `mut` bindings through shared fields/indices/ranges now reject; invalid argument diagnostic details can change.
The preserved invariant is that explicit `mut` cannot create exclusive access from shared access. AIR independently enforces borrow/store modes, aliases, lifetimes and linear content/projection restrictions. No runtime state, allocation, cleanup, deterministic interpreter behavior, syntax, code/reason, feature or evaluation input changes. Positive source/AIR runs exercise scalar/field/index/range writes and owning content; rejected cases cover shared modes and retained verifier rules. Q-049 records the separate unresolved Copy-let frame-content policy. Bundle growth is recorded in docs/agent/README.md; P6-2 remains open.
Validation: full conformance passes 1,543/1,543; units and Zig formatting pass. The 46 new fixtures pass with collection before every instruction; CLI integration passes 348 checks, including exact fields/positions, diagnostic commands, formatting, imported generic templates and source/AIR/hosted execution. Existing array-argument/non-value/linear/borrow-return CLI suites pass 85/635/49/133 checks. Comparison on 1,211 preceding public sources (673 successful old lowerings) changes only the message of two rejected inputs; accepted AIR is byte-identical. All eleven new runtime fixtures also retain the previous compiler's AIR. The formatter corpus passes 5,289 checks on 1,257 public files (1,249 parsed, eight existing syntax errors, 686 identical AIR outputs); coverage remains 121/121. Independent read-only review by Claude approved the access-mode checks and confirmed the two diagnostic-message changes.
+1
View File
@@ -400,6 +400,7 @@ A byte string or array used where a `slice<u8>` is expected is materialized in f
- arity and argument types of functions and built-in operations;
- the existence of fields and variants;
- `ref`/`mut` compatibility;
- A place store MUST require `mut<T>`; a `mut` binding or argument MUST require an exclusive place before whole-array-to-slice conversion. A shared place rejects E7005 at the place (binding/store) or argument with `expected`/`found` borrow types. The RHS is still evaluated first; whole-array stores retain E7006. Content linearity and alias/lifetime checks remain verifier rules.
- lossless widening by `as` (the verifier's own rule);
- slice element types of `slice.copy`;
- the element rules of channels and mutexes (no borrow, scope, task, shield or guard).
+17 -2
View File
@@ -1230,7 +1230,11 @@ const Lowerer = struct {
if (x.place.kind == .call) if (pathOf(x.place.kind.call.callee.*)) |p| if (std.mem.eql(u8, p, "buffer.view")) return l.bufferView(s, x.name, x.mutable, x.place);
if (x.place.kind == .call) if (pathOf(x.place.kind.call.callee.*)) |p| if (std.mem.endsWith(u8, p, ".at"))
return l.graphAccess(s, x.name, x.mutable, x.place);
const p = try l.uncheckedPlace(x.place, x.mutable, x.unchecked, true, s.span);
// Check the original mode before a whole array becomes a
// slice: shape conversion must not upgrade shared access.
var p = try l.uncheckedPlace(x.place, x.mutable, x.unchecked, false, s.span);
if (x.mutable) try l.requireExclusivePlace(x.place.span, p.v.ty);
p = try l.borrowShape(p, x.mutable, true, s.span);
if (p.ends.len == 0) {
// Borrowing a borrow binding: a reborrow, ended at scope end.
const rb = try l.defBorrow(x.name, p.v.ty, try l.fmt("{s} %{s}", .{ if (x.mutable) "borrow.mut" else "borrow.ref", p.v.name }), p.v.name, s.span);
@@ -2262,7 +2266,9 @@ const Lowerer = struct {
// Only a known array-borrow parameter preserves the array
// (spec 15 §5). Bindings and all other arguments keep slices.
const want_slice = if (want) |t| !(t == .ref or t == .mut) or t.inner().? != .array else true;
const p = try l.uncheckedPlace(x.place.*, x.mutable, x.unchecked, want_slice, a.span);
var p = try l.uncheckedPlace(x.place.*, x.mutable, x.unchecked, false, a.span);
if (x.mutable) try l.requireExclusivePlace(a.span, p.v.ty);
p = try l.borrowShape(p, x.mutable, want_slice, a.span);
if (p.ends.len == 0) {
const rb = try l.defBorrow("b", p.v.ty, try l.fmt("{s} %{s}", .{ if (x.mutable) "borrow.mut" else "borrow.ref", p.v.name }), p.v.name, a.span);
try temps.append(l.arena, rb.name);
@@ -2332,6 +2338,14 @@ const Lowerer = struct {
return if (want_slice) l.borrowShape(p, mutable, true, span) else p;
}
/// Explicit `mut` cannot upgrade shared access, including before array
/// shape conversion. Aliasing and lifetime legality stay with AIR.
fn requireExclusivePlace(l: *Lowerer, span: Span, t: Ty) Error!void {
if (t.isMutBorrow()) return;
const expected = if (t == .slice) try l.wrap(.slice_mut, t.inner().?) else try l.wrap(.mut, t.inner().?);
return l.fail("E7005", span, "mut needs an exclusive place", &.{ .{ .name = "expected", .value = l.diagnosticTyText(expected) }, .{ .name = "found", .value = l.diagnosticTyText(t) } });
}
/// Explicit argument/binding modes apply independently of array shape.
/// In particular, `ref` through `mut<[T; N]>` still reborrows shared.
fn borrowShape(l: *Lowerer, raw: Placed, mutable: bool, want_slice: bool, span: Span) Error!Placed {
@@ -2469,6 +2483,7 @@ const Lowerer = struct {
const p = try l.placeBorrow(place, true, false, span);
const in = p.v.ty.inner() orelse return l.fail("E7005", place.span, "assignment needs a mutable place", &.{});
if (in == .array and place.kind == .deref) return l.invalid(.invalid_assignment, place.span, "write array elements with `s[i] = …`", &.{});
if (p.v.ty != .mut) return l.fail("E7005", place.span, "assignment needs a mutable place", &.{ .{ .name = "expected", .value = l.diagnosticTyText(try l.wrap(.mut, in)) }, .{ .name = "found", .value = l.diagnosticTyText(p.v.ty) } });
try l.checkOperandTy(value.span, v.ty, in);
try l.emit(try l.fmt("store %{s}, %{s}", .{ p.v.name, v.name }), span);
try l.endNow(p.ends, span);
@@ -0,0 +1,9 @@
// expect: reject E2101
// phase: 6
// spec: docs/spec/15-source-language.md §10
module test.store_modes
struct Pair { a: u64 }
struct Box { pair: Pair, data: [u64; 2] }
fn stop() -> never effects [] { trap; }
fn unused(p: mut<Pair>) -> void effects [] { ref q = p; p.a = 42u64; assert(q.a == 42u64); }
fn main() -> u64 effects [] { return 42; }
@@ -0,0 +1,9 @@
// expect: reject E7005
// phase: 6
// spec: docs/spec/15-source-language.md §10
module test.store_modes
struct Pair { a: u64 }
struct Box { pair: Pair, data: [u64; 2] }
fn stop() -> never effects [] { trap; }
fn unused(s: slice<u64>) -> void effects [] { s[true] = 1u64; }
fn main() -> u64 effects [] { return 42; }
@@ -0,0 +1,8 @@
// expect: run
// result: 42
// phase: 6
// spec: docs/spec/15-source-language.md §5/§10
module test.store_modes
struct Pair { a: u64 }
struct Box { pair: Pair, data: [u64; 2] }
fn main() -> u64 effects [] { let x = Pair { a: 0u64 }; x.a = 42u64; return x.a; }
@@ -0,0 +1,9 @@
// expect: reject E1001
// phase: 6
// spec: docs/spec/15-source-language.md §10
module test.store_modes
struct Pair { a: u64 }
struct Box { pair: Pair, data: [u64; 2] }
fn stop() -> never effects [] { trap; }
fn unused(p: mut<own<u64>>, v: own<u64>) -> void effects [] { *p = move v; }
fn main() -> u64 effects [] { return 42; }
@@ -0,0 +1,8 @@
// expect: reject E7005
// phase: 6
// spec: docs/spec/15-source-language.md §10
module test.store_modes
struct Data { a: [u64; 2] }
fn write(s: slice_mut<u64>) -> void effects [] { s[0] = 42u64; }
fn unused(p: ref<[u64; 2]>) -> void effects [] { write(mut p); }
fn main() -> u64 effects [] { return 42; }
@@ -0,0 +1,9 @@
// expect: run
// result: 42
// phase: 6
// spec: docs/spec/15-source-language.md §5/§10
module test.store_modes
struct Data { a: [u64; 2] }
fn write(s: slice_mut<u64>) -> void effects [] { s[0] = 42u64; }
fn update(p: mut<[u64; 2]>) -> void effects [] { write(mut p); }
fn main() -> u64 effects [] { var x: [u64; 2] = [0u64; 2]; update(mut x); return x[0]; }
@@ -0,0 +1,9 @@
// expect: run
// result: 42
// phase: 6
// spec: docs/spec/15-source-language.md §5/§10
module test.store_modes
struct Pair { a: u64 }
struct Box { pair: Pair, data: [u64; 2] }
fn update(p: mut<[u64; 2]>) -> void effects [] { mut q = p; q[0] = 42u64; }
fn main() -> u64 effects [] { var x: [u64; 2] = [0u64; 2]; update(mut x); return x[0]; }
@@ -0,0 +1,8 @@
// expect: reject E7005
// phase: 6
// spec: docs/spec/15-source-language.md §10
module test.store_modes
struct Pair { a: u64 }
struct Box { pair: Pair, data: [u64; 2] }
fn unused(p: ref<[u64; 2]>) -> void effects [] { mut q = *p; }
fn main() -> u64 effects [] { return 42; }
@@ -0,0 +1,8 @@
// expect: reject E7005
// phase: 6
// spec: docs/spec/15-source-language.md §10
module test.store_modes
struct Pair { a: u64 }
struct Box { pair: Pair, data: [u64; 2] }
fn unused(p: ref<Box>) -> void effects [] { mut q = p.data; }
fn main() -> u64 effects [] { return 42; }
@@ -0,0 +1,8 @@
// expect: reject E7005
// phase: 6
// spec: docs/spec/15-source-language.md §10
module test.store_modes
struct Pair { a: u64 }
struct Box { pair: Pair, data: [u64; 2] }
fn unused(p: ref<u64>) -> void effects [] { mut q = p; }
fn main() -> u64 effects [] { return 42; }
@@ -0,0 +1,9 @@
// expect: run
// result: 42
// phase: 6
// spec: docs/spec/15-source-language.md §5/§10
module test.store_modes
struct Pair { a: u64 }
struct Box { pair: Pair, data: [u64; 2] }
fn update(p: mut<u64>) -> void effects [] { *p = 42u64; }
fn main() -> u64 effects [] { var x: u64 = 0u64; update(mut x); return x; }
@@ -0,0 +1,9 @@
// expect: run
// result: 42
// phase: 6
// spec: docs/spec/15-source-language.md §5/§10
module test.store_modes
struct Pair { a: u64 }
struct Box { pair: Pair, data: [u64; 2] }
fn update(p: mut<Pair>) -> void effects [] { p.a = 42u64; }
fn main() -> u64 effects [] { var x: Pair = Pair { a: 0u64 }; update(mut x); return x.a; }
@@ -0,0 +1,8 @@
// expect: reject E7005
// phase: 6
// spec: docs/spec/15-source-language.md §10
module test.store_modes
struct Data { a: [u64; 2] }
fn write(s: slice_mut<u64>) -> void effects [] { s[0] = 42u64; }
fn unused(p: ref<Data>) -> void effects [] { write(mut p.a); }
fn main() -> u64 effects [] { return 42; }
@@ -0,0 +1,8 @@
// expect: reject E7005
// phase: 6
// spec: docs/spec/15-source-language.md §10
module test.store_modes
struct Pair { a: u64 }
struct Box { pair: Pair, data: [u64; 2] }
fn unused(p: ref<Pair>) -> void effects [] { mut q = p.a; }
fn main() -> u64 effects [] { return 42; }
@@ -0,0 +1,8 @@
// expect: reject E7005
// phase: 6
// spec: docs/spec/15-source-language.md §10
module test.store_modes
struct Pair { a: u64 }
struct Box { pair: Pair, data: [u64; 2] }
fn unused(s: slice<u64>) -> void effects [] { mut q = s[0]; }
fn main() -> u64 effects [] { return 42; }
@@ -0,0 +1,9 @@
// expect: run
// result: 42
// phase: 6
// spec: docs/spec/15-source-language.md §5/§10
module test.store_modes
struct Pair { a: u64 }
struct Box { pair: Pair, data: [u64; 2] }
fn update(p: mut<Box>) -> void effects [] { { mut q = p.pair.a; *q = 21u64; } { mut q = p.data; q[0] = 21u64; } }
fn main() -> u64 effects [] { var x: Box = Box { pair: Pair { a: 0u64 }, data: [0u64; 2] }; update(mut x); return x.pair.a + x.data[0]; }
@@ -0,0 +1,9 @@
// expect: run
// result: 42
// phase: 6
// spec: docs/spec/15-source-language.md §5/§10
module test.store_modes
struct Pair { a: u64 }
struct Box { pair: Pair, data: [u64; 2] }
fn update(p: slice_mut<u64>) -> void effects [] { mut q = p[0..1]; q[0] = 42u64; }
fn main() -> u64 effects [] { var x: [u64; 2] = [0u64; 2]; update(mut x); return x[0]; }
@@ -0,0 +1,8 @@
// expect: reject E7005
// phase: 6
// spec: docs/spec/15-source-language.md §10
module test.store_modes
struct Data { a: [u64; 2] }
fn write(s: slice_mut<u64>) -> void effects [] { s[0] = 42u64; }
fn unused(p: slice<u64>) -> void effects [] { write(mut p[0..1]); }
fn main() -> u64 effects [] { return 42; }
@@ -0,0 +1,8 @@
// expect: reject E7005
// phase: 6
// spec: docs/spec/15-source-language.md §10
module test.store_modes
struct Pair { a: u64 }
struct Box { pair: Pair, data: [u64; 2] }
fn unused(s: slice<u64>) -> void effects [] { mut q = s[0..1]; }
fn main() -> u64 effects [] { return 42; }
@@ -0,0 +1,8 @@
// expect: reject E7005
// phase: 6
// spec: docs/spec/15-source-language.md §10
module test.store_modes
struct Data { a: [u64; 2] }
fn write(s: slice_mut<u64>) -> void effects [] { s[0] = 42u64; }
fn unused(p: ref<[u64; 2]>) -> void effects [] { mut q = p[0..1]; }
fn main() -> u64 effects [] { return 42; }
@@ -0,0 +1,9 @@
// expect: run
// result: 42
// phase: 6
// spec: docs/spec/15-source-language.md §5/§10
module test.store_modes
struct Pair { a: u64 }
struct Box { pair: Pair, data: [u64; 2] }
fn update(s: slice_mut<u64>) -> void effects [] { s[0] = 42u64; }
fn main() -> u64 effects [] { var x: [u64; 2] = [0u64; 2]; update(mut x); return x[0]; }
@@ -0,0 +1,8 @@
// expect: reject E7005
// phase: 6
// spec: docs/spec/15-source-language.md §10
module test.store_modes
struct Pair { a: u64 }
struct Box { pair: Pair, data: [u64; 2] }
fn unused(s: slice<u64>) -> void effects [] { mut q = s; }
fn main() -> u64 effects [] { return 42; }
@@ -0,0 +1,8 @@
// expect: reject E7005
// phase: 6
// spec: docs/spec/15-source-language.md §10
module test.store_modes
struct Pair { a: u64 }
struct Box { pair: Pair, data: [u64; 2] }
fn unused(s: slice_mut<u64>) -> void effects [] { *s = 1u64; }
fn main() -> u64 effects [] { return 42; }
@@ -0,0 +1,8 @@
// expect: reject E7005
// phase: 6
// spec: docs/spec/15-source-language.md §10
module test.store_modes
struct Data { a: [u64; 2] }
fn write(s: slice_mut<u64>) -> void effects [] { s[0] = 42u64; }
fn unused(p: slice<u64>) -> void effects [] { mut unchecked q = p[0]; }
fn main() -> u64 effects [] { return 42; }
@@ -0,0 +1,9 @@
// expect: run
// result: 42
// grant: heap=mem.alloc:128
// phase: 6
// spec: docs/spec/15-source-language.md §5/§10
module test.store_modes
struct Pair { a: u64 }
struct Box { pair: Pair, data: [u64; 2] }
fn main(heap: cap<mem.alloc>) -> u64 effects [mem.alloc] { let x = own.array(2usize, 0u64, heap) or trap; x[0] = 42u64; let n = x[0]; own.free(move x); return n; }
@@ -0,0 +1,9 @@
// expect: run
// result: 42
// grant: heap=mem.alloc:128
// phase: 6
// spec: docs/spec/15-source-language.md §5/§10
module test.store_modes
struct Pair { a: u64 }
struct Box { pair: Pair, data: [u64; 2] }
fn main(heap: cap<mem.alloc>) -> u64 effects [mem.alloc] { let x = own.new(0u64, heap) or trap; { mut p = x; *p = 42u64; } return own.take(move x); }
@@ -0,0 +1,9 @@
// expect: reject E7006
// phase: 6
// spec: docs/spec/15-source-language.md §10
module test.store_modes
struct Pair { a: u64 }
struct Box { pair: Pair, data: [u64; 2] }
fn stop() -> never effects [] { trap; }
fn unused(p: Pair) -> void effects [] { p.a = 42u64; }
fn main() -> u64 effects [] { return 42; }
@@ -0,0 +1,9 @@
// expect: reject E7005
// phase: 6
// spec: docs/spec/15-source-language.md §10
module test.store_modes
struct Pair { a: u64 }
struct Box { pair: Pair, data: [u64; 2] }
fn stop() -> never effects [] { trap; }
fn unused(p: ref<u64>) -> void effects [] { *p = stop(); }
fn main() -> u64 effects [] { return 42; }
@@ -0,0 +1,9 @@
// expect: reject E7005
// phase: 6
// spec: docs/spec/15-source-language.md §10
module test.store_modes
struct Pair { a: u64 }
struct Box { pair: Pair, data: [u64; 2] }
fn stop() -> never effects [] { trap; }
fn unused(p: ref<u64>) -> void effects [] { *p = true; }
fn main() -> u64 effects [] { return 42; }
@@ -0,0 +1,9 @@
// expect: reject E7002
// phase: 6
// spec: docs/spec/15-source-language.md §10
module test.store_modes
struct Pair { a: u64 }
struct Box { pair: Pair, data: [u64; 2] }
fn stop() -> never effects [] { trap; }
fn unused(p: ref<u64>) -> void effects [] { *p = missing; }
fn main() -> u64 effects [] { return 42; }
@@ -0,0 +1,8 @@
// expect: reject E7005
// phase: 6
// spec: docs/spec/15-source-language.md §10
module test.store_modes
struct Pair { a: u64 }
struct Box { pair: Pair, data: [u64; 2] }
fn unused(p: ref<[u64; 2]>) -> void effects [] { p[0] = 1u64; }
fn main() -> u64 effects [] { return 42; }
@@ -0,0 +1,8 @@
// expect: reject E7005
// phase: 6
// spec: docs/spec/15-source-language.md §10
module test.store_modes
struct Pair { a: u64 }
struct Box { pair: Pair, data: [u64; 2] }
fn unused(p: ref<Box>) -> void effects [] { p.data[0] = 1u64; }
fn main() -> u64 effects [] { return 42; }
@@ -0,0 +1,8 @@
// expect: reject E7005
// phase: 6
// spec: docs/spec/15-source-language.md §10
module test.store_modes
struct Pair { a: u64 }
struct Box { pair: Pair, data: [u64; 2] }
fn unused(p: ref<u64>) -> void effects [] { *p = 1u64; }
fn main() -> u64 effects [] { return 42; }
@@ -0,0 +1,8 @@
// expect: reject E7005
// phase: 6
// spec: docs/spec/15-source-language.md §10
module test.store_modes
struct Pair { a: u64 }
struct Box { pair: Pair, data: [u64; 2] }
fn unused(p: ref<Pair>) -> void effects [] { p.a = 1u64; }
fn main() -> u64 effects [] { return 42; }
@@ -0,0 +1,8 @@
// expect: reject E7005
// phase: 6
// spec: docs/spec/15-source-language.md §10
module test.store_modes
struct Pair { a: u64 }
struct Box { pair: Pair, data: [u64; 2] }
fn unused() -> void effects [] { var x: u64 = 0u64; ref p = x; *p = 1u64; }
fn main() -> u64 effects [] { return 42; }
@@ -0,0 +1,8 @@
// expect: reject E7005
// phase: 6
// spec: docs/spec/15-source-language.md §10
module test.store_modes
struct Pair { a: u64 }
struct Box { pair: Pair, data: [u64; 2] }
fn unused() -> void effects [] { var x: Pair = Pair { a: 0u64 }; ref p = x; p.a = 1u64; }
fn main() -> u64 effects [] { return 42; }
@@ -0,0 +1,8 @@
// expect: reject E7005
// phase: 6
// spec: docs/spec/15-source-language.md §10
module test.store_modes
struct Pair { a: u64 }
struct Box { pair: Pair, data: [u64; 2] }
fn unused() -> void effects [] { var x: [u64; 2] = [0u64; 2]; ref s = x; s[0] = 1u64; }
fn main() -> u64 effects [] { return 42; }
@@ -0,0 +1,6 @@
// expect: reject E7005
// phase: 6
// spec: docs/spec/15-source-language.md §10
module test.store_modes
fn unused(p: ref<[u64; 2]>) -> void effects [] { mut q = p; }
fn main() -> u64 effects [] { return 42; }
@@ -0,0 +1,8 @@
// expect: reject E7005
// phase: 6
// spec: docs/spec/15-source-language.md §10
module test.store_modes
struct Pair { a: u64 }
struct Box { pair: Pair, data: [u64; 2] }
fn unused(p: ref<Box>) -> void effects [] { p.pair.a = 1u64; }
fn main() -> u64 effects [] { return 42; }
@@ -0,0 +1,7 @@
// expect: reject E7005
// phase: 6
// spec: docs/spec/15-source-language.md §5/§10
module test.store_modes
struct Pair { a: u64 }
struct Box { pair: Pair, data: [u64; 2] }
fn main(heap: cap<mem.alloc>) -> u64 effects [mem.alloc] { let x = own.array(2usize, 0u64, heap) or trap; { ref s = x; s[0] = 1u64; } own.free(move x); return 42; }
@@ -0,0 +1,9 @@
// expect: run
// result: 42
// phase: 6
// spec: docs/spec/15-source-language.md §5/§10
module test.store_modes
struct Pair { a: u64 }
struct Box { pair: Pair, data: [u64; 2] }
fn read(p: mut<Box>) -> u64 effects [] { ref q = p.data; return q[0]; }
fn main() -> u64 effects [] { var x: Box = Box { pair: Pair { a: 0u64 }, data: [42u64; 2] }; return read(mut x); }
@@ -0,0 +1,8 @@
// expect: reject E7005
// phase: 6
// spec: docs/spec/15-source-language.md §10
module test.store_modes
struct Pair { a: u64 }
struct Box { pair: Pair, data: [u64; 2] }
fn unused(p: mut<Pair>) -> void effects [] { ref q = p; q.a = 1u64; }
fn main() -> u64 effects [] { return 42; }
@@ -0,0 +1,8 @@
// expect: reject E7005
// phase: 6
// spec: docs/spec/15-source-language.md §10
module test.store_modes
struct Pair { a: u64 }
struct Box { pair: Pair, data: [u64; 2] }
fn unused(s: slice<u64>) -> void effects [] { s[0] = 1u64; }
fn main() -> u64 effects [] { return 42; }
@@ -0,0 +1,8 @@
// expect: reject E7005
// phase: 6
// spec: docs/spec/15-source-language.md §10
module test.store_modes
struct Pair { a: u64 }
struct Box { pair: Pair, data: [u64; 2] }
fn unused(s: slice<u64>) -> void effects [] { *s = 1u64; }
fn main() -> u64 effects [] { return 42; }
@@ -0,0 +1,9 @@
// expect: reject E7002
// phase: 6
// spec: docs/spec/15-source-language.md §10
module test.store_modes
struct Pair { a: u64 }
struct Box { pair: Pair, data: [u64; 2] }
fn stop() -> never effects [] { trap; }
fn unused(p: ref<Pair>) -> void effects [] { p.missing = 1u64; }
fn main() -> u64 effects [] { return 42; }
@@ -0,0 +1,9 @@
// expect: reject E7006
// phase: 6
// spec: docs/spec/15-source-language.md §10
module test.store_modes
struct Pair { a: u64 }
struct Box { pair: Pair, data: [u64; 2] }
fn stop() -> never effects [] { trap; }
fn unused(p: ref<[u64; 2]>) -> void effects [] { *p = [1u64; 2]; }
fn main() -> u64 effects [] { return 42; }
+121
View File
@@ -0,0 +1,121 @@
#!/usr/bin/env python3
"""Source access modes, exact diagnostics and preserved execution/lowering."""
import json
from pathlib import Path
import subprocess
import tempfile
REPO = Path(__file__).resolve().parents[2]
AJI = REPO / 'zig-out/bin/aji'
CORPUS = REPO / 'tests/conformance/source/store_modes'
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
def reject(path, token, expected, found, *args, expected_file=None):
error = json.loads(cli('check', *args, code=1))
assert (error['code'], error['expected'], error['found']) == ('E7005', expected, found), error
lines = path.read_text().splitlines()
line = next(i for i, text in enumerate(lines, 1) if token in text and text.startswith('fn '))
assert (error['file'], error['line'], error['col']) == (
expected_file or str(path), line, lines[line - 1].rindex(token) + 1), error
assert 'air_line' not in error and 'air_col' not in error, error
for command in ('lower', 'run', 'report', 'explore'):
assert json.loads(cli(command, *args, code=1)) == error
for name, token in (
('shared_deref', '*p'), ('shared_field', 'p.a'),
('shared_nested_field', 'p.pair.a'), ('shared_slice', 's[0]'),
('shared_array', 'p[0]'), ('shared_array_field', 'p.data[0]'),
('shared_local', '*p'), ('shared_local_field', 'p.a'),
('shared_local_slice', 's[0]'), ('shared_reborrow', 'q.a'),
('shared_own_array', 's[0]'), ('mut_binding', 'p;'),
('mut_field_binding', 'p.a'), ('mut_index_binding', 's[0]'),
('mut_unchecked_binding', 'p[0]'),
):
path = CORPUS / f'{name}.aji'
reject(path, token, 'mut<u64>', 'ref<u64>', path)
for name, token in (
('shared_mut_array_binding', 'p;'), ('mut_array_deref_binding', '*p'),
('mut_array_field_binding', 'p.data'), ('mut_array_argument', 'mut p'),
('mut_field_argument', 'mut p.a'),
):
path = CORPUS / f'{name}.aji'
reject(path, token, 'mut<array<u64, 2>>', 'ref<array<u64, 2>>', path)
for name, token in (
('mut_slice_binding', 's;'), ('mut_range_binding', 's[0..1]'),
('mut_range_argument', 'mut p[0..1]'), ('mut_ref_array_range', 'p[0..1]'),
):
path = CORPUS / f'{name}.aji'
reject(path, token, 'slice_mut<u64>', 'slice<u64>', path)
for name, found in (('slice_deref', 'slice<u64>'), ('mut_slice_deref', 'slice_mut<u64>')):
path = CORPUS / f'{name}.aji'
reject(path, '*s', 'mut<u64>', found, path)
# RHS evaluation still precedes target borrowing; linear/alias restrictions
# remain AIR diagnostics when the place already has the required mode.
for name, code, fields in (
('rhs_never', 'E7005', {'expected': 'u64', 'found': 'never'}),
('rhs_unknown', 'E7002', {'name': 'missing'}),
('whole_array', 'E7006', {'reason': 'invalid_assignment'}),
('unknown_field', 'E7002', {'field': 'missing'}),
('bad_index', 'E7005', {'expected': 'usize', 'found': 'bool'}),
('linear_store', 'E1001', {}), ('alias_store', 'E2101', {}),
('parameter_place', 'E7006', {'reason': 'invalid_place'}),
):
error = json.loads(cli('check', CORPUS / f'{name}.aji', code=1))
assert error['code'] == code, error
assert all(error[key] == value for key, value in fields.items()), error
if code in ('E1001', 'E2101'):
assert 'air_line' in error, error
with tempfile.TemporaryDirectory(prefix='aji-store-modes-') as directory:
root = Path(directory)
for source in sorted(CORPUS.glob('*.aji')):
formatted = root / source.name
formatted.write_text(cli('fmt', source))
cli('fmt', '--check', formatted)
assert cli('fmt', formatted) == formatted.read_text()
if '// expect: run' not in source.read_text():
continue
air = cli('lower', source)
assert cli('lower', formatted) == air
raw = root / f'{source.stem}.air'
raw.write_text(air)
cli('check', raw)
grants = ['--grant', 'heap=mem.alloc:128'] if 'heap: cap' in source.read_text() else []
record = json.loads(cli('run', '--fuel', '100000', *grants, source))
assert record['value'] == 42, record
assert json.loads(cli('run', '--fuel', '100000', *grants, raw)) == record
result = root / f'{source.stem}.json'
cli('run', '--profile', 'hosted', '--fuel', '100000', *grants,
'--result-file', result, source)
assert json.loads(result.read_text())['value'] == 42
entry, library, manifest = (root / name for name in ('main.aji', 'places.aji', 'aji.toml'))
manifest.write_text('[package]\nname = "places"\nversion = "1"\nentry = "main.aji"\n'
'[modules]\nlib.places = "places.aji"\n')
entry.write_text('module app\nimport lib.places;\n'
'fn use(p: ref<u64>) -> void effects [] { lib.places.write::<u64>(p, 1u64); }\n'
'fn main() -> u64 effects [] { return 42; }\n')
library.write_text('module lib.places\n'
'fn write<T: Copy>(p: ref<T>, v: T) -> void effects [] { *p = v; }\n')
reject(library, '*p', 'mut<u64>', 'ref<u64>', '--manifest', manifest,
expected_file='places.aji')
library.write_text(library.read_text().replace('p: ref<T>', 'p: mut<T>'))
entry.write_text('module app\nimport lib.places;\n'
'fn main() -> u64 effects [] { var x: u64 = 0u64; lib.places.write::<u64>(mut x, 42u64); return x; }\n')
assert json.loads(cli('run', '--manifest', manifest))['value'] == 42
print(f'store modes CLI integration passed ({checks} checks)')