diag: add stable source reasons and diagnostic incidence reporting
This commit is contained in:
1 parent
911982dd1c
commit
dbd9763713
21 files changed
+571
-156
No files matched your search
@@ -0,0 +1,103 @@
|
||||
#!/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;}'),
|
||||
('invalid_move', 'fn f(x:void)->u64 effects [] {return move x;} fn main()->u64 effects [] {0u64}'),
|
||||
('loop_control_outside_loop', 'fn main()->void effects [] {break;}'),
|
||||
('zero_not_zeroable', 'fn main()->u64 effects [] {var x:own<u64> = 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<T:Unknown>(x:T)->T effects [] {x} fn main()->u64 effects [] {0u64}'),
|
||||
('invalid_borrow_return', 'fn f(x:ref<u64>)->ref<u64> 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)
|
||||
|
||||
# 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<T: Copy>(x:T)->T effects [] {move x} '
|
||||
'fn main()->u64 effects [] {f::<u64>(42u64)}')
|
||||
assert json.loads(invoke('run', '--fuel', '10000', valid))['value'] == 42
|
||||
bad = root / 'borrow-generic.aji'
|
||||
bad.write_text('module bg fn f<T>(x:T)->T effects [] {move x} '
|
||||
'fn main()->u64 effects [] {let x=1u64;ref p=x;let y=f::<ref<u64>>(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<T>(x:ref<T>)->u64 effects [] {let y=move x;return 0u64;} '
|
||||
'fn main()->u64 effects [] {let x=1u64;ref p=x;return f::<u64>(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()
|
||||
reasons = set(re.findall(r'^\| `([a-z_]+)` \|', registry, 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
|
||||
print(f'source diagnostic reasons passed ({checks} checks; {fixtures} public E7006 fixtures)')
|
||||
Reference in new issue
Block a user