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

+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)')