cli: validate Copy content of source owning arrays

This commit is contained in:
dev committed 2026-10-08 09:01:58 +03:00
1 parent caf3b86e80
commit cfbe01a16f
42 files changed
+608 -3

No files matched your search

@@ -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<u64> }
fn unused(p: own_array<N0>) -> void effects [] { trap; }
fn main() -> void effects [] {}
@@ -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<u64>) }
fn unused(p: own_array<Linear>) -> void effects [] { trap; }
fn main() -> void effects [] {}
@@ -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<own<u64>> }
fn main() -> void effects [] {}
@@ -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<u64> }
fn main() -> void effects [] {}
@@ -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<T>(p: own_array<own<T>>) -> void effects [] { trap; }
fn main() -> void effects [] {}
@@ -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<T> { items: own_array<own<T>> }
fn main() -> void effects [] {}
@@ -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<T> { item: own<T> }
fn unused<T>(p: own_array<Linear<T>>) -> void effects [] { trap; }
fn main() -> void effects [] {}
@@ -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<island<Node>>) -> void effects [] { trap; }
fn main() -> void effects [] {}
@@ -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<own_array<own<u64>>>) -> void effects [] { trap; }
fn main() -> void effects [] {}
@@ -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<u64> }
fn unused(p: own_array<iref<Node>>) -> void effects [] { trap; }
fn main() -> void effects [] {}
@@ -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<own_array<own<u64>>>) -> void effects [] { trap; }
fn main() -> void effects [] {}
@@ -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<own<u64>> = zero; trap; }
fn main() -> void effects [] {}
@@ -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<u64> }
struct B { back: A }
struct Second<X, Y> { item: Y }
fn unused<T>(p: own_array<Second<A, B>>, t: T) -> void effects [] { trap; }
fn main() -> void effects [] {}
@@ -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<own_array<own<u64>>>) -> void effects [] { trap; }
fn main() -> void effects [] {}
@@ -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<T> { item: T }
fn unused<T>(p: own_array<Pair<Pair<own<T>>>>) -> void effects [] { trap; }
fn main() -> void effects [] {}
@@ -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<mem.alloc>, value: own<u64>) -> void effects [mem.alloc] { let x = own.array(2, move value, heap) or trap; trap; }
fn main() -> void effects [] {}
@@ -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<mem.alloc>, p: ref<u64>) -> void effects [mem.alloc] { let a = [p, p]; let items = own.array(1, a, heap) or trap; trap; }
fn main() -> void effects [] {}
@@ -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<u64>; 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<mem.alloc>, owner: own<u64>) -> void effects [mem.alloc] { let values = own.array(1, Deep { item: [[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[move owner]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]] }, heap) or trap; trap; }
fn main() -> void effects [] {}
@@ -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<mem.alloc>, p: ref<u64>) -> void effects [mem.alloc] { let items = own.array(1, p, heap) or trap; trap; }
fn main() -> void effects [] {}
@@ -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<mem.alloc>, p: slice<u64>) -> void effects [mem.alloc] { let items = own.array(1, p, heap) or trap; trap; }
fn main() -> void effects [] {}
@@ -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<mem.alloc>) -> void effects [mem.alloc] { let items = own.array(1, assert(true), heap) or trap; trap; }
fn main() -> void effects [] {}
@@ -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<own<u64>>) -> void effects [] { trap; }
fn main() -> void effects [] {}
@@ -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<T> { item: u64 }
fn unused<T>(p: own_array<Phantom<own<T>>>) -> void effects [] { trap; }
fn main() -> void effects [] {}
@@ -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<Node> }
fn main() -> void effects [] {}
@@ -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<Node>) -> void effects [] { trap; }
fn main() -> void effects [] {}
@@ -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, B, C> { a: A, n: [S<B, C, A>; 1] }
fn unused<T>(p: own_array<S<u64, u64, own<u64>>>, t: T) -> void effects [] { trap; }
fn main() -> void effects [] {}
@@ -0,0 +1,7 @@
// expect: accept
// phase: 6
// spec: docs/spec/15-source-language.md §10
module test.own_array_content
struct S<A, B, C> { a: A, n: [S<B, C, A>; 1] }
fn unused<T>(p: own_array<S<u64, u64, u64>>, t: T) -> void effects [] { trap; }
fn main() -> void effects [] {}
@@ -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<T>(a: own_array<region>, b: own_array<stream>, c: own_array<listener>, t: T) -> void effects [] { trap; }
fn concrete(a: own_array<region>) -> void effects [] { trap; }
fn main() -> void effects [] {}
@@ -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<T>() -> own_array<T> effects [] { trap; }
fn use() -> void effects [] { let x = bad::<own<u64>>(); trap; }
fn main() -> void effects [] {}
@@ -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<T, F: fn(own_array<own<T>>) -> void effects []>() -> void effects [] { trap; }
fn main() -> void effects [] {}
@@ -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<T: Copy>(value: T, heap: cap<mem.alloc>) -> own_array<T> effects [mem.alloc] { return own.array(2, move value, heap) or trap; }
fn main(heap: cap<mem.alloc>) -> u64 effects [mem.alloc] { let values = make::<Plain>(Plain { item: 21u64 }, heap); let n = { ref items = values; items[0].item + items[1].item }; own.free(move values); return n; }
@@ -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<u64> }
struct Plain { values: [u64; 2] }
fn unused<T, F: fn(own_array<T>) -> void effects []>(a: own_array<T>, b: own_array<Plain>, c: own_array<iref<Node>>) -> void effects [] { trap; }
fn main() -> void effects [] {}
+102
View File
@@ -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<u64>'),
('nested', 'own<u64>'),
('field', 'own<u64>'),
('generic', 'own<T>'),
('generic_member', 'own<T>'),
('static', 'own<T>'),
('local', 'own<u64>'),
('operation', 'move value'),
('operation_ref', 'p, heap'),
('operation_slice', 'p, heap'),
('operation_void', 'assert(true)'),
('operation_array_ref', 'a, heap'),
('iref_nested', 'own<u64>'),
):
path = CORPUS / f'{name}.aji'
rejection(path, token, path)
for name, token in (('specialized', 'own<u64>'), ('forward', '[Linear; 2]'),
('enum', 'Linear>'), ('generic_nominal', 'Linear<T>'),
('nested_generic', 'Pair<Pair<own<T>>>'), ('graph_owner', 'island<Node>')):
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<T>(p: own_array<own<T>>) -> void effects [] {}\n')
manifest.write_text('[package]\nname = "positions"\nversion = "1"\nentry = "main.aji"\n'
'[modules]\nlib.types = "types.aji"\n')
rejection(library, 'own<T>', '--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<T>(p: own_array<T>) -> 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<u64> }\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<lib.types.Container>) -> 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<T>(p: own_array<own<T>>) -> void effects [] {}\n')
assert json.loads(cli('run', '--manifest', manifest))['value'] == 42
print(f'owning array content CLI integration passed ({checks} checks)')