cli: validate structural source type positions before AIR

This commit is contained in:
dev committed 2026-10-07 23:54:55 +03:00
1 parent 5f76e92ff3
commit caf3b86e80
48 files changed
+423 -33

No files matched your search

+89
View File
@@ -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<u64>'),
('nested_mut', 'borrowed', 'slice', 'slice<u64>'),
('array_ref', 'member', 'ref', 'ref<u64>'),
('field', 'member', 'ref', 'ref<u64>'),
('payload', 'member', 'slice', 'slice<u8>'),
('void_param', 'param', 'void', 'void)'),
('unused_generic', 'borrowed', 'mut', 'mut<T>'),
('unused_generic_field', 'member', 'ref', 'ref<T>'),
('unused_static_void', 'param', 'void', 'void)'),
('byte_annotation', 'borrowed', 'ref', 'ref<u8>'),
('iref_ref', 'member', 'ref', 'ref<u64>'),
('buffer_new_ref', 'member', 'ref', 'ref<u64>'),
):
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<T>(p: ref<ref<T>>) -> 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<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: ref<T>) -> 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<T>(p: ref<ref<T>>) -> void effects [] {}\n')
assert json.loads(cli('run', '--manifest', manifest))['value'] == 42
print(f'type position CLI integration passed ({checks} checks)')