#!/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, borrow_literal=False): error = json.loads(cli('check', *args, code=1)) if borrow_literal: assert error['code'] == 'E7006' and error['reason'] == 'invalid_literal', error else: 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'), ('nested', 'own'), ('field', 'own'), ('generic', 'own'), ('generic_member', 'own'), ('static', 'own'), ('local', 'own'), ('operation', 'move value'), ('operation_ref', 'p, heap'), ('operation_slice', 'p, heap'), ('operation_void', 'assert(true)'), ('iref_nested', 'own'), ): path = CORPUS / f'{name}.aji' rejection(path, token, path) borrow_array = CORPUS / 'operation_array_ref.aji' rejection(borrow_array, 'p, p', borrow_array, borrow_literal=True) for name, token in (('specialized', 'own'), ('forward', '[Linear; 2]'), ('enum', 'Linear>'), ('generic_nominal', 'Linear'), ('nested_generic', 'Pair>>'), ('graph_owner', 'island')): 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(p: own_array>) -> void effects [] {}\n') manifest.write_text('[package]\nname = "positions"\nversion = "1"\nentry = "main.aji"\n' '[modules]\nlib.types = "types.aji"\n') rejection(library, 'own', '--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(p: own_array) -> 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 }\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) -> 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(p: own_array>) -> void effects [] {}\n') assert json.loads(cli('run', '--manifest', manifest))['value'] == 42 print(f'owning array content CLI integration passed ({checks} checks)')