diff --git a/docs/design/open-questions.md b/docs/design/open-questions.md index ecf3a76..927c437 100644 --- a/docs/design/open-questions.md +++ b/docs/design/open-questions.md @@ -308,3 +308,9 @@ Independent E7006 review found that `module scope_move fn main()->u64 effects [] Clarify whether `let`/`move` aliases and rebinding of a scope name are permitted, how their availability relates to the originating block, and which existing diagnostic applies to rejected forms. Keep AIR scope identity, live-child/loan checks, deterministic completion and the no-escape invariant intact; add source acceptance/rejection tests for the chosen contract before changing lowering. This is separate from DD-017's reason field and RFC 0038's proposed repairs; neither adopts a new scope rule. **Resolved (DD-020, spec 15 §7):** a source scope name is a lexical identifier usable only as the named first operand of `task.spawn`. Ordinary `s`/`move s` value uses reject E7006 `invalid_expression`, with `name` and `type: scope`; borrowing remains E7006 `invalid_place`. Ordinary shadowing remains legal and never replaces the originating block's saved completion identity. Source/AIR tests retain existing live-child and loan rules, deterministic completion and function-local AIR scope flow. Previously accepted aliases and scope-valued expressions now reject; no new syntax, reason, code or verifier/runtime state is added. This resolves one P6-2 question, not the phase. + +## Q-042 — Source type-position validation before AIR verification + +Read-only P6-2 review reproduced `module nested fn f(p: ref>) -> void effects [] {} fn main() -> void effects [] {}` on the preceding compiler. It reaches the verifier and reports E1001 (`position: borrowed`, `found: ref`) at the source function. Spec 15 §10 requires errors fully decided by source types to be reported as E7xxx, and the AIR type-position rule already rejects this referent. This is an existing frontend validation gap, independent of dereferenced argument parsing; no new borrow semantics is proposed. + +Audit source type-position checks against the existing verifier rules, including unused generic signatures where a restriction is already known. Add valid/rejected source fixtures and report E7005 at the offending type annotation before generating invalid AIR; preserve the AIR rules and determine compatibility for existing source diagnostic consumers. The extent beyond this reproduced nested-borrow case is unverified. diff --git a/docs/plan/phase-6-source-language.md b/docs/plan/phase-6-source-language.md index 2404686..e2e5cad 100644 --- a/docs/plan/phase-6-source-language.md +++ b/docs/plan/phase-6-source-language.md @@ -227,3 +227,13 @@ Validation: unit tests, formatting checks and full conformance pass, 1,225/1,225 Q-023 is resolved in spec 15 §5 / DD-021. Explicit temporary arguments preserve the array for a known ref/mut array parameter, including specialized and imported calls; other arguments and borrow bindings retain slice behavior. Shared reborrows and child-first temporary cleanup use existing AIR primitives. Exact types/lengths/modes, aliasing, bounds, named retained origins and task loans remain authoritative. Local whole-array bindings cannot serve as array-borrow origins or task loans; forwarding an existing array-borrow parameter can. No syntax, instruction, code, feature, verifier/runtime state or frozen evaluation input changes. Source bundle growth is recorded in docs/agent/README.md; AIR is unchanged. P6-5 remains open. Validation: 32 new conformance cases pass with per-instruction collection and 200 scheduler seeds; full conformance is 1,257/1,257 and unit tests pass. CLI integration passes 85 checks, including deterministic/hosted and package provenance. Formatter checks 971 public files (963 parsed, eight existing syntax errors, 624 identical AIR outputs); source coverage is 121/121. Old/new CLI comparison over the 940 preceding public sources retains all diagnostic outputs and 606 successful canonical lowerings. Independent read-only Claude review passes. + +## P6-2 dereferenced argument places (2026-10-07) + +The existing grammar (`place = … | "*" unary`) and spec 15 §5 permit `ref *p` / `mut *p` as call arguments. The parser formerly rejected the leading `*`, although `ref (*p)` worked. Inline borrow parsing now admits that existing dereference production, preserving postfix binding and stopping before outer binary/cast operations. Other invalid prefix operands remain syntax errors. No normative rule, syntax form, diagnostic code, bundle, feature, verifier/runtime state or evaluation input changes; P6-2 remains open. + +The invariant is unchanged borrow mode, aliasing, provenance and child-first temporary cleanup through existing AIR verification. Runtime and deterministic interpreter semantics are unchanged. Positive source examples forward shared/mutable scalar, array and generic parameters; rejected examples retain E7005 for invalid referents/modes, E2101 for aliasing, and E7006 for misplaced/unchecked borrows. Former syntax errors for these grammatical places now reach those semantic checks; previously accepted source retains canonical AIR. Two parser regression groups failed before the fix and cover precedence, complete ranges, comments and CRLF. Formatter checks compare bare and parenthesized spellings and preserve canonical AIR. + +Read-only review also found a missing formatter space in grouped `ref (*p)` / `mut (*p)` arguments. The grouped-form regression failed before correcting the keyword spacing; both spellings now preserve tokens and canonical AIR. The separate existing source type-position gap is recorded as Q-042 without changing its semantics here. + +Validation: 12 new conformance tests pass with per-instruction collection; full conformance is 1,269/1,269 and unit tests 126/126. Formatter passes 4,357 checks over 983 public files (975 parsed, eight existing syntax errors, 631 identical AIR outputs); source coverage stays 121/121. Old/new CLI comparison over the 971 preceding public sources preserves all diagnostics and 624 successful canonical lowerings. Final independent Claude review passes. diff --git a/src/frontend/format.zig b/src/frontend/format.zig index 09a661f..3ba3b66 100644 --- a/src/frontend/format.zig +++ b/src/frontend/format.zig @@ -379,7 +379,7 @@ fn needsSpace(a: Token, b: Token, am: Mark, bm: Mark) bool { if (am.prefix) return word(a, "not"); if (a.tag == .dot_dot or b.tag == .dot_dot) return false; if (b.tag == .l_paren) { - if (a.tag == .ident) return word(a, "if") or word(a, "while") or word(a, "return") or word(a, "match") or word(a, "not") or word(a, "and") or word(a, "or") or word(a, "let") or word(a, "in"); + if (a.tag == .ident) return word(a, "if") or word(a, "while") or word(a, "return") or word(a, "match") or word(a, "not") or word(a, "and") or word(a, "or") or word(a, "let") or word(a, "in") or word(a, "ref") or word(a, "mut"); if (am.angle and (a.tag == .gt or a.tag == .shr)) return false; return a.tag != .r_paren and a.tag != .r_bracket and a.tag != .int and a.tag != .bytes; } diff --git a/src/frontend/parse.zig b/src/frontend/parse.zig index d130bc0..76a811b 100644 --- a/src/frontend/parse.zig +++ b/src/frontend/parse.zig @@ -736,7 +736,9 @@ const Parser = struct { _ = p.next(); unchecked = true; } - const place = try p.postfix(); + // The grammar's place production also permits `* unary`. + // Keep other prefix forms out of an inline borrow operand. + const place = if (p.peek().tag == .star) try p.unary() else try p.postfix(); return .{ .span = p.span(start), .kind = .{ .borrow = .{ .mutable = isWord(t, "mut"), .unchecked = unchecked, .place = try p.box(ast.Expr, place) } } }; } if (isWord(t, "move")) { @@ -1193,3 +1195,48 @@ test "each half of a nested type closer contributes to its type range" { try expectSourceRange(source, box_type.kind.applied.args[0].span, "ref>"); try expectSourceRange(source, box_type.kind.applied.args[0].kind.wrapped.inner.span, "own"); } + +test "inline borrows accept dereferenced places with postfix operands" { + var arena: std.heap.ArenaAllocator = .init(std.testing.allocator); + defer arena.deinit(); + const a = arena.allocator(); + for ([_][]const u8{ + "ref *value", "mut *value", "ref **value", "ref *value[index]", + "mut *(value).field", "ref *make(value)", + "ref // UTF-8: λ\n *value", + }) |fragment| { + for ([_][]const u8{ "\n", "\r\n" }) |newline| { + const argument = try std.mem.replaceOwned(u8, a, fragment, "\n", newline); + const source = try std.fmt.allocPrint(a, "module spans{s}fn f() -> void effects [] {{ apply({s}); }}", .{ newline, argument }); + const r = try parseWithFile(a, source, "ranges.aji"); + try std.testing.expect(r == .ok); + try expectContainedRanges(r.ok, null); + const arg = r.ok.functions[0].body.stmts[0].kind.expr.kind.call.args[0]; + try std.testing.expect(arg.kind == .borrow); + try std.testing.expect(arg.kind.borrow.place.kind == .deref); + try expectSourceRange(source, arg.span, argument); + try expectSourceRange(source, arg.kind.borrow.place.span, argument[std.mem.indexOfScalar(u8, argument, '*').?..]); + } + } +} + +test "dereferenced borrow consumes unary place without swallowing outer operations" { + var arena: std.heap.ArenaAllocator = .init(std.testing.allocator); + defer arena.deinit(); + const a = arena.allocator(); + const r = try parse(a, "module places fn f() -> void effects [] { apply(ref *p[0], mut *q); apply(ref *p + 1u64); }"); + try std.testing.expect(r == .ok); + const statements = r.ok.functions[0].body.stmts; + const arguments = statements[0].kind.expr.kind.call.args; + try std.testing.expect(arguments[0].kind.borrow.place.kind.deref.kind == .index); + try std.testing.expect(arguments[1].kind.borrow.mutable); + const addition = statements[1].kind.expr.kind.call.args[0]; + try std.testing.expect(addition.kind == .binary); + try std.testing.expect(addition.kind.binary.l.kind == .borrow); + try std.testing.expect(addition.kind.binary.r.kind == .int); + // Do not broaden invalid operand spellings into new prefix forms. + for ([_][]const u8{ "ref -p", "mut not p", "ref move p", "ref ref p", "ref *", "mut *p." }) |argument| { + const source = try std.fmt.allocPrint(a, "module places fn f() -> void effects [] {{ apply({s}); }}", .{argument}); + try std.testing.expect((try parse(a, source)) == .err); + } +} diff --git a/tests/conformance/source/deref_arguments/array.aji b/tests/conformance/source/deref_arguments/array.aji new file mode 100644 index 0000000..7b0f41c --- /dev/null +++ b/tests/conformance/source/deref_arguments/array.aji @@ -0,0 +1,9 @@ +// expect: run +// result: 42 +// phase: 6 +// spec: docs/spec/15-source-language.md §5; docs/reference/grammar.ebnf place/unary +module test.deref_arguments +fn first(p: ref<[u64; 2]>) -> u64 effects [] { return p[0]; } +fn set(p: mut<[u64; 2]>) -> void effects [] { p[0] = 42; } +fn forward(p: mut<[u64; 2]>) -> u64 effects [] { set(mut *p); return first(ref *p); } +fn main() -> u64 effects [] { var a: [u64; 2] = zero; return forward(mut a); } diff --git a/tests/conformance/source/deref_arguments/conflict.aji b/tests/conformance/source/deref_arguments/conflict.aji new file mode 100644 index 0000000..ec74097 --- /dev/null +++ b/tests/conformance/source/deref_arguments/conflict.aji @@ -0,0 +1,7 @@ +// expect: reject E2101 E2101 +// phase: 6 +// spec: docs/spec/15-source-language.md §5; docs/reference/grammar.ebnf place/unary +module test.deref_arguments +fn both(a: mut, b: ref) -> u64 effects [] { return *b; } +fn forward(p: mut) -> u64 effects [] { return both(mut *p, ref *p); } +fn main() -> u64 effects [] { var x: u64 = 0; return forward(mut x); } diff --git a/tests/conformance/source/deref_arguments/element.aji b/tests/conformance/source/deref_arguments/element.aji new file mode 100644 index 0000000..3234c0c --- /dev/null +++ b/tests/conformance/source/deref_arguments/element.aji @@ -0,0 +1,9 @@ +// expect: run +// result: 42 +// phase: 6 +// spec: docs/spec/15-source-language.md §5; docs/reference/grammar.ebnf place/unary +module test.deref_arguments +fn read(p: ref) -> u64 effects [] { return *p; } +fn write(p: mut) -> void effects [] { *p = 42; } +fn forward(p: slice_mut) -> u64 effects [] { { mut item = p[0]; write(mut *item); } { ref item = p[0]; return read(ref *item); } } +fn main() -> u64 effects [] { var a: [u64; 2] = zero; return forward(mut a); } diff --git a/tests/conformance/source/deref_arguments/generic.aji b/tests/conformance/source/deref_arguments/generic.aji new file mode 100644 index 0000000..bb7ad1c --- /dev/null +++ b/tests/conformance/source/deref_arguments/generic.aji @@ -0,0 +1,8 @@ +// expect: run +// result: 42 +// phase: 6 +// spec: docs/spec/15-source-language.md §5; docs/reference/grammar.ebnf place/unary +module test.deref_arguments +fn read(p: ref) -> T effects [] { return *p; } +fn forward(p: ref) -> T effects [] { return read::(ref *p); } +fn main() -> u64 effects [] { var x: u64 = 42; return forward::(ref x); } diff --git a/tests/conformance/source/deref_arguments/mutable.aji b/tests/conformance/source/deref_arguments/mutable.aji new file mode 100644 index 0000000..1711018 --- /dev/null +++ b/tests/conformance/source/deref_arguments/mutable.aji @@ -0,0 +1,9 @@ +// expect: run +// result: 42 +// phase: 6 +// spec: docs/spec/15-source-language.md §5; docs/reference/grammar.ebnf place/unary +module test.deref_arguments +fn read(p: ref) -> u64 effects [] { return *p; } +fn write(p: mut) -> void effects [] { *p = 42; } +fn forward(p: mut) -> u64 effects [] { write(mut *p); let n = read(ref *p); *p = 0; return n; } +fn main() -> u64 effects [] { var x: u64 = 1; return forward(mut x); } diff --git a/tests/conformance/source/deref_arguments/not_borrow.aji b/tests/conformance/source/deref_arguments/not_borrow.aji new file mode 100644 index 0000000..e0ee741 --- /dev/null +++ b/tests/conformance/source/deref_arguments/not_borrow.aji @@ -0,0 +1,6 @@ +// expect: reject E7005 +// phase: 6 +// spec: docs/spec/15-source-language.md §5; docs/reference/grammar.ebnf place/unary +module test.deref_arguments +fn read(p: ref) -> u64 effects [] { return *p; } +fn main() -> u64 effects [] { let x = 42u64; return read(ref *x); } diff --git a/tests/conformance/source/deref_arguments/postfix_grouping.aji b/tests/conformance/source/deref_arguments/postfix_grouping.aji new file mode 100644 index 0000000..db61ac5 --- /dev/null +++ b/tests/conformance/source/deref_arguments/postfix_grouping.aji @@ -0,0 +1,8 @@ +// expect: run +// result: 42 +// phase: 6 +// spec: docs/reference/grammar.ebnf place/unary/postfix +module test.deref_arguments +fn read(p: ref) -> u64 effects [] { return *p; } +fn forward(p: ref<[u64; 2]>) -> u64 effects [] { return read(ref (*p)[1]); } +fn main() -> u64 effects [] { let a = [42u64; 2]; return forward(ref a); } diff --git a/tests/conformance/source/deref_arguments/postfix_precedence.aji b/tests/conformance/source/deref_arguments/postfix_precedence.aji new file mode 100644 index 0000000..6f24344 --- /dev/null +++ b/tests/conformance/source/deref_arguments/postfix_precedence.aji @@ -0,0 +1,8 @@ +// expect: reject E7005 +// phase: 6 +// spec: docs/reference/grammar.ebnf place/unary/postfix +// Postfix binds first: *p[0] dereferences the loaded scalar, not the array. +module test.deref_arguments +fn read(p: ref) -> u64 effects [] { return *p; } +fn forward(p: ref<[u64; 2]>) -> u64 effects [] { return read(ref *p[0]); } +fn main() -> u64 effects [] { let a = [42u64; 2]; return forward(ref a); } diff --git a/tests/conformance/source/deref_arguments/shared.aji b/tests/conformance/source/deref_arguments/shared.aji new file mode 100644 index 0000000..5deddcf --- /dev/null +++ b/tests/conformance/source/deref_arguments/shared.aji @@ -0,0 +1,8 @@ +// expect: run +// result: 42 +// phase: 6 +// spec: docs/spec/15-source-language.md §5; docs/reference/grammar.ebnf place/unary +module test.deref_arguments +fn read(p: ref) -> u64 effects [] { return *p; } +fn forward(p: ref) -> u64 effects [] { return read(ref *p) + 1u64; } +fn main() -> u64 effects [] { var x: u64 = 41; return forward(ref x); } diff --git a/tests/conformance/source/deref_arguments/unchecked_place.aji b/tests/conformance/source/deref_arguments/unchecked_place.aji new file mode 100644 index 0000000..df71b5b --- /dev/null +++ b/tests/conformance/source/deref_arguments/unchecked_place.aji @@ -0,0 +1,7 @@ +// expect: reject E7006 +// phase: 6 +// spec: docs/spec/15-source-language.md §5; docs/reference/grammar.ebnf place/unary +module test.deref_arguments +fn read(p: ref) -> u64 effects [] { return *p; } +fn forward(p: ref) -> u64 effects [unsafe] { return read(ref unchecked *p); } +fn main() -> u64 effects [unsafe] { var x: u64 = 42; return forward(ref x); } diff --git a/tests/conformance/source/deref_arguments/value_expression.aji b/tests/conformance/source/deref_arguments/value_expression.aji new file mode 100644 index 0000000..6ad5647 --- /dev/null +++ b/tests/conformance/source/deref_arguments/value_expression.aji @@ -0,0 +1,6 @@ +// expect: reject E7006 +// phase: 6 +// spec: docs/spec/15-source-language.md §5; docs/reference/grammar.ebnf place/unary +module test.deref_arguments +fn forward(p: ref) -> u64 effects [] { let r = ref *p; return *r; } +fn main() -> u64 effects [] { var x: u64 = 42; return forward(ref x); } diff --git a/tests/conformance/source/deref_arguments/wrong_mode.aji b/tests/conformance/source/deref_arguments/wrong_mode.aji new file mode 100644 index 0000000..8b6620a --- /dev/null +++ b/tests/conformance/source/deref_arguments/wrong_mode.aji @@ -0,0 +1,7 @@ +// expect: reject E7005 +// phase: 6 +// spec: docs/spec/15-source-language.md §5; docs/reference/grammar.ebnf place/unary +module test.deref_arguments +fn write(p: mut) -> void effects [] { *p = 42; } +fn forward(p: ref) -> void effects [] { write(mut *p); } +fn main() -> void effects [] { var x: u64 = 0; forward(ref x); } diff --git a/tests/hosted/source_format.py b/tests/hosted/source_format.py index ac845ef..d007017 100644 --- a/tests/hosted/source_format.py +++ b/tests/hosted/source_format.py @@ -115,6 +115,7 @@ with tempfile.TemporaryDirectory(prefix="aji-format-") as directory: b"module m fn f(p:own >)->u64 effects [] {let n=(1u64+2u64)*3u64;return n>>1u64;}", b"module m fn f()->void effects [] {select { recv(rx){ok(x)=>{},closed=>{},cancelled=>{},} cancelled=>{},}}", b"module m fn f()->void effects [] {let p=P{a:1u64, // lit \t\nb:2u64};\n\n\nlet n=not (true and false);}", + b"module m fn f(p:mut)->void effects [] {apply(ref // shared\n*p,mut *p);}", ] for i, source in enumerate(cases): sample = root / f"case{i}.aji" @@ -123,6 +124,33 @@ with tempfile.TemporaryDirectory(prefix="aji-format-") as directory: sample.write_bytes(text) assert cli("fmt", sample) == text, (i, text) cli("fmt", "--check", sample) + + # The grammar permits inline dereferenced places without parentheses. + # Both spellings must lower identically; formatting preserves the new + # prefix boundary and comments without introducing grouping tokens. + bare = root / "deref.aji" + grouped = root / "deref-grouped.aji" + source = (REPO / "tests/conformance/source/deref_arguments/mutable.aji").read_bytes() + for gap in (b" ", b"\r\n", " // λ\n".encode()): + bare.write_bytes(source.replace(b"mut *p", b"mut" + gap + b"*p") + .replace(b"ref *p", b"ref" + gap + b"*p")) + grouped.write_bytes(source.replace(b"mut *p", b"mut (*p)").replace(b"ref *p", b"ref (*p)")) + air = cli("lower", bare) + assert cli("lower", grouped) == air + cli("check", bare) + result = json.loads(cli("run", "--fuel", "100000", bare)) + assert result["value"] == 42 + text = cli("fmt", bare) + bare.write_bytes(text) + assert cli("fmt", bare) == text + assert cli("lower", bare) == air + cli("fmt", "--check", bare) + text = cli("fmt", grouped) + assert b"ref (*p)" in text and b"mut (*p)" in text, text + grouped.write_bytes(text) + assert cli("fmt", grouped) == text + assert cli("lower", grouped) == air + cli("fmt", "--check", grouped) if b"// lit" in source: assert b"// lit \t\n b: 2u64" in text assert b"not (true and false)" in text