103 lines
4.8 KiB
Python
103 lines
4.8 KiB
Python
#!/usr/bin/env python3
|
|
"""Dynamic owning-array Copy content: annotations, operations and package parity."""
|
|
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/own_array_content'
|
|
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, *args, expected_file=None, line=5):
|
|
error = json.loads(cli('check', *args, code=1))
|
|
assert error['code'] == 'E7005', error
|
|
assert error['constraint'] == 'Copy' and error['type_constructor'] == 'own_array', 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, token in (
|
|
('owner', 'own<u64>'),
|
|
('nested', 'own<u64>'),
|
|
('field', 'own<u64>'),
|
|
('generic', 'own<T>'),
|
|
('generic_member', 'own<T>'),
|
|
('static', 'own<T>'),
|
|
('local', 'own<u64>'),
|
|
('operation', 'move value'),
|
|
('operation_ref', 'p, heap'),
|
|
('operation_slice', 'p, heap'),
|
|
('operation_void', 'assert(true)'),
|
|
('operation_array_ref', 'a, heap'),
|
|
('iref_nested', 'own<u64>'),
|
|
):
|
|
path = CORPUS / f'{name}.aji'
|
|
rejection(path, token, path)
|
|
for name, token in (('specialized', 'own<u64>'), ('forward', '[Linear; 2]'),
|
|
('enum', 'Linear>'), ('generic_nominal', 'Linear<T>'),
|
|
('nested_generic', 'Pair<Pair<own<T>>>'), ('graph_owner', 'island<Node>')):
|
|
path = CORPUS / f'{name}.aji'
|
|
rejection(path, token, path, line=6 if name != 'forward' else 5)
|
|
deep = CORPUS / 'operation_deep_nominal.aji'
|
|
rejection(deep, 'Deep {', deep, line=6)
|
|
|
|
with tempfile.TemporaryDirectory(prefix='aji-own-array-content-') 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', '--grant', 'heap=mem.alloc:4096', source))
|
|
assert expected['value'] == 42
|
|
assert json.loads(cli('run', '--fuel', '100000', '--grant', 'heap=mem.alloc:4096', raw)) == expected
|
|
result = root / 'hosted.json'
|
|
cli('run', '--profile', 'hosted', '--fuel', '100000', '--result-file', result, '--grant', 'heap=mem.alloc:4096', 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: own_array<own<T>>) -> void effects [] {}\n')
|
|
manifest.write_text('[package]\nname = "positions"\nversion = "1"\nentry = "main.aji"\n'
|
|
'[modules]\nlib.types = "types.aji"\n')
|
|
rejection(library, 'own<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: own_array<T>) -> void effects [] {}\n')
|
|
assert json.loads(cli('run', '--manifest', manifest))['value'] == 42
|
|
|
|
# Traverse fields in their own imported namespace, including a second import.
|
|
payload = root / 'payload.aji'
|
|
payload.write_text('module lib.payload\nstruct Value { owner: own<u64> }\n')
|
|
library.write_text('module lib.types\nimport lib.payload;\nstruct Container { value: lib.payload.Value }\n')
|
|
manifest.write_text(manifest.read_text() + 'lib.payload = "payload.aji"\n')
|
|
entry.write_text('module app\nimport lib.types;\nfn unused(p: own_array<lib.types.Container>) -> void effects [] { trap; }\nfn main() -> u64 effects [] { return 42; }\n')
|
|
rejection(entry, 'lib.types.Container', '--manifest', manifest,
|
|
expected_file=str(entry), line=3)
|
|
payload.write_text('module lib.payload\nstruct Value { owner: u64 }\n')
|
|
assert json.loads(cli('run', '--manifest', manifest))['value'] == 42
|
|
|
|
# Invalid mapped modules that are unreachable remain outside compilation.
|
|
entry.write_text('module app\nfn main() -> u64 effects [] { return 42; }\n')
|
|
library.write_text('module lib.types\nfn unused<T>(p: own_array<own<T>>) -> void effects [] {}\n')
|
|
assert json.loads(cli('run', '--manifest', manifest))['value'] == 42
|
|
|
|
print(f'owning array content CLI integration passed ({checks} checks)')
|