#!/usr/bin/env python3 """Stable source E7006 categories and canonical CLI/package diagnostics.""" import json from pathlib import Path import re import subprocess import tempfile REPO = Path(__file__).resolve().parents[2] AJI = REPO / 'zig-out/bin/aji' checks = 0 def invoke(*args, code=0): global checks p = subprocess.run([str(AJI), *map(str, args)], capture_output=True) assert p.returncode == code, (args, p.returncode, p.stdout, p.stderr) checks += 1 return p.stdout with tempfile.TemporaryDirectory(prefix='aji-reasons-') as d: root = Path(d) cases = [ ('move_copy', 'fn main()->u64 effects [] {let x=1u64;return move x;}'), ('move_copy', 'struct S{x:u64} fn main()->u64 effects [] {let x=S{x:1u64};let y=move x;return 0u64;}'), ('move_copy', 'fn main()->u64 effects [] {let x=[1u64;2];let y=move x;return 0u64;}'), ('move_borrow', 'fn main()->u64 effects [] {let x=1u64;ref p=x;return move p;}'), ('loop_control_outside_loop', 'fn main()->void effects [] {break;}'), ('zero_not_zeroable', 'fn main()->u64 effects [] {var x:own = zero;return 0u64;}'), ('value_block_statement', 'fn main()->i64 effects [] {{2i64} -3i64}'), ('unreachable', 'fn main()->void effects [] {return; return;}'), ('missing_return', 'fn main()->u64 effects [] {}'), ('duplicate_declaration', 'fn main()->u64 effects [] {0u64} fn main()->u64 effects [] {0u64}'), ('invalid_generic', 'fn f(x:T)->T effects [] {x} fn main()->u64 effects [] {0u64}'), ('invalid_borrow_return', 'fn f(x:ref)->ref effects [] {x} fn main()->u64 effects [] {0u64}'), ] for i, (reason, body) in enumerate(cases): path = root / f'case{i}.aji' path.write_text('module reasons\n' + body + '\n') error = json.loads(invoke('check', path, code=1)) assert error['code'] == 'E7006' and error['reason'] == reason, error assert error['file'] == str(path) and error['line'] == 2 if i == 0: assert list(error) == ['code', 'kind', 'severity', 'file', 'line', 'col', 'name', 'type', 'reason', 'message'], error assert json.loads(invoke('lower', path, code=1)) == error assert invoke('check', path, code=1) == invoke('check', path, code=1) # Void parameters are rejected by annotation validation before body # lowering; this must never be attributed to an invalid_move body error. invalid_parameter = root / 'void-parameter.aji' invalid_parameter.write_text('module reasons\nfn f(x:void)->u64 effects [] {return move x;} ' 'fn main()->u64 effects [] {0u64}\n') error = json.loads(invoke('check', invalid_parameter, code=1)) assert (error['code'], error['position'], error['type_constructor']) == ('E7005', 'param', 'void'), error assert 'reason' not in error and json.loads(invoke('lower', invalid_parameter, code=1)) == error # Copy substitution in a generic transfer is permitted, not a Copy-move # diagnostic. Borrowed substitution still rejects with its own reason. valid = root / 'generic.aji' valid.write_text('module generic fn f(x:T)->T effects [] {move x} ' 'fn main()->u64 effects [] {f::(42u64)}') assert json.loads(invoke('run', '--fuel', '10000', valid))['value'] == 42 bad = root / 'borrow-generic.aji' bad.write_text('module bg fn f(x:T)->T effects [] {move x} ' 'fn main()->u64 effects [] {let x=1u64;ref p=x;let y=f::>(p);return 0u64;}') error = json.loads(invoke('check', bad, code=1)) # The missing explicit origin in the specialized signature is diagnosed # before its body, and must never be counted as move_copy. assert error['reason'] == 'invalid_borrow_return', error bad.write_text('module bg fn f(x:ref)->u64 effects [] {let y=move x;return 0u64;} ' 'fn main()->u64 effects [] {let x=1u64;ref p=x;return f::(p);}') error = json.loads(invoke('check', bad, code=1)) assert error['reason'] == 'move_borrow', error entry = root / 'entry.aji' dep = root / 'dep.aji' entry.write_text('module app import dep; fn main()->u64 effects [] {dep.f()}') dep.write_text('module dep fn f()->u64 effects [] {let n=1u64;return move n;}') manifest = root / 'aji.toml' manifest.write_text('[package]\nname = "reasons"\nversion = "1"\nentry = "entry.aji"\n' '[modules]\ndep = "dep.aji"\n') error = json.loads(invoke('check', '--manifest', manifest, code=1)) assert error['reason'] == 'move_copy' and error['file'] == dep.name, error dep.write_text('module different fn f()->u64 effects [] {1u64}') error = json.loads(invoke('check', '--manifest', manifest, code=1)) assert error['reason'] == 'module_header_mismatch' and error['file'] == dep.name, error # Exercise all public standalone E7006 fixtures, without visiting any # benchmark corpus or unrelated untracked directory. registry = (REPO / 'docs/reference/error-codes.md').read_text() reason_section = registry.split('## E7006 reasons\n', 1)[1].split('\n## ', 1)[0] reasons = set(re.findall(r'^\| `([a-z_]+)` \|', reason_section, re.M)) fixtures = 0 for path in sorted((REPO / 'tests/conformance/source').rglob('*.aji')): if not re.search(r'^// expect: reject E7006\s*$', path.read_text(), re.M): continue p = subprocess.run([str(AJI), 'check', str(path)], capture_output=True) if p.returncode == 1: errors = [json.loads(line) for line in p.stdout.splitlines()] for error in errors: if error['code'] == 'E7006': assert error['reason'] in reasons, (path, error) fixtures += 1 checks += 1 assert fixtures > 0, fixtures ordinary = root / 'type-error.aji' ordinary.write_text('module ordinary fn main()->u64 effects [] {return true;}') error = json.loads(invoke('check', ordinary, code=1)) assert error['code'] == 'E7005' and 'reason' not in error # A scope name never reaches ordinary value lowering, including generic # bodies and branch/block yields. No misleading move-repair is emitted. scope_cases = REPO / 'tests/conformance/source/scopes' for name in ('array', 'bare_array', 'bare_alias', 'block_yield', 'call_argument', 'discard', 'equality', 'generic_alias', 'if_yield', 'match_yield', 'move_alias', 'move_discard', 'return', 'scope_tail'): path = scope_cases / f'{name}.aji' error = json.loads(invoke('check', path, code=1)) assert (error['code'], error['reason'], error['name'], error['type']) == ( 'E7006', 'invalid_expression', 's', 'scope'), error assert 'suggestion' not in error, error line = path.read_text().splitlines()[error['line'] - 1] assert line[error['col'] - 1:].startswith(('s', 'move s')), (path, error) assert json.loads(invoke('lower', path, code=1)) == error for name, code, reason in (('borrow', 'E7006', 'invalid_place'), ('assignment', 'E7006', 'invalid_assignment'), ('moved_spawn', 'E7005', None), ('spawn_shadow', 'E7005', None), ('out_of_block', 'E7002', None), ('unjoined_shadow', 'E4301', None)): error = json.loads(invoke('check', scope_cases / f'{name}.aji', code=1)) assert error['code'] == code and error.get('reason') == reason, error # Imported specialized bodies retain the location of their forbidden # scope use, rather than the call or the generated AIR. entry.write_text('module app import dep; fn main()->u64 effects [] {dep.f::(42u64)}') dep.write_text('module dep\nfn f(x:T)->T effects [] {\n' 'scope s { let alias = move s; }\nmove x\n}\n') error = json.loads(invoke('check', '--manifest', manifest, code=1)) assert (error['code'], error['reason'], error['file'], error['line'], error['col']) == ( 'E7006', 'invalid_expression', dep.name, 3, 23), error assert json.loads(invoke('lower', '--manifest', manifest, code=1)) == error # Lexical completion and task/borrow cleanup are identical under both # profiles and independent scheduler choices, even with shadowed names. for name in ('nested_same_name', 'normal_exits', 'visible_outer', 'shadow_value'): path = scope_cases / f'{name}.aji' for quantum in (1, 5, 16): args = ['--fuel', '10000', '--quantum', str(quantum), '--schedule-seed', '7'] assert json.loads(invoke('run', *args, path))['value'] == 42 result_file = root / 'scope-result.json' invoke('run', '--profile', 'hosted', '--result-file', result_file, *args, path) assert json.loads(result_file.read_text())['value'] == 42 print(f'source diagnostic reasons passed ({checks} checks; {fixtures} public E7006 fixtures)')