cli: lower place bases iteratively
This commit is contained in:
5 files changed
+529
-54
No files matched your search
@@ -1282,4 +1282,92 @@ with tempfile.TemporaryDirectory(prefix='aji-expression-depth-') as directory:
|
||||
print(f'index operand rejection depth: {leaf}', flush=True)
|
||||
|
||||
|
||||
with tempfile.TemporaryDirectory(prefix='aji-place-base-depth-') as directory:
|
||||
root = Path(directory)
|
||||
for kind in ('array', 'field', 'range'):
|
||||
for depth in (1, 16, 256, 1024):
|
||||
if kind == 'array':
|
||||
ty = '[' * depth + 'usize' + '; 1]' * depth
|
||||
source = f'module depth\nfn main() -> usize effects [] {{ var a: {ty} = zero; return a' + '[0usize]' * depth + '; }\n'
|
||||
elif kind == 'field':
|
||||
declarations = ''.join(f'struct T{i} {{ data: ' + (f'T{i+1}' if i+1 < depth else 'usize') + ' }\n' for i in range(depth))
|
||||
initial = ''.join(f'T{i} {{ data: ' for i in range(depth)) + '0usize' + ' }' * depth
|
||||
source = 'module depth\n' + declarations + 'fn main() -> usize effects [] { var a: T0 = ' + initial + '; ref r = a; return r' + '.data' * depth + '; }\n'
|
||||
else:
|
||||
source = 'module depth\nfn main() -> usize effects [] { var a: [usize; 1] = zero; ref s = a' + '[0usize..1usize]' * depth + '; return s[0usize]; }\n'
|
||||
path = root / f'place-base-{kind}-{depth}.aji'
|
||||
path.write_text(source)
|
||||
assert cli('check', path) == b''
|
||||
lowered = cli('lower', path)
|
||||
expected_op = b'borrow.field @' if kind == 'field' else b'slice.sub %' if kind == 'range' else b'borrow.index %'
|
||||
assert sum(expected_op in line for line in lowered.splitlines()) == depth, (kind, depth)
|
||||
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 <= 256:
|
||||
instructions = sum(line.startswith(b' ') and (b' = ' in line or line.strip().startswith((b'ret ', b'borrow.end ', b'local.store ', b'local.zero ', b'store '))) for line in lowered.splitlines())
|
||||
air = path.with_suffix('.air')
|
||||
air.write_bytes(lowered)
|
||||
options = ('--quantum', 1, '--schedule-seed', 7, '--fuel', instructions)
|
||||
result = cli('run', path, *options)
|
||||
assert json.loads(result) == {'status': 'returned', 'value': 0}, (kind, depth, lowered, result)
|
||||
assert cli('run', air, *options) == result
|
||||
exhaustion = ('--quantum', 1, '--schedule-seed', 7, '--fuel', instructions - 1)
|
||||
limit = cli('run', path, *exhaustion, code=5)
|
||||
assert json.loads(limit) == {'status': 'limit_exceeded', 'kind': 'fuel', 'limit': instructions - 1, 'steps': instructions - 1}, (kind, depth, instructions, limit)
|
||||
assert cli('run', air, *exhaustion, code=5) == limit
|
||||
print(f'place base {kind} {depth} passed', flush=True)
|
||||
|
||||
# Bound-mode, RHS-first store, ancestor cleanup and a mixed projection spine.
|
||||
for label, source, value, marker in (
|
||||
('write', 'struct S { data: [[usize; 1]; 1] }\nfn main() -> usize effects [] { var s: S = S { data: [[0usize]] }; s.data[0usize][0usize] = s.data[0usize][0usize] + 1usize; return s.data[0usize][0usize]; }', 1, b'store %'),
|
||||
('unchecked', 'struct S { data: [[usize; 1]; 1] }\nfn main() -> usize effects [unsafe] { var s: S = S { data: [[0usize]] }; ref unchecked r = s.data[0usize][0usize]; return *r; }', 0, b'borrow.index.unchecked %'),
|
||||
('deref', 'struct S { data: [[usize; 1]; 1] }\nfn read(p: ref<S>) -> usize effects [] { (*p).data[0usize][0usize] }\nfn main() -> usize effects [] { var s: S = S { data: [[0usize]] }; return read(ref s); }', 0, b'borrow.field @S.data'),
|
||||
('proven', 'fn main() -> usize effects [] { var a: [usize; 1] = zero; ref s = a[0usize..1usize]; let i: usize = 0usize; if i < s.len { return s[i]; } else { return 2usize; } }', 0, b'borrow.index.proven %'),
|
||||
):
|
||||
path = root / f'place-context-{label}.aji'
|
||||
path.write_text('module depth\n' + source + '\n')
|
||||
assert cli('check', path) == b''
|
||||
lowered = cli('lower', path)
|
||||
assert marker in lowered, (label, lowered)
|
||||
air = path.with_suffix('.air')
|
||||
air.write_bytes(lowered)
|
||||
opts = ('--quantum', 1, '--schedule-seed', 7, '--fuel', 128)
|
||||
result = cli('run', path, *opts)
|
||||
expected = {'status': 'returned', 'value': value}
|
||||
if label != 'proven':
|
||||
expected.update(stdout='', stderr='', files={})
|
||||
assert json.loads(result) == expected, result
|
||||
assert cli('run', air, *opts) == 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
|
||||
|
||||
for expression, code, offending in (
|
||||
('s.data[unknown][true]', 'E7002', 'unknown'),
|
||||
('s.data[true][unknown]', 'E7005', 'true'),
|
||||
('s.missing[unknown]', 'E7002', 's.missing'),
|
||||
('a[unknown..true]', 'E7002', 'unknown'),
|
||||
('a[true..unknown]', 'E7005', 'true'),
|
||||
('a[0usize..unknown]', 'E7002', 'unknown'),
|
||||
('a[0usize..true]', 'E7005', 'true'),
|
||||
):
|
||||
path = root / 'place-error.aji'
|
||||
source = 'module depth\nstruct S { data: [[usize; 1]; 1] }\nfn main() -> usize effects [] { var s: S = S { data: [[0usize]] }; var a: [usize; 1] = zero; ref r = ' + expression + '; return 0usize; }\n'
|
||||
path.write_text(source)
|
||||
diag = cli('check', path, code=1)
|
||||
record = json.loads(diag)
|
||||
assert record['code'] == code, record
|
||||
start = source.index(offending, source.index('ref r ='))
|
||||
assert record['line'] == source.count('\n', 0, start) + 1
|
||||
assert record['col'] == start - source.rfind('\n', 0, start)
|
||||
for command in ('lower', 'run', 'report', 'explore'):
|
||||
assert cli(command, path, code=1) == diag
|
||||
print('place base contexts and rejection positions passed', flush=True)
|
||||
|
||||
|
||||
print(f'source expression depth: {checks} checks passed')
|
||||
Reference in new issue
Block a user