From cfbe01a16f4763ff0f310e591991dbbed57a303f Mon Sep 17 00:00:00 2001 From: "A.Shakhmatov" Date: Thu, 8 Oct 2026 09:01:58 +0300 Subject: [PATCH] cli: validate Copy content of source owning arrays --- .github/workflows/ci.yml | 1 + CONTRIBUTING.md | 1 + docs/agent/README.md | 2 + docs/design/open-questions.md | 14 +- docs/plan/phase-6-source-language.md | 8 + docs/spec/15-source-language.md | 1 + src/frontend/generics.zig | 14 +- src/frontend/lower.zig | 22 ++- src/frontend/type_properties.zig | 150 ++++++++++++++++++ .../source/own_array_content/deep_nominal.aji | 86 ++++++++++ .../source/own_array_content/enum.aji | 7 + .../source/own_array_content/field.aji | 6 + .../source/own_array_content/forward.aji | 7 + .../source/own_array_content/generic.aji | 6 + .../own_array_content/generic_member.aji | 6 + .../own_array_content/generic_nominal.aji | 7 + .../source/own_array_content/graph_owner.aji | 7 + .../own_array_content/graph_property.aji | 6 + .../source/own_array_content/iref_copy.aji | 7 + .../source/own_array_content/iref_nested.aji | 6 + .../source/own_array_content/local.aji | 6 + .../source/own_array_content/memo_cycle.aji | 9 ++ .../source/own_array_content/nested.aji | 6 + .../own_array_content/nested_generic.aji | 7 + .../source/own_array_content/operation.aji | 6 + .../own_array_content/operation_array_ref.aji | 6 + .../operation_deep_nominal.aji | 7 + .../own_array_content/operation_ref.aji | 6 + .../own_array_content/operation_slice.aji | 6 + .../own_array_content/operation_void.aji | 6 + .../source/own_array_content/owner.aji | 6 + .../source/own_array_content/phantom.aji | 7 + .../own_array_content/recursive_array.aji | 6 + .../own_array_content/recursive_value.aji | 7 + .../own_array_content/rotated_generic.aji | 7 + .../source/own_array_content/rotated_safe.aji | 7 + .../own_array_content/shadow_builtin.aji | 10 ++ .../source/own_array_content/specialized.aji | 7 + .../source/own_array_content/static.aji | 6 + .../source/own_array_content/valid.aji | 9 ++ .../source/own_array_content/valid_unused.aji | 8 + tests/hosted/source_own_array_content.py | 102 ++++++++++++ 42 files changed, 608 insertions(+), 3 deletions(-) create mode 100644 src/frontend/type_properties.zig create mode 100644 tests/conformance/source/own_array_content/deep_nominal.aji create mode 100644 tests/conformance/source/own_array_content/enum.aji create mode 100644 tests/conformance/source/own_array_content/field.aji create mode 100644 tests/conformance/source/own_array_content/forward.aji create mode 100644 tests/conformance/source/own_array_content/generic.aji create mode 100644 tests/conformance/source/own_array_content/generic_member.aji create mode 100644 tests/conformance/source/own_array_content/generic_nominal.aji create mode 100644 tests/conformance/source/own_array_content/graph_owner.aji create mode 100644 tests/conformance/source/own_array_content/graph_property.aji create mode 100644 tests/conformance/source/own_array_content/iref_copy.aji create mode 100644 tests/conformance/source/own_array_content/iref_nested.aji create mode 100644 tests/conformance/source/own_array_content/local.aji create mode 100644 tests/conformance/source/own_array_content/memo_cycle.aji create mode 100644 tests/conformance/source/own_array_content/nested.aji create mode 100644 tests/conformance/source/own_array_content/nested_generic.aji create mode 100644 tests/conformance/source/own_array_content/operation.aji create mode 100644 tests/conformance/source/own_array_content/operation_array_ref.aji create mode 100644 tests/conformance/source/own_array_content/operation_deep_nominal.aji create mode 100644 tests/conformance/source/own_array_content/operation_ref.aji create mode 100644 tests/conformance/source/own_array_content/operation_slice.aji create mode 100644 tests/conformance/source/own_array_content/operation_void.aji create mode 100644 tests/conformance/source/own_array_content/owner.aji create mode 100644 tests/conformance/source/own_array_content/phantom.aji create mode 100644 tests/conformance/source/own_array_content/recursive_array.aji create mode 100644 tests/conformance/source/own_array_content/recursive_value.aji create mode 100644 tests/conformance/source/own_array_content/rotated_generic.aji create mode 100644 tests/conformance/source/own_array_content/rotated_safe.aji create mode 100644 tests/conformance/source/own_array_content/shadow_builtin.aji create mode 100644 tests/conformance/source/own_array_content/specialized.aji create mode 100644 tests/conformance/source/own_array_content/static.aji create mode 100644 tests/conformance/source/own_array_content/valid.aji create mode 100644 tests/conformance/source/own_array_content/valid_unused.aji create mode 100644 tests/hosted/source_own_array_content.py diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 2dff482..edcb47f 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -29,6 +29,7 @@ jobs: - 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/source_own_array_content.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 eb381ac..8af7551 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -25,6 +25,7 @@ python3 tests/hosted/related_locations.py # factual conflict/spawn positions, so 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/source_own_array_content.py # Copy content, nominal/generic types and repeated allocations 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 ab37a8e..13a539e 100644 --- a/docs/agent/README.md +++ b/docs/agent/README.md @@ -156,3 +156,5 @@ Q-041 source scope names (DD-020, cl100k): Quick Reference 8,859 → 8,889 (+30) 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. + +Q-043 dynamic owning-array content (cl100k): Quick Reference stays 8,948; other source docs 66,761 → 66,827 (+66), full source 75,709 → 75,775 (+0.09%). o200k groups are 8,984 / 67,031 / 76,015; Qwen2.5 9,226 / 68,466 / 77,692. AIR remains 184,628 bytes / 47,445 cl100k tokens; library/std.md is unchanged. Existing Copy-content rules now reject annotations and repeated allocation values with structured E7005 before AIR, including known unused generic/static contracts and nominal contents. Unknown parameters defer to substitution; iref remains phantom. No new concept, syntax, code, feature or verifier/runtime state is added. Earlier invalid-source diagnostics and priority may change; graph-root 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 036198b..1c2ed1c 100644 --- a/docs/design/open-questions.md +++ b/docs/design/open-questions.md @@ -319,4 +319,16 @@ Read-only P6-2 review reproduced `module nested fn f(p: ref>) -> void e 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. +**Resolved (spec 15 §10):** annotation and repeated-allocation checks report E7005 at the non-Copy content/value, with `constraint: Copy` and `type_constructor: own_array`. The shared AST property query follows nominal fields and enum payloads in their declaring namespace, honors user types before built-in names, and keeps `iref` content phantom. Unused generic/static headers reject when linear content is already known; unknown parameter properties wait for substitution. Recursive generic paths are distinguished by the known-linear parameter vector, so parameter rotations cannot hide a known linear leaf. Pure by-value cycles retain E1003; a proven Copy-content violation may reject before a simultaneous layout error. Graph-root content checks retain E2111. AIR acceptance, codes, type identity, allocation/cleanup and interpreter behavior are unchanged; invalid source programs now stop earlier instead of producing invalid AIR. No new syntax, code, feature or verifier/runtime state is introduced. + +## Q-044 — Nominal linearity query cost before layout rejection + +Q-043 review identified a separate existing cost in `Lowerer.linearAt`: repeated fields and by-value cycles revisit nominal declarations until its depth bound, with no shared-result cache. `checkRequirements` asks this query before `checkFiniteLayouts` rejects E1003. The bound guarantees finite recursion but does not bound the number of branching revisits. Q-043's new annotation query has counted tests for shared acyclic/cyclic branches; the old classification query is unchanged. The precise cost and scope of other nominal property queries remain unverified. + +Audit the existing classification/layout ordering and shared-query cost with counted tests, preserving structural ownership classes, graph/iref boundaries, source E1003 provenance, accepted-source canonical AIR and frozen AIR behavior. Avoid wall-clock timeout tests. This is an implementation audit, not a new recursion or memory rule. + +## Q-045 — Source array literals with second-class elements + +Q-043 review reproduced `fn unused(p: ref) -> void effects [] { let a = [p, p]; }`: the source array literal reaches AIR type checking with E1001 because borrows cannot be stored as array elements. Q-042 validates annotations; an inferred literal has none. Q-043 now rejects that array as a repeated `own.array` value, but constructing it without that operation remains a separate source validation gap. + +Audit inferred list/repeat literals against existing member-position and Copy-duplication rules, including nested arrays and nominal values. Add source positives/rejections with E7005 at the offending source value while preserving explicit moves in fixed linear arrays, borrow provenance and frozen AIR semantics. Scope beyond this reproduced literal remains unverified. diff --git a/docs/plan/phase-6-source-language.md b/docs/plan/phase-6-source-language.md index f2b8f30..55e650f 100644 --- a/docs/plan/phase-6-source-language.md +++ b/docs/plan/phase-6-source-language.md @@ -245,3 +245,11 @@ Q-042/spec 15 §10 closes source annotation gaps using the verifier's existing p 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. + +## P6-2 dynamic owning-array content (2026-10-08) + +Q-043/spec 15 §10 enforces the existing Copy-content requirement for source `own_array` annotations and `own.array` repeated values before AIR emission. One AST property query follows nominal fields/payloads and generic parameter states without calling lowering recursively. User type names precede built-ins; imported fields use their declaring namespace. Unused generic/static headers reject provable linear content; unknown parameters defer to substitution. A query-local cache shares completed branches, keeps positive witnesses final and rechecks negative results whenever a new positive proof is learned. Parameter-state queries repeat until that generation stabilizes; memo lookup is a linear scan, trading peak cost for implementation simplicity. Parameter rotations and deep nominal chains cannot hide a known linear leaf; iref content remains phantom and graph-root properties retain E2111. + +The preserved invariant is that repeated dynamic heap arrays contain Copy values, so bulk release cannot destroy nested resources. AIR independently retains the same type/operation checks; allocation budgets, initialization, owner transfer, explicit release and deterministic interpreter behavior are unchanged. Valid Copy allocations run with explicit release; rejected annotations and repeated linear values report E7005 with `constraint` and `type_constructor`. Existing invalid-source codes/priority may change, including Copy violations preceding simultaneous layout errors. No syntax, code, feature, verifier/runtime state or evaluation input changes. Source bundle growth is recorded in docs/agent/README.md; P6-2 remains open. + +Validation: full conformance 1,335/1,335; all 32 new fixtures also pass with per-instruction collection. Unit tests pass 128/128, including counted acyclic/cyclic diamond queries; the cyclic case failed before the cache correction. CLI integration passes 119 checks with exact annotations/value positions, nested package imports and source/AIR/hosted parity. Formatter passes 4,563 checks over 1,047 public files (1,039 parsed, eight existing syntax errors, 638 identical AIR outputs); source coverage remains 121/121. Comparison of all 1,017 preceding public sources preserves every check/lower result and all 631 successful canonical lowerings. No AIR verifier/runtime files change. Independent Claude review passes after checking shadowing, generic rotations, cache invalidation and the complete declaration snapshot used by the shared Copy predicate. diff --git a/docs/spec/15-source-language.md b/docs/spec/15-source-language.md index 745ae84..438b77c 100644 --- a/docs/spec/15-source-language.md +++ b/docs/spec/15-source-language.md @@ -403,6 +403,7 @@ A byte string or array used where a `slice` is expected is materialized in f - 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). +- Copy content of `own_array` and the repeated value of `own.array`; an invalid content type is E7005 at that annotation/value, with `constraint: Copy` and `type_constructor: own_array`. Known linear content in unused generic signatures and static contracts MUST reject; unknown type parameters are checked after substitution. 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. diff --git a/src/frontend/generics.zig b/src/frontend/generics.zig index 06f8f50..0a5c0ca 100644 --- a/src/frontend/generics.zig +++ b/src/frontend/generics.zig @@ -3,6 +3,7 @@ const std = @import("std"); const ast = @import("ast.zig"); const lower = @import("lower.zig"); const type_positions = @import("type_positions.zig"); +const type_properties = @import("type_properties.zig"); const authority = @import("../air/authority.zig"); const A = std.mem.Allocator; const Error = error{ OutOfMemory, InvalidGeneric }; @@ -189,7 +190,18 @@ const Generator = struct { }) }; return error.InvalidGeneric; } - return g.headerType(t, params, depth); + try g.headerType(t, params, depth); + if (try type_properties.firstInvalid(t, g, propertyDecl, params)) |bad| { + g.problem = .{ .code = "E7005", .span = bad.span, .message = "dynamic heap arrays require Copy content", .fields = try g.arena.dupe(lower.Problem.Field, &.{ + .{ .name = "constraint", .value = "Copy" }, + .{ .name = "type_constructor", .value = "own_array" }, + }) }; + return error.InvalidGeneric; + } + } + + fn propertyDecl(g: *Generator, name: []const u8, span: ast.Span) ?ast.TypeDecl { + return g.typeDecl(name, span); } fn headerType(g: *Generator, t: ast.Type, params: []const ast.TypeParam, depth: usize) Error!void { diff --git a/src/frontend/lower.zig b/src/frontend/lower.zig index 5e37dd0..6074685 100644 --- a/src/frontend/lower.zig +++ b/src/frontend/lower.zig @@ -32,6 +32,7 @@ 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 type_properties = @import("type_properties.zig"); const Allocator = std.mem.Allocator; const Span = ast.Span; @@ -325,6 +326,8 @@ const Lowerer = struct { fns: std.ArrayList(FnOut) = .empty, linear_enabled: bool = false, + /// Complete declaration snapshot, with finite layouts checked before bodies. + property_module: ?air_module.Module = null, fn probeType(l: *Lowerer, t: ast.Type) ?Ty { const previous = l.problem; @@ -343,6 +346,7 @@ const Lowerer = struct { fn isLinear(l: *Lowerer, t: Ty) bool { return l.linearAt(t, 0); } + fn linearAt(l: *Lowerer, t: Ty, depth: usize) bool { if (t.isLinear()) return true; if (depth > l.m.types.len + 32) return false; @@ -559,6 +563,7 @@ const Lowerer = struct { const d = l.m.types[layout_context.diags.items[0].span.?.line - 1]; return l.fail("E1003", d.span, "aggregate type contains itself by value", &.{.{ .name = "type", .value = d.display_name orelse d.name }}); } + l.property_module = m; for (l.m.requirements) |r| { const ty = try l.typeOf(r.ty); const t = try l.airType(ty); @@ -682,7 +687,21 @@ const Lowerer = struct { .{ .name = "position", .value = @tagName(bad.position) }, .{ .name = "type_constructor", .value = bad.constructor }, }); - return l.resolveType(t); + const ty = try l.resolveType(t); + if (try type_properties.firstInvalid(t, l, propertyDecl, &.{})) |bad| return l.copyArrayError(bad.span); + return ty; + } + + fn propertyDecl(l: *Lowerer, name: []const u8, span: Span) ?ast.TypeDecl { + const ty = l.enumOrStruct(name, span) orelse return null; + return l.m.types[ty.named]; + } + + fn copyArrayError(l: *Lowerer, span: Span) Error { + return l.fail("E7005", span, "dynamic heap arrays require Copy content", &.{ + .{ .name = "constraint", .value = "Copy" }, + .{ .name = "type_constructor", .value = "own_array" }, + }); } fn resolveType(l: *Lowerer, t: ast.Type) Error!Ty { @@ -3242,6 +3261,7 @@ const Lowerer = struct { const n = try l.expr(args[0], usize_ty); try l.checkTy(args[0].span, n.ty, usize_ty); const v = try l.consuming(args[1], null); + if (!verifier_types.isCopy(l.property_module.?, try l.airType(v.ty))) return l.copyArrayError(args[1].span); const c = try l.expectCap(args[2], "mem.alloc"); return .{ .text = try l.fmt("own.array %{s}, %{s}, %{s}", .{ n.name, v.name, c.name }), .outcomes = try l.outs(&.{ .{ .role = "ok", .params = &.{try l.wrap(.own_array, v.ty)} }, .{ .role = "err", .params = &.{alloc_err} } }) }; } diff --git a/src/frontend/type_properties.zig b/src/frontend/type_properties.zig new file mode 100644 index 0000000..71b54a3 --- /dev/null +++ b/src/frontend/type_properties.zig @@ -0,0 +1,150 @@ +//! Known structural linearity without resolving types or specializing templates. +//! Unknown parameters add no known linear leaf; iref content is phantom. +const std = @import("std"); +const ast = @import("ast.zig"); + +const Env = struct { + params: []const ast.TypeParam, + known_linear: []const bool = &.{}, +}; +// Parameter states, rather than argument syntax, distinguish rotations along +// recursive generic edges. Repeated states add no new known linear leaf. +const Path = struct { name: []const u8, known_linear: []const bool, parent: ?*const Path }; +const Memo = struct { + entries: std.ArrayList(struct { name: []const u8, known_linear: []const bool, value: bool, generation: usize }) = .empty, + generation: usize = 0, +}; + +pub fn firstInvalid(t: ast.Type, context: anytype, lookup: anytype, params: []const ast.TypeParam) std.mem.Allocator.Error!?ast.Type { + switch (t.kind) { + .wrapped => |w| { + if (std.mem.eql(u8, w.wrapper, "own_array")) { + var memo: Memo = .{}; + while (true) { + const generation = memo.generation; + if (try linear(w.inner.*, context, lookup, &.{ .params = params }, null, &memo)) return w.inner.*; + if (generation == memo.generation) break; + } + } + // Graph well-formedness keeps its independently specified E2111. + for ([_][]const u8{ "island", "frozen", "arc" }) |tag| if (std.mem.eql(u8, w.wrapper, tag)) return null; + return firstInvalid(w.inner.*, context, lookup, params); + }, + .array => |a| return firstInvalid(a.elem.*, context, lookup, params), + else => return null, + } +} + +fn linear(t: ast.Type, context: anytype, lookup: anytype, env: *const Env, path: ?*const Path, memo: *Memo) std.mem.Allocator.Error!bool { + switch (t.kind) { + .file => return true, + .wrapped => |w| { + for ([_][]const u8{ "own", "own_array", "buffer", "task", "sender", "receiver", "mutex", "island", "frozen", "arc", "atomic" }) |tag| if (std.mem.eql(u8, w.wrapper, tag)) return true; + return false; + }, + .array => |a| return linear(a.elem.*, context, lookup, env, path, memo), + .name => |name| { + for (env.params, 0..) |p, i| if (std.mem.eql(u8, p.name, name)) { + return i < env.known_linear.len and env.known_linear[i]; + }; + if (lookup(context, name, t.span)) |d| return declaration(d, &.{}, context, lookup, env, path, memo); + for ([_][]const u8{ "region", "stream", "listener" }) |tag| if (std.mem.eql(u8, name, tag)) return true; + return false; + }, + .applied => |a| { + const d = lookup(context, a.name, t.span) orelse return false; + return declaration(d, a.args, context, lookup, env, path, memo); + }, + else => return false, + } +} + +fn declaration(d: ast.TypeDecl, args: []const ast.Type, context: anytype, lookup: anytype, env: *const Env, path: ?*const Path, memo: *Memo) std.mem.Allocator.Error!bool { + const generation = memo.generation; + const known = try context.arena.alloc(bool, d.generics.len); + for (known, 0..) |*flag, i| flag.* = i < args.len and try linear(args[i], context, lookup, env, path, memo); + for (memo.entries.items) |entry| if ((entry.value or entry.generation == memo.generation) and std.mem.eql(u8, entry.name, d.name) and std.mem.eql(bool, entry.known_linear, known)) return entry.value; + var previous = path; + while (previous) |p| : (previous = p.parent) { + if (std.mem.eql(u8, p.name, d.name) and std.mem.eql(bool, p.known_linear, known)) { + return false; + } + } + const next: Path = .{ .name = d.name, .known_linear = known, .parent = path }; + const bound: Env = .{ .params = d.generics, .known_linear = known }; + const value = switch (d.kind) { + .@"struct" => |fs| for (fs) |f| { + if (try linear(f.ty, context, lookup, &bound, &next, memo)) break true; + } else false, + .@"enum" => |vs| result: { + for (vs) |v| for (v.payload) |t| { + if (try linear(t, context, lookup, &bound, &next, memo)) break :result true; + }; + break :result false; + }, + }; + // Positive witnesses are final. Learning one invalidates provisional + // negatives, including cycles whose back edge could reach that witness. + // A negative root is queried again until no new positive proof is learned. + if (value) memo.generation += 1; + try memo.entries.append(context.arena, .{ .name = d.name, .known_linear = known, .value = value, .generation = if (value) memo.generation else generation }); + return value; +} + +test "Copy-content query shares completed diamond branches" { + var arena_state = std.heap.ArenaAllocator.init(std.testing.allocator); + defer arena_state.deinit(); + const arena = arena_state.allocator(); + var source: std.ArrayList(u8) = .empty; + try source.appendSlice(arena, "module diamond\n"); + for (0..64) |i| { + const child = if (i == 63) "u64" else try std.fmt.allocPrint(arena, "N{d}", .{i + 1}); + try source.appendSlice(arena, try std.fmt.allocPrint(arena, "struct N{d} {{ a: {s}, b: {s} }}\n", .{ i, child, child })); + } + try source.appendSlice(arena, "fn unused(p: own_array) -> void effects [] { trap; }\nfn main() -> void effects [] {}\n"); + const parsed = try @import("parse.zig").parse(arena, source.items); + try std.testing.expect(parsed == .ok); + const Probe = struct { + arena: std.mem.Allocator, + module: ast.Module, + lookups: usize = 0, + fn lookup(p: *@This(), name: []const u8, _: ast.Span) ?ast.TypeDecl { + p.lookups += 1; + for (p.module.types) |d| if (std.mem.eql(u8, d.name, name)) return d; + return null; + } + }; + var probe: Probe = .{ .arena = arena, .module = parsed.ok }; + try std.testing.expect(try firstInvalid(parsed.ok.functions[0].params[0].ty, &probe, Probe.lookup, &.{}) == null); + // An unshared walk visits 2^64 branches. Count queries, not elapsed time. + try std.testing.expect(probe.lookups <= 130); +} + +test "Copy-content query shares cyclic diamond branches" { + var arena_state = std.heap.ArenaAllocator.init(std.testing.allocator); + defer arena_state.deinit(); + const arena = arena_state.allocator(); + var source: std.ArrayList(u8) = .empty; + try source.appendSlice(arena, "module diamond\n"); + for (0..10) |i| { + const child = if (i == 9) "N0" else try std.fmt.allocPrint(arena, "N{d}", .{i + 1}); + try source.appendSlice(arena, try std.fmt.allocPrint(arena, "struct N{d} {{ a: {s}, b: {s} }}\n", .{ i, child, child })); + } + try source.appendSlice(arena, "fn unused(p: own_array) -> void effects [] { trap; }\nfn main() -> void effects [] {}\n"); + const parsed = try @import("parse.zig").parse(arena, source.items); + try std.testing.expect(parsed == .ok); + const Probe = struct { + arena: std.mem.Allocator, + module: ast.Module, + lookups: usize = 0, + fn lookup(p: *@This(), name: []const u8, _: ast.Span) ?ast.TypeDecl { + p.lookups += 1; + for (p.module.types) |d| if (std.mem.eql(u8, d.name, name)) return d; + return null; + } + }; + var probe: Probe = .{ .arena = arena, .module = parsed.ok }; + try std.testing.expect(try firstInvalid(parsed.ok.functions[0].params[0].ty, &probe, Probe.lookup, &.{}) == null); + // An unshared walk repeats every path to the same cycle. Count queries, not elapsed time. + try std.testing.expect(probe.lookups <= 22); +} diff --git a/tests/conformance/source/own_array_content/deep_nominal.aji b/tests/conformance/source/own_array_content/deep_nominal.aji new file mode 100644 index 0000000..96c6103 --- /dev/null +++ b/tests/conformance/source/own_array_content/deep_nominal.aji @@ -0,0 +1,86 @@ +// expect: reject E7005 +// phase: 6 +// spec: docs/spec/15-source-language.md §10; docs/spec/03-memory-model.md §25 +module test.own_array_content +struct N0 { item: N1 } +struct N1 { item: N2 } +struct N2 { item: N3 } +struct N3 { item: N4 } +struct N4 { item: N5 } +struct N5 { item: N6 } +struct N6 { item: N7 } +struct N7 { item: N8 } +struct N8 { item: N9 } +struct N9 { item: N10 } +struct N10 { item: N11 } +struct N11 { item: N12 } +struct N12 { item: N13 } +struct N13 { item: N14 } +struct N14 { item: N15 } +struct N15 { item: N16 } +struct N16 { item: N17 } +struct N17 { item: N18 } +struct N18 { item: N19 } +struct N19 { item: N20 } +struct N20 { item: N21 } +struct N21 { item: N22 } +struct N22 { item: N23 } +struct N23 { item: N24 } +struct N24 { item: N25 } +struct N25 { item: N26 } +struct N26 { item: N27 } +struct N27 { item: N28 } +struct N28 { item: N29 } +struct N29 { item: N30 } +struct N30 { item: N31 } +struct N31 { item: N32 } +struct N32 { item: N33 } +struct N33 { item: N34 } +struct N34 { item: N35 } +struct N35 { item: N36 } +struct N36 { item: N37 } +struct N37 { item: N38 } +struct N38 { item: N39 } +struct N39 { item: N40 } +struct N40 { item: N41 } +struct N41 { item: N42 } +struct N42 { item: N43 } +struct N43 { item: N44 } +struct N44 { item: N45 } +struct N45 { item: N46 } +struct N46 { item: N47 } +struct N47 { item: N48 } +struct N48 { item: N49 } +struct N49 { item: N50 } +struct N50 { item: N51 } +struct N51 { item: N52 } +struct N52 { item: N53 } +struct N53 { item: N54 } +struct N54 { item: N55 } +struct N55 { item: N56 } +struct N56 { item: N57 } +struct N57 { item: N58 } +struct N58 { item: N59 } +struct N59 { item: N60 } +struct N60 { item: N61 } +struct N61 { item: N62 } +struct N62 { item: N63 } +struct N63 { item: N64 } +struct N64 { item: N65 } +struct N65 { item: N66 } +struct N66 { item: N67 } +struct N67 { item: N68 } +struct N68 { item: N69 } +struct N69 { item: N70 } +struct N70 { item: N71 } +struct N71 { item: N72 } +struct N72 { item: N73 } +struct N73 { item: N74 } +struct N74 { item: N75 } +struct N75 { item: N76 } +struct N76 { item: N77 } +struct N77 { item: N78 } +struct N78 { item: N79 } +struct N79 { item: own } +fn unused(p: own_array) -> void effects [] { trap; } +fn main() -> void effects [] {} diff --git a/tests/conformance/source/own_array_content/enum.aji b/tests/conformance/source/own_array_content/enum.aji new file mode 100644 index 0000000..3fdc726 --- /dev/null +++ b/tests/conformance/source/own_array_content/enum.aji @@ -0,0 +1,7 @@ +// expect: reject E7005 +// phase: 6 +// spec: docs/spec/15-source-language.md §10; docs/spec/03-memory-model.md §25 +module test.own_array_content +enum Linear { Plain, Owned(own) } +fn unused(p: own_array) -> void effects [] { trap; } +fn main() -> void effects [] {} diff --git a/tests/conformance/source/own_array_content/field.aji b/tests/conformance/source/own_array_content/field.aji new file mode 100644 index 0000000..cce1d47 --- /dev/null +++ b/tests/conformance/source/own_array_content/field.aji @@ -0,0 +1,6 @@ +// expect: reject E7005 +// phase: 6 +// spec: docs/spec/15-source-language.md §10; docs/spec/03-memory-model.md §25 +module test.own_array_content +struct Bad { items: own_array> } +fn main() -> void effects [] {} diff --git a/tests/conformance/source/own_array_content/forward.aji b/tests/conformance/source/own_array_content/forward.aji new file mode 100644 index 0000000..aeb0f58 --- /dev/null +++ b/tests/conformance/source/own_array_content/forward.aji @@ -0,0 +1,7 @@ +// expect: reject E7005 +// phase: 6 +// spec: docs/spec/15-source-language.md §10; docs/spec/03-memory-model.md §25 +module test.own_array_content +fn unused(p: own_array<[Linear; 2]>) -> void effects [] { trap; } +struct Linear { item: own } +fn main() -> void effects [] {} diff --git a/tests/conformance/source/own_array_content/generic.aji b/tests/conformance/source/own_array_content/generic.aji new file mode 100644 index 0000000..027e0f7 --- /dev/null +++ b/tests/conformance/source/own_array_content/generic.aji @@ -0,0 +1,6 @@ +// expect: reject E7005 +// phase: 6 +// spec: docs/spec/15-source-language.md §10; docs/spec/03-memory-model.md §25 +module test.own_array_content +fn unused(p: own_array>) -> void effects [] { trap; } +fn main() -> void effects [] {} diff --git a/tests/conformance/source/own_array_content/generic_member.aji b/tests/conformance/source/own_array_content/generic_member.aji new file mode 100644 index 0000000..0172785 --- /dev/null +++ b/tests/conformance/source/own_array_content/generic_member.aji @@ -0,0 +1,6 @@ +// expect: reject E7005 +// phase: 6 +// spec: docs/spec/15-source-language.md §10; docs/spec/03-memory-model.md §25 +module test.own_array_content +struct Bad { items: own_array> } +fn main() -> void effects [] {} diff --git a/tests/conformance/source/own_array_content/generic_nominal.aji b/tests/conformance/source/own_array_content/generic_nominal.aji new file mode 100644 index 0000000..aae284d --- /dev/null +++ b/tests/conformance/source/own_array_content/generic_nominal.aji @@ -0,0 +1,7 @@ +// expect: reject E7005 +// phase: 6 +// spec: docs/spec/15-source-language.md §10; docs/spec/03-memory-model.md §25 +module test.own_array_content +struct Linear { item: own } +fn unused(p: own_array>) -> void effects [] { trap; } +fn main() -> void effects [] {} diff --git a/tests/conformance/source/own_array_content/graph_owner.aji b/tests/conformance/source/own_array_content/graph_owner.aji new file mode 100644 index 0000000..5dd1914 --- /dev/null +++ b/tests/conformance/source/own_array_content/graph_owner.aji @@ -0,0 +1,7 @@ +// expect: reject E7005 +// phase: 6 +// spec: docs/spec/15-source-language.md §10; docs/spec/03-memory-model.md §25 +module test.own_array_content +struct Node { item: u64 } +fn unused(p: own_array>) -> void effects [] { trap; } +fn main() -> void effects [] {} diff --git a/tests/conformance/source/own_array_content/graph_property.aji b/tests/conformance/source/own_array_content/graph_property.aji new file mode 100644 index 0000000..1eb36f7 --- /dev/null +++ b/tests/conformance/source/own_array_content/graph_property.aji @@ -0,0 +1,6 @@ +// expect: reject E2111 +// phase: 6 +// spec: docs/spec/15-source-language.md §10; docs/spec/03-memory-model.md §25 +module test.own_array_content +fn unused(p: island>>) -> void effects [] { trap; } +fn main() -> void effects [] {} diff --git a/tests/conformance/source/own_array_content/iref_copy.aji b/tests/conformance/source/own_array_content/iref_copy.aji new file mode 100644 index 0000000..a57f5c4 --- /dev/null +++ b/tests/conformance/source/own_array_content/iref_copy.aji @@ -0,0 +1,7 @@ +// expect: accept +// phase: 6 +// spec: docs/spec/15-source-language.md §10 +module test.own_array_content +struct Node { item: own } +fn unused(p: own_array>) -> void effects [] { trap; } +fn main() -> void effects [] {} diff --git a/tests/conformance/source/own_array_content/iref_nested.aji b/tests/conformance/source/own_array_content/iref_nested.aji new file mode 100644 index 0000000..a4acb5b --- /dev/null +++ b/tests/conformance/source/own_array_content/iref_nested.aji @@ -0,0 +1,6 @@ +// expect: reject E7005 +// phase: 6 +// spec: docs/spec/15-source-language.md §10; docs/spec/03-memory-model.md §25 +module test.own_array_content +fn unused(p: iref>>) -> void effects [] { trap; } +fn main() -> void effects [] {} diff --git a/tests/conformance/source/own_array_content/local.aji b/tests/conformance/source/own_array_content/local.aji new file mode 100644 index 0000000..a12c544 --- /dev/null +++ b/tests/conformance/source/own_array_content/local.aji @@ -0,0 +1,6 @@ +// expect: reject E7005 +// phase: 6 +// spec: docs/spec/15-source-language.md §10; docs/spec/03-memory-model.md §25 +module test.own_array_content +fn use() -> void effects [] { var x: own_array> = zero; trap; } +fn main() -> void effects [] {} diff --git a/tests/conformance/source/own_array_content/memo_cycle.aji b/tests/conformance/source/own_array_content/memo_cycle.aji new file mode 100644 index 0000000..1b2418d --- /dev/null +++ b/tests/conformance/source/own_array_content/memo_cycle.aji @@ -0,0 +1,9 @@ +// expect: reject E7005 +// phase: 6 +// spec: docs/spec/15-source-language.md §10 +module test.own_array_content +struct A { first: B, witness: own } +struct B { back: A } +struct Second { item: Y } +fn unused(p: own_array>, t: T) -> void effects [] { trap; } +fn main() -> void effects [] {} diff --git a/tests/conformance/source/own_array_content/nested.aji b/tests/conformance/source/own_array_content/nested.aji new file mode 100644 index 0000000..87d02b0 --- /dev/null +++ b/tests/conformance/source/own_array_content/nested.aji @@ -0,0 +1,6 @@ +// expect: reject E7005 +// phase: 6 +// spec: docs/spec/15-source-language.md §10; docs/spec/03-memory-model.md §25 +module test.own_array_content +fn unused(p: own>>) -> void effects [] { trap; } +fn main() -> void effects [] {} diff --git a/tests/conformance/source/own_array_content/nested_generic.aji b/tests/conformance/source/own_array_content/nested_generic.aji new file mode 100644 index 0000000..e86976f --- /dev/null +++ b/tests/conformance/source/own_array_content/nested_generic.aji @@ -0,0 +1,7 @@ +// expect: reject E7005 +// phase: 6 +// spec: docs/spec/15-source-language.md §10; docs/spec/03-memory-model.md §25 +module test.own_array_content +struct Pair { item: T } +fn unused(p: own_array>>>) -> void effects [] { trap; } +fn main() -> void effects [] {} diff --git a/tests/conformance/source/own_array_content/operation.aji b/tests/conformance/source/own_array_content/operation.aji new file mode 100644 index 0000000..b0e79f9 --- /dev/null +++ b/tests/conformance/source/own_array_content/operation.aji @@ -0,0 +1,6 @@ +// expect: reject E7005 +// phase: 6 +// spec: docs/spec/15-source-language.md §10; docs/spec/03-memory-model.md §25 +module test.own_array_content +fn use(heap: cap, value: own) -> void effects [mem.alloc] { let x = own.array(2, move value, heap) or trap; trap; } +fn main() -> void effects [] {} diff --git a/tests/conformance/source/own_array_content/operation_array_ref.aji b/tests/conformance/source/own_array_content/operation_array_ref.aji new file mode 100644 index 0000000..1764c52 --- /dev/null +++ b/tests/conformance/source/own_array_content/operation_array_ref.aji @@ -0,0 +1,6 @@ +// expect: reject E7005 +// phase: 6 +// spec: docs/spec/15-source-language.md §10 +module test.own_array_content +fn unused(heap: cap, p: ref) -> void effects [mem.alloc] { let a = [p, p]; let items = own.array(1, a, heap) or trap; trap; } +fn main() -> void effects [] {} diff --git a/tests/conformance/source/own_array_content/operation_deep_nominal.aji b/tests/conformance/source/own_array_content/operation_deep_nominal.aji new file mode 100644 index 0000000..62cd08d --- /dev/null +++ b/tests/conformance/source/own_array_content/operation_deep_nominal.aji @@ -0,0 +1,7 @@ +// expect: reject E7005 +// phase: 6 +// spec: docs/spec/15-source-language.md §10 +module test.own_array_content +struct Deep { item: [[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[own; 1]; 1]; 1]; 1]; 1]; 1]; 1]; 1]; 1]; 1]; 1]; 1]; 1]; 1]; 1]; 1]; 1]; 1]; 1]; 1]; 1]; 1]; 1]; 1]; 1]; 1]; 1]; 1]; 1]; 1]; 1]; 1]; 1]; 1]; 1]; 1]; 1]; 1]; 1]; 1] } +fn unused(heap: cap, owner: own) -> void effects [mem.alloc] { let values = own.array(1, Deep { item: [[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[move owner]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]] }, heap) or trap; trap; } +fn main() -> void effects [] {} diff --git a/tests/conformance/source/own_array_content/operation_ref.aji b/tests/conformance/source/own_array_content/operation_ref.aji new file mode 100644 index 0000000..fb0c577 --- /dev/null +++ b/tests/conformance/source/own_array_content/operation_ref.aji @@ -0,0 +1,6 @@ +// expect: reject E7005 +// phase: 6 +// spec: docs/spec/15-source-language.md §10 +module test.own_array_content +fn unused(heap: cap, p: ref) -> void effects [mem.alloc] { let items = own.array(1, p, heap) or trap; trap; } +fn main() -> void effects [] {} diff --git a/tests/conformance/source/own_array_content/operation_slice.aji b/tests/conformance/source/own_array_content/operation_slice.aji new file mode 100644 index 0000000..8971a36 --- /dev/null +++ b/tests/conformance/source/own_array_content/operation_slice.aji @@ -0,0 +1,6 @@ +// expect: reject E7005 +// phase: 6 +// spec: docs/spec/15-source-language.md §10 +module test.own_array_content +fn unused(heap: cap, p: slice) -> void effects [mem.alloc] { let items = own.array(1, p, heap) or trap; trap; } +fn main() -> void effects [] {} diff --git a/tests/conformance/source/own_array_content/operation_void.aji b/tests/conformance/source/own_array_content/operation_void.aji new file mode 100644 index 0000000..7c00ce6 --- /dev/null +++ b/tests/conformance/source/own_array_content/operation_void.aji @@ -0,0 +1,6 @@ +// expect: reject E7005 +// phase: 6 +// spec: docs/spec/15-source-language.md §10 +module test.own_array_content +fn unused(heap: cap) -> void effects [mem.alloc] { let items = own.array(1, assert(true), heap) or trap; trap; } +fn main() -> void effects [] {} diff --git a/tests/conformance/source/own_array_content/owner.aji b/tests/conformance/source/own_array_content/owner.aji new file mode 100644 index 0000000..58cd206 --- /dev/null +++ b/tests/conformance/source/own_array_content/owner.aji @@ -0,0 +1,6 @@ +// expect: reject E7005 +// phase: 6 +// spec: docs/spec/15-source-language.md §10; docs/spec/03-memory-model.md §25 +module test.own_array_content +fn unused(p: own_array>) -> void effects [] { trap; } +fn main() -> void effects [] {} diff --git a/tests/conformance/source/own_array_content/phantom.aji b/tests/conformance/source/own_array_content/phantom.aji new file mode 100644 index 0000000..5252a71 --- /dev/null +++ b/tests/conformance/source/own_array_content/phantom.aji @@ -0,0 +1,7 @@ +// expect: accept +// phase: 6 +// spec: docs/spec/15-source-language.md §10; docs/spec/03-memory-model.md §25 +module test.own_array_content +struct Phantom { item: u64 } +fn unused(p: own_array>>) -> void effects [] { trap; } +fn main() -> void effects [] {} diff --git a/tests/conformance/source/own_array_content/recursive_array.aji b/tests/conformance/source/own_array_content/recursive_array.aji new file mode 100644 index 0000000..93d0a20 --- /dev/null +++ b/tests/conformance/source/own_array_content/recursive_array.aji @@ -0,0 +1,6 @@ +// expect: reject E7005 +// phase: 6 +// spec: docs/spec/15-source-language.md §10; docs/spec/03-memory-model.md §25 +module test.own_array_content +struct Node { items: own_array } +fn main() -> void effects [] {} diff --git a/tests/conformance/source/own_array_content/recursive_value.aji b/tests/conformance/source/own_array_content/recursive_value.aji new file mode 100644 index 0000000..a03e49d --- /dev/null +++ b/tests/conformance/source/own_array_content/recursive_value.aji @@ -0,0 +1,7 @@ +// expect: reject E1003 +// phase: 6 +// spec: docs/spec/15-source-language.md §10; docs/spec/03-memory-model.md §25 +module test.own_array_content +struct Node { items: [Node; 1] } +fn unused(p: own_array) -> void effects [] { trap; } +fn main() -> void effects [] {} diff --git a/tests/conformance/source/own_array_content/rotated_generic.aji b/tests/conformance/source/own_array_content/rotated_generic.aji new file mode 100644 index 0000000..4e9922d --- /dev/null +++ b/tests/conformance/source/own_array_content/rotated_generic.aji @@ -0,0 +1,7 @@ +// expect: reject E7005 +// phase: 6 +// spec: docs/spec/15-source-language.md §10 +module test.own_array_content +struct S { a: A, n: [S; 1] } +fn unused(p: own_array>>, t: T) -> void effects [] { trap; } +fn main() -> void effects [] {} diff --git a/tests/conformance/source/own_array_content/rotated_safe.aji b/tests/conformance/source/own_array_content/rotated_safe.aji new file mode 100644 index 0000000..e52adc2 --- /dev/null +++ b/tests/conformance/source/own_array_content/rotated_safe.aji @@ -0,0 +1,7 @@ +// expect: accept +// phase: 6 +// spec: docs/spec/15-source-language.md §10 +module test.own_array_content +struct S { a: A, n: [S; 1] } +fn unused(p: own_array>, t: T) -> void effects [] { trap; } +fn main() -> void effects [] {} diff --git a/tests/conformance/source/own_array_content/shadow_builtin.aji b/tests/conformance/source/own_array_content/shadow_builtin.aji new file mode 100644 index 0000000..ab8fb61 --- /dev/null +++ b/tests/conformance/source/own_array_content/shadow_builtin.aji @@ -0,0 +1,10 @@ +// expect: accept +// phase: 6 +// spec: docs/spec/15-source-language.md §10 +module test.own_array_content +struct region { x: u64 } +struct stream { x: u64 } +struct listener { x: u64 } +fn unused(a: own_array, b: own_array, c: own_array, t: T) -> void effects [] { trap; } +fn concrete(a: own_array) -> void effects [] { trap; } +fn main() -> void effects [] {} diff --git a/tests/conformance/source/own_array_content/specialized.aji b/tests/conformance/source/own_array_content/specialized.aji new file mode 100644 index 0000000..df1121e --- /dev/null +++ b/tests/conformance/source/own_array_content/specialized.aji @@ -0,0 +1,7 @@ +// expect: reject E7005 +// phase: 6 +// spec: docs/spec/15-source-language.md §10; docs/spec/03-memory-model.md §25 +module test.own_array_content +fn bad() -> own_array effects [] { trap; } +fn use() -> void effects [] { let x = bad::>(); trap; } +fn main() -> void effects [] {} diff --git a/tests/conformance/source/own_array_content/static.aji b/tests/conformance/source/own_array_content/static.aji new file mode 100644 index 0000000..72593a6 --- /dev/null +++ b/tests/conformance/source/own_array_content/static.aji @@ -0,0 +1,6 @@ +// expect: reject E7005 +// phase: 6 +// spec: docs/spec/15-source-language.md §10; docs/spec/03-memory-model.md §25 +module test.own_array_content +fn unused>) -> void effects []>() -> void effects [] { trap; } +fn main() -> void effects [] {} diff --git a/tests/conformance/source/own_array_content/valid.aji b/tests/conformance/source/own_array_content/valid.aji new file mode 100644 index 0000000..6f3eaa7 --- /dev/null +++ b/tests/conformance/source/own_array_content/valid.aji @@ -0,0 +1,9 @@ +// expect: run +// result: 42 +// grant: heap=mem.alloc:4096 +// phase: 6 +// spec: docs/spec/15-source-language.md §10; docs/spec/03-memory-model.md §25 +module test.own_array_content +struct Plain { item: u64 } +fn make(value: T, heap: cap) -> own_array effects [mem.alloc] { return own.array(2, move value, heap) or trap; } +fn main(heap: cap) -> u64 effects [mem.alloc] { let values = make::(Plain { item: 21u64 }, heap); let n = { ref items = values; items[0].item + items[1].item }; own.free(move values); return n; } diff --git a/tests/conformance/source/own_array_content/valid_unused.aji b/tests/conformance/source/own_array_content/valid_unused.aji new file mode 100644 index 0000000..428e865 --- /dev/null +++ b/tests/conformance/source/own_array_content/valid_unused.aji @@ -0,0 +1,8 @@ +// expect: accept +// phase: 6 +// spec: docs/spec/15-source-language.md §10 +module test.own_array_content +struct Node { item: own } +struct Plain { values: [u64; 2] } +fn unused) -> void effects []>(a: own_array, b: own_array, c: own_array>) -> void effects [] { trap; } +fn main() -> void effects [] {} diff --git a/tests/hosted/source_own_array_content.py b/tests/hosted/source_own_array_content.py new file mode 100644 index 0000000..894f930 --- /dev/null +++ b/tests/hosted/source_own_array_content.py @@ -0,0 +1,102 @@ +#!/usr/bin/env python3 +"""Dynamic owning-array Copy content: annotations, operations and package parity.""" +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/own_array_content' +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, token, *args, expected_file=None, line=5): + error = json.loads(cli('check', *args, code=1)) + assert error['code'] == 'E7005', error + assert error['constraint'] == 'Copy' and error['type_constructor'] == 'own_array', 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, token in ( + ('owner', 'own'), + ('nested', 'own'), + ('field', 'own'), + ('generic', 'own'), + ('generic_member', 'own'), + ('static', 'own'), + ('local', 'own'), + ('operation', 'move value'), + ('operation_ref', 'p, heap'), + ('operation_slice', 'p, heap'), + ('operation_void', 'assert(true)'), + ('operation_array_ref', 'a, heap'), + ('iref_nested', 'own'), +): + path = CORPUS / f'{name}.aji' + rejection(path, token, path) +for name, token in (('specialized', 'own'), ('forward', '[Linear; 2]'), + ('enum', 'Linear>'), ('generic_nominal', 'Linear'), + ('nested_generic', 'Pair>>'), ('graph_owner', 'island')): + path = CORPUS / f'{name}.aji' + rejection(path, token, path, line=6 if name != 'forward' else 5) +deep = CORPUS / 'operation_deep_nominal.aji' +rejection(deep, 'Deep {', deep, line=6) + +with tempfile.TemporaryDirectory(prefix='aji-own-array-content-') 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', '--grant', 'heap=mem.alloc:4096', source)) + assert expected['value'] == 42 + assert json.loads(cli('run', '--fuel', '100000', '--grant', 'heap=mem.alloc:4096', raw)) == expected + result = root / 'hosted.json' + cli('run', '--profile', 'hosted', '--fuel', '100000', '--result-file', result, '--grant', 'heap=mem.alloc:4096', 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: own_array>) -> void effects [] {}\n') + manifest.write_text('[package]\nname = "positions"\nversion = "1"\nentry = "main.aji"\n' + '[modules]\nlib.types = "types.aji"\n') + rejection(library, 'own', '--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: own_array) -> void effects [] {}\n') + assert json.loads(cli('run', '--manifest', manifest))['value'] == 42 + + # Traverse fields in their own imported namespace, including a second import. + payload = root / 'payload.aji' + payload.write_text('module lib.payload\nstruct Value { owner: own }\n') + library.write_text('module lib.types\nimport lib.payload;\nstruct Container { value: lib.payload.Value }\n') + manifest.write_text(manifest.read_text() + 'lib.payload = "payload.aji"\n') + entry.write_text('module app\nimport lib.types;\nfn unused(p: own_array) -> void effects [] { trap; }\nfn main() -> u64 effects [] { return 42; }\n') + rejection(entry, 'lib.types.Container', '--manifest', manifest, + expected_file=str(entry), line=3) + payload.write_text('module lib.payload\nstruct Value { owner: u64 }\n') + assert json.loads(cli('run', '--manifest', manifest))['value'] == 42 + + # Invalid mapped modules that are unreachable remain outside compilation. + entry.write_text('module app\nfn main() -> u64 effects [] { return 42; }\n') + library.write_text('module lib.types\nfn unused(p: own_array>) -> void effects [] {}\n') + assert json.loads(cli('run', '--manifest', manifest))['value'] == 42 + +print(f'owning array content CLI integration passed ({checks} checks)')