diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index eb4a4e4..2dff482 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -28,6 +28,7 @@ jobs: - run: python3 tests/hosted/related_locations.py - run: python3 tests/hosted/source_lifetimes.py - run: python3 tests/hosted/source_array_arguments.py + - run: python3 tests/hosted/source_type_positions.py - run: python3 tests/hosted/examples.py --stress 200 - run: python3 tests/hosted/source_static_functions.py - run: python3 tests/hosted/source_linear.py diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index ff88c2c..eb381ac 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -24,6 +24,7 @@ python3 tests/hosted/source_diagnostic_reasons.py # stable E7006 categories and python3 tests/hosted/related_locations.py # factual conflict/spawn positions, source mapping and package provenance python3 tests/hosted/source_lifetimes.py # AIR rejection twins and exact lifetime/join diagnostic positions python3 tests/hosted/source_array_arguments.py # array argument shape, cleanup, AIR and package parity +python3 tests/hosted/source_type_positions.py # structural type errors at annotations, templates and packages 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 diff --git a/docs/agent/README.md b/docs/agent/README.md index 27d7834..ab37a8e 100644 --- a/docs/agent/README.md +++ b/docs/agent/README.md @@ -154,3 +154,5 @@ DD-019 unique diagnostic fields (cl100k): Quick Reference stays 8,859; other sou Q-041 source scope names (DD-020, cl100k): Quick Reference 8,859 → 8,889 (+30), other source docs 66,312 → 66,484 (+172), full source 75,171 → 75,373 (+0.27%). o200k groups are 8,925 / 66,690 / 75,615; Qwen2.5 9,167 / 68,115 / 77,282. AIR remains 184,628 bytes / 47,445 cl100k tokens; library/std.md is unchanged. One existing lexical block identifier is explicitly excluded from ordinary value expressions, removing ineffective transfers and misleading repair hints. There is no new syntax, diagnostic code/reason, verifier/runtime state or feature. Former source aliases now reject; ordinary shadowing and AIR scope flow remain available. No empirical MCC benefit is claimed. Q-023 whole-array arguments (DD-021, cl100k): Quick Reference 8,889 → 8,948 (+59), other source docs 66,484 → 66,669 (+185), full source 75,373 → 75,617 (+0.32%). o200k groups are 8,984 / 66,873 / 75,857; Qwen2.5 9,226 / 68,303 / 77,529. AIR stays 184,628 bytes / 47,445 cl100k tokens; library/std.md is unchanged. One rule follows an explicit ordinary-call signature so existing array-borrow parameters are callable; exact modes, lengths, lexical cleanup, named retained origins and task loans remain checked. No new syntax, diagnostic, feature or verifier/runtime state. Existing primitives suffice; no empirical MCC benefit is claimed. + +Q-042 structural type positions (cl100k): Quick Reference stays 8,948; other source docs 66,669 → 66,761 (+92), full source 75,617 → 75,709 (+0.12%). o200k groups are 8,984 / 66,965 / 75,949; Qwen2.5 9,226 / 68,398 / 77,624. AIR remains 184,628 bytes / 47,445 cl100k tokens; library/std.md is unchanged. Existing type-position predicates now reject invalid source annotations before AIR, including known unused generic headers, with structured E7005 fields at the constructor. No new concept, syntax, code, feature or verifier/runtime state is added. Earlier source diagnostic codes/fields may change; graph properties retain verifier diagnostics. No empirical MCC benefit is claimed. diff --git a/docs/design/open-questions.md b/docs/design/open-questions.md index 927c437..036198b 100644 --- a/docs/design/open-questions.md +++ b/docs/design/open-questions.md @@ -313,4 +313,10 @@ Clarify whether `let`/`move` aliases and rebinding of a scope name are permitted 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. +**Resolved for structural positions (spec 15 §10):** frontend annotation checks use the verifier's pure type-position predicates. Parameters, returns, fields, payloads, nested constructors, local annotations and known unused generic/static-contract headers reject E7005 at the invalid constructor with `position` and `type_constructor`; unknown parameter restrictions are checked after substitution. Graph-content properties retain E2111. Existing AIR acceptance, diagnostics and runtime rules are unchanged. Invalid source types formerly rejected by E1001 or a feature diagnostic now reject earlier; known invalid unused generic headers and annotations previously skipped by lowering also reject. Generic argument diagnostics may have more precise coordinates and different fields. Property checks beyond structural positions are separate (Q-043); this does not establish complete source type-check coverage. + +## Q-043 — Copy content validation for source dynamic owning arrays + +Read-only review after Q-042 reproduced `module nested fn unused(p: own_array>) -> void effects [] { trap; } fn main() -> void effects [] {}`. It reaches the AIR verifier with E1001, `dynamic heap arrays require Copy content`. Spec 03 §25 requires Copy content for `own_array`, and spec 15 §10 requires fully source-type-decided errors to be E7xxx. This is a content-property check, not a structural type position. Graph-content properties and Send/Share checks retain their separately specified verifier diagnostics. + +Audit this source check, including nominal and substituted generic content, then add positive/rejected source tests and report E7005 before emitting invalid AIR. Preserve AIR's Copy predicate, type identity, allocation/cleanup rules and deterministic behavior. The extent beyond the reproduced signature is unverified. diff --git a/docs/plan/phase-6-source-language.md b/docs/plan/phase-6-source-language.md index e2e5cad..f2b8f30 100644 --- a/docs/plan/phase-6-source-language.md +++ b/docs/plan/phase-6-source-language.md @@ -237,3 +237,11 @@ The invariant is unchanged borrow mode, aliasing, provenance and child-first tem 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. + +## P6-2 structural type positions (2026-10-07) + +Q-042/spec 15 §10 closes source annotation gaps using the verifier's existing pure position predicates. Nested borrows, non-value content, fields/payloads, local annotations and known unused generic/static-contract constructors reject E7005 at their own annotation; restrictions involving unknown parameters wait for substitution. Graph-content properties retain E2111. The invariant remains that second-class borrows cannot be stored or nested in forbidden positions. AIR independently enforces the same rule; no ownership transfer, runtime allocation/cleanup or deterministic interpreter behavior changes. + +Thirty-four conformance fixtures cover valid runs/unused headers, forbidden positions, specialization and graph-diagnostic preservation. CLI checks exercise exact fields/positions, all diagnostic commands, package reachability and source/AIR/hosted parity. Existing invalid-source fixtures now expect the earlier E7005. No syntax, code, feature, verifier/runtime state or evaluation input changes; source bundle growth is recorded in docs/agent/README.md. Q-043 records the separately reproduced Copy-content validation gap. P6-2 remains open; the historical P6A-12 checkpoint does not establish completeness for the current language. + +Validation: full conformance 1,303/1,303; all 34 new fixtures also pass with per-instruction collection. Unit tests and formatting checks pass. CLI integration passes 74 checks, existing generics/buffers/linear suites pass, and formatter passes 4,453 checks over 1,015 public files (1,007 parsed, eight existing syntax errors, 631 identical AIR outputs). Source coverage remains 121/121. Comparison of 983 preceding public sources finds only five expected invalid-type diagnostic changes, with no check acceptance changes and all other canonical lowerings identical. All 333 public AIR files preserve check diagnostics and acceptance. Independent Claude review passes after clarifying feature-gate precedence. diff --git a/docs/spec/15-source-language.md b/docs/spec/15-source-language.md index 74058e7..745ae84 100644 --- a/docs/spec/15-source-language.md +++ b/docs/spec/15-source-language.md @@ -404,6 +404,8 @@ A byte string or array used where a `slice` is expected is materialized in f - slice element types of `slice.copy`; - the element rules of channels and mutexes (no borrow, scope, task, shield or guard). +Source type annotations MUST satisfy the existing AIR type-position rules (spec 07 §8.3/§8.8), including nested constructors. An invalid constructor is E7005 at its annotation, with `position` and `type_constructor` fields. Known constructors in unused generic signatures and static function contracts MUST be checked; restrictions depending on a type parameter are checked after substitution. Graph-content properties and feature gates for otherwise correctly positioned types retain their verifier diagnostics. + The verifier remains the authority for: - ownership state, linearity and moves; diff --git a/src/frontend/generics.zig b/src/frontend/generics.zig index 4d9c3ec..06f8f50 100644 --- a/src/frontend/generics.zig +++ b/src/frontend/generics.zig @@ -2,6 +2,7 @@ const std = @import("std"); const ast = @import("ast.zig"); const lower = @import("lower.zig"); +const type_positions = @import("type_positions.zig"); const authority = @import("../air/authority.zig"); const A = std.mem.Allocator; const Error = error{ OutOfMemory, InvalidGeneric }; @@ -162,8 +163,8 @@ const Generator = struct { for (params[0..i]) |previous| if (std.mem.eql(u8, p.name, previous.name)) return g.invalid(.duplicate_declaration, p.span, "duplicate type parameter", p.name); if (p.callable) |c| { if (p.constraints.len != 0 or g.function(p.name, p.span) != null) return g.invalid(.invalid_static_function, p.span, "invalid static function parameter", p.name); - for (c.params) |t| try g.headerType(t, params, 0); - try g.headerType(c.ret, params, 0); + for (c.params) |t| try g.headerAt(t, params, 0, .param); + try g.headerAt(c.ret, params, 0, .ret); if (c.ret.kind == .wrapped) { const w = c.ret.kind.wrapped.wrapper; if (std.mem.eql(u8, w, "ref") or std.mem.eql(u8, w, "mut") or std.mem.eql(u8, w, "slice") or std.mem.eql(u8, w, "slice_mut")) return g.invalid(.invalid_static_function, c.ret.span, "static function contracts do not support borrowed returns", p.name); @@ -180,6 +181,17 @@ const Generator = struct { } } + fn headerAt(g: *Generator, t: ast.Type, params: []const ast.TypeParam, depth: usize, position: type_positions.Position) Error!void { + if (type_positions.firstInvalid(t, position)) |bad| { + g.problem = .{ .code = "E7005", .span = bad.span, .message = "type constructor not allowed in this position", .fields = try g.arena.dupe(lower.Problem.Field, &.{ + .{ .name = "position", .value = @tagName(bad.position) }, + .{ .name = "type_constructor", .value = bad.constructor }, + }) }; + return error.InvalidGeneric; + } + return g.headerType(t, params, depth); + } + fn headerType(g: *Generator, t: ast.Type, params: []const ast.TypeParam, depth: usize) Error!void { if (depth >= 64) return g.invalid(.implementation_limit, t.span, "type traversal exceeds depth 64", "type"); switch (t.kind) { @@ -212,7 +224,7 @@ const Generator = struct { if (depth >= 64) return g.invalid(.implementation_limit, arg.span, "generic argument traversal exceeds depth 64", expected.name); if (expected.callable == null) { if (arg.kind == .function_ref) return g.fail("E7005", arg.span, "expected a type argument, found a static function", expected.name); - return g.headerType(arg, params, depth); + return g.headerAt(arg, params, depth, .param); } if (arg.kind != .function_ref) return g.fail("E7005", arg.span, "expected an explicit static function argument", expected.name); const f = arg.kind.function_ref; @@ -266,8 +278,8 @@ const Generator = struct { }, } if (t.generics.len != 0) switch (t.kind) { - .@"struct" => |fs| for (fs) |field| try g.headerType(field.ty, t.generics, 0), - .@"enum" => |vs| for (vs) |v| for (v.payload) |payload_type| try g.headerType(payload_type, t.generics, 0), + .@"struct" => |fs| for (fs) |field| try g.headerAt(field.ty, t.generics, 0, .member), + .@"enum" => |vs| for (vs) |v| for (v.payload) |payload_type| try g.headerAt(payload_type, t.generics, 0, .member), }; generic = generic or t.generics.len != 0; } @@ -281,9 +293,9 @@ const Generator = struct { for (f.params, 0..) |p, j| { try g.checkBinding(p.name, p.span, .{ .params = f.generics }); for (f.params[0..j]) |prev| if (std.mem.eql(u8, p.name, prev.name)) return g.invalid(.duplicate_declaration, p.span, "duplicate value parameter", p.name); - try g.headerType(p.ty, f.generics, 0); + try g.headerAt(p.ty, f.generics, 0, .param); } - try g.headerType(f.ret, f.generics, 0); + try g.headerAt(f.ret, f.generics, 0, .ret); try g.borrowReturnContract(f); } generic = generic or f.generics.len != 0; diff --git a/src/frontend/lower.zig b/src/frontend/lower.zig index e5568b6..5e37dd0 100644 --- a/src/frontend/lower.zig +++ b/src/frontend/lower.zig @@ -31,6 +31,7 @@ const authority = @import("../air/authority.zig"); const features_mod = @import("../air/features.zig"); const air_module = @import("../air/module.zig"); const verifier_types = @import("../verifier/types.zig"); +const type_positions = @import("type_positions.zig"); const Allocator = std.mem.Allocator; const Span = ast.Span; @@ -462,7 +463,7 @@ const Lowerer = struct { if (l.sigs.contains(f.name)) return l.invalid(.duplicate_declaration, f.span, "function defined twice", &.{.{ .name = "name", .value = f.name }}); const ps = try l.arena.alloc(Ty, f.params.len); for (f.params, ps) |p, *t| t.* = try l.typeOf(p.ty); - const ret = try l.typeOf(f.ret); + const ret = try l.typeAt(f.ret, .ret); var origin: ?usize = null; if (f.from) |name| { for (f.params, 0..) |param, i| if (std.mem.eql(u8, param.name, name)) { @@ -669,12 +670,22 @@ const Lowerer = struct { } fn memberType(l: *Lowerer, t: ast.Type) Error!Ty { - const ty = try l.typeOf(t); - if (ty == .void or ty == .never) return l.fail("E7005", t.span, "a field or payload needs a value type", &.{}); - return ty; + return l.typeAt(t, .member); } fn typeOf(l: *Lowerer, t: ast.Type) Error!Ty { + return l.typeAt(t, .param); + } + + fn typeAt(l: *Lowerer, t: ast.Type, position: type_positions.Position) Error!Ty { + if (type_positions.firstInvalid(t, position)) |bad| return l.fail("E7005", bad.span, "type constructor not allowed in this position", &.{ + .{ .name = "position", .value = @tagName(bad.position) }, + .{ .name = "type_constructor", .value = bad.constructor }, + }); + return l.resolveType(t); + } + + fn resolveType(l: *Lowerer, t: ast.Type) Error!Ty { switch (t.kind) { .applied => return l.fail("E7002", t.span, "unknown generic type", &.{}), .function_ref => return l.invalid(.invalid_static_function, t.span, "static functions are not runtime types", &.{}), @@ -692,12 +703,14 @@ const Lowerer = struct { }, .array => |a| { if (a.len == 0 or a.len > (@as(u64, 1) << 32)) return l.fail("E7005", t.span, "array length must be between 1 and 2^32", &.{}); - return l.arrayOf(try l.memberType(a.elem.*), a.len); + const elem = try l.resolveType(a.elem.*); + if (elem == .void or elem == .never) return l.fail("E7005", a.elem.span, "an array element needs a value type", &.{}); + return l.arrayOf(elem, a.len); }, .wrapped => |w| { const Wr = enum { ref, mut, slice, slice_mut, own, own_array, buffer, task, sender, receiver, mutex, island, frozen, arc, iref, atomic }; const which = std.meta.stringToEnum(Wr, w.wrapper) orelse return l.notYet(t.span, "this type (later layers)"); - const in = try l.typeOf(w.inner.*); + const in = try l.resolveType(w.inner.*); return switch (which) { .ref => l.wrap(.ref, in), .mut => l.wrap(.mut, in), @@ -1066,6 +1079,7 @@ const Lowerer = struct { fn stmt(l: *Lowerer, s: ast.Stmt) Error!void { switch (s.kind) { .let => |x| { + const annotation: ?Ty = if (x.ty) |t| try l.typeOf(t) else null; if (x.e.kind == .call) if (pathOf(x.e.kind.call.callee.*)) |p| if (std.mem.eql(u8, p, "struct.unpack") or std.mem.eql(u8, p, "array.unpack")) return l.unpackLet(s, x.names, x.e, x.tuple); if (x.tuple and x.names.len == 1) return l.invalid(.invalid_binding, s.span, "a single parenthesized binding requires aggregate unpack", &.{}); if (x.names.len != 1) { @@ -1079,7 +1093,7 @@ const Lowerer = struct { try l.bindings.append(l.arena, .{ .name = x.names[0], .mutable = false, .value = st.name, .ty = st.ty, .kind = .storage }); return; } - var want: ?Ty = if (x.ty) |t| try l.typeOf(t) else null; + var want: ?Ty = annotation; // An untyped `let` takes its type from how the name is used // later (`return`, arguments, operations, annotations). if (want == null and l.peekType(x.e) == null) want = l.inferUses(x.names[0]); @@ -3181,7 +3195,7 @@ const Lowerer = struct { if (std.mem.eql(u8, path, "buffer.new")) { try want(l, e.span, args, 2, path); const source_ty = e.kind.call.type_arg orelse return l.fail("E7005", e.span, "buffer.new needs an explicit content type", &.{}); - const ty = try l.typeOf(source_ty); + const ty = try l.memberType(source_ty); const n = try l.expr(args[0], usize_ty); try l.checkTy(args[0].span, n.ty, usize_ty); const cap = try l.expectCap(args[1], "mem.alloc"); diff --git a/src/frontend/type_positions.zig b/src/frontend/type_positions.zig new file mode 100644 index 0000000..8c8226f --- /dev/null +++ b/src/frontend/type_positions.zig @@ -0,0 +1,35 @@ +//! Structural source type positions, using the frozen AIR predicate. +//! Unknown generic names remain unknown; graph-content properties stay in +//! the verifier. Traversal follows syntax, never nominal type definitions. +const std = @import("std"); +const ast = @import("ast.zig"); +const air = @import("../air/module.zig"); +const types = @import("../verifier/types.zig"); + +pub const Position = types.Position; +pub const Failure = struct { span: ast.Span, position: Position, constructor: []const u8 }; + +pub fn firstInvalid(annotation: ast.Type, position: Position) ?Failure { + var t = annotation; + var pos = position; + while (true) { + const tag: std.meta.Tag(air.Type) = switch (t.kind) { + .name => |n| if (std.mem.eql(u8, n, "void")) .void else if (std.mem.eql(u8, n, "never")) .never else .named, + .array => .array, + .wrapped => |w| std.meta.stringToEnum(std.meta.Tag(air.Type), w.wrapper) orelse return null, + .cap => .cap, + .file => .resource, + .applied, .function_ref => return null, // Resolved by specialization/name checks. + }; + // Published source linear aggregates permit complete linear members; + // their feature closure and property checks remain independently verified. + if (!types.allowedPosition(tag, pos, true)) return .{ .span = t.span, .position = pos, .constructor = @tagName(tag) }; + if (tag == .island or tag == .frozen or tag == .arc) return null; + pos = types.innerPosition(tag); + t = switch (t.kind) { + .array => |a| a.elem.*, + .wrapped => |w| w.inner.*, + else => return null, + }; + } +} diff --git a/src/verifier/types.zig b/src/verifier/types.zig index 7dba96e..96d69fc 100644 --- a/src/verifier/types.zig +++ b/src/verifier/types.zig @@ -90,7 +90,26 @@ fn visitValue(c: *Context, state: anytype, t: Type) std.mem.Allocator.Error!void } } -const Position = enum { member, param, ret, block_param, borrowed }; +pub const Position = enum { member, param, ret, block_param, borrowed }; + +/// Shared with source annotation checking; no state or diagnostics. +pub fn allowedPosition(tag: std.meta.Tag(Type), pos: Position, linear_aggregates: bool) bool { + return switch (tag) { + .void, .never => pos == .ret, + .local => false, + .ref, .mut, .slice, .slice_mut => pos != .member and pos != .borrowed, + .scope, .task, .shield, .guard => pos != .member, + .resource, .own, .own_array, .buffer, .region, .sender, .receiver, .mutex, .island, .frozen, .arc, .atomic => pos != .member or linear_aggregates, + else => true, + }; +} + +pub fn innerPosition(tag: std.meta.Tag(Type)) Position { + return switch (tag) { + .ref, .mut, .task, .sender, .receiver, .mutex, .guard => .borrowed, + else => .member, + }; +} /// Where a type may appear (spec 07 §8.3, §8.8): /// - `void`/`never` only as return types; @@ -99,14 +118,7 @@ const Position = enum { member, param, ret, block_param, borrowed }; /// never as members (fields, payloads, elements, locals); /// - the inner type of every compound type is a value type. fn wellFormed(c: *Context, span: air.Span, t: Type, pos: Position, owner: []const u8) !void { - const ok = switch (t) { - .void, .never => pos == .ret, - .local => false, - .ref, .mut, .slice, .slice_mut => pos != .member and pos != .borrowed, - .scope, .task, .shield, .guard => pos != .member, - .resource, .own, .own_array, .buffer, .region, .sender, .receiver, .mutex, .island, .frozen, .arc, .atomic => pos != .member or c.module.declares("linear_aggregates"), - else => true, - }; + const ok = allowedPosition(std.meta.activeTag(t), pos, c.module.declares("linear_aggregates")); if (t == .own_array and c.module.declares("linear_aggregates") and c.module.isLinearType(t.own_array.*)) try c.report("E1001", span, "dynamic heap arrays require Copy content", &.{}); // An atomic cell holds an AtomicScalar (spec 04 §23). if (t == .atomic and !isAtomicScalar(t.atomic.*)) try c.report("E1001", span, "atomic needs bool or an integer of at most 64 bits", &.{ @@ -131,10 +143,7 @@ fn wellFormed(c: *Context, span: air.Span, t: Type, pos: Position, owner: []cons // a task may return one; every other compound holds value types only. // Channel elements and mutex contents may be linear (spec 04 §38.1); // their remaining rules are the element rules of `checkElement`. - if (t.inner()) |inner| try wellFormed(c, span, inner, switch (t) { - .ref, .mut, .task, .sender, .receiver, .mutex, .guard => .borrowed, - else => .member, - }, owner); + if (t.inner()) |inner| try wellFormed(c, span, inner, innerPosition(std.meta.activeTag(t)), owner); } /// Validate a phantom or used source generic argument by ordinary type rules. diff --git a/tests/conformance/source/buffers/borrowed_element.aji b/tests/conformance/source/buffers/borrowed_element.aji index b3b1e59..e9a67d3 100644 --- a/tests/conformance/source/buffers/borrowed_element.aji +++ b/tests/conformance/source/buffers/borrowed_element.aji @@ -1,5 +1,5 @@ -// Allocation and destruction each reject the borrow element at their own source construct. -// expect: reject E1001 E1001 +// The content annotation rejects before allocation or destruction reaches AIR. +// expect: reject E7005 // grant: heap=mem.alloc:128 // phase: 6 // spec: docs/spec/03-memory-model.md §26; docs/spec/15-source-language.md §17 diff --git a/tests/conformance/source/generics/std_result_borrow.aji b/tests/conformance/source/generics/std_result_borrow.aji index 18784bf..8c41816 100644 --- a/tests/conformance/source/generics/std_result_borrow.aji +++ b/tests/conformance/source/generics/std_result_borrow.aji @@ -1,4 +1,4 @@ -// expect: reject E1001 +// expect: reject E7005 // phase: 6 // spec: docs/spec/15-source-language.md §15 module demo.generic diff --git a/tests/conformance/source/linear/task_member.aji b/tests/conformance/source/linear/task_member.aji index 65a0c5d..ff5a8c3 100644 --- a/tests/conformance/source/linear/task_member.aji +++ b/tests/conformance/source/linear/task_member.aji @@ -1,4 +1,4 @@ -// expect: reject E0006 +// expect: reject E7005 // phase: 6 // spec: docs/spec/03-memory-model.md §25; docs/spec/15-source-language.md §16 module test.linear diff --git a/tests/conformance/source/type_positions/array_field.aji b/tests/conformance/source/type_positions/array_field.aji new file mode 100644 index 0000000..4a84e46 --- /dev/null +++ b/tests/conformance/source/type_positions/array_field.aji @@ -0,0 +1,6 @@ +// expect: reject E7005 +// phase: 6 +// spec: docs/spec/15-source-language.md §10; docs/spec/07-air.md §8.8 +module test.type_positions +struct Bad { p: [ref; 2] } +fn main() -> void effects [] {} diff --git a/tests/conformance/source/type_positions/array_ref.aji b/tests/conformance/source/type_positions/array_ref.aji new file mode 100644 index 0000000..667b0f0 --- /dev/null +++ b/tests/conformance/source/type_positions/array_ref.aji @@ -0,0 +1,6 @@ +// expect: reject E7005 +// phase: 6 +// spec: docs/spec/15-source-language.md §10; docs/spec/07-air.md §8.8 +module test.type_positions +fn unused(p: [ref; 2]) -> void effects [] {} +fn main() -> void effects [] {} diff --git a/tests/conformance/source/type_positions/buffer_new_ref.aji b/tests/conformance/source/type_positions/buffer_new_ref.aji new file mode 100644 index 0000000..dec55d3 --- /dev/null +++ b/tests/conformance/source/type_positions/buffer_new_ref.aji @@ -0,0 +1,5 @@ +// expect: reject E7005 +// phase: 6 +// spec: docs/spec/15-source-language.md §10; docs/spec/03-memory-model.md §26 +module test.type_positions +fn main(heap: cap) -> void effects [mem.alloc] { let data = buffer.new>(0, heap) or trap; trap; } diff --git a/tests/conformance/source/type_positions/buffer_ref.aji b/tests/conformance/source/type_positions/buffer_ref.aji new file mode 100644 index 0000000..1a1599a --- /dev/null +++ b/tests/conformance/source/type_positions/buffer_ref.aji @@ -0,0 +1,6 @@ +// expect: reject E7005 +// phase: 6 +// spec: docs/spec/15-source-language.md §10; docs/spec/07-air.md §8.8 +module test.type_positions +fn unused(p: buffer>) -> void effects [] {} +fn main() -> void effects [] {} diff --git a/tests/conformance/source/type_positions/byte_annotation.aji b/tests/conformance/source/type_positions/byte_annotation.aji new file mode 100644 index 0000000..b9347f3 --- /dev/null +++ b/tests/conformance/source/type_positions/byte_annotation.aji @@ -0,0 +1,6 @@ +// expect: reject E7005 +// phase: 6 +// spec: docs/spec/15-source-language.md §10; docs/spec/07-air.md §8.8 +module test.type_positions +fn bad() -> void effects [] { let q: ref> = b"hi"; } +fn main() -> void effects [] {} diff --git a/tests/conformance/source/type_positions/field.aji b/tests/conformance/source/type_positions/field.aji new file mode 100644 index 0000000..e94bf7b --- /dev/null +++ b/tests/conformance/source/type_positions/field.aji @@ -0,0 +1,6 @@ +// expect: reject E7005 +// phase: 6 +// spec: docs/spec/15-source-language.md §10; docs/spec/07-air.md §8.8 +module test.type_positions +struct Bad { p: ref } +fn main() -> void effects [] {} diff --git a/tests/conformance/source/type_positions/graph_property.aji b/tests/conformance/source/type_positions/graph_property.aji new file mode 100644 index 0000000..4f1e389 --- /dev/null +++ b/tests/conformance/source/type_positions/graph_property.aji @@ -0,0 +1,6 @@ +// expect: reject E2111 +// phase: 6 +// spec: docs/spec/15-source-language.md §10; docs/spec/07-air.md §8.8 +module test.type_positions +fn unused(p: island>) -> void effects [] {} +fn main() -> void effects [] {} diff --git a/tests/conformance/source/type_positions/iref_ref.aji b/tests/conformance/source/type_positions/iref_ref.aji new file mode 100644 index 0000000..33fce2b --- /dev/null +++ b/tests/conformance/source/type_positions/iref_ref.aji @@ -0,0 +1,6 @@ +// expect: reject E7005 +// phase: 6 +// spec: docs/spec/15-source-language.md §10; docs/spec/07-air.md §8.8 +module test.type_positions +fn unused(p: iref>) -> void effects [] { trap; } +fn main() -> void effects [] {} diff --git a/tests/conformance/source/type_positions/let_annotation.aji b/tests/conformance/source/type_positions/let_annotation.aji new file mode 100644 index 0000000..064fc74 --- /dev/null +++ b/tests/conformance/source/type_positions/let_annotation.aji @@ -0,0 +1,6 @@ +// expect: reject E7005 +// phase: 6 +// spec: docs/spec/15-source-language.md §10; docs/spec/07-air.md §8.8 +module test.type_positions +fn bad(p: ref) -> void effects [] { let q: ref> = p; } +fn main() -> void effects [] {} diff --git a/tests/conformance/source/type_positions/local.aji b/tests/conformance/source/type_positions/local.aji new file mode 100644 index 0000000..71593ed --- /dev/null +++ b/tests/conformance/source/type_positions/local.aji @@ -0,0 +1,6 @@ +// expect: reject E7005 +// phase: 6 +// spec: docs/spec/15-source-language.md §10; docs/spec/07-air.md §8.8 +module test.type_positions +fn bad(p: ref) -> void effects [] { var q: ref> = zero; } +fn main() -> void effects [] {} diff --git a/tests/conformance/source/type_positions/mutex_ref.aji b/tests/conformance/source/type_positions/mutex_ref.aji new file mode 100644 index 0000000..94f437d --- /dev/null +++ b/tests/conformance/source/type_positions/mutex_ref.aji @@ -0,0 +1,6 @@ +// expect: reject E7005 +// phase: 6 +// spec: docs/spec/15-source-language.md §10; docs/spec/07-air.md §8.8 +module test.type_positions +fn unused(p: mutex>) -> void effects [] {} +fn main() -> void effects [] {} diff --git a/tests/conformance/source/type_positions/nested_mut.aji b/tests/conformance/source/type_positions/nested_mut.aji new file mode 100644 index 0000000..e069d23 --- /dev/null +++ b/tests/conformance/source/type_positions/nested_mut.aji @@ -0,0 +1,6 @@ +// expect: reject E7005 +// phase: 6 +// spec: docs/spec/15-source-language.md §10; docs/spec/07-air.md §8.8 +module test.type_positions +fn unused(p: mut>) -> void effects [] {} +fn main() -> void effects [] {} diff --git a/tests/conformance/source/type_positions/nested_ref.aji b/tests/conformance/source/type_positions/nested_ref.aji new file mode 100644 index 0000000..87bd248 --- /dev/null +++ b/tests/conformance/source/type_positions/nested_ref.aji @@ -0,0 +1,6 @@ +// expect: reject E7005 +// phase: 6 +// spec: docs/spec/15-source-language.md §10; docs/spec/07-air.md §8.8 +module test.type_positions +fn unused(p: ref>) -> void effects [] {} +fn main() -> void effects [] {} diff --git a/tests/conformance/source/type_positions/never_param.aji b/tests/conformance/source/type_positions/never_param.aji new file mode 100644 index 0000000..6ff4a43 --- /dev/null +++ b/tests/conformance/source/type_positions/never_param.aji @@ -0,0 +1,6 @@ +// expect: reject E7005 +// phase: 6 +// spec: docs/spec/15-source-language.md §10; docs/spec/07-air.md §8.8 +module test.type_positions +fn unused(p: never) -> void effects [] {} +fn main() -> void effects [] {} diff --git a/tests/conformance/source/type_positions/owner_ref.aji b/tests/conformance/source/type_positions/owner_ref.aji new file mode 100644 index 0000000..eeb2214 --- /dev/null +++ b/tests/conformance/source/type_positions/owner_ref.aji @@ -0,0 +1,6 @@ +// expect: reject E7005 +// phase: 6 +// spec: docs/spec/15-source-language.md §10; docs/spec/07-air.md §8.8 +module test.type_positions +fn unused(p: own>) -> void effects [] {} +fn main() -> void effects [] {} diff --git a/tests/conformance/source/type_positions/payload.aji b/tests/conformance/source/type_positions/payload.aji new file mode 100644 index 0000000..4edc4ed --- /dev/null +++ b/tests/conformance/source/type_positions/payload.aji @@ -0,0 +1,6 @@ +// expect: reject E7005 +// phase: 6 +// spec: docs/spec/15-source-language.md §10; docs/spec/07-air.md §8.8 +module test.type_positions +enum Bad { item(slice) } +fn main() -> void effects [] {} diff --git a/tests/conformance/source/type_positions/ref_void.aji b/tests/conformance/source/type_positions/ref_void.aji new file mode 100644 index 0000000..a51ce56 --- /dev/null +++ b/tests/conformance/source/type_positions/ref_void.aji @@ -0,0 +1,6 @@ +// expect: reject E7005 +// phase: 6 +// spec: docs/spec/15-source-language.md §10; docs/spec/07-air.md §8.8 +module test.type_positions +fn unused(p: ref) -> void effects [] {} +fn main() -> void effects [] {} diff --git a/tests/conformance/source/type_positions/sender_ref.aji b/tests/conformance/source/type_positions/sender_ref.aji new file mode 100644 index 0000000..8390def --- /dev/null +++ b/tests/conformance/source/type_positions/sender_ref.aji @@ -0,0 +1,6 @@ +// expect: reject E7005 +// phase: 6 +// spec: docs/spec/15-source-language.md §10; docs/spec/07-air.md §8.8 +module test.type_positions +fn unused(p: sender>) -> void effects [] {} +fn main() -> void effects [] {} diff --git a/tests/conformance/source/type_positions/slice_never.aji b/tests/conformance/source/type_positions/slice_never.aji new file mode 100644 index 0000000..0d2efbf --- /dev/null +++ b/tests/conformance/source/type_positions/slice_never.aji @@ -0,0 +1,6 @@ +// expect: reject E7005 +// phase: 6 +// spec: docs/spec/15-source-language.md §10; docs/spec/07-air.md §8.8 +module test.type_positions +fn unused(p: slice) -> void effects [] {} +fn main() -> void effects [] {} diff --git a/tests/conformance/source/type_positions/slice_ref.aji b/tests/conformance/source/type_positions/slice_ref.aji new file mode 100644 index 0000000..2a3c9a5 --- /dev/null +++ b/tests/conformance/source/type_positions/slice_ref.aji @@ -0,0 +1,6 @@ +// expect: reject E7005 +// phase: 6 +// spec: docs/spec/15-source-language.md §10; docs/spec/07-air.md §8.8 +module test.type_positions +fn unused(p: slice>) -> void effects [] {} +fn main() -> void effects [] {} diff --git a/tests/conformance/source/type_positions/specialized_field.aji b/tests/conformance/source/type_positions/specialized_field.aji new file mode 100644 index 0000000..b7f9f74 --- /dev/null +++ b/tests/conformance/source/type_positions/specialized_field.aji @@ -0,0 +1,7 @@ +// expect: reject E7005 +// phase: 6 +// spec: docs/spec/15-source-language.md §10; docs/spec/07-air.md §8.8 +module test.type_positions +struct Box { item: T } +fn bad(p: Box>) -> void effects [] {} +fn main() -> void effects [] {} diff --git a/tests/conformance/source/type_positions/specialized_inner.aji b/tests/conformance/source/type_positions/specialized_inner.aji new file mode 100644 index 0000000..8635bfd --- /dev/null +++ b/tests/conformance/source/type_positions/specialized_inner.aji @@ -0,0 +1,6 @@ +// expect: reject E7005 +// phase: 6 +// spec: docs/spec/15-source-language.md §10; docs/spec/07-air.md §8.8 +module test.type_positions +fn bad() -> own effects [] { trap; } +fn main() -> void effects [] { let x = bad::>(); trap; } diff --git a/tests/conformance/source/type_positions/task_member.aji b/tests/conformance/source/type_positions/task_member.aji new file mode 100644 index 0000000..685b7ea --- /dev/null +++ b/tests/conformance/source/type_positions/task_member.aji @@ -0,0 +1,6 @@ +// expect: reject E7005 +// phase: 6 +// spec: docs/spec/15-source-language.md §10; docs/spec/07-air.md §8.8 +module test.type_positions +fn unused(p: [task; 2]) -> void effects [] {} +fn main() -> void effects [] {} diff --git a/tests/conformance/source/type_positions/task_ref.aji b/tests/conformance/source/type_positions/task_ref.aji new file mode 100644 index 0000000..eaaaaea --- /dev/null +++ b/tests/conformance/source/type_positions/task_ref.aji @@ -0,0 +1,6 @@ +// expect: reject E7005 +// phase: 6 +// spec: docs/spec/15-source-language.md §10; docs/spec/07-air.md §8.8 +module test.type_positions +fn unused(p: task>) -> void effects [] {} +fn main() -> void effects [] {} diff --git a/tests/conformance/source/type_positions/unused_argument.aji b/tests/conformance/source/type_positions/unused_argument.aji new file mode 100644 index 0000000..80f5988 --- /dev/null +++ b/tests/conformance/source/type_positions/unused_argument.aji @@ -0,0 +1,7 @@ +// expect: reject E7005 +// phase: 6 +// spec: docs/spec/15-source-language.md §10; docs/spec/07-air.md §8.8 +module test.type_positions +struct Marker {} +fn unused(p: Marker>>) -> void effects [] {} +fn main() -> void effects [] {} diff --git a/tests/conformance/source/type_positions/unused_generic.aji b/tests/conformance/source/type_positions/unused_generic.aji new file mode 100644 index 0000000..2df53f2 --- /dev/null +++ b/tests/conformance/source/type_positions/unused_generic.aji @@ -0,0 +1,6 @@ +// expect: reject E7005 +// phase: 6 +// spec: docs/spec/15-source-language.md §10; docs/spec/07-air.md §8.8 +module test.type_positions +fn unused(p: ref>) -> void effects [] {} +fn main() -> void effects [] {} diff --git a/tests/conformance/source/type_positions/unused_generic_field.aji b/tests/conformance/source/type_positions/unused_generic_field.aji new file mode 100644 index 0000000..6a45fc2 --- /dev/null +++ b/tests/conformance/source/type_positions/unused_generic_field.aji @@ -0,0 +1,6 @@ +// expect: reject E7005 +// phase: 6 +// spec: docs/spec/15-source-language.md §10; docs/spec/07-air.md §8.8 +module test.type_positions +struct Bad { p: ref } +fn main() -> void effects [] {} diff --git a/tests/conformance/source/type_positions/unused_generic_payload.aji b/tests/conformance/source/type_positions/unused_generic_payload.aji new file mode 100644 index 0000000..6cdb39d --- /dev/null +++ b/tests/conformance/source/type_positions/unused_generic_payload.aji @@ -0,0 +1,6 @@ +// expect: reject E7005 +// phase: 6 +// spec: docs/spec/15-source-language.md §10; docs/spec/07-air.md §8.8 +module test.type_positions +enum Bad { item([mut; 2]) } +fn main() -> void effects [] {} diff --git a/tests/conformance/source/type_positions/unused_generic_return.aji b/tests/conformance/source/type_positions/unused_generic_return.aji new file mode 100644 index 0000000..63945ae --- /dev/null +++ b/tests/conformance/source/type_positions/unused_generic_return.aji @@ -0,0 +1,6 @@ +// expect: reject E7005 +// phase: 6 +// spec: docs/spec/15-source-language.md §10; docs/spec/07-air.md §8.8 +module test.type_positions +fn unused(p: mut) -> ref> from p effects [] {} +fn main() -> void effects [] {} diff --git a/tests/conformance/source/type_positions/unused_static_contract.aji b/tests/conformance/source/type_positions/unused_static_contract.aji new file mode 100644 index 0000000..b671d49 --- /dev/null +++ b/tests/conformance/source/type_positions/unused_static_contract.aji @@ -0,0 +1,6 @@ +// expect: reject E7005 +// phase: 6 +// spec: docs/spec/15-source-language.md §10; docs/spec/07-air.md §8.8 +module test.type_positions +fn unused>) -> void effects []>() -> void effects [] {} +fn main() -> void effects [] {} diff --git a/tests/conformance/source/type_positions/unused_static_void.aji b/tests/conformance/source/type_positions/unused_static_void.aji new file mode 100644 index 0000000..a033066 --- /dev/null +++ b/tests/conformance/source/type_positions/unused_static_void.aji @@ -0,0 +1,6 @@ +// expect: reject E7005 +// phase: 6 +// spec: docs/spec/15-source-language.md §10; docs/spec/07-air.md §8.8 +module test.type_positions +fn unused void effects []>() -> void effects [] {} +fn main() -> void effects [] {} diff --git a/tests/conformance/source/type_positions/valid.aji b/tests/conformance/source/type_positions/valid.aji new file mode 100644 index 0000000..4c59105 --- /dev/null +++ b/tests/conformance/source/type_positions/valid.aji @@ -0,0 +1,11 @@ +// expect: run +// result: 42 +// phase: 6 +// spec: docs/spec/15-source-language.md §10 +module test.type_positions +struct Pair { values: [u64; 2] } +fn first(p: ref<[u64; 2]>) -> ref from p effects [] { ref item = p[0]; return item; } +fn read(p: ref) -> u64 effects [] { ref values = p.values; return values[0]; } +fn forward(p: ref) -> T effects [] { return *p; } +fn unused(p: own) -> void effects [] { trap; } +fn main() -> u64 effects [] { let p = Pair { values: [21u64; 2] }; var x: u64 = 21; return read(ref p) + forward::(ref x); } diff --git a/tests/conformance/source/type_positions/valid_unused.aji b/tests/conformance/source/type_positions/valid_unused.aji new file mode 100644 index 0000000..fb87f19 --- /dev/null +++ b/tests/conformance/source/type_positions/valid_unused.aji @@ -0,0 +1,7 @@ +// expect: accept +// phase: 6 +// spec: docs/spec/15-source-language.md §15/§18 +module test.type_positions +struct Box { item: T } +fn unused, mut) -> void effects []>(p: ref, b: Box) -> T effects [] { trap; } +fn main() -> void effects [] {} diff --git a/tests/conformance/source/type_positions/void_param.aji b/tests/conformance/source/type_positions/void_param.aji new file mode 100644 index 0000000..82c1a7b --- /dev/null +++ b/tests/conformance/source/type_positions/void_param.aji @@ -0,0 +1,6 @@ +// expect: reject E7005 +// phase: 6 +// spec: docs/spec/15-source-language.md §10; docs/spec/07-air.md §8.8 +module test.type_positions +fn unused(p: void) -> void effects [] {} +fn main() -> void effects [] {} diff --git a/tests/hosted/source_type_positions.py b/tests/hosted/source_type_positions.py new file mode 100644 index 0000000..b717f08 --- /dev/null +++ b/tests/hosted/source_type_positions.py @@ -0,0 +1,89 @@ +#!/usr/bin/env python3 +"""Source type-position errors: exact annotations, CLI/package parity and deferred types.""" +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/type_positions' +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 rejection(path, position, constructor, token, *args, expected_file=None, line=5): + error = json.loads(cli('check', *args, code=1)) + assert error['code'] == 'E7005', error + assert error['position'] == position and error['type_constructor'] == constructor, error + text = path.read_text().splitlines()[line - 1] + assert (error['file'], error['line'], error['col']) == ( + expected_file or str(path), line, text.index(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, position, constructor, token in ( + ('nested_ref', 'borrowed', 'ref', 'ref'), + ('nested_mut', 'borrowed', 'slice', 'slice'), + ('array_ref', 'member', 'ref', 'ref'), + ('field', 'member', 'ref', 'ref'), + ('payload', 'member', 'slice', 'slice'), + ('void_param', 'param', 'void', 'void)'), + ('unused_generic', 'borrowed', 'mut', 'mut'), + ('unused_generic_field', 'member', 'ref', 'ref'), + ('unused_static_void', 'param', 'void', 'void)'), + ('byte_annotation', 'borrowed', 'ref', 'ref'), + ('iref_ref', 'member', 'ref', 'ref'), + ('buffer_new_ref', 'member', 'ref', 'ref'), +): + path = CORPUS / f'{name}.aji' + rejection(path, position, constructor, token, path) + +# This header is legal while T is unknown, then fails only at substitution. +specialized = json.loads(cli('check', CORPUS / 'specialized_inner.aji', code=1)) +assert (specialized['code'], specialized['position'], specialized['type_constructor']) == ( + 'E7005', 'member', 'ref'), specialized +assert specialized['line'] == 6 and specialized['col'] == 46, specialized +assert 'air_line' not in specialized, specialized + +with tempfile.TemporaryDirectory(prefix='aji-type-positions-') as directory: + root = Path(directory) + raw = root / 'lowered.air' + source = CORPUS / 'valid.aji' + air = cli('lower', source) + assert cli('lower', source) == air + raw.write_text(air) + cli('check', raw) + expected = json.loads(cli('run', '--fuel', '100000', source)) + assert expected['value'] == 42 + assert json.loads(cli('run', '--fuel', '100000', raw)) == expected + result = root / 'hosted.json' + cli('run', '--profile', 'hosted', '--fuel', '100000', '--result-file', result, source) + assert json.loads(result.read_text())['value'] == 42 + + entry, library, manifest = (root / name for name in ('main.aji', 'types.aji', 'aji.toml')) + entry.write_text('module app\nimport lib.types;\nfn main() -> u64 effects [] { return 42; }\n') + library.write_text('module lib.types\nfn unused(p: ref>) -> void effects [] {}\n') + manifest.write_text('[package]\nname = "positions"\nversion = "1"\nentry = "main.aji"\n' + '[modules]\nlib.types = "types.aji"\n') + rejection(library, 'borrowed', 'ref', 'ref', '--manifest', manifest, + expected_file='types.aji', line=2) + + # A type parameter whose class is unknown stays legal in an unused header. + library.write_text('module lib.types\nfn unused(p: ref) -> void effects [] {}\n') + assert json.loads(cli('run', '--manifest', manifest))['value'] == 42 + # Invalid mapped modules that are unreachable remain outside compilation. + entry.write_text(entry.read_text().replace('import lib.types;\n', '')) + library.write_text('module lib.types\nfn unused(p: ref>) -> void effects [] {}\n') + assert json.loads(cli('run', '--manifest', manifest))['value'] == 42 + +print(f'type position CLI integration passed ({checks} checks)')