Files
aji/tests/hosted/source_array_arguments.py
T

87 lines
4.2 KiB
Python

#!/usr/bin/env python3
"""Whole-array arguments: CLI/AIR parity, temporary cleanup and package provenance."""
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_arguments'
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
with tempfile.TemporaryDirectory(prefix='aji-array-arguments-') as directory:
root = Path(directory)
raw = root / 'lowered.air'
for name, value in {'direct': 42, 'projections': 84, 'reborrow': 43,
'shared_forward': 42, 'generic': 42, 'static': 42,
'bytes_explicit': 104, 'retained_other_temporary': 42,
'unchecked': 42, 'slices': 42}.items():
source = CORPUS / f'{name}.aji'
cli('check', source)
air = cli('lower', source)
assert cli('lower', source) == air
raw.write_text(air)
cli('check', raw)
result = json.loads(cli('run', '--fuel', '100000', source))
assert result['value'] == value, (name, result)
assert json.loads(cli('run', '--fuel', '100000', raw)) == result
hosted = root / 'hosted.json'
assert cli('run', '--profile', 'hosted', '--fuel', '100000',
'--result-file', hosted, source) == ''
assert json.loads(hosted.read_text())['value'] == value
if name == 'slices':
assert 'slice.make' in air
else:
# The unrelated retained origin is intentionally still a slice.
main = air.split('fn @main(', 1)[1]
if name != 'retained_other_temporary':
assert 'slice.make' not in main, (name, main)
# Real package linking, including a specialized imported signature.
entry, library, manifest = (root / name for name in ('main.aji', 'arrays.aji', 'aji.toml'))
entry.write_text((CORPUS / 'imported.aji').read_text())
library.write_text((CORPUS / 'arrays.aji.inc').read_text())
manifest.write_text('[package]\nname = "arrays"\nversion = "1"\nentry = "main.aji"\n'
'[modules]\ntest.arrays = "arrays.aji"\n')
cli('check', '--manifest', manifest)
assert json.loads(cli('run', '--fuel', '100000', '--manifest', manifest))['value'] == 42
air = cli('lower', '--manifest', manifest)
assert cli('lower', '--manifest', manifest) == air
raw.write_text(air)
cli('check', raw)
assert json.loads(cli('run', '--fuel', '100000', raw))['value'] == 42
cli('report', '--manifest', manifest)
cli('explore', '--max-runs', '8', '--manifest', manifest)
# Type mismatch comes from the imported body with its original source position.
library.write_text(library.read_text().replace('return a[0];', 'return first::<T>(ref a[0]);'))
error = json.loads(cli('check', '--manifest', manifest, code=1))
assert error['code'] == 'E7005', error
assert error['file'] == 'arrays.aji' and error['line'] == 2, error
assert error['expected'] == 'ref<array<u64, 2>>' and error['found'] == 'ref<u64>', error
for command in ('run', 'report', 'explore'):
assert json.loads(cli(command, '--manifest', manifest, code=1)) == error
# Aliasing failures retain the AIR verifier code and a useful argument position.
source = CORPUS / 'conflict.aji'
errors = [json.loads(line) for line in cli('check', source, code=1).splitlines()]
assert len(errors) == 2 and all(e['code'] == 'E2101' for e in errors), errors
assert all(e['file'] == str(source) and e['line'] == 7 and e['col'] > 1 for e in errors), errors
raw.write_text(cli('lower', source))
air_errors = [json.loads(line) for line in cli('check', raw, code=1).splitlines()]
# Source deduplication collapses the two conflicting call operands at
# the same source position; raw AIR retains both factual identities.
assert len(air_errors) == 3 and all(e['code'] == 'E2101' for e in air_errors), air_errors
print(f'array argument CLI integration passed ({checks} checks)')