cli: check inferred array elements before AIR

This commit is contained in:
dev committed 2026-10-08 09:26:33 +03:00
1 parent cfbe01a16f
commit 1bbfed24a8
40 files changed
+337 -9

No files matched your search

@@ -0,0 +1,6 @@
// expect: reject E7006
// phase: 6
// spec: docs/spec/15-source-language.md §5/§10
module test.array_literals
fn unused(p: ref<u64>) -> void effects [] { let a: [u64; 2] = [p, p]; }
fn main() -> void effects [] {}
@@ -0,0 +1,6 @@
// expect: reject E7005
// phase: 6
// spec: docs/spec/15-source-language.md §5/§10
module test.array_literals
fn unused(p: ref<u64>) -> void effects [] { let a: [ref<u64>; 2] = [p, p]; }
fn main() -> void effects [] {}
@@ -0,0 +1,6 @@
// expect: reject E7005
// phase: 6
// spec: docs/spec/15-source-language.md §5/§10
module test.array_literals
fn unused(p: own<u64>) -> void effects [] { let a: [own<u64>; 2] = [move p; 2]; trap; }
fn main() -> void effects [] {}
@@ -0,0 +1,6 @@
// expect: reject E7005
// phase: 6
// spec: docs/spec/15-source-language.md §5/§10
module test.array_literals
fn unused() -> void effects [] { let a: [void; 2] = [assert(true); 2]; }
fn main() -> void effects [] {}
@@ -0,0 +1,7 @@
// expect: reject E7006
// phase: 6
// spec: docs/spec/15-source-language.md §5/§10
module test.array_literals
fn read(p: slice<u64>) -> u64 effects [] { return p[0]; }
fn unused(p: ref<u64>) -> void effects [] { let x = read([p, p]); }
fn main() -> void effects [] {}
@@ -0,0 +1,7 @@
// expect: accept
// phase: 6
// spec: docs/spec/15-source-language.md §5/§10
module test.array_literals
struct Node { value: own<u64> }
fn unused(a: iref<Node>, b: iref<Node>, cap: cap<mem.alloc>) -> void effects [] { let refs = [a, b]; let repeated = [a; 2]; let caps = [cap; 2]; }
fn main() -> void effects [] {}
@@ -0,0 +1,7 @@
// expect: run
// result: 42
// phase: 6
// spec: docs/spec/15-source-language.md §5/§10
module test.array_literals
struct Item { value: u64 }
fn main() -> u64 effects [] { let values = [[Item { value: 21u64 }; 2]; 3]; ref r = values; return r[0][0].value + r[2][1].value; }
@@ -0,0 +1,7 @@
// expect: reject E7006
// phase: 6
// spec: docs/spec/15-source-language.md §5/§10
module test.array_literals
fn bad<T>(x: T) -> void effects [] { let a = [x, x]; }
fn use(p: ref<u64>) -> void effects [] { bad::<ref<u64>>(p); }
fn main() -> void effects [] {}
@@ -0,0 +1,16 @@
// expect: run
// result: 42
// grant: heap=mem.alloc:64
// phase: 6
// spec: docs/spec/15-source-language.md §5/§10
module test.array_literals
struct Item { value: own<u64> }
fn main(heap: cap<mem.alloc>) -> u64 effects [mem.alloc] {
let left = own.new(20u64, heap) or trap;
let right = own.new(22u64, heap) or trap;
let data = [Item { value: move left }, Item { value: move right }];
let (a, b) = array.unpack(move data);
let (x,) = struct.unpack(move a);
let (y,) = struct.unpack(move b);
return own.take(move x) + own.take(move y);
}
@@ -0,0 +1,6 @@
// expect: reject E2002
// phase: 6
// spec: docs/spec/15-source-language.md §5/§10; docs/spec/07-air.md §8.8
module test.array_literals
fn unused(p: own<u64>) -> void effects [] { let a = [move p, move p]; trap; }
fn main() -> void effects [] {}
@@ -0,0 +1,6 @@
// expect: reject E7003
// phase: 6
// spec: docs/spec/15-source-language.md §5/§10; docs/spec/07-air.md §8.8
module test.array_literals
fn unused(p: own<u64>) -> void effects [] { let a = [p]; trap; }
fn main() -> void effects [] {}
@@ -0,0 +1,6 @@
// expect: reject E7006
// phase: 6
// spec: docs/spec/15-source-language.md §5/§10
module test.array_literals
fn unused(p: ref<u64>) -> void effects [] { let a = [1u64, p]; }
fn main() -> void effects [] {}
@@ -0,0 +1,6 @@
// expect: reject E7006
// phase: 6
// spec: docs/spec/15-source-language.md §5/§10; docs/spec/07-air.md §8.8
module test.array_literals
fn unused(p: ref<u64>) -> void effects [] { let a = [p, 1u64]; }
fn main() -> void effects [] {}
@@ -0,0 +1,6 @@
// expect: reject E7006
// phase: 6
// spec: docs/spec/15-source-language.md §5/§10; docs/spec/07-air.md §8.8
module test.array_literals
fn unused(p: ref<u64>) -> void effects [] { let a = [[p, p], [p, p]]; }
fn main() -> void effects [] {}
@@ -0,0 +1,6 @@
// expect: reject E7006
// phase: 6
// spec: docs/spec/15-source-language.md §5/§10; docs/spec/07-air.md §8.8
module test.array_literals
fn unused(p: ref<u64>) -> void effects [] { let a = [p, p]; }
fn main() -> void effects [] {}
@@ -0,0 +1,6 @@
// expect: reject E7006
// phase: 6
// spec: docs/spec/15-source-language.md §5/§10; docs/spec/07-air.md §8.8
module test.array_literals
fn unused(p: slice<u64>) -> void effects [] { let a = [p, p]; }
fn main() -> void effects [] {}
@@ -0,0 +1,6 @@
// expect: reject E7005
// phase: 6
// spec: docs/spec/15-source-language.md §5/§10; docs/spec/07-air.md §8.8
module test.array_literals
fn unused(p: task<u64>) -> void effects [] { let a = [move p]; trap; }
fn main() -> void effects [] {}
@@ -0,0 +1,6 @@
// expect: reject E7005
// phase: 6
// spec: docs/spec/15-source-language.md §5/§10; docs/spec/07-air.md §8.8
module test.array_literals
fn unused() -> void effects [] { let a = [1u64, true]; }
fn main() -> void effects [] {}
@@ -0,0 +1,6 @@
// expect: reject E7005
// phase: 6
// spec: docs/spec/15-source-language.md §5/§10; docs/spec/07-air.md §8.8
module test.array_literals
fn unused() -> void effects [] { let a = [assert(true), assert(true)]; }
fn main() -> void effects [] {}
@@ -0,0 +1,6 @@
// expect: reject E7005
// phase: 6
// spec: docs/spec/15-source-language.md §5/§10; docs/spec/07-air.md §8.8
module test.array_literals
fn unused(p: own<u64>) -> void effects [] { let a = [[move p]; 2]; trap; }
fn main() -> void effects [] {}
@@ -0,0 +1,7 @@
// expect: reject E7005
// phase: 6
// spec: docs/spec/15-source-language.md §5/§10; docs/spec/07-air.md §8.8
module test.array_literals
struct Item { value: own<u64> }
fn unused(p: Item) -> void effects [] { let a = [move p; 2]; trap; }
fn main() -> void effects [] {}
@@ -0,0 +1,6 @@
// expect: reject E7005
// phase: 6
// spec: docs/spec/15-source-language.md §5/§10; docs/spec/07-air.md §8.8
module test.array_literals
fn unused(p: own<u64>) -> void effects [] { let a = [move p; 2]; trap; }
fn main() -> void effects [] {}
@@ -0,0 +1,6 @@
// expect: reject E7005
// phase: 6
// spec: docs/spec/15-source-language.md §5/§10; docs/spec/07-air.md §8.8
module test.array_literals
fn unused(p: own_array<u64>) -> void effects [] { let a = [move p; 2]; trap; }
fn main() -> void effects [] {}
@@ -0,0 +1,6 @@
// expect: reject E7005
// phase: 6
// spec: docs/spec/15-source-language.md §5/§10; docs/spec/07-air.md §8.8
module test.array_literals
fn unused(p: own<u64>) -> void effects [] { let a = [p; 1]; trap; }
fn main() -> void effects [] {}
@@ -0,0 +1,6 @@
// expect: reject E7006
// phase: 6
// spec: docs/spec/15-source-language.md §5/§10; docs/spec/07-air.md §8.8
module test.array_literals
fn unused(p: ref<u64>) -> void effects [] { let a = [p; 2]; }
fn main() -> void effects [] {}
@@ -0,0 +1,6 @@
// expect: reject E7006
// phase: 6
// spec: docs/spec/15-source-language.md §5/§10; docs/spec/07-air.md §8.8
module test.array_literals
fn unused(p: slice<u64>) -> void effects [] { let a = [p; 2]; }
fn main() -> void effects [] {}
@@ -0,0 +1,6 @@
// expect: reject E7005
// phase: 6
// spec: docs/spec/15-source-language.md §5/§10; docs/spec/07-air.md §8.8
module test.array_literals
fn unused(p: task<u64>) -> void effects [] { let a = [move p; 2]; trap; }
fn main() -> void effects [] {}
@@ -0,0 +1,6 @@
// expect: reject E7005
// phase: 6
// spec: docs/spec/15-source-language.md §5/§10; docs/spec/07-air.md §8.8
module test.array_literals
fn unused() -> void effects [] { let a = [assert(true); 2]; }
fn main() -> void effects [] {}
@@ -0,0 +1,7 @@
// expect: run
// result: 42
// phase: 6
// spec: docs/spec/15-source-language.md §5/§10
module test.array_literals
fn first(p: slice<u64>) -> u64 effects [] { return p[0]; }
fn main() -> u64 effects [] { let a = [21u64, 21u64]; let b = [21u64; 2]; return first(ref a) + first(ref b); }
@@ -1,4 +1,4 @@
// expect: reject E1001
// expect: reject E7005
// grant: heap=mem.alloc:64
// phase: 6
// spec: docs/spec/03-memory-model.md §25; docs/spec/15-source-language.md §16
@@ -1,4 +1,4 @@
// expect: reject E7005
// expect: reject E7006
// phase: 6
// spec: docs/spec/15-source-language.md §10
module test.own_array_content
+101
View File
@@ -0,0 +1,101 @@
#!/usr/bin/env python3
"""Inferred array element diagnostics, ownership and package/profile 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/array_literals'
checks = 0
def cli(*args, code=0):
global checks
result = subprocess.run([str(AJI), *map(str, args)], capture_output=True, text=True)
assert result.returncode == code, (args, result.returncode, result.stdout, result.stderr)
checks += 1
return result.stdout
def reject(path, token, code, fields, *args, line=5, expected_file=None):
error = json.loads(cli('check', *args, code=1))
assert error['code'] == code, error
for key, value in fields.items():
assert error[key] == value, 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 (
('list_ref', 'p, p'), ('list_nested_ref', 'p, p'),
('list_slice', 'p, p'), ('list_mixed_ref', 'p, 1u64'),
('list_mixed_later_ref', 'p]'), ('rep_ref', 'p; 2'),
('rep_slice', 'p; 2'), ('generic_ref', 'x, x'),
('annotated_expected_ref', 'p, p'),
):
path = CORPUS / f'{name}.aji'
reject(path, token, 'E7006', {'reason': 'invalid_literal'}, path)
for name, token, constructor in (
('list_void', 'assert(true)', 'void'),
('rep_void', 'assert(true)', 'void'),
('list_task', 'move p', 'task'), ('rep_task', 'move p', 'task'),
('annotated_ref', 'ref<u64>; 2', 'ref'),
('annotated_void', 'void; 2', 'void'),
):
path = CORPUS / f'{name}.aji'
reject(path, token, 'E7005', {'position': 'member', 'type_constructor': constructor}, path)
for name, token, line in (
('rep_owner', 'move p', 5), ('rep_owner_name', 'p; 1', 5),
('rep_owner_array', 'move p', 5), ('rep_nominal', 'move p', 6),
('rep_nested_linear', '[move p]', 5), ('annotated_repeat_owner', 'move p', 5),
):
path = CORPUS / f'{name}.aji'
reject(path, token, 'E7005', {'constraint': 'Copy', 'type_constructor': 'array'}, path, line=line)
with tempfile.TemporaryDirectory(prefix='aji-array-literals-') as directory:
root = Path(directory)
for name, literal in (('never_list', '[stop()]'), ('never_repeat', '[stop(); 2]')):
path = root / f'{name}.aji'
path.write_text('module test.never\nfn stop() -> never effects [] { trap; }\n'
f'fn unused() -> void effects [] {{ let a = {literal}; }}\n'
'fn main() -> void effects [] {}\n')
reject(path, 'stop()', 'E7005', {'position': 'member', 'type_constructor': 'never'}, path, line=3)
for name in ('linear_list', 'copy_nested', 'slice_materialization'):
source = CORPUS / f'{name}.aji'
raw = root / f'{name}.air'
air = cli('lower', source)
assert cli('lower', source) == air
raw.write_text(air)
cli('check', raw)
grants = ['--grant', 'heap=mem.alloc:64'] if name == 'linear_list' else []
expected = json.loads(cli('run', '--fuel', '100000', *grants, source))
assert expected['value'] == 42
assert json.loads(cli('run', '--fuel', '100000', *grants, raw)) == expected
result = root / f'{name}.json'
cli('run', '--profile', 'hosted', '--fuel', '100000', *grants, '--result-file', result, source)
assert json.loads(result.read_text())['value'] == 42
formatted = root / f'{name}.aji'
formatted.write_text(cli('fmt', source))
cli('fmt', '--check', formatted)
assert cli('fmt', formatted) == formatted.read_text()
assert cli('lower', formatted) == air
entry, library, manifest = (root / name for name in ('main.aji', 'literals.aji', 'aji.toml'))
entry.write_text('module app\nimport lib.literals;\nfn use(p: ref<u64>) -> void effects [] { lib.literals.bad::<ref<u64>>(p); }\nfn main() -> u64 effects [] { return 42; }\n')
library.write_text('module lib.literals\nfn bad<T>(x: T) -> void effects [] { let a = [x, x]; }\n')
manifest.write_text('[package]\nname = "literals"\nversion = "1"\nentry = "main.aji"\n[modules]\nlib.literals = "literals.aji"\n')
reject(library, 'x, x', 'E7006', {'reason': 'invalid_literal'}, '--manifest', manifest,
line=2, expected_file='literals.aji')
library.write_text('module lib.literals\nfn bad<T>(x: T) -> void effects [] { let a = [21u64; 2]; }\n')
assert json.loads(cli('run', '--manifest', manifest))['value'] == 42
entry.write_text('module app\nfn main() -> u64 effects [] { return 42; }\n')
library.write_text('module lib.literals\nfn bad<T>(x: T) -> void effects [] { let a = [x, x]; }\n')
assert json.loads(cli('run', '--manifest', manifest))['value'] == 42
print(f'array literal CLI integration passed ({checks} checks)')
+8 -4
View File
@@ -19,10 +19,13 @@ def cli(*args, code=0):
return result.stdout
def rejection(path, token, *args, expected_file=None, line=5):
def rejection(path, token, *args, expected_file=None, line=5, borrow_literal=False):
error = json.loads(cli('check', *args, code=1))
assert error['code'] == 'E7005', error
assert error['constraint'] == 'Copy' and error['type_constructor'] == 'own_array', error
if borrow_literal:
assert error['code'] == 'E7006' and error['reason'] == 'invalid_literal', error
else:
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
@@ -43,11 +46,12 @@ for name, token in (
('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)
borrow_array = CORPUS / 'operation_array_ref.aji'
rejection(borrow_array, 'p, p', borrow_array, borrow_literal=True)
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>')):