frontend: align source grammar and reject empty enum payload lists
This commit is contained in:
1 parent
48b9b42fed
commit
4c9c5626c9
44 files changed
+501
-33
No files matched your search
@@ -0,0 +1,7 @@
|
||||
// expect: run
|
||||
// result: 42
|
||||
// phase: 6
|
||||
// spec: docs/spec/15-source-language.md §2, §5, §6
|
||||
module grammar.array_places
|
||||
fn change(p: mut<[u64; 2]>) -> u64 effects [] { (*p)[0] = 42u64; return (*p)[0]; }
|
||||
fn main() -> u64 effects [] { var a: [u64; 2] = zero; return change(mut (a)); }
|
||||
@@ -0,0 +1,7 @@
|
||||
// expect: run
|
||||
// result: 42
|
||||
// phase: 6
|
||||
// spec: docs/spec/15-source-language.md §2, §5, §6
|
||||
module grammar.borrow_calls
|
||||
fn first<T: Copy>(s: slice<T>,) -> ref<T> from s effects [] { ref x = s[0]; return x; }
|
||||
fn main() -> u64 effects [] { let a = [42u64]; ref s = (a); ref x = first::<u64>(s,); return *x; }
|
||||
@@ -0,0 +1,5 @@
|
||||
// expect: reject E7001
|
||||
// phase: 6
|
||||
// spec: docs/reference/grammar.ebnf
|
||||
module grammar.cast_spacing
|
||||
fn main() -> u64 effects [] { return 42u64 as ! u8; }
|
||||
@@ -0,0 +1,6 @@
|
||||
// expect: run
|
||||
// result: 42
|
||||
// phase: 6
|
||||
// spec: docs/spec/15-source-language.md §5
|
||||
module grammar.casts
|
||||
fn main() -> u64 effects [] { let n = 42u64 as! u8; return (n as% u16) as u64; }
|
||||
@@ -0,0 +1,9 @@
|
||||
// expect: run
|
||||
// result: 42
|
||||
// phase: 6
|
||||
// spec: docs/spec/15-source-language.md §2, §5, §6
|
||||
module grammar.context_words
|
||||
struct Words { recv: u64, send: u64, }
|
||||
fn recv() -> u64 effects [] { return 40u64; }
|
||||
fn send() -> u64 effects [] { return 2u64; }
|
||||
fn main() -> u64 effects [] { let import = Words { recv: recv(), send: send(), }; return import.recv + import.send; }
|
||||
@@ -0,0 +1,6 @@
|
||||
// expect: reject E7001
|
||||
// phase: 6
|
||||
// spec: docs/spec/15-source-language.md §2, §5, §6
|
||||
module grammar.empty_builtin_call
|
||||
|
||||
fn main() -> u64 effects [] { let v = alloc_error::budget_exceeded(); return 0u64; }
|
||||
@@ -0,0 +1,6 @@
|
||||
// expect: reject E7001
|
||||
// phase: 6
|
||||
// spec: docs/spec/15-source-language.md §2, §5, §6
|
||||
module grammar.empty_builtin_pattern
|
||||
|
||||
fn main() -> u64 effects [] { let v = alloc_error::budget_exceeded; return match v { alloc_error::budget_exceeded() => 42u64 }; }
|
||||
@@ -0,0 +1,7 @@
|
||||
// expect: reject E7001
|
||||
// phase: 6
|
||||
// spec: docs/spec/15-source-language.md §2, §5, §6
|
||||
module grammar.empty_comment_call
|
||||
enum Value { Empty, }
|
||||
fn main() -> u64 effects [] { let v = Value::Empty( // no payload
|
||||
); return 0u64; }
|
||||
@@ -0,0 +1,6 @@
|
||||
// expect: reject E7001
|
||||
// phase: 6
|
||||
// spec: docs/spec/15-source-language.md §2, §5, §6
|
||||
module grammar.empty_enum_call
|
||||
enum Value { Empty, }
|
||||
fn main() -> u64 effects [] { let v = Value::Empty(); return 0u64; }
|
||||
@@ -0,0 +1,6 @@
|
||||
// expect: reject E7001
|
||||
// phase: 6
|
||||
// spec: docs/spec/15-source-language.md §2, §5, §6
|
||||
module grammar.empty_enum_pattern
|
||||
enum Value { Empty, }
|
||||
fn main() -> u64 effects [] { let v = Value::Empty; return match v { Value::Empty() => 42u64 }; }
|
||||
@@ -0,0 +1,6 @@
|
||||
// expect: reject E7001
|
||||
// phase: 6
|
||||
// spec: docs/spec/15-source-language.md §2, §5, §6
|
||||
module grammar.empty_generic_call
|
||||
enum Value<T> { Empty, Pair(T), }
|
||||
fn main() -> u64 effects [] { let v = Value::<u64>::Empty(); return 0u64; }
|
||||
@@ -0,0 +1,6 @@
|
||||
// expect: reject E7001
|
||||
// phase: 6
|
||||
// spec: docs/spec/15-source-language.md §2, §5, §6
|
||||
module grammar.empty_generic_pattern
|
||||
enum Value<T> { Empty, Pair(T), }
|
||||
fn main() -> u64 effects [] { let v = Value::<u64>::Empty; return match v { Value::<u64>::Empty() => 42u64 }; }
|
||||
@@ -0,0 +1,6 @@
|
||||
// expect: reject E7001
|
||||
// phase: 6
|
||||
// spec: docs/spec/15-source-language.md §2, §5, §6
|
||||
module grammar.empty_grouped_call
|
||||
enum Value { Empty, }
|
||||
fn main() -> u64 effects [] { let v = (Value::Empty)(); return 0u64; }
|
||||
@@ -0,0 +1,6 @@
|
||||
// expect: reject E7001
|
||||
// phase: 6
|
||||
// spec: docs/spec/15-source-language.md §2, §5, §6
|
||||
module grammar.empty_payload_call
|
||||
enum Value { Pair(u64), }
|
||||
fn main() -> u64 effects [] { let v = Value::Pair(); return 0u64; }
|
||||
@@ -0,0 +1,6 @@
|
||||
// expect: reject E7001
|
||||
// phase: 6
|
||||
// spec: docs/spec/15-source-language.md §2, §5, §6
|
||||
module grammar.empty_payload_pattern
|
||||
enum Value { Pair(u64), }
|
||||
fn main() -> u64 effects [] { let v = Value::Pair(42u64); return match v { Value::Pair() => 42u64 }; }
|
||||
@@ -0,0 +1,7 @@
|
||||
// expect: reject E7001
|
||||
// phase: 6
|
||||
// spec: docs/spec/15-source-language.md §2, §5, §6
|
||||
module grammar.empty_unused_template
|
||||
enum Value { Empty, }
|
||||
fn unused<T>(x: T) -> T effects [] { let v = Value::Empty(); return x; }
|
||||
fn main() -> u64 effects [] { return 42u64; }
|
||||
@@ -0,0 +1,7 @@
|
||||
// expect: run
|
||||
// result: 42
|
||||
// phase: 6
|
||||
// spec: docs/spec/15-source-language.md §2, §5, §6
|
||||
module grammar.enum_binds
|
||||
enum Value<T> { Empty, Pair(T, T), }
|
||||
fn main() -> u64 effects [] { let v = Value::<u64>::Pair(1u64,42u64,); return match v { Value::<u64>::Empty => {0u64}, Value::<u64>::Pair(_, n,) => n }; }
|
||||
@@ -0,0 +1,6 @@
|
||||
// expect: reject E7001
|
||||
// phase: 6
|
||||
// spec: docs/spec/15-source-language.md §2, §5, §6
|
||||
module grammar.enum_decl_empty
|
||||
enum Value { Empty(), }
|
||||
fn main() -> u64 effects [] { return 42u64; }
|
||||
@@ -0,0 +1,6 @@
|
||||
// expect: reject E7001
|
||||
// phase: 6
|
||||
// spec: docs/spec/15-source-language.md §2, §5, §6
|
||||
module grammar.enum_decl_trailing
|
||||
enum Value { Pair(u64,), }
|
||||
fn main() -> u64 effects [] { return 42u64; }
|
||||
@@ -0,0 +1,8 @@
|
||||
// expect: run
|
||||
// result: 42
|
||||
// phase: 6
|
||||
// spec: docs/spec/15-source-language.md §2, §5, §6
|
||||
module grammar.header_structs
|
||||
struct Pair { a: u64, }
|
||||
fn test(p: Pair) -> bool effects [] { return p.a == 42u64; }
|
||||
fn main() -> u64 effects [] { if test(Pair { a: 42u64 }) { return 42u64; } return 0u64; }
|
||||
@@ -0,0 +1,7 @@
|
||||
// expect: run
|
||||
// result: 42
|
||||
// phase: 6
|
||||
// spec: docs/spec/15-source-language.md §2, §5, §6
|
||||
module grammar.lists
|
||||
fn answer(value: u64,) -> u64 effects [] { return value; }
|
||||
fn main() -> u64 effects [] { return answer(0b10_1010u64,); }
|
||||
@@ -0,0 +1,5 @@
|
||||
// expect: reject E7001
|
||||
// phase: 6
|
||||
// spec: docs/spec/15-source-language.md §2, §5, §6
|
||||
module grammar.lone_underscore
|
||||
fn _() -> u64 effects [] { return 42u64; }
|
||||
@@ -0,0 +1,5 @@
|
||||
// expect: reject E7001
|
||||
// phase: 6
|
||||
// spec: docs/spec/15-source-language.md §2, §5, §6
|
||||
module grammar.missing_arm_comma
|
||||
fn main() -> u64 effects [] { return match 1u64 { 1u64 => 42u64 _ => 0u64 }; }
|
||||
@@ -0,0 +1,6 @@
|
||||
// expect: run
|
||||
// result: 42
|
||||
// phase: 6
|
||||
// spec: docs/spec/15-source-language.md §2, §5, §6
|
||||
module grammar.negative_pattern
|
||||
fn main() -> u64 effects [] { return match -1i64 { -1i64 => {42u64}, _ => 0u64 }; }
|
||||
@@ -0,0 +1,6 @@
|
||||
// expect: reject E7005
|
||||
// phase: 6
|
||||
// spec: docs/spec/15-source-language.md §5
|
||||
module grammar.nonempty_arity
|
||||
enum Value { Pair(u64), }
|
||||
fn main() -> u64 effects [] { let v = Value::Pair(1u64, 2u64); return 42u64; }
|
||||
@@ -0,0 +1,8 @@
|
||||
// expect: run
|
||||
// result: 42
|
||||
// phase: 6
|
||||
// spec: docs/spec/15-source-language.md §2, §5, §6
|
||||
module grammar.ordinary_empty_call
|
||||
enum Value { Empty, }
|
||||
fn value() -> Value effects [] { return Value::Empty; }
|
||||
fn main() -> u64 effects [] { return match value() { Value::Empty => 42u64 }; }
|
||||
@@ -0,0 +1,6 @@
|
||||
// expect: run
|
||||
// result: 42
|
||||
// phase: 6
|
||||
// spec: docs/spec/15-source-language.md §2, §5, §6
|
||||
module grammar.outcome_empty
|
||||
fn main() -> u64 effects [task.spawn,] { match task.check_cancel() { ok() => {return 42u64;}, cancelled() => {trap;} } }
|
||||
@@ -0,0 +1,8 @@
|
||||
// expect: run
|
||||
// result: 42
|
||||
// phase: 6
|
||||
// spec: docs/spec/15-source-language.md §2, §5, §6
|
||||
module grammar.places
|
||||
struct Pair { a: u64, }
|
||||
fn read(p: ref<Pair>) -> u64 effects [] { return (*p).a; }
|
||||
fn main() -> u64 effects [] { var p: Pair = zero; { mut q = (p); (*q).a = 42u64; } return read(ref (p)); }
|
||||
@@ -0,0 +1,5 @@
|
||||
// expect: reject E7001
|
||||
// phase: 6
|
||||
// spec: docs/spec/15-source-language.md §2, §5, §6
|
||||
module grammar.reserved_type_word
|
||||
fn type() -> u64 effects [] { return 42u64; }
|
||||
@@ -0,0 +1,22 @@
|
||||
// expect: run
|
||||
// result: 42
|
||||
// grant: heap=mem.alloc:256
|
||||
// schedules: 10
|
||||
// phase: 6
|
||||
// spec: docs/spec/15-source-language.md §2, §6, §7
|
||||
module grammar.select_lists
|
||||
fn main(heap: cap<mem.alloc>,) -> u64 effects [mem.alloc, task.wait,] {
|
||||
match channel.make<u64>(1usize, heap,) {
|
||||
ok(tx, rx,) => {
|
||||
channel.close_sender(move tx,);
|
||||
var answer: u64 = 0;
|
||||
select {
|
||||
recv(rx) { ok(n,) => {answer = n;}, closed() => {answer = 42u64;} }
|
||||
cancelled() => {trap;},
|
||||
}
|
||||
match channel.close_receiver(move rx,) { ok() => {}, pending(rx,) => {trap;} }
|
||||
return answer;
|
||||
},
|
||||
err(e,) => {trap;},
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
// expect: run
|
||||
// result: 42
|
||||
// phase: 6
|
||||
// spec: docs/spec/15-source-language.md §2, §5, §6
|
||||
module grammar.static_lists
|
||||
fn identity(x: u64,) -> u64 effects [] { return x; }
|
||||
fn apply<F: fn(u64,) -> u64 effects []>(x: u64,) -> u64 effects [] { return F(x,); }
|
||||
fn main() -> u64 effects [] { return apply::<fn identity>(42u64,); }
|
||||
@@ -0,0 +1,7 @@
|
||||
// expect: run
|
||||
// result: 42
|
||||
// phase: 6
|
||||
// spec: docs/spec/15-source-language.md §2, §16
|
||||
module grammar.tuple_empty
|
||||
struct Empty {}
|
||||
fn main() -> u64 effects [] { let value = Empty {}; let () = struct.unpack(value,); return 42u64; }
|
||||
@@ -0,0 +1,6 @@
|
||||
// expect: reject E7001
|
||||
// phase: 6
|
||||
// spec: docs/spec/15-source-language.md §2, §5, §6
|
||||
module grammar.type_args_trailing
|
||||
struct Pair<T> { a: T, }
|
||||
fn main() -> u64 effects [] { let x: Pair<u64,> = Pair::<u64> { a: 42u64 }; return x.a; }
|
||||
@@ -0,0 +1,6 @@
|
||||
// expect: reject E7001
|
||||
// phase: 6
|
||||
// spec: docs/spec/15-source-language.md §2, §5, §6
|
||||
module grammar.type_params_trailing
|
||||
struct Pair<T,> { a: T, }
|
||||
fn main() -> u64 effects [] { return 42u64; }
|
||||
@@ -0,0 +1,98 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Grammar boundaries, exact syntax positions and source/AIR/formatter fidelity."""
|
||||
import json
|
||||
import re
|
||||
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/grammar_surface'
|
||||
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 position(source, offset):
|
||||
return source[:offset].count('\n') + 1, offset - source.rfind('\n', 0, offset)
|
||||
|
||||
|
||||
with tempfile.TemporaryDirectory(prefix='aji-grammar-') as directory:
|
||||
root = Path(directory)
|
||||
for path in sorted(CORPUS.glob('*.aji')):
|
||||
source = path.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] and error['file'] == str(path), 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, path, code=1)) == error
|
||||
if error['code'] == 'E7001':
|
||||
assert json.loads(cli('fmt', path, code=1)) == error
|
||||
assert json.loads(cli('fmt', '--check', path, code=1)) == error
|
||||
else:
|
||||
cli('fmt', path)
|
||||
if path.name.startswith('empty_'):
|
||||
# Qualified payload syntax points at the empty list, even
|
||||
# across comments/newlines; never at a declaration/function.
|
||||
marker = '::budget_exceeded' if '::budget_exceeded' in source else '::Pair' if 'empty_payload' in path.name else '::Empty'
|
||||
if 'pattern' in path.name:
|
||||
start = source.rindex(marker)
|
||||
else:
|
||||
start = source.index(marker, source.index('fn '))
|
||||
offset = source.index('(', start)
|
||||
assert (error['line'], error['col']) == position(source, offset), error
|
||||
assert error['found'] == '(' and 'payload' in error['expected'], error
|
||||
continue
|
||||
cli('check', path)
|
||||
formatted = root / path.name
|
||||
formatted.write_text(cli('fmt', path))
|
||||
cli('fmt', '--check', formatted)
|
||||
assert cli('fmt', formatted) == formatted.read_text()
|
||||
lowered = cli('lower', path)
|
||||
assert cli('lower', formatted) == lowered
|
||||
air = root / (path.stem + '.air')
|
||||
air.write_text(lowered)
|
||||
cli('check', air)
|
||||
if '// expect: run' in source:
|
||||
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 == json.loads(cli('run', '--fuel', '100000', *grants, air))
|
||||
assert result == json.loads(cli('run', '--fuel', '100000', *grants, formatted))
|
||||
assert result['value'] == 42, result
|
||||
|
||||
# Imported qualifiers take the same syntax path and retain the actual file.
|
||||
helper = root / 'model.aji'
|
||||
helper.write_text('module app.model\nenum Value<T> { Empty, Payload(T), }\n')
|
||||
main = root / 'main.aji'
|
||||
main.write_text('module app.main\nimport app.model;\nfn main() -> u64 effects [] { let value = app.model.Value::<u64>::Empty(); return 42u64; }\n')
|
||||
manifest = root / 'aji.toml'
|
||||
manifest.write_text('[package]\nname = \"grammar\"\nversion = \"0.1.0\"\nentry = \"main.aji\"\n[modules]\napp.model = \"model.aji\"\n')
|
||||
error = json.loads(cli('check', '--manifest', manifest, code=1))
|
||||
assert error['code'] == 'E7001' and error['file'] == str(main), error
|
||||
offset = main.read_text().index('(', main.read_text().index('::Empty'))
|
||||
assert (error['line'], error['col']) == position(main.read_text(), offset), error
|
||||
main.write_text(main.read_text().replace('::Empty()', '::Empty'))
|
||||
cli('check', '--manifest', manifest)
|
||||
assert json.loads(cli('run', '--fuel', '100000', '--manifest', manifest))['value'] == 42
|
||||
main.write_text('module app.main\nimport app.model;\n'
|
||||
'fn main() -> u64 effects [] { let value = app.model.Value::<u64>::Empty; '
|
||||
'return match value { app.model.Value::<u64>::Empty() => 42u64, '
|
||||
'app.model.Value::<u64>::Payload(_) => 0u64 }; }\n')
|
||||
error = json.loads(cli('check', '--manifest', manifest, code=1))
|
||||
assert error['code'] == 'E7001' and error['file'] == str(main), error
|
||||
offset = main.read_text().index('(', main.read_text().rindex('::Empty'))
|
||||
assert (error['line'], error['col']) == position(main.read_text(), offset), error
|
||||
for command in ('lower', 'run', 'report', 'explore'):
|
||||
assert json.loads(cli(command, '--manifest', manifest, code=1)) == error
|
||||
main.write_text(main.read_text().replace('::Empty()', '::Empty'))
|
||||
assert json.loads(cli('run', '--fuel', '100000', '--manifest', manifest))['value'] == 42
|
||||
|
||||
print(f'source grammar: {checks} checks passed')
|
||||
Reference in new issue
Block a user