diag: preserve envelope fields and reject JSON key collisions in tests

This commit is contained in:
dev committed 2026-10-07 19:28:36 +03:00
1 parent 8ee0f4c2bf
commit 364eb57bbc
16 files changed
+166 -12

No files matched your search

@@ -0,0 +1,5 @@
// expect: accept
// phase: 6
// spec: docs/spec/10-diagnostics-and-agent-protocol.md §2
module diagnostic.known_capability
fn main(c: cap<mem.alloc>) -> void effects [] {}
@@ -0,0 +1,5 @@
// expect: reject E7002
// phase: 6
// spec: docs/spec/10-diagnostics-and-agent-protocol.md §2
module diagnostic.unknown_capability
fn main(c: cap<unknown>) -> void effects [] {}
+27 -6
View File
@@ -20,6 +20,16 @@ def cli(*args, code=0):
return p.stdout
def decode_json(output):
def unique_object(pairs):
result = {}
for key, value in pairs:
assert key not in result, ('duplicate JSON member', key, output)
result[key] = value
return result
return json.loads(output, object_pairs_hook=unique_object)
def related(error, source=False):
global covered
field = 'task' if error['code'] == 'E4302' else 'conflicts_with'
@@ -59,13 +69,13 @@ def air_origin(text, error):
def source_check(root, *args):
errors = [json.loads(line) for line in cli('check', *args, code=1).splitlines()]
errors = [decode_json(line) for line in cli('check', *args, code=1).splitlines()]
assert cli('check', *args, code=1) == cli('check', *args, code=1)
canonical = cli('lower', *args)
annotated = cli('lower', '--annotate', *args)
path = root / 'lowered.air'
path.write_text(canonical)
raw = [json.loads(line) for line in cli('check', path, code=1).splitlines()]
raw = [decode_json(line) for line in cli('check', path, code=1).splitlines()]
# Independently reconstruct the public annotation map, then use the
# unchanged primary-code/location deduplication to predict source output.
mapping = {}
@@ -165,7 +175,7 @@ entry:
text = 'module definitions\nair 0\nlang 0.1\nfeatures [arrays, effects, ownership]\n' + body + callees
path = root / 'definitions.air'
path.write_text(text)
errors = [json.loads(line) for line in cli('check', path, code=1).splitlines()]
errors = [decode_json(line) for line in cli('check', path, code=1).splitlines()]
assert len(errors) == 1 and errors[0]['code'] == 'E2101', errors
air_origin(text, errors[0])
context = errors[0]['related_locations'][0]
@@ -178,7 +188,7 @@ entry:
text = path.read_text()
if not re.search(r'^; expect: reject .*E(?:2101|2104|4302)', text, re.M):
continue
errors = [json.loads(line) for line in cli('check', path, code=1).splitlines()]
errors = [decode_json(line) for line in cli('check', path, code=1).splitlines()]
for error in errors:
air_origin(text, error)
for path in sorted((corpus / 'source').rglob('*.aji')):
@@ -191,7 +201,7 @@ entry:
errors = source_check(root, corpus / 'source/related_locations/borrow_live.aji')
assert errors[0]['line'] == 8 and errors[0]['related_locations'][0]['line'] == 7
assert json.loads(cli('run', '--fuel', '10000', corpus / 'source/related_locations/borrow_ended.aji'))['value'] == 2
assert decode_json(cli('run', '--fuel', '10000', corpus / 'source/related_locations/borrow_ended.aji'))['value'] == 2
# A returned view's definition is the caller's call, not the callee's from.
errors = source_check(root, corpus / 'source/borrow_returns/parent_access.aji')
context = errors[0]['related_locations'][0]
@@ -216,7 +226,18 @@ entry:
# Codes outside the scope keep the old envelope.
other = root / 'other.aji'
other.write_text('module other fn main()->u64 effects [] {return true;}')
assert 'related_locations' not in json.loads(cli('check', other, code=1))
assert 'related_locations' not in decode_json(cli('check', other, code=1))
for fixture, code, kind, field, value in (
('phase2/reject/unknown_narrowing_form.air', 'E3202', 'capability.widen', 'capability_kind', 'io.write'),
('phase3/reject/sync_unlock_live_borrow.air', 'E2102', 'borrow.outlives_owner', 'guard', '%g'),
('source/diagnostic_envelope/capability_kind_unknown.aji', 'E7002', 'source.unknown_name', 'capability_kind', 'unknown'),
):
error = decode_json(cli('check', corpus / fixture, code=1))
assert error['code'] == code and error['kind'] == kind and error[field] == value, error
if field == 'guard':
assert error['borrow'] == '%p', error
cli('check', corpus / 'source/diagnostic_envelope/capability_kind_known.aji')
assert covered > 0
print(f'related location CLI passed ({checks} checks; {covered} context records)')