Files
aji/tests/hosted/source_effect_names.py
T

115 lines
6.2 KiB
Python

#!/usr/bin/env python3
"""Effect registry errors are source diagnostics; AIR contracts remain intact."""
import json
from pathlib import Path
import re
import subprocess
import tempfile
REPO = Path(__file__).resolve().parents[2]
AJI = REPO / 'zig-out/bin/aji'
CORPUS = REPO / 'tests/conformance/source/effect_names'
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
expected = {
'ordinary_unused': ('unknown', 1), 'entry': ('bogus.effect', 1),
'body_priority': ('bogus.effect', 1), 'other_body_priority': ('bogus.effect', 1),
'template_unused': ('bogus.effect', 1), 'template_used': ('bogus.effect', 1),
'template_effect_priority': ('bogus.effect', 1), 'ordinary_trusted': ('bogus.effect', 1),
'static_unused': ('bogus.effect', 11), 'static_type': ('bogus.effect', 15),
'keyword_atom': ('task.cancelled', 1), 'first_unknown': ('bogus.first', 1),
}
with tempfile.TemporaryDirectory(prefix='aji-effects-') as directory:
root = Path(directory)
for path in sorted(CORPUS.glob('*.aji')):
source = path.read_text()
formatted = root / path.name
formatted.write_text(cli('fmt', path))
cli('fmt', '--check', formatted)
assert cli('fmt', formatted) == formatted.read_text()
if '// expect: reject' in source:
error = json.loads(cli('check', path, code=1))
assert error['code'] == re.search(r'// expect: reject (E\d+)', source)[1], error
if path.stem in expected:
atom, col = expected[path.stem]
assert (error['name'], error['effect']) == (atom, atom), error
line = next(i for i, text in enumerate(source.splitlines(), 1)
if 'effects [' in text and atom in text)
assert (error['file'], error['line'], error['col']) == (str(path), line, col), error
assert 'reason' not in error and 'air_code' not in error and 'air_line' not in error, error
elif path.stem in ('type_priority', 'return_priority', 'static_type_priority', 'from_priority'):
assert error['name'] == ('missing' if path.stem == 'from_priority' else 'Missing'), error
assert 'effect' not in error, error
elif path.stem in ('static_duplicate', 'template_trusted'):
assert error['reason'] == ('invalid_static_function' if path.stem == 'static_duplicate' else 'trusted_unavailable'), error
commands = ('lower', 'run', 'report', 'explore') if error['code'].startswith('E7') else ('run', 'report', 'explore')
for command in commands:
assert json.loads(cli(command, path, code=1)) == error
if not error['code'].startswith('E7'):
raw = root / (path.stem + '.air')
raw.write_text(cli('lower', path))
assert json.loads(cli('check', raw, code=1))['code'] == error['code']
formatted_error = json.loads(cli('check', formatted, code=1))
assert formatted_error['code'] == error['code']
if error['code'] == 'E7002' and 'effect' in error:
assert formatted_error['name'] == formatted_error['effect'] == error['effect']
continue
cli('check', path)
lowered = cli('lower', path)
assert cli('lower', formatted) == lowered
air = root / (path.stem + '.air')
air.write_text(lowered)
cli('check', air)
grants = [value for grant in re.findall(r'// grant: (.+)', source)
for value in ('--grant', grant)]
result = json.loads(cli('run', '--fuel', '100000', *grants, path))
assert result['value'] == 42, result
assert result == json.loads(cli('run', '--fuel', '100000', *grants, air))
if path.stem == 'allocation':
assert json.loads(cli('run', '--fuel', '100000', path, code=6))['status'] == 'not_instantiated'
# Imported unused ordinary/template/static signatures retain module provenance.
entry, library, manifest = (root / name for name in ('main.aji', 'lib.aji', 'aji.toml'))
manifest.write_text('[package]\nname = "effects"\nversion = "1"\nentry = "main.aji"\n'
'[modules]\nlib.test = "lib.aji"\n')
entry.write_text('module app\nimport lib.test;\nfn main() -> u64 effects [] { return 42; }\n')
for declaration, col in (
('fn unused() -> void effects [bogus.effect] {}', 1),
('fn unused<T>(x: T) -> T effects [bogus.effect] { return x; }', 1),
('fn unused<F: fn(u64) -> u64 effects [bogus.effect]>() -> void effects [] {}', 11),
):
library.write_text('module lib.test\n' + declaration + '\n')
error = json.loads(cli('check', '--manifest', manifest, code=1))
assert (error['code'], error['name'], error['effect']) == ('E7002', 'bogus.effect', 'bogus.effect'), error
assert (error['file'], error['line'], error['col']) == ('lib.aji', 2, col), error
for command in ('lower', 'run', 'report', 'explore'):
assert json.loads(cli(command, '--manifest', manifest, code=1)) == error
# Existing template validation still precedes ordinary signature lowering.
entry.write_text('module app\nfn first() -> void effects [bogus.effect] {}\n'
'fn unused<lowercase>() -> void effects [] {}\n'
'fn main() -> u64 effects [] { return 42; }\n')
error = json.loads(cli('check', entry, code=1))
assert error['code'] == 'E7006' and error['reason'] == 'invalid_generic', error
# Text AIR independently keeps E0004; it does not become a source name error.
raw = root / 'unknown.air'
raw.write_text('module test\nair 0\nlang 0.1\nfeatures [effects]\n'
'fn @main() -> u64\neffects [bogus.effect]\nentry:\n'
' %x = const.u64 42\n ret %x\n')
error = json.loads(cli('check', raw, code=1))
assert error['code'] == 'E0004' and error['name'] == 'bogus.effect', error
assert 'effect' not in error and 'air_code' not in error, error
print(f'source effect names: {checks} checks passed')