104 lines
5.4 KiB
Python
104 lines
5.4 KiB
Python
#!/usr/bin/env python3
|
|
"""Never operands reject; divergent statements retain control flow and effects."""
|
|
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/nonvalue_operands'
|
|
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, token, fields, *args, function='fn main', expected_file=None, code='E7005'):
|
|
error = json.loads(cli('check', *args, code=1))
|
|
assert error['code'] == code, error
|
|
for key, value in fields.items():
|
|
assert error[key] == value, error
|
|
lines = path.read_text().splitlines()
|
|
line = next(i for i, text in enumerate(lines, 1) if text.startswith(function))
|
|
assert (error['file'], error['line'], error['col']) == (
|
|
expected_file or str(path), line, lines[line - 1].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
|
|
|
|
|
|
bool_cases = {'and_left', 'and_right', 'or_left', 'or_right', 'not',
|
|
'if_condition', 'while_condition', 'assert', 'assume'}
|
|
usize_cases = {'index', 'index_argument', 'range_low', 'range_high', 'for_high',
|
|
'split', 'array_size', 'buffer_size', 'buffer_reserve',
|
|
'channel_size', 'cap_budget', 'region_size'}
|
|
tokens = {'call_if': 'if true', 'call_match': 'match Choice::None',
|
|
'call_block': '{ return 42u64;', 'frame_initialization_unreachable': 'ref p = x'}
|
|
|
|
with tempfile.TemporaryDirectory(prefix='aji-nonvalue-operands-') as directory:
|
|
root = Path(directory)
|
|
for source in sorted(CORPUS.glob('*.aji')):
|
|
name, text = source.stem, source.read_text()
|
|
if text.startswith('// expect: reject E7005'):
|
|
base = name.removesuffix('_void')
|
|
wanted = 'a stored value' if name.startswith('target_') else 'bool' if base in bool_cases else 'usize' if base in usize_cases else 'i64' if base == 'negate' else 'u64'
|
|
found = 'void' if name.endswith('_void') else 'never'
|
|
rejection(source, tokens.get(name, 'nothing()' if found == 'void' else 'stop()'),
|
|
{'expected': wanted, 'found': found}, source)
|
|
elif text.startswith('// expect: reject E7006'):
|
|
rejection(source, tokens[name], {'reason': 'unreachable'}, source, code='E7006')
|
|
formatted = root / f'{name}.aji'
|
|
formatted.write_text(cli('fmt', source))
|
|
cli('fmt', '--check', formatted)
|
|
assert cli('fmt', formatted) == formatted.read_text()
|
|
if not text.startswith('// expect: run'):
|
|
continue
|
|
air = cli('lower', source)
|
|
assert cli('lower', formatted) == air
|
|
raw = root / f'{name}.air'
|
|
raw.write_text(air)
|
|
cli('check', source)
|
|
cli('check', raw)
|
|
trapped = '// trap: T0001' in text
|
|
grants = ['--grant', 'heap=mem.alloc:64'] if name == 'linear_branch' else []
|
|
exit_code = 4 if trapped else 0
|
|
expected = json.loads(cli('run', '--fuel', '100000', *grants, source, code=exit_code))
|
|
actual = json.loads(cli('run', '--fuel', '100000', *grants, raw, code=exit_code))
|
|
if trapped:
|
|
assert expected['status'] == 'trapped' and expected['code'] == 'T0001', expected
|
|
assert expected['source_location']['file'] == str(source), expected
|
|
expected.pop('source_location')
|
|
else:
|
|
assert expected['value'] == 42, expected
|
|
assert actual == expected, (name, actual, expected)
|
|
result = root / f'{name}.json'
|
|
cli('run', '--profile', 'hosted', '--fuel', '100000', *grants, '--result-file', result,
|
|
source, code=exit_code)
|
|
hosted = json.loads(result.read_text())
|
|
if trapped:
|
|
assert hosted['status'] == 'trapped' and hosted['code'] == 'T0001', hosted
|
|
else:
|
|
assert hosted['value'] == 42, hosted
|
|
|
|
entry, library, manifest = (root / name for name in ('main.aji', 'values.aji', 'aji.toml'))
|
|
entry.write_text('module app\nimport lib.values;\nfn main() -> u64 effects [] { return lib.values.bad::<u64>(); }\n')
|
|
library.write_text('module lib.values\nfn stop() -> never effects [] { trap; }\n'
|
|
'fn id<T: Copy>(x: T) -> T effects [] { return x; }\n'
|
|
'fn bad<T: Copy>() -> T effects [] { return id::<T>(stop()); }\n')
|
|
manifest.write_text('[package]\nname = "values"\nversion = "1"\nentry = "main.aji"\n[modules]\nlib.values = "values.aji"\n')
|
|
rejection(library, 'stop()', {'expected': 'u64', 'found': 'never'}, '--manifest', manifest,
|
|
function='fn bad', expected_file='values.aji')
|
|
library.write_text(library.read_text().replace('id::<T>(stop())', 'id::<T>(42u64)'))
|
|
assert json.loads(cli('run', '--manifest', manifest))['value'] == 42
|
|
entry.write_text('module app\nfn main() -> u64 effects [] { return 42; }\n')
|
|
library.write_text(library.read_text().replace('id::<T>(42u64)', 'id::<T>(stop())'))
|
|
assert json.loads(cli('run', '--manifest', manifest))['value'] == 42
|
|
|
|
print(f'nonvalue operand CLI integration passed ({checks} checks)')
|