cli: transform generic source ASTs iteratively
This commit is contained in:
1 parent
50a7575efb
commit
d39a422b2b
5 files changed
+978
-89
No files matched your search
@@ -1,5 +1,5 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Operand depth, exact lowering/CFG/fuel and rejected-value positions (Q-061)."""
|
||||
"""Operand/generic depth, exact lowering/CFG/fuel and rejection positions (Q-061)."""
|
||||
import json
|
||||
from pathlib import Path
|
||||
import subprocess
|
||||
@@ -182,6 +182,68 @@ with tempfile.TemporaryDirectory(prefix='aji-expression-depth-') as directory:
|
||||
for command in ('lower', 'run', 'report', 'explore'):
|
||||
assert json.loads(cli(command, path, code=1)) == error
|
||||
|
||||
# Force monomorphization: every body traverses generic AST cloning before
|
||||
# ordinary lowering. These are full CLI acceptance/fuel tests, separate
|
||||
# from synthetic mixed-AST helper tests that need not be valid programs.
|
||||
for depth in (0, 1, 16, 1024, 16384, 65536):
|
||||
for kind in ('not', 'neg', 'cast', 'add'):
|
||||
if kind == 'not':
|
||||
ty, argument = 'bool', 'false'
|
||||
expression, expected, instructions = 'not ' * depth + 'x', bool(depth % 2), 2 * depth + 4
|
||||
elif kind == 'neg':
|
||||
ty, argument = 'i64', '1i64'
|
||||
expression, expected, instructions = '- ' * depth + 'x', 1 if depth % 2 == 0 else -1, 2 * depth + 4
|
||||
elif kind == 'cast':
|
||||
ty, argument = 'i64', '1i64'
|
||||
expression = 'x' + ''.join(' ' + ('as i64', 'as! i64', 'as% i64')[i % 3] for i in range(depth))
|
||||
expected, instructions = 1, depth + 4
|
||||
else:
|
||||
ty, argument = 'i64', '1i64'
|
||||
expression, expected, instructions = 'x' + ' + 1i64' * depth, depth + 1, 2 * depth + 4
|
||||
path = root / f'generic-{kind}-{depth}.aji'
|
||||
path.write_text('module depth\nfn f<T>(x: T) -> T effects [] { return ' + expression + '; }\n'
|
||||
f'fn main() -> {ty} effects [] {{ return f::<{ty}>({argument}); }}\n')
|
||||
assert cli('check', path) == b''
|
||||
lowered = cli('lower', path)
|
||||
assert sum(b' = ' in line or line.strip().startswith(b'ret ') for line in lowered.splitlines()) == instructions
|
||||
if depth <= 1024:
|
||||
returned = cli('run', path, '--fuel', instructions)
|
||||
assert json.loads(returned) == {'status': 'returned', 'value': expected}
|
||||
assert json.loads(cli('run', path, '--fuel', instructions - 1, code=5)) == {
|
||||
'status': 'limit_exceeded', 'kind': 'fuel', 'limit': instructions - 1, 'steps': instructions - 1}
|
||||
air = path.with_suffix('.air')
|
||||
air.write_bytes(lowered)
|
||||
assert cli('run', air, '--fuel', instructions) == returned
|
||||
record = root / 'hosted.json'
|
||||
assert cli('run', path, '--fuel', instructions, '--profile', 'hosted', '--result-file', record) == b''
|
||||
assert json.loads(record.read_text()) == json.loads(returned)
|
||||
print(f'generic depth: {kind} {depth}', flush=True)
|
||||
|
||||
for depth in (1024, 16384, 65536):
|
||||
header = 'fn f<T>(x: T) -> T effects [] { return '
|
||||
path = root / f'generic-deref-{depth}.aji'
|
||||
path.write_text('module depth\n' + header + '* ' * depth + 'x; }\n'
|
||||
'fn main() -> i64 effects [] { return f::<i64>(1i64); }\n')
|
||||
error = json.loads(cli('check', path, code=1))
|
||||
assert (error['code'], error['line'], error['col'], error['file']) == (
|
||||
'E7005', 2, len(header) + 1 + 2 * (depth - 1), str(path)), error
|
||||
for command in ('lower', 'run', 'report', 'explore'):
|
||||
assert json.loads(cli(command, path, code=1)) == error
|
||||
|
||||
for depth in (16384, 65536):
|
||||
# An unused template still forces transformation of nongeneric bodies.
|
||||
# Formerly generics.path aborted before unknown-callee diagnostics.
|
||||
name = 'ns.' * depth + 'unknown'
|
||||
header = 'fn main() -> i64 effects [] { return '
|
||||
path = root / f'generic-callee-{depth}.aji'
|
||||
path.write_text('module depth\nfn unused<T>(x: T) -> T effects [] { return move x; }\n'
|
||||
+ header + name + '(); }\n')
|
||||
error = json.loads(cli('check', path, code=1))
|
||||
assert (error['code'], error['name'], error['file'], error['line'], error['col']) == (
|
||||
'E7002', name, str(path), 3, len(header) + 1), error
|
||||
for command in ('lower', 'run', 'report', 'explore'):
|
||||
assert json.loads(cli(command, path, code=1)) == error
|
||||
|
||||
# Signed boundary literal remains one negative constant; the outer
|
||||
# checked negation traps rather than changing literal acceptance.
|
||||
minimum = root / 'minimum.aji'
|
||||
|
||||
Reference in new issue
Block a user