cli: lower scalar index operands iteratively

This commit is contained in:
dev committed 2026-10-11 19:15:23 +03:00
1 parent 7ccded2b59
commit 29c1928aa1
5 files changed
+557 -14

No files matched your search

+106
View File
@@ -1176,4 +1176,110 @@ with tempfile.TemporaryDirectory(prefix='aji-expression-depth-') as directory:
print(f'mutex operand rejection depth: {leaf}', flush=True)
# Value indices preserve base borrows, usize hints, bounds and child-first endings.
for mixed in (False, True):
for depth in (0, 1, 16, 256, 1024, 16384, 32768, 65536):
opening, closing = ('a[idx(idx(', '))]') if mixed else ('a[', ']')
expression = opening * depth + '0usize' + closing * depth
worker = 'fn idx(x: usize) -> usize effects [] { x }\n' if mixed else ''
path = root / f'index-operand-{mixed}-{depth}.aji'
path.write_text('module depth\n' + worker + 'fn main() -> usize effects [] { let a: [usize; 1] = [0usize]; return ' + expression + '; }\n')
instructions = (7 * depth + 7 if depth else 5) if mixed else (5 * depth + 6 if depth else 4)
fuel = (9 * depth + 6 if depth else 4) if mixed else (5 * depth + 6 if depth else 4)
limited = depth == 65536
checked = cli('check', path, code=1 if limited else 0)
if limited:
error = json.loads(checked)
main_instructions = instructions - int(mixed)
assert (error['code'], error['limit'], error['max'], error['found']) == ('E5101', 'instructions per function', 262144, main_instructions), error
for command in ('run', 'report', 'explore'):
assert cli(command, path, code=1) == checked
else:
assert checked == b''
lowered = cli('lower', path)
actual = sum((line.lstrip().startswith(b'%') and b' = ' in line) or line.strip().startswith((b'ret', b'borrow.end ', b'local.store ')) for line in lowered.splitlines())
assert actual == instructions, (mixed, depth, actual, instructions)
if depth <= 1024:
options = ('--quantum', 1, '--schedule-seed', 7, '--fuel', fuel)
result = cli('run', path, *options)
assert json.loads(result) == {'status': 'returned', 'value': 0, 'stdout': '', 'stderr': '', 'files': {}}, result
exhausted = {'status': 'limit_exceeded', 'kind': 'fuel', 'limit': fuel - 1, 'steps': fuel - 1, 'stdout': '', 'stderr': '', 'files': {}}
assert json.loads(cli('run', path, '--quantum', 1, '--schedule-seed', 7, '--fuel', fuel - 1, code=5)) == exhausted
air = path.with_suffix('.air')
air.write_bytes(lowered)
assert cli('run', air, *options) == result
assert json.loads(cli('run', air, '--quantum', 1, '--schedule-seed', 7, '--fuel', fuel - 1, code=5)) == exhausted
formatted = path.with_suffix('.formatted.aji')
output = cli('fmt', path)
formatted.write_bytes(output)
assert cli('fmt', formatted) == output
assert cli('lower', formatted) == lowered
if depth <= 1024:
assert cli('check', formatted) == b''
print(f'index operand accepted depth: mixed={mixed} depth={depth}', flush=True)
# Bounded public CFG/dereference/proven-fact cases complement scalar depth probes.
for label, source, proven in (
('if-child', 'fn main() -> usize effects [] { let a: [usize; 1] = [0usize]; return a[if true { a[0usize] } else { 0usize }]; }', False),
('boolean-child', 'fn main() -> usize effects [] { let a: [usize; 1] = [0usize]; return a[if false or true { 0usize } else { 0usize }]; }', False),
('deref-base', 'fn read(p: ref<[usize; 1]>, i: usize) -> usize effects [] { (*p)[i] }\nfn main() -> usize effects [] { let a: [usize; 1] = [0usize]; return read(ref a, a[0usize]); }', False),
('proven-child', 'fn main() -> usize effects [] { let a: [usize; 1] = [0usize]; ref s = a; let i: usize = 0usize; if i < s.len { return a[s[i]]; } else { return 0usize; } }', True),
):
path = root / f'index-context-{label}.aji'
path.write_text('module depth\n' + source + '\n')
assert cli('check', path) == b''
lowered = cli('lower', path)
if proven:
assert b' = borrow.index.proven ' in lowered, lowered
options = ('--quantum', 1, '--schedule-seed', 7, '--fuel', 128)
result = cli('run', path, *options)
assert json.loads(result) == {'status': 'returned', 'value': 0, 'stdout': '', 'stderr': '', 'files': {}}, result
air = path.with_suffix('.air')
air.write_bytes(lowered)
assert cli('run', air, *options) == result
formatted = path.with_suffix('.formatted.aji')
output = cli('fmt', path)
formatted.write_bytes(output)
assert cli('fmt', formatted) == output
assert cli('lower', formatted) == lowered
print('index operand bounded CFG/dereference/proven contexts passed', flush=True)
for leaf, code, offset, extra in (
('unknown', 'E7002', 0, {}),
('true', 'E7005', 0, {'found': 'bool', 'expected': 'usize'}),
('1i64', 'E7005', 0, {'found': 'i64', 'expected': 'usize'}),
('ref n', 'E7006', 0, {'reason': 'invalid_borrow'}),
('b"xy"', 'E7006', 0, {'reason': 'invalid_literal'}),
('a[0usize..1usize]', 'E7006', 0, {'reason': 'invalid_borrow'}),
('n[unknown]', 'E7005', 0, {'found': 'ref<usize>'}),
('a[stop()]', 'E7005', len('a['), {'found': 'never', 'expected': 'usize'}),
):
for depth in (16, 1024, 65536):
prefix = 'module depth\nfn stop() -> never effects [] { trap; }\nfn main() -> usize effects [] { let a: [usize; 1] = [0usize]; var n: usize = 0usize; return '
opening = 'a['
leaf_name = re.sub(r'[^A-Za-z0-9]+', '-', leaf).strip('-')
path = root / f'index-reject-{code}-{leaf_name}-{depth}.aji'
path.write_text(prefix + opening * depth + leaf + ']' * depth + '; }\n')
expected = cli('check', path, code=1)
error = json.loads(expected)
assert (error['code'], error['file'], error['line'], error['col']) == (code, str(path), 3, len(prefix.split('\n')[-1]) + len(opening) * depth + offset + 1), error
for key, value in extra.items():
assert error[key] == value, error
for command in ('lower', 'run', 'report', 'explore'):
assert cli(command, path, code=1) == expected
formatted = path.with_suffix('.formatted.aji')
output = cli('fmt', path)
formatted.write_bytes(output)
assert cli('fmt', formatted) == output
formatted_error = json.loads(cli('check', formatted, code=1))
text = output.decode()
pos = text.rfind(leaf)
assert pos >= 0, (leaf, depth)
pos += offset
assert (formatted_error['code'], formatted_error['file'], formatted_error['line'], formatted_error['col']) == (code, str(formatted), text[:pos].count('\n') + 1, pos - text.rfind('\n', 0, pos)), formatted_error
for key, value in extra.items():
assert formatted_error[key] == value, formatted_error
print(f'index operand rejection depth: {leaf}', flush=True)
print(f'source expression depth: {checks} checks passed')