cli: validate structural source type positions before AIR
This commit is contained in:
1 parent
5f76e92ff3
commit
caf3b86e80
48 files changed
+423
-33
No files matched your search
@@ -1,5 +1,5 @@
|
||||
// Allocation and destruction each reject the borrow element at their own source construct.
|
||||
// expect: reject E1001 E1001
|
||||
// The content annotation rejects before allocation or destruction reaches AIR.
|
||||
// expect: reject E7005
|
||||
// grant: heap=mem.alloc:128
|
||||
// phase: 6
|
||||
// spec: docs/spec/03-memory-model.md §26; docs/spec/15-source-language.md §17
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
// expect: reject E1001
|
||||
// expect: reject E7005
|
||||
// phase: 6
|
||||
// spec: docs/spec/15-source-language.md §15
|
||||
module demo.generic
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
// expect: reject E0006
|
||||
// expect: reject E7005
|
||||
// phase: 6
|
||||
// spec: docs/spec/03-memory-model.md §25; docs/spec/15-source-language.md §16
|
||||
module test.linear
|
||||
|
||||
@@ -0,0 +1,6 @@
|
||||
// expect: reject E7005
|
||||
// phase: 6
|
||||
// spec: docs/spec/15-source-language.md §10; docs/spec/07-air.md §8.8
|
||||
module test.type_positions
|
||||
struct Bad { p: [ref<u64>; 2] }
|
||||
fn main() -> void effects [] {}
|
||||
@@ -0,0 +1,6 @@
|
||||
// expect: reject E7005
|
||||
// phase: 6
|
||||
// spec: docs/spec/15-source-language.md §10; docs/spec/07-air.md §8.8
|
||||
module test.type_positions
|
||||
fn unused(p: [ref<u64>; 2]) -> void effects [] {}
|
||||
fn main() -> void effects [] {}
|
||||
@@ -0,0 +1,5 @@
|
||||
// expect: reject E7005
|
||||
// phase: 6
|
||||
// spec: docs/spec/15-source-language.md §10; docs/spec/03-memory-model.md §26
|
||||
module test.type_positions
|
||||
fn main(heap: cap<mem.alloc>) -> void effects [mem.alloc] { let data = buffer.new<ref<u64>>(0, heap) or trap; trap; }
|
||||
@@ -0,0 +1,6 @@
|
||||
// expect: reject E7005
|
||||
// phase: 6
|
||||
// spec: docs/spec/15-source-language.md §10; docs/spec/07-air.md §8.8
|
||||
module test.type_positions
|
||||
fn unused(p: buffer<ref<u64>>) -> void effects [] {}
|
||||
fn main() -> void effects [] {}
|
||||
@@ -0,0 +1,6 @@
|
||||
// expect: reject E7005
|
||||
// phase: 6
|
||||
// spec: docs/spec/15-source-language.md §10; docs/spec/07-air.md §8.8
|
||||
module test.type_positions
|
||||
fn bad() -> void effects [] { let q: ref<ref<u8>> = b"hi"; }
|
||||
fn main() -> void effects [] {}
|
||||
@@ -0,0 +1,6 @@
|
||||
// expect: reject E7005
|
||||
// phase: 6
|
||||
// spec: docs/spec/15-source-language.md §10; docs/spec/07-air.md §8.8
|
||||
module test.type_positions
|
||||
struct Bad { p: ref<u64> }
|
||||
fn main() -> void effects [] {}
|
||||
@@ -0,0 +1,6 @@
|
||||
// expect: reject E2111
|
||||
// phase: 6
|
||||
// spec: docs/spec/15-source-language.md §10; docs/spec/07-air.md §8.8
|
||||
module test.type_positions
|
||||
fn unused(p: island<ref<u64>>) -> void effects [] {}
|
||||
fn main() -> void effects [] {}
|
||||
@@ -0,0 +1,6 @@
|
||||
// expect: reject E7005
|
||||
// phase: 6
|
||||
// spec: docs/spec/15-source-language.md §10; docs/spec/07-air.md §8.8
|
||||
module test.type_positions
|
||||
fn unused(p: iref<ref<u64>>) -> void effects [] { trap; }
|
||||
fn main() -> void effects [] {}
|
||||
@@ -0,0 +1,6 @@
|
||||
// expect: reject E7005
|
||||
// phase: 6
|
||||
// spec: docs/spec/15-source-language.md §10; docs/spec/07-air.md §8.8
|
||||
module test.type_positions
|
||||
fn bad(p: ref<u64>) -> void effects [] { let q: ref<ref<u64>> = p; }
|
||||
fn main() -> void effects [] {}
|
||||
@@ -0,0 +1,6 @@
|
||||
// expect: reject E7005
|
||||
// phase: 6
|
||||
// spec: docs/spec/15-source-language.md §10; docs/spec/07-air.md §8.8
|
||||
module test.type_positions
|
||||
fn bad(p: ref<u64>) -> void effects [] { var q: ref<ref<u64>> = zero; }
|
||||
fn main() -> void effects [] {}
|
||||
@@ -0,0 +1,6 @@
|
||||
// expect: reject E7005
|
||||
// phase: 6
|
||||
// spec: docs/spec/15-source-language.md §10; docs/spec/07-air.md §8.8
|
||||
module test.type_positions
|
||||
fn unused(p: mutex<ref<u64>>) -> void effects [] {}
|
||||
fn main() -> void effects [] {}
|
||||
@@ -0,0 +1,6 @@
|
||||
// expect: reject E7005
|
||||
// phase: 6
|
||||
// spec: docs/spec/15-source-language.md §10; docs/spec/07-air.md §8.8
|
||||
module test.type_positions
|
||||
fn unused(p: mut<slice<u64>>) -> void effects [] {}
|
||||
fn main() -> void effects [] {}
|
||||
@@ -0,0 +1,6 @@
|
||||
// expect: reject E7005
|
||||
// phase: 6
|
||||
// spec: docs/spec/15-source-language.md §10; docs/spec/07-air.md §8.8
|
||||
module test.type_positions
|
||||
fn unused(p: ref<ref<u64>>) -> void effects [] {}
|
||||
fn main() -> void effects [] {}
|
||||
@@ -0,0 +1,6 @@
|
||||
// expect: reject E7005
|
||||
// phase: 6
|
||||
// spec: docs/spec/15-source-language.md §10; docs/spec/07-air.md §8.8
|
||||
module test.type_positions
|
||||
fn unused(p: never) -> void effects [] {}
|
||||
fn main() -> void effects [] {}
|
||||
@@ -0,0 +1,6 @@
|
||||
// expect: reject E7005
|
||||
// phase: 6
|
||||
// spec: docs/spec/15-source-language.md §10; docs/spec/07-air.md §8.8
|
||||
module test.type_positions
|
||||
fn unused(p: own<ref<u64>>) -> void effects [] {}
|
||||
fn main() -> void effects [] {}
|
||||
@@ -0,0 +1,6 @@
|
||||
// expect: reject E7005
|
||||
// phase: 6
|
||||
// spec: docs/spec/15-source-language.md §10; docs/spec/07-air.md §8.8
|
||||
module test.type_positions
|
||||
enum Bad { item(slice<u8>) }
|
||||
fn main() -> void effects [] {}
|
||||
@@ -0,0 +1,6 @@
|
||||
// expect: reject E7005
|
||||
// phase: 6
|
||||
// spec: docs/spec/15-source-language.md §10; docs/spec/07-air.md §8.8
|
||||
module test.type_positions
|
||||
fn unused(p: ref<void>) -> void effects [] {}
|
||||
fn main() -> void effects [] {}
|
||||
@@ -0,0 +1,6 @@
|
||||
// expect: reject E7005
|
||||
// phase: 6
|
||||
// spec: docs/spec/15-source-language.md §10; docs/spec/07-air.md §8.8
|
||||
module test.type_positions
|
||||
fn unused(p: sender<ref<u64>>) -> void effects [] {}
|
||||
fn main() -> void effects [] {}
|
||||
@@ -0,0 +1,6 @@
|
||||
// expect: reject E7005
|
||||
// phase: 6
|
||||
// spec: docs/spec/15-source-language.md §10; docs/spec/07-air.md §8.8
|
||||
module test.type_positions
|
||||
fn unused(p: slice<never>) -> void effects [] {}
|
||||
fn main() -> void effects [] {}
|
||||
@@ -0,0 +1,6 @@
|
||||
// expect: reject E7005
|
||||
// phase: 6
|
||||
// spec: docs/spec/15-source-language.md §10; docs/spec/07-air.md §8.8
|
||||
module test.type_positions
|
||||
fn unused(p: slice<ref<u64>>) -> void effects [] {}
|
||||
fn main() -> void effects [] {}
|
||||
@@ -0,0 +1,7 @@
|
||||
// expect: reject E7005
|
||||
// phase: 6
|
||||
// spec: docs/spec/15-source-language.md §10; docs/spec/07-air.md §8.8
|
||||
module test.type_positions
|
||||
struct Box<T> { item: T }
|
||||
fn bad(p: Box<ref<u64>>) -> void effects [] {}
|
||||
fn main() -> void effects [] {}
|
||||
@@ -0,0 +1,6 @@
|
||||
// expect: reject E7005
|
||||
// phase: 6
|
||||
// spec: docs/spec/15-source-language.md §10; docs/spec/07-air.md §8.8
|
||||
module test.type_positions
|
||||
fn bad<T>() -> own<T> effects [] { trap; }
|
||||
fn main() -> void effects [] { let x = bad::<ref<u64>>(); trap; }
|
||||
@@ -0,0 +1,6 @@
|
||||
// expect: reject E7005
|
||||
// phase: 6
|
||||
// spec: docs/spec/15-source-language.md §10; docs/spec/07-air.md §8.8
|
||||
module test.type_positions
|
||||
fn unused(p: [task<u64>; 2]) -> void effects [] {}
|
||||
fn main() -> void effects [] {}
|
||||
@@ -0,0 +1,6 @@
|
||||
// expect: reject E7005
|
||||
// phase: 6
|
||||
// spec: docs/spec/15-source-language.md §10; docs/spec/07-air.md §8.8
|
||||
module test.type_positions
|
||||
fn unused(p: task<ref<u64>>) -> void effects [] {}
|
||||
fn main() -> void effects [] {}
|
||||
@@ -0,0 +1,7 @@
|
||||
// expect: reject E7005
|
||||
// phase: 6
|
||||
// spec: docs/spec/15-source-language.md §10; docs/spec/07-air.md §8.8
|
||||
module test.type_positions
|
||||
struct Marker<T> {}
|
||||
fn unused<T>(p: Marker<ref<ref<T>>>) -> void effects [] {}
|
||||
fn main() -> void effects [] {}
|
||||
@@ -0,0 +1,6 @@
|
||||
// expect: reject E7005
|
||||
// phase: 6
|
||||
// spec: docs/spec/15-source-language.md §10; docs/spec/07-air.md §8.8
|
||||
module test.type_positions
|
||||
fn unused<T>(p: ref<mut<T>>) -> void effects [] {}
|
||||
fn main() -> void effects [] {}
|
||||
@@ -0,0 +1,6 @@
|
||||
// expect: reject E7005
|
||||
// phase: 6
|
||||
// spec: docs/spec/15-source-language.md §10; docs/spec/07-air.md §8.8
|
||||
module test.type_positions
|
||||
struct Bad<T> { p: ref<T> }
|
||||
fn main() -> void effects [] {}
|
||||
@@ -0,0 +1,6 @@
|
||||
// expect: reject E7005
|
||||
// phase: 6
|
||||
// spec: docs/spec/15-source-language.md §10; docs/spec/07-air.md §8.8
|
||||
module test.type_positions
|
||||
enum Bad<T> { item([mut<T>; 2]) }
|
||||
fn main() -> void effects [] {}
|
||||
@@ -0,0 +1,6 @@
|
||||
// expect: reject E7005
|
||||
// phase: 6
|
||||
// spec: docs/spec/15-source-language.md §10; docs/spec/07-air.md §8.8
|
||||
module test.type_positions
|
||||
fn unused<T>(p: mut<T>) -> ref<ref<T>> from p effects [] {}
|
||||
fn main() -> void effects [] {}
|
||||
@@ -0,0 +1,6 @@
|
||||
// expect: reject E7005
|
||||
// phase: 6
|
||||
// spec: docs/spec/15-source-language.md §10; docs/spec/07-air.md §8.8
|
||||
module test.type_positions
|
||||
fn unused<T, F: fn(own<ref<T>>) -> void effects []>() -> void effects [] {}
|
||||
fn main() -> void effects [] {}
|
||||
@@ -0,0 +1,6 @@
|
||||
// expect: reject E7005
|
||||
// phase: 6
|
||||
// spec: docs/spec/15-source-language.md §10; docs/spec/07-air.md §8.8
|
||||
module test.type_positions
|
||||
fn unused<F: fn(void) -> void effects []>() -> void effects [] {}
|
||||
fn main() -> void effects [] {}
|
||||
@@ -0,0 +1,11 @@
|
||||
// expect: run
|
||||
// result: 42
|
||||
// phase: 6
|
||||
// spec: docs/spec/15-source-language.md §10
|
||||
module test.type_positions
|
||||
struct Pair { values: [u64; 2] }
|
||||
fn first(p: ref<[u64; 2]>) -> ref<u64> from p effects [] { ref item = p[0]; return item; }
|
||||
fn read(p: ref<Pair>) -> u64 effects [] { ref values = p.values; return values[0]; }
|
||||
fn forward<T: Copy>(p: ref<T>) -> T effects [] { return *p; }
|
||||
fn unused<T>(p: own<T>) -> void effects [] { trap; }
|
||||
fn main() -> u64 effects [] { let p = Pair { values: [21u64; 2] }; var x: u64 = 21; return read(ref p) + forward::<u64>(ref x); }
|
||||
@@ -0,0 +1,7 @@
|
||||
// expect: accept
|
||||
// phase: 6
|
||||
// spec: docs/spec/15-source-language.md §15/§18
|
||||
module test.type_positions
|
||||
struct Box<T> { item: T }
|
||||
fn unused<T, F: fn(ref<T>, mut<T>) -> void effects []>(p: ref<T>, b: Box<T>) -> T effects [] { trap; }
|
||||
fn main() -> void effects [] {}
|
||||
@@ -0,0 +1,6 @@
|
||||
// expect: reject E7005
|
||||
// phase: 6
|
||||
// spec: docs/spec/15-source-language.md §10; docs/spec/07-air.md §8.8
|
||||
module test.type_positions
|
||||
fn unused(p: void) -> void effects [] {}
|
||||
fn main() -> void effects [] {}
|
||||
@@ -0,0 +1,89 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Source type-position errors: exact annotations, CLI/package parity and deferred types."""
|
||||
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/type_positions'
|
||||
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 rejection(path, position, constructor, token, *args, expected_file=None, line=5):
|
||||
error = json.loads(cli('check', *args, code=1))
|
||||
assert error['code'] == 'E7005', error
|
||||
assert error['position'] == position and error['type_constructor'] == constructor, error
|
||||
text = path.read_text().splitlines()[line - 1]
|
||||
assert (error['file'], error['line'], error['col']) == (
|
||||
expected_file or str(path), line, text.index(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 name, position, constructor, token in (
|
||||
('nested_ref', 'borrowed', 'ref', 'ref<u64>'),
|
||||
('nested_mut', 'borrowed', 'slice', 'slice<u64>'),
|
||||
('array_ref', 'member', 'ref', 'ref<u64>'),
|
||||
('field', 'member', 'ref', 'ref<u64>'),
|
||||
('payload', 'member', 'slice', 'slice<u8>'),
|
||||
('void_param', 'param', 'void', 'void)'),
|
||||
('unused_generic', 'borrowed', 'mut', 'mut<T>'),
|
||||
('unused_generic_field', 'member', 'ref', 'ref<T>'),
|
||||
('unused_static_void', 'param', 'void', 'void)'),
|
||||
('byte_annotation', 'borrowed', 'ref', 'ref<u8>'),
|
||||
('iref_ref', 'member', 'ref', 'ref<u64>'),
|
||||
('buffer_new_ref', 'member', 'ref', 'ref<u64>'),
|
||||
):
|
||||
path = CORPUS / f'{name}.aji'
|
||||
rejection(path, position, constructor, token, path)
|
||||
|
||||
# This header is legal while T is unknown, then fails only at substitution.
|
||||
specialized = json.loads(cli('check', CORPUS / 'specialized_inner.aji', code=1))
|
||||
assert (specialized['code'], specialized['position'], specialized['type_constructor']) == (
|
||||
'E7005', 'member', 'ref'), specialized
|
||||
assert specialized['line'] == 6 and specialized['col'] == 46, specialized
|
||||
assert 'air_line' not in specialized, specialized
|
||||
|
||||
with tempfile.TemporaryDirectory(prefix='aji-type-positions-') as directory:
|
||||
root = Path(directory)
|
||||
raw = root / 'lowered.air'
|
||||
source = CORPUS / 'valid.aji'
|
||||
air = cli('lower', source)
|
||||
assert cli('lower', source) == air
|
||||
raw.write_text(air)
|
||||
cli('check', raw)
|
||||
expected = json.loads(cli('run', '--fuel', '100000', source))
|
||||
assert expected['value'] == 42
|
||||
assert json.loads(cli('run', '--fuel', '100000', raw)) == expected
|
||||
result = root / 'hosted.json'
|
||||
cli('run', '--profile', 'hosted', '--fuel', '100000', '--result-file', result, source)
|
||||
assert json.loads(result.read_text())['value'] == 42
|
||||
|
||||
entry, library, manifest = (root / name for name in ('main.aji', 'types.aji', 'aji.toml'))
|
||||
entry.write_text('module app\nimport lib.types;\nfn main() -> u64 effects [] { return 42; }\n')
|
||||
library.write_text('module lib.types\nfn unused<T>(p: ref<ref<T>>) -> void effects [] {}\n')
|
||||
manifest.write_text('[package]\nname = "positions"\nversion = "1"\nentry = "main.aji"\n'
|
||||
'[modules]\nlib.types = "types.aji"\n')
|
||||
rejection(library, 'borrowed', 'ref', 'ref<T>', '--manifest', manifest,
|
||||
expected_file='types.aji', line=2)
|
||||
|
||||
# A type parameter whose class is unknown stays legal in an unused header.
|
||||
library.write_text('module lib.types\nfn unused<T>(p: ref<T>) -> void effects [] {}\n')
|
||||
assert json.loads(cli('run', '--manifest', manifest))['value'] == 42
|
||||
# Invalid mapped modules that are unreachable remain outside compilation.
|
||||
entry.write_text(entry.read_text().replace('import lib.types;\n', ''))
|
||||
library.write_text('module lib.types\nfn unused<T>(p: ref<ref<T>>) -> void effects [] {}\n')
|
||||
assert json.loads(cli('run', '--manifest', manifest))['value'] == 42
|
||||
|
||||
print(f'type position CLI integration passed ({checks} checks)')
|
||||
Reference in new issue
Block a user