diag: guide parameter mutability and explicit array slice arguments

This commit is contained in:
dev committed 2026-10-09 21:12:26 +03:00
1 parent 6a02659f31
commit 9549a3dec6
22 files changed
+197 -4

No files matched your search

+92
View File
@@ -182,4 +182,96 @@ with tempfile.TemporaryDirectory(prefix='source-advice-', dir=cache) as director
'fn main() -> void effects [] {}\n')
cli('check', probe)
# Parameter syntax and ordinary-call array mismatches preserve the
# original envelope, across source APIs and imported bodies.
call_corpus = REPO / 'tests/conformance/source/call_repair_advice'
for path in sorted(call_corpus.glob('*.aji')):
if path.stem == 'explicit_contracts':
cli('check', path)
assert json.loads(cli('run', '--fuel', '100000', path))['value'] == 42
continue
error = json.loads(cli('check', path, code=1))
if path.stem == 'parameter_mutability':
advice(error, 'parameter_mutability')
assert (error['expected'], error['found'], error['message']) == (
'parameter name', 'mut', 'expected parameter name'), error
elif path.stem in ('array_slice_shared', 'array_slice_exclusive'):
exclusive = path.stem.endswith('exclusive')
advice(error, 'array_slice_argument', ['mut' if exclusive else 'ref'])
assert (error['expected'], error['found'], error['message']) == (
'slice_mut<u8>' if exclusive else 'slice<u8>', 'array<u8, 7>', 'type mismatch'), error
else:
assert 'repair_candidates' not in error, error
assert 'air_line' not in error and 'air_col' not in error, error
for command in ('lower', 'run', 'report', 'explore'):
assert json.loads(cli(command, path, code=1)) == error
if error['code'] == 'E7001':
assert json.loads(cli('fmt', path, code=1)) == error
library.write_text(path.read_text().replace('module probe', 'module lib.advice')
.replace('fn main(', 'fn user('))
package_error = json.loads(cli('check', '--manifest', manifest, code=1))
assert package_error == dict(error, file='lib.aji'), package_error
for command in ('lower', 'run', 'report', 'explore'):
assert json.loads(cli(command, '--manifest', manifest, code=1)) == package_error
# Generic/static calls use the resolved expected type, without guessing
# one from prose. Applying the explicit shared prefix is valid here.
for declarations, call in [
('fn f<T: Copy>(s: slice<T>) -> void effects [] {}', 'f::<u8>(a)'),
('fn f(s: slice<u8>) -> void effects [] {}\n'
'fn apply<F: fn(slice<u8>) -> void effects []>(a: [u8; 7]) '
'-> void effects [] { var b: [u8; 7] = a; F(b); }', 'apply::<fn f>(a)'),
]:
body = ('module probe\n' + declarations + '\nfn main() -> void effects [] '
'{ let a: [u8; 7] = [0u8; 7]; ' + call + '; }\n')
probe.write_text(body)
error = json.loads(cli('check', probe, code=1))
advice(error, 'array_slice_argument', ['ref'])
for command in ('lower', 'run', 'report', 'explore'):
assert json.loads(cli(command, probe, code=1)) == error
probe.write_text(body.replace('f::<u8>(a)', 'f::<u8>(ref a)').replace('F(b)', 'F(ref b)'))
cli('check', probe)
assert json.loads(cli('run', '--fuel', '100000', probe))['status'] == 'returned'
# A prefix is advice, not a promise of addressability. Value parameters
# need local storage before an explicit borrow can verify.
body = ('module probe\nfn f(s: slice<u8>) -> void effects [] {}\n'
'fn g(a: [u8; 7]) -> void effects [] { f(a); }\n'
'fn main() -> void effects [] {}\n')
probe.write_text(body)
advice(json.loads(cli('check', probe, code=1)), 'array_slice_argument', ['ref'])
probe.write_text(body.replace('f(a)', 'f(ref a)'))
error = json.loads(cli('check', probe, code=1))
assert error['code'] == 'E7006' and error['reason'] == 'invalid_place', error
assert 'repair_candidates' not in error, error
probe.write_text(body.replace('f(a)', 'var b: [u8; 7] = a; f(ref b)'))
cli('check', probe)
# Nearby mistakes and contexts without call-scoped loans receive no
# array/parameter advice; earlier name/move/arity errors stay earlier.
for body in [
'fn f(mut x u8) -> void effects [] {}',
'fn f(mut ref: u8) -> void effects [] {}',
'fn f(ref x: u8) -> void effects [] {}',
'fn f(mut.x: u8) -> void effects [] {}',
'fn f(s: slice<u8>) -> void effects [] {}\n'
'fn main() -> void effects [] { f(missing); }',
'fn f(s: slice<u8>) -> void effects [] {}\n'
'fn main() -> void effects [] { let a: [u8; 7] = [0u8; 7]; f(a, a); }',
'fn g(s: slice<u8>) -> slice<u8> from s effects [] '
'{ let a: [u8; 7] = [0u8; 7]; return a; }',
'struct S { a: [u8; 7] }\nfn f(s: slice<u8>) -> void effects [] {}\n'
'fn main() -> void effects [] { let s = S { a: [0u8; 7] }; f(s.a); }',
'fn f(s: slice<u8>) -> void effects [] {}\n'
'fn main() -> void effects [] { let a: [u8; 7] = [0u8; 7]; f(move a); }',
'fn f(s: slice<u8>) -> void effects [] {}\n'
'fn main() -> void effects [] { let a: [u8; 7] = [0u8; 7]; '
'scope workers { let t = task.spawn(workers, f, a); task.join(t); } }',
]:
if 'fn main(' not in body:
body += '\nfn main() -> void effects [] {}'
probe.write_text('module probe\n' + body + '\n')
error = json.loads(cli('check', probe, code=1))
assert 'repair_candidates' not in error, error
print(f'source repair advice: {checks} checks passed')