#!/usr/bin/env python3 """Inferred constructor content: source diagnostics, transfers and recovery.""" 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/inferred_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 reject(path, token, fields, *args, function='fn unused', expected_file=None): error = json.loads(cli('check', *args, code=1)) assert error['code'] == 'E7005', 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].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 operation in ('own_new', 'own_pack', 'own_array', 'region_new', 'region_array', 'mutex', 'island_create', 'island_alloc', 'arc_new'): for suffix, token, constructor in (('void', 'assert(true)', 'void'), ('never', 'stop()', 'never')): path = CORPUS / f'{operation}_{suffix}.aji' fields = {'constraint': 'Copy', 'type_constructor': 'own_array'} if operation == 'own_array' else { 'position': 'member', 'type_constructor': constructor} reject(path, token, fields, path) for operation in ('own.new', 'region.new', 'region.array'): for suffix in ('ref', 'slice', 'owner', 'nominal', 'task'): path = CORPUS / f'{operation.replace(".", "_")}_{suffix}.aji' token = 'move p' if suffix in ('owner', 'nominal', 'task') else 'p)' if operation.startswith('region') else 'p, heap' reject(path, token, {'constraint': 'Copy', 'operation': operation}, path) for name, constructor in (('own_pack_ref', 'ref'), ('own_pack_slice', 'slice'), ('own_pack_ref_match', 'ref')): path = CORPUS / f'{name}.aji' reject(path, 'p, heap', {'position': 'member', 'type_constructor': constructor}, path) for name, fields in ( ('generic_ref', {'constraint': 'Copy', 'operation': 'own.new'}), ('generic_pack_ref', {'position': 'member', 'type_constructor': 'ref'}), ): path = CORPUS / f'{name}.aji' reject(path, 'x, heap', fields, path, function='fn wrap') # These are verifier rules, not new frontend content diagnostics. Preserve the # feature gates, whole-value ownership errors and graph diagnostic precedence. for name, expected_codes, field in ( ('own_pack_task', ['E0006', 'E0006'], ('feature', 'task_transfer')), ('mutex_endpoint', ['E0006'], ('feature', 'channel_transfer')), ('own_new_missing_move', ['E7003'], ('name', 'p')), ('own_pack_missing_move', ['E7003'], ('name', 'p')), ('graph_cap', ['E2111'], ('found', 'cap')), ('graph_ref', ['E2111'], ('found', 'ref')), ('arc_owner', ['E2111'], ('found', 'own')), ('graph_alloc_nominal', ['E2111'], ('property', 'IslandSafe')), ('graph_alloc_owner', ['E1001'], ('position', 'member')), ('graph_alloc_owner_linear', ['E2111'], ('property', 'IslandSafe')), ('graph_alloc_ref', ['E1001'], ('position', 'member')), ('graph_alloc_ref_block', ['E1001'], ('position', 'member')), ): errors = [json.loads(line) for line in cli('check', CORPUS / f'{name}.aji', code=1).splitlines()] assert [error['code'] for error in errors] == expected_codes, errors assert all(error[field[0]] == field[1] for error in errors), errors for name in ('graph_alloc_owner_linear', 'graph_alloc_ref_block'): path = CORPUS / f'{name}.aji' with tempfile.TemporaryDirectory(prefix='aji-graph-content-format-') as directory: formatted = Path(directory) / 'formatted.aji' formatted.write_text(cli('fmt', path)) cli('fmt', '--check', formatted) assert cli('lower', formatted) == cli('lower', path) with tempfile.TemporaryDirectory(prefix='aji-inferred-content-') as directory: root = Path(directory) for name in ('pack_linear', 'pack_failure', 'pack_mutex', 'mutex_linear', 'region_copy', 'copy_cap', 'copy_iref', 'generic_copy'): source = CORPUS / f'{name}.aji' raw = root / f'{name}.air' air = cli('lower', source) assert cli('lower', source) == air raw.write_text(air) cli('check', raw) grants = ['--grant', f'heap=mem.alloc:{8 if name == "pack_failure" else 128}'] expected = json.loads(cli('run', '--fuel', '100000', *grants, source)) assert expected['value'] == 42, expected assert json.loads(cli('run', '--fuel', '100000', *grants, raw)) == expected result = root / f'{name}.json' cli('run', '--profile', 'hosted', '--fuel', '100000', *grants, '--result-file', result, source) assert json.loads(result.read_text())['value'] == 42 formatted = root / f'{name}.aji' formatted.write_text(cli('fmt', source)) cli('fmt', '--check', formatted) assert cli('fmt', formatted) == formatted.read_text() assert cli('lower', formatted) == air entry, library, manifest = (root / name for name in ('main.aji', 'content.aji', 'aji.toml')) entry.write_text('module app\nimport lib.content;\n' 'fn use(heap: cap, p: ref) -> void effects [mem.alloc] { lib.content.bad::>(heap, p); }\n' 'fn main() -> u64 effects [] { return 42; }\n') manifest.write_text('[package]\nname = "content"\nversion = "1"\nentry = "main.aji"\n[modules]\nlib.content = "content.aji"\n') for operation, fields in ( ('own.new', {'constraint': 'Copy', 'operation': 'own.new'}), ('own.pack', {'position': 'member', 'type_constructor': 'ref'}), ): library.write_text('module lib.content\n' f'fn bad(heap: cap, x: T) -> void effects [mem.alloc] {{ let value = {operation}(x, heap) or trap; trap; }}\n') reject(library, 'x, heap', fields, '--manifest', manifest, function='fn bad', expected_file='content.aji') library.write_text('module lib.content\nfn bad(heap: cap, x: T) -> void effects [mem.alloc] { let value = own.new(42u64, heap) or trap; trap; }\n') assert json.loads(cli('run', '--manifest', manifest))['value'] == 42 entry.write_text('module app\nfn main() -> u64 effects [] { return 42; }\n') library.write_text('module lib.content\nfn bad(heap: cap, x: T) -> void effects [mem.alloc] { let value = own.new(x, heap) or trap; trap; }\n') assert json.loads(cli('run', '--manifest', manifest))['value'] == 42 print(f'inferred content CLI integration passed ({checks} checks)')