frontend: validate unknown effect names before body lowering

This commit is contained in:
dev committed 2026-10-08 14:49:49 +03:00
1 parent 4c9c5626c9
commit 762c5abe7b
40 files changed
+335 -9

No files matched your search

@@ -0,0 +1,10 @@
// expect: run
// result: 42
// grant: heap=mem.alloc:8
// phase: 6
// spec: docs/spec/15-source-language.md §8, §9
module test.effects.allocation
fn main(heap: cap<mem.alloc>) -> u64 effects [mem.alloc] {
let value = own.new(42u64, heap) or trap;
return own.take(move value);
}
@@ -0,0 +1,5 @@
// expect: reject E7002
// phase: 6
// spec: docs/spec/15-source-language.md §9, §18
module test.effects.body_priority
fn main() -> u64 effects [bogus.effect] { return missing; }
@@ -0,0 +1,8 @@
// expect: run
// phase: 6
// spec: docs/spec/15-source-language.md §9, §18
// result: 42
module test.effects.duplicates
fn worker() -> u64 effects [atomic, atomic] { return 42; }
fn identity<T: Copy>(x: T) -> T effects [atomic, atomic] { return x; }
fn main() -> u64 effects [atomic, atomic] { return identity::<u64>(worker()); }
@@ -0,0 +1,5 @@
// expect: reject E7002
// phase: 6
// spec: docs/spec/15-source-language.md §9, §18
module test.effects.entry
fn main() -> u64 effects [bogus.effect] { return 42; }
@@ -0,0 +1,6 @@
// expect: reject E7002
// phase: 6
// spec: docs/spec/15-source-language.md §9, §18
module test.effects.first_unknown
fn unused() -> void effects [atomic, bogus.first, bogus.second,] {}
fn main() -> u64 effects [] { return 42; }
@@ -0,0 +1,6 @@
// expect: reject E3101
// phase: 6
// spec: docs/spec/05-effects-and-capabilities.md §9.2
module test.effects.flat_effects
fn worker() -> u64 effects [atomic.weak] { return 42; }
fn main() -> u64 effects [atomic] { return worker(); }
@@ -0,0 +1,6 @@
// expect: reject E7002
// phase: 6
// spec: docs/spec/15-source-language.md §9, §18
module test.effects.from_priority
fn unused(x: ref<u64>) -> ref<u64> from missing effects [bogus.effect] { return x; }
fn main() -> u64 effects [] { return 42; }
@@ -0,0 +1,6 @@
// expect: reject E7002
// phase: 6
// spec: docs/spec/15-source-language.md §9, §18
module test.effects.keyword_atom
fn unused() -> void effects [task.cancelled] {}
fn main() -> u64 effects [] { return 42; }
@@ -0,0 +1,6 @@
// expect: reject E3101
// phase: 6
// spec: docs/spec/15-source-language.md §9, §18
module test.effects.missing_effect
fn worker() -> u64 effects [atomic] { return 42; }
fn main() -> u64 effects [] { return worker(); }
@@ -0,0 +1,6 @@
// expect: reject E7002
// phase: 6
// spec: docs/spec/15-source-language.md §9, §18
module test.effects.ordinary_trusted
fn unused() -> void effects [bogus.effect] trusted "not allowed" {}
fn main() -> u64 effects [] { return 42; }
@@ -0,0 +1,6 @@
// expect: reject E7002
// phase: 6
// spec: docs/spec/15-source-language.md §9, §18
module test.effects.ordinary_unused
fn unused() -> void effects [unknown] {}
fn main() -> u64 effects [] { return 42; }
@@ -0,0 +1,7 @@
// expect: reject E7002
// phase: 6
// spec: docs/spec/15-source-language.md §9, §18
module test.effects.other_body_priority
fn first() -> u64 effects [] { return missing; }
fn last() -> void effects [bogus.effect] {}
fn main() -> u64 effects [] { return 42; }
@@ -0,0 +1,7 @@
// expect: run
// phase: 6
// spec: docs/spec/15-source-language.md §9, §18
// result: 42
module test.effects.registered_unused
fn unused() -> void effects [atomic, atomic.weak, clock.read, ffi, fs.read, fs.write, io.read, io.write, mem.alloc, mem.map, net.connect, net.listen, net.read, net.write, process.env, process.spawn, random.read, task.spawn, task.wait, thread.block, thread.spawn, unsafe] {}
fn main() -> u64 effects [] { return 42; }
@@ -0,0 +1,6 @@
// expect: reject E7002
// phase: 6
// spec: docs/spec/15-source-language.md §9, §18
module test.effects.return_priority
fn unused() -> Missing effects [bogus.effect] { trap; }
fn main() -> u64 effects [] { return 42; }
@@ -0,0 +1,6 @@
// expect: reject E7006
// phase: 6
// spec: docs/spec/15-source-language.md §9, §18
module test.effects.static_duplicate
fn unused<F: fn(u64) -> u64 effects [atomic, atomic]>() -> void effects [] {}
fn main() -> u64 effects [] { return 42; }
@@ -0,0 +1,6 @@
// expect: reject E7002
// phase: 6
// spec: docs/spec/15-source-language.md §9, §18
module test.effects.static_type
struct Holder<F: fn(u64) -> u64 effects [bogus.effect]> { value: u64 }
fn main() -> u64 effects [] { return 42; }
@@ -0,0 +1,6 @@
// expect: reject E7002
// phase: 6
// spec: docs/spec/15-source-language.md §9, §18
module test.effects.static_type_priority
fn unused<F: fn(Missing) -> u64 effects [bogus.effect]>() -> void effects [] {}
fn main() -> u64 effects [] { return 42; }
@@ -0,0 +1,6 @@
// expect: reject E7002
// phase: 6
// spec: docs/spec/15-source-language.md §9, §18
module test.effects.static_unused
fn unused<F: fn(u64) -> u64 effects [bogus.effect]>() -> void effects [] {}
fn main() -> u64 effects [] { return 42; }
@@ -0,0 +1,8 @@
// expect: run
// result: 42
// phase: 6
// spec: docs/spec/15-source-language.md §18
module test.effects.static_valid
fn worker() -> u64 effects [atomic.weak, atomic] { return 42; }
fn apply<F: fn() -> u64 effects [atomic, atomic.weak]>() -> u64 effects [atomic, atomic.weak] { return F(); }
fn main() -> u64 effects [atomic, atomic.weak] { return apply::<fn worker>(); }
@@ -0,0 +1,6 @@
// expect: reject E7002
// phase: 6
// spec: docs/spec/15-source-language.md §9, §18
module test.effects.template_effect_priority
fn unused<T>(x: Missing) -> T effects [bogus.effect] { trap; }
fn main() -> u64 effects [] { return 42; }
@@ -0,0 +1,6 @@
// expect: reject E7006
// phase: 6
// spec: docs/spec/15-source-language.md §9, §18
module test.effects.template_trusted
fn unused<T>() -> void effects [bogus.effect] trusted "not allowed" {}
fn main() -> u64 effects [] { return 42; }
@@ -0,0 +1,6 @@
// expect: reject E7002
// phase: 6
// spec: docs/spec/15-source-language.md §9, §18
module test.effects.template_unused
fn unused<T>(x: T) -> T effects [bogus.effect] { return x; }
fn main() -> u64 effects [] { return 42; }
@@ -0,0 +1,6 @@
// expect: reject E7002
// phase: 6
// spec: docs/spec/15-source-language.md §9, §18
module test.effects.template_used
fn identity<T>(x: T) -> T effects [bogus.effect] { return x; }
fn main() -> u64 effects [] { return identity::<u64>(42); }
@@ -0,0 +1,6 @@
// expect: reject E7002
// phase: 6
// spec: docs/spec/15-source-language.md §9, §18
module test.effects.type_priority
fn unused(x: Missing) -> void effects [bogus.effect] {}
fn main() -> u64 effects [] { return 42; }
@@ -1,4 +1,4 @@
// expect: reject E0006
// expect: reject E7002
// phase: 6
// spec: docs/spec/15-source-language.md §15
module demo.generic
@@ -1,4 +1,4 @@
// expect: reject E0006
// expect: reject E7002
// phase: 6
// spec: docs/spec/15-source-language.md §18
module test.static_functions
+114
View File
@@ -0,0 +1,114 @@
#!/usr/bin/env python3
"""Effect registry errors are source diagnostics; AIR contracts remain intact."""
import json
from pathlib import Path
import re
import subprocess
import tempfile
REPO = Path(__file__).resolve().parents[2]
AJI = REPO / 'zig-out/bin/aji'
CORPUS = REPO / 'tests/conformance/source/effect_names'
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
expected = {
'ordinary_unused': ('unknown', 1), 'entry': ('bogus.effect', 1),
'body_priority': ('bogus.effect', 1), 'other_body_priority': ('bogus.effect', 1),
'template_unused': ('bogus.effect', 1), 'template_used': ('bogus.effect', 1),
'template_effect_priority': ('bogus.effect', 1), 'ordinary_trusted': ('bogus.effect', 1),
'static_unused': ('bogus.effect', 11), 'static_type': ('bogus.effect', 15),
'keyword_atom': ('task.cancelled', 1), 'first_unknown': ('bogus.first', 1),
}
with tempfile.TemporaryDirectory(prefix='aji-effects-') as directory:
root = Path(directory)
for path in sorted(CORPUS.glob('*.aji')):
source = path.read_text()
formatted = root / path.name
formatted.write_text(cli('fmt', path))
cli('fmt', '--check', formatted)
assert cli('fmt', formatted) == formatted.read_text()
if '// expect: reject' in source:
error = json.loads(cli('check', path, code=1))
assert error['code'] == re.search(r'// expect: reject (E\d+)', source)[1], error
if path.stem in expected:
atom, col = expected[path.stem]
assert (error['name'], error['effect']) == (atom, atom), error
line = next(i for i, text in enumerate(source.splitlines(), 1)
if 'effects [' in text and atom in text)
assert (error['file'], error['line'], error['col']) == (str(path), line, col), error
assert 'reason' not in error and 'air_code' not in error and 'air_line' not in error, error
elif path.stem in ('type_priority', 'return_priority', 'static_type_priority', 'from_priority'):
assert error['name'] == ('missing' if path.stem == 'from_priority' else 'Missing'), error
assert 'effect' not in error, error
elif path.stem in ('static_duplicate', 'template_trusted'):
assert error['reason'] == ('invalid_static_function' if path.stem == 'static_duplicate' else 'trusted_unavailable'), error
commands = ('lower', 'run', 'report', 'explore') if error['code'].startswith('E7') else ('run', 'report', 'explore')
for command in commands:
assert json.loads(cli(command, path, code=1)) == error
if not error['code'].startswith('E7'):
raw = root / (path.stem + '.air')
raw.write_text(cli('lower', path))
assert json.loads(cli('check', raw, code=1))['code'] == error['code']
formatted_error = json.loads(cli('check', formatted, code=1))
assert formatted_error['code'] == error['code']
if error['code'] == 'E7002' and 'effect' in error:
assert formatted_error['name'] == formatted_error['effect'] == error['effect']
continue
cli('check', path)
lowered = cli('lower', path)
assert cli('lower', formatted) == lowered
air = root / (path.stem + '.air')
air.write_text(lowered)
cli('check', air)
grants = [value for grant in re.findall(r'// grant: (.+)', source)
for value in ('--grant', grant)]
result = json.loads(cli('run', '--fuel', '100000', *grants, path))
assert result['value'] == 42, result
assert result == json.loads(cli('run', '--fuel', '100000', *grants, air))
if path.stem == 'allocation':
assert json.loads(cli('run', '--fuel', '100000', path, code=6))['status'] == 'not_instantiated'
# Imported unused ordinary/template/static signatures retain module provenance.
entry, library, manifest = (root / name for name in ('main.aji', 'lib.aji', 'aji.toml'))
manifest.write_text('[package]\nname = "effects"\nversion = "1"\nentry = "main.aji"\n'
'[modules]\nlib.test = "lib.aji"\n')
entry.write_text('module app\nimport lib.test;\nfn main() -> u64 effects [] { return 42; }\n')
for declaration, col in (
('fn unused() -> void effects [bogus.effect] {}', 1),
('fn unused<T>(x: T) -> T effects [bogus.effect] { return x; }', 1),
('fn unused<F: fn(u64) -> u64 effects [bogus.effect]>() -> void effects [] {}', 11),
):
library.write_text('module lib.test\n' + declaration + '\n')
error = json.loads(cli('check', '--manifest', manifest, code=1))
assert (error['code'], error['name'], error['effect']) == ('E7002', 'bogus.effect', 'bogus.effect'), error
assert (error['file'], error['line'], error['col']) == ('lib.aji', 2, col), error
for command in ('lower', 'run', 'report', 'explore'):
assert json.loads(cli(command, '--manifest', manifest, code=1)) == error
# Existing template validation still precedes ordinary signature lowering.
entry.write_text('module app\nfn first() -> void effects [bogus.effect] {}\n'
'fn unused<lowercase>() -> void effects [] {}\n'
'fn main() -> u64 effects [] { return 42; }\n')
error = json.loads(cli('check', entry, code=1))
assert error['code'] == 'E7006' and error['reason'] == 'invalid_generic', error
# Text AIR independently keeps E0004; it does not become a source name error.
raw = root / 'unknown.air'
raw.write_text('module test\nair 0\nlang 0.1\nfeatures [effects]\n'
'fn @main() -> u64\neffects [bogus.effect]\nentry:\n'
' %x = const.u64 42\n ret %x\n')
error = json.loads(cli('check', raw, code=1))
assert error['code'] == 'E0004' and error['name'] == 'bogus.effect', error
assert 'effect' not in error and 'air_code' not in error, error
print(f'source effect names: {checks} checks passed')