cli: lower task join operands iteratively

This commit is contained in:
dev committed 2026-10-11 16:22:31 +03:00
1 parent 8d6792d4f1
commit b85df6777e
5 files changed
+355 -24

No files matched your search

+82
View File
@@ -873,6 +873,88 @@ with tempfile.TemporaryDirectory(prefix='aji-expression-depth-') as directory:
assert cli(command, path, code=1) == expected
print(f'atomic operand error priority: {leaf}', flush=True)
# task.join shares consuming operand continuations. Valid tasks retain their
# scope, result/void emission and scheduling; malformed deep joins reject.
for void_result in (False, True):
for depth in (0, 1, 16, 256, 1024, 16384, 65536):
path = root / f'task-join-operand-{void_result}-{depth}.aji'
program = 'module depth\nfn id(x: i64) -> i64 effects [] { x }\n'
if void_result:
program += 'fn work(x: i64) -> void effects [] {}\n'
program += 'fn main() -> ' + ('void' if void_result else 'i64') + ' effects [task.spawn] { scope s { '
expression = 'task.join(task.spawn(s, ' + ('work' if void_result else 'id') + ', ' + 'id(' * depth + '42i64' + ')' * depth + '))'
program += (expression + ';' if void_result else 'let r = ' + expression + '; return r;') + ' } }\n'
path.write_text(program)
assert cli('check', path) == b''
lowered = cli('lower', path)
actual = sum((line.lstrip().startswith(b'%') and b' = ' in line) or line.strip().startswith((b'ret', b'task.scope.end ', b'task.join ')) for line in lowered.splitlines())
assert actual == depth + 7 + int(void_result), (void_result, depth, actual)
if depth <= 1024:
fuel = 2 * depth + 7
options = ('--quantum', 1, '--schedule-seed', 7, '--fuel', fuel)
result = cli('run', path, *options)
assert json.loads(result) == {'status': 'returned', 'value': None if void_result else 42, 'schedule': {'seed': 7, 'quantum': 1, 'choices': 1}}, result
assert json.loads(cli('run', path, '--quantum', 1, '--schedule-seed', 7, '--fuel', fuel - 1, code=5)) == {'status': 'limit_exceeded', 'kind': 'fuel', 'limit': fuel - 1, 'steps': fuel - 1, 'schedule': {'seed': 7, 'quantum': 1, 'choices': 1}}
air = path.with_suffix('.air')
air.write_bytes(lowered)
assert cli('run', air, *options) == result
if depth <= 16:
for seed in (0, 1):
options = ('--quantum', 1, '--schedule-seed', seed, '--fuel', fuel)
seeded = cli('run', path, *options)
assert json.loads(seeded) == {'status': 'returned', 'value': None if void_result else 42, 'schedule': {'seed': seed, 'quantum': 1, 'choices': 1}}
assert cli('run', air, *options) == seeded
formatted = path.with_suffix('.formatted.aji')
output = cli('fmt', path)
formatted.write_bytes(output)
assert cli('fmt', formatted) == output
assert cli('check', formatted) == b''
assert cli('lower', formatted) == lowered
print(f'task join accepted operand depth: void={void_result}', flush=True)
import re
for leaf, code, extra in (
('unknown', 'E7002', {}),
('true', 'E7005', {'found': 'bool'}),
('1', 'E7007', {}),
('task.join()', 'E7005', {}),
('task.join(unknown, false)', 'E7005', {}),
('task.cancelled()', 'E7005', {'found': 'bool'}),
('t', 'E7003', {'suggestion': 'move t'}),
('move t', 'E7005', {'found': 'i64'}),
):
for mixed in (False, True):
prefix = 'module depth\nfn id(x: i64) -> i64 effects [] { x }\nfn work() -> i64 effects [] { 42i64 }\nfn main() -> i64 effects [task.spawn] { scope s { let t = task.spawn(s, work); return '
opening, closing = ('id(task.join(', '))') if mixed else ('task.join(', ')')
for depth in (16, 1024, 65536):
expression = opening * depth + leaf + closing * depth
path = root / f'task-join-reject-{code}-{len(leaf)}-{mixed}-{depth}.aji'
path.write_text(prefix + expression + '; } }\n')
expected = cli('check', path, code=1)
error = json.loads(expected)
at = len(opening) * (depth - int(leaf == 'move t'))
assert (error['code'], error['file'], error['line'], error['col']) == (code, str(path), 4, len(prefix.split('\n')[-1]) + at + 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()
if leaf == 'move t':
pos = text.rfind('id(' if mixed else 'task.join(')
elif leaf == 't':
pos = list(re.finditer(r'(?<![A-Za-z0-9_])t(?![A-Za-z0-9_])', text))[-1].start()
else:
pos = text.index(leaf)
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'task join rejection depth: {leaf}', flush=True)
# Block/control callbacks remain native. Their existing shallow/deeper
# syntax must retain its capacity while expression dispatch is moved out
# of the native stack, including explicit-return callback re-entry.