cli: lower enum payloads iteratively

This commit is contained in:
dev committed 2026-10-11 13:23:36 +03:00
1 parent 8c34a4d64d
commit 627ad8f857
5 files changed
+397 -17

No files matched your search

+107
View File
@@ -529,6 +529,113 @@ with tempfile.TemporaryDirectory(prefix='aji-expression-depth-') as directory:
assert cli(command, path, code=1) == expected
print(f'array invalid depth: {depth}', flush=True)
# Enum payloads share scalar/call/aggregate continuations; validate arity
# before arguments and type/consume/check each payload in source order.
for depth in (0, 1, 16, 256, 1024, 16384, 32768, 65536):
for kind in ('variant-call', 'two-payload', 'generic-variant', 'mixed-variant'):
if depth == 32768 and kind != 'mixed-variant':
continue
generic, two, mixed = kind == 'generic-variant', kind == 'two-payload', kind == 'mixed-variant'
constructor = 'Wrap::<i64>::some' if generic else 'Wrap::some'
enum = 'enum Wrap<T> { some(T) }\n' if generic else ('enum Wrap { some(i64, i64) }\n' if two else 'enum Wrap { some(i64) }\n')
annotation = 'Wrap<i64>' if generic else 'Wrap'
binders = 'x, y' if two else 'x'
helper = enum + f'fn unwrap(w: {annotation}) -> i64 effects [] {{ return match w {{ {constructor}({binders}) => x }}; }}\n'
prefix, suffix = f'unwrap({constructor}(', ', 2))' if two else '))'
if mixed:
helper += 'struct Box { n: i64 }\nfn id(x: i64) -> i64 effects [] { return x; }\n'
prefix, suffix = 'unwrap(Wrap::some(Box { n: - id(', ') }.n))'
path = root / f'enum-{kind}-{depth}.aji'
path.write_text('module depth\n' + helper + 'fn main() -> i64 effects [] { return ' +
prefix * depth + '1i64' + suffix * depth + '; }\n')
main_instructions = (7 if mixed else 3 if two else 2) * depth + 2
limited = main_instructions > 262144
checked = cli('check', path, code=1 if limited else 0)
if limited:
diagnostic = json.loads(checked)
assert (diagnostic['code'], diagnostic['limit'], diagnostic['max'], diagnostic['found']) == (
'E5101', 'instructions per function', 262144, main_instructions), diagnostic
for command in ('run', 'report', 'explore'):
assert cli(command, path, code=1) == checked
else:
assert checked == b''
lowered = cli('lower', path)
instructions = sum((line.lstrip().startswith(b'%') and b' = ' in line) or
line.strip().startswith((b'ret ', b'br ', b'enum.switch ')) for line in lowered.splitlines())
assert instructions == main_instructions + 3 + int(mixed), (kind, depth, instructions)
if depth <= 1024:
fuel = main_instructions + (4 if mixed else 3) * depth
value = -1 if mixed and depth % 2 else 1
assert json.loads(cli('run', path, '--fuel', fuel)) == {'status': 'returned', 'value': value}
assert json.loads(cli('run', path, '--fuel', fuel - 1, code=5)) == {
'status': 'limit_exceeded', 'kind': 'fuel', 'limit': fuel - 1, 'steps': fuel - 1}
air = path.with_suffix('.air')
air.write_bytes(lowered)
assert cli('run', air, '--fuel', fuel) == cli('run', path, '--fuel', fuel)
output = cli('fmt', path)
formatted = path.with_suffix('.formatted.aji')
formatted.write_bytes(output)
assert cli('fmt', formatted) == output
assert json.loads(cli('fmt', formatted, '--check')) == {
'file': str(formatted), 'status': 'formatted', 'changed': False}
formatted_check = cli('check', formatted, code=1 if limited else 0)
if limited:
diagnostic = json.loads(formatted_check)
assert (diagnostic['code'], diagnostic['limit'], diagnostic['max'], diagnostic['found']) == (
'E5101', 'instructions per function', 262144, main_instructions), diagnostic
else:
assert formatted_check == b''
assert cli('lower', formatted) == lowered
print(f'variant expression depth: {kind} {depth}', flush=True)
for depth in (1024, 16384, 65536):
for leaf, code in (('missing()', 'E7002'), ('Wrap::some(missing(), true)', 'E7005'),
('Wrap::absent(missing())', 'E7002'), ('Wrap::some(true)', 'E7005')):
path = root / f'variant-invalid-{depth}-{code}-{len(leaf)}.aji'
header = 'fn main() -> i64 effects [] { return '
prefix = 'unwrap(Wrap::some(' * depth
path.write_text('module depth\nenum Wrap { some(i64) }\nfn unwrap(w: Wrap) -> i64 effects [] { return match w { Wrap::some(x) => x }; }\n' +
header + prefix + leaf + '))' * depth + '; }\n')
expected = cli('check', path, code=1)
error = json.loads(expected)
assert (error['code'], error['line'], error['col'], error['file']) == (
code, 4, len(header) + len(prefix) + (len('Wrap::some(') if leaf == 'Wrap::some(true)' else 0) + 1, str(path)), error
if code == 'E7005':
assert (error['expected'], error['found']) == (('i64', 'bool') if leaf == 'Wrap::some(true)' else ('1', '2')), error
for command in ('lower', 'run', 'report', 'explore'):
assert cli(command, path, code=1) == expected
print(f'variant invalid depth: {depth}', flush=True)
# Empty named constructors also finish immediately at the deepest leaf.
for depth in (0, 1, 16, 1024, 65536):
path = root / f'empty-variant-{depth}.aji'
path.write_text('module depth\nenum Wrap { some(i64), empty }\n' +
'fn unwrap(w: Wrap) -> i64 effects [] { return match w { Wrap::some(x) => x, Wrap::empty => 1i64 }; }\n' +
'fn main() -> i64 effects [] { return ' + 'unwrap(Wrap::some(' * depth +
'unwrap(Wrap::empty)' + '))' * depth + '; }\n')
assert cli('check', path) == b''
lowered = cli('lower', path)
instructions = sum((line.lstrip().startswith(b'%') and b' = ' in line) or
line.strip().startswith((b'ret ', b'br ', b'enum.switch ')) for line in lowered.splitlines())
assert instructions == 2 * depth + 8, (depth, instructions)
if depth <= 1024:
fuel = 5 * depth + 7
assert json.loads(cli('run', path, '--fuel', fuel)) == {'status': 'returned', 'value': 1}
assert json.loads(cli('run', path, '--fuel', fuel - 1, code=5)) == {
'status': 'limit_exceeded', 'kind': 'fuel', 'limit': fuel - 1, 'steps': fuel - 1}
air = path.with_suffix('.air')
air.write_bytes(lowered)
assert cli('run', air, '--fuel', fuel) == cli('run', path, '--fuel', fuel)
formatted = path.with_suffix('.formatted.aji')
output = cli('fmt', path)
formatted.write_bytes(output)
assert cli('fmt', formatted) == output
assert json.loads(cli('fmt', formatted, '--check')) == {
'file': str(formatted), 'status': 'formatted', 'changed': False}
assert cli('check', formatted) == b''
assert cli('lower', formatted) == lowered
print(f'empty variant depth: {depth}', flush=True)
# Builtin callbacks are still native; preserve the prior diagnostic at
# bounded nesting while ordinary calls gain explicit continuations.
for depth in (16, 64, 256):