Files
aji/tests/hosted/source_store_modes.py
T

122 lines
5.6 KiB
Python

#!/usr/bin/env python3
"""Source access modes, exact diagnostics and preserved execution/lowering."""
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/store_modes'
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, expected, found, *args, expected_file=None):
error = json.loads(cli('check', *args, code=1))
assert (error['code'], error['expected'], error['found']) == ('E7005', expected, found), error
lines = path.read_text().splitlines()
line = next(i for i, text in enumerate(lines, 1) if token in text and text.startswith('fn '))
assert (error['file'], error['line'], error['col']) == (
expected_file or str(path), line, lines[line - 1].rindex(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 (
('shared_deref', '*p'), ('shared_field', 'p.a'),
('shared_nested_field', 'p.pair.a'), ('shared_slice', 's[0]'),
('shared_array', 'p[0]'), ('shared_array_field', 'p.data[0]'),
('shared_local', '*p'), ('shared_local_field', 'p.a'),
('shared_local_slice', 's[0]'), ('shared_reborrow', 'q.a'),
('shared_own_array', 's[0]'), ('mut_binding', 'p;'),
('mut_field_binding', 'p.a'), ('mut_index_binding', 's[0]'),
('mut_unchecked_binding', 'p[0]'),
):
path = CORPUS / f'{name}.aji'
reject(path, token, 'mut<u64>', 'ref<u64>', path)
for name, token in (
('shared_mut_array_binding', 'p;'), ('mut_array_deref_binding', '*p'),
('mut_array_field_binding', 'p.data'), ('mut_array_argument', 'mut p'),
('mut_field_argument', 'mut p.a'),
):
path = CORPUS / f'{name}.aji'
reject(path, token, 'mut<array<u64, 2>>', 'ref<array<u64, 2>>', path)
for name, token in (
('mut_slice_binding', 's;'), ('mut_range_binding', 's[0..1]'),
('mut_range_argument', 'mut p[0..1]'), ('mut_ref_array_range', 'p[0..1]'),
):
path = CORPUS / f'{name}.aji'
reject(path, token, 'slice_mut<u64>', 'slice<u64>', path)
for name, found in (('slice_deref', 'slice<u64>'), ('mut_slice_deref', 'slice_mut<u64>')):
path = CORPUS / f'{name}.aji'
reject(path, '*s', 'mut<u64>', found, path)
# RHS evaluation still precedes target borrowing; linear/alias restrictions
# remain AIR diagnostics when the place already has the required mode.
for name, code, fields in (
('rhs_never', 'E7005', {'expected': 'u64', 'found': 'never'}),
('rhs_unknown', 'E7002', {'name': 'missing'}),
('whole_array', 'E7006', {'reason': 'invalid_assignment'}),
('unknown_field', 'E7002', {'field': 'missing'}),
('bad_index', 'E7005', {'expected': 'usize', 'found': 'bool'}),
('linear_store', 'E1001', {}), ('alias_store', 'E2101', {}),
('parameter_place', 'E7006', {'reason': 'invalid_place'}),
):
error = json.loads(cli('check', CORPUS / f'{name}.aji', code=1))
assert error['code'] == code, error
assert all(error[key] == value for key, value in fields.items()), error
if code in ('E1001', 'E2101'):
assert 'air_line' in error, error
with tempfile.TemporaryDirectory(prefix='aji-store-modes-') as directory:
root = Path(directory)
for source in sorted(CORPUS.glob('*.aji')):
formatted = root / source.name
formatted.write_text(cli('fmt', source))
cli('fmt', '--check', formatted)
assert cli('fmt', formatted) == formatted.read_text()
if '// expect: run' not in source.read_text():
continue
air = cli('lower', source)
assert cli('lower', formatted) == air
raw = root / f'{source.stem}.air'
raw.write_text(air)
cli('check', raw)
grants = ['--grant', 'heap=mem.alloc:128'] if 'heap: cap' in source.read_text() else []
record = json.loads(cli('run', '--fuel', '100000', *grants, source))
assert record['value'] == 42, record
assert json.loads(cli('run', '--fuel', '100000', *grants, raw)) == record
result = root / f'{source.stem}.json'
cli('run', '--profile', 'hosted', '--fuel', '100000', *grants,
'--result-file', result, source)
assert json.loads(result.read_text())['value'] == 42
entry, library, manifest = (root / name for name in ('main.aji', 'places.aji', 'aji.toml'))
manifest.write_text('[package]\nname = "places"\nversion = "1"\nentry = "main.aji"\n'
'[modules]\nlib.places = "places.aji"\n')
entry.write_text('module app\nimport lib.places;\n'
'fn use(p: ref<u64>) -> void effects [] { lib.places.write::<u64>(p, 1u64); }\n'
'fn main() -> u64 effects [] { return 42; }\n')
library.write_text('module lib.places\n'
'fn write<T: Copy>(p: ref<T>, v: T) -> void effects [] { *p = v; }\n')
reject(library, '*p', 'mut<u64>', 'ref<u64>', '--manifest', manifest,
expected_file='places.aji')
library.write_text(library.read_text().replace('p: ref<T>', 'p: mut<T>'))
entry.write_text('module app\nimport lib.places;\n'
'fn main() -> u64 effects [] { var x: u64 = 0u64; lib.places.write::<u64>(mut x, 42u64); return x; }\n')
assert json.loads(cli('run', '--manifest', manifest))['value'] == 42
print(f'store modes CLI integration passed ({checks} checks)')