cli: preserve whole-array arguments for explicit array parameters

This commit is contained in:
dev committed 2026-10-07 22:29:45 +03:00
1 parent f733f7c7bb
commit dde801c653
44 files changed
+397 -9

No files matched your search

@@ -0,0 +1,3 @@
module test.arrays
fn first<T: Copy>(a: ref<[T; 2]>) -> T effects [] { return a[0]; }
fn set(a: mut<[u64; 2]>) -> void effects [] { a[0] = 42; }
@@ -0,0 +1,6 @@
// expect: reject E7005
// phase: 6
// spec: docs/spec/15-source-language.md §5
module test.array_arguments
fn first(a: ref<[u64; 2]>) -> ref<u64> from a effects [] { ref item = a[0]; return item; }
fn main() -> u64 effects [] { let a = [42u64; 2]; ref r = a; ref item = first(r); return *item; }
@@ -0,0 +1,6 @@
// expect: reject E7005
// phase: 6
// spec: docs/spec/15-source-language.md §5
module test.array_arguments
fn first(a: ref<[u64; 2]>) -> u64 effects [] { return a[0]; }
fn main() -> u64 effects [] { let a = [1u64; 2]; ref s = a; return first(ref s); }
@@ -0,0 +1,6 @@
// expect: reject E7005
// phase: 6
// spec: docs/spec/15-source-language.md §5
module test.array_arguments
fn first(a: ref<[u64; 2]>) -> u64 effects [] { return a[0]; }
fn main() -> u64 effects [task.spawn] { let a = [42u64; 2]; ref r = a; scope s { let t = task.spawn(s, first, r); return task.join(move t); } }
@@ -0,0 +1,7 @@
// expect: run
// trap: T0008
// phase: 6
// spec: docs/spec/15-source-language.md §5
module test.array_arguments
fn read(a: ref<[u64; 2]>) -> u64 effects [] { return a[2]; }
fn main() -> u64 effects [] { let a = [42u64; 2]; return read(ref a); }
@@ -0,0 +1,7 @@
// expect: run
// result: 104
// phase: 6
// spec: docs/spec/15-source-language.md §5
module test.array_arguments
fn first(a: ref<[u8; 2]>) -> u8 effects [] { return a[0]; }
fn main() -> u8 effects [] { return first(ref b"hi"); }
@@ -0,0 +1,6 @@
// expect: reject E7005
// phase: 6
// spec: docs/spec/15-source-language.md §5
module test.array_arguments
fn first(a: ref<[u8; 2]>) -> u8 effects [] { return a[0]; }
fn main() -> u8 effects [] { return first(b"hi"); }
@@ -0,0 +1,7 @@
// expect: reject E2101 E2101
// The conflicting reborrow and its use at the call each reject.
// phase: 6
// spec: docs/spec/15-source-language.md §5
module test.array_arguments
fn read(a: mut<[u64; 2]>, b: ref<[u64; 2]>) -> u64 effects [] { return b[0]; }
fn main() -> u64 effects [] { var a: [u64; 2] = zero; return read(mut a, ref a); }
@@ -0,0 +1,42 @@
; expect: run
; result: 42
; phase: 1
; spec: docs/spec/07a-air-instructions.md; docs/spec/15-source-language.md §5
; Independent format-0 baseline: temporary borrows end before the next access.
module test.array_arguments
air 0
lang 0.1
features [arrays, effects, ownership]
fn @first(%a: ref<array<u64, 2>>) -> u64
effects []
entry:
%index = const.usize 0
%element = borrow.index %a, %index
%value = load %element
borrow.end %element
ret %value
fn @set(%a: mut<array<u64, 2>>) -> void
effects []
entry:
%answer = const.u64 42
%index = const.usize 0
%element = borrow.index %a, %index
store %element, %answer
borrow.end %element
ret
fn @main() -> u64
effects []
entry:
%data = local.alloc array<u64, 2>
local.zero %data
%writer = borrow.mut %data
call @set(%writer)
borrow.end %writer
%reader = borrow.ref %data
%answer = call @first(%reader)
borrow.end %reader
local.zero %data
ret %answer
@@ -0,0 +1,9 @@
// expect: run
// result: 42
// equivalent: source/array_arguments/direct.air
// phase: 6
// spec: docs/spec/15-source-language.md §5
module test.array_arguments
fn first(a: ref<[u64; 2]>) -> u64 effects [] { return a[0]; }
fn set(a: mut<[u64; 2]>) -> void effects [] { a[0] = 42; }
fn main() -> u64 effects [] { var a: [u64; 2] = [1u64; 2]; set(mut a); let n = first(ref a); a[0] = 0; return n; }
@@ -0,0 +1,7 @@
// expect: run
// result: 42
// phase: 6
// spec: docs/spec/15-source-language.md §5
module test.array_arguments
fn first<T: Copy>(a: ref<[T; 2]>) -> T effects [] { return a[0]; }
fn main() -> u64 effects [] { let a = [42u64; 2]; return first::<u64>(ref a); }
@@ -0,0 +1,9 @@
// expect: run
// result: 84
// phase: 6
// spec: docs/spec/15-source-language.md §5
module test.array_arguments
fn first(a: ref<[u64; 2]>) -> u64 effects [] { return a[0]; }
fn use<T, F: fn(T) -> u64 effects []>(a: T) -> u64 effects [] { return F(a); }
fn slice_first(a: slice<u64>) -> u64 effects [] { return a[0]; }
fn main() -> u64 effects [] { let a = [42u64; 2]; return use::<ref<[u64; 2]>, fn first>(ref a) + use::<slice<u64>, fn slice_first>(ref a); }
@@ -0,0 +1,8 @@
// expect: run
// result: 42
// phase: 6
// spec: docs/spec/15-source-language.md §5
// module: test.arrays=source/array_arguments/arrays.aji.inc
module test.array_arguments
import test.arrays;
fn main() -> u64 effects [] { var a: [u64; 2] = [1u64; 2]; test.arrays.set(mut a); return test.arrays.first::<u64>(ref a); }
@@ -0,0 +1,6 @@
// expect: reject E7006
// phase: 6
// spec: docs/spec/15-source-language.md §5
module test.array_arguments
fn first(a: ref<[u64; 2]>) -> ref<u64> from a effects [] { ref item = a[0]; return item; }
fn main() -> u64 effects [] { let a = [42u64; 2]; ref item = first(ref a); return *item; }
@@ -0,0 +1,6 @@
// expect: reject E7006
// phase: 6
// spec: docs/spec/15-source-language.md §5
module test.array_arguments
fn first(a: ref<[u64; 2]>) -> u64 effects [] { return a[0]; }
fn main() -> u64 effects [task.spawn] { let a = [42u64; 2]; scope s { let t = task.spawn(s, first, ref a); return task.join(move t); } }
@@ -0,0 +1,8 @@
// expect: reject E2101 E2101
// The conflicting reborrow and its use at the call each reject.
// phase: 6
// spec: docs/spec/15-source-language.md §5
module test.array_arguments
fn set(a: mut<[u64; 2]>) -> void effects [] { a[0] = 42; }
fn forward(a: mut<[u64; 2]>) -> u64 effects [] { ref item = a[0]; set(mut a); return *item; }
fn main() -> u64 effects [] { var a: [u64; 2] = zero; return forward(mut a); }
@@ -0,0 +1,8 @@
// expect: run
// result: 42
// phase: 6
// spec: docs/spec/15-source-language.md §5
module test.array_arguments
fn first(a: ref<[u64; 2]>) -> ref<u64> from a effects [] { ref item = a[0]; return item; }
fn forward(a: ref<[u64; 2]>) -> u64 effects [] { ref item = first(a); return *item; }
fn main() -> u64 effects [] { let a = [42u64; 2]; return forward(ref a); }
@@ -0,0 +1,9 @@
// expect: run
// result: 42
// phase: 6
// spec: docs/spec/15-source-language.md §5
// schedules: 8
module test.array_arguments
fn first(a: ref<[u64; 2]>) -> u64 effects [] { return a[0]; }
fn forward(a: ref<[u64; 2]>) -> u64 effects [task.spawn] { scope s { let t = task.spawn(s, first, a); return task.join(move t); } }
fn main() -> u64 effects [task.spawn] { let a = [42u64; 2]; return forward(ref a); }
@@ -0,0 +1,9 @@
// expect: run
// result: 84
// phase: 6
// spec: docs/spec/15-source-language.md §5
module test.array_arguments
struct Data { a: [u64; 2] }
fn first(a: ref<[u64; 2]>) -> u64 effects [] { return a[0]; }
fn set(a: mut<[u64; 2]>) -> void effects [] { a[0] = 42; }
fn main() -> u64 effects [] { var d: Data = Data { a: [1u64; 2] }; set(mut d.a); let x = first(ref d.a); var rows: [[u64; 2]; 2] = [[1u64; 2]; 2]; set(mut rows[1]); return x + first(ref rows[1]); }
@@ -0,0 +1,6 @@
// expect: reject E7005
// phase: 6
// spec: docs/spec/15-source-language.md §5
module test.array_arguments
fn first(a: ref<[u64; 2]>) -> u64 effects [] { return a[0]; }
fn main() -> u64 effects [] { let a = [1u64; 2]; return first(ref a[0..2]); }
@@ -0,0 +1,9 @@
// expect: run
// result: 43
// phase: 6
// spec: docs/spec/15-source-language.md §5
module test.array_arguments
fn first(a: ref<[u64; 2]>) -> u64 effects [] { return a[0]; }
fn set(a: mut<[u64; 2]>) -> void effects [] { a[0] = 42; }
fn forward(a: mut<[u64; 2]>) -> u64 effects [] { let n = first(ref a); set(mut a); return n + first(ref (*a)); }
fn main() -> u64 effects [] { var a: [u64; 2] = [1u64; 2]; return forward(mut a); }
@@ -0,0 +1,7 @@
// expect: run
// result: 42
// phase: 6
// spec: docs/spec/15-source-language.md §5
module test.array_arguments
fn first(a: slice<u64>, unused: mut<[u64; 2]>) -> ref<u64> from a effects [] { ref item = a[0]; return item; }
fn main() -> u64 effects [] { let a = [42u64; 2]; var b: [u64; 2] = zero; ref s = a; ref item = first(s, mut b); b[0] = 1; return *item; }
@@ -0,0 +1,9 @@
// expect: run
// result: 42
// phase: 6
// spec: docs/spec/15-source-language.md §5
module test.array_arguments
struct Data { a: [u64; 2] }
fn first(a: ref<[u64; 2]>) -> u64 effects [] { return a[0]; }
fn forward(d: mut<Data>) -> u64 effects [] { let n = first(ref d.a); d.a[0] = 0; return n; }
fn main() -> u64 effects [] { var d: Data = Data { a: [42u64; 2] }; return forward(mut d); }
@@ -0,0 +1,8 @@
// expect: run
// result: 42
// phase: 6
// spec: docs/spec/15-source-language.md §5
module test.array_arguments
fn first(a: ref<[u64; 2]>) -> u64 effects [] { return a[0]; }
fn forward(a: ref<[u64; 2]>) -> u64 effects [] { return first(ref a) + first(ref (*a)); }
fn main() -> u64 effects [] { let a = [21u64; 2]; return forward(ref a); }
@@ -0,0 +1,7 @@
// expect: reject E7005
// phase: 6
// spec: docs/spec/15-source-language.md §5
module test.array_arguments
fn set(a: mut<[u64; 2]>) -> void effects [] { a[0] = 42; }
fn forward(a: ref<[u64; 2]>) -> void effects [] { set(mut a); }
fn main() -> void effects [] { var a: [u64; 2] = zero; forward(ref a); }
@@ -0,0 +1,9 @@
// expect: run
// result: 42
// phase: 6
// spec: docs/spec/15-source-language.md §5
module test.array_arguments
fn first(a: ref<[u64; 2]>) -> u64 effects [] { return a[0]; }
fn total(a: slice<u64>) -> u64 effects [] { return a[0] + a[1]; }
fn set(a: slice_mut<u64>) -> void effects [] { a[1] = 21; }
fn main() -> u64 effects [] { var a: [u64; 2] = [21u64; 2]; set(mut a); ref s = a; return total(s); }
@@ -0,0 +1,8 @@
// expect: run
// result: 42
// phase: 6
// spec: docs/spec/15-source-language.md §5
module test.array_arguments
fn first(a: ref<[u64; 2]>) -> u64 effects [] { return a[0]; }
fn apply<F: fn(ref<[u64; 2]>) -> u64 effects []>(a: ref<[u64; 2]>) -> u64 effects [] { return F(ref a); }
fn main() -> u64 effects [] { let a = [42u64; 2]; return apply::<fn first>(ref a); }
@@ -0,0 +1,7 @@
// expect: run
// result: 42
// phase: 6
// spec: docs/spec/15-source-language.md §5
module test.array_arguments
fn set(a: mut<[u64; 2]>) -> void effects [] { a[0] = 42; }
fn main() -> u64 effects [unsafe] { var rows: [[u64; 2]; 2] = zero; set(mut unchecked rows[1]); return rows[1][0]; }
@@ -0,0 +1,6 @@
// expect: reject E6001
// phase: 6
// spec: docs/spec/15-source-language.md §5
module test.array_arguments
fn set(a: mut<[u64; 2]>) -> void effects [] { a[0] = 42; }
fn main() -> void effects [] { var rows: [[u64; 2]; 2] = zero; set(mut unchecked rows[1]); }
@@ -0,0 +1,6 @@
// expect: reject E7005
// phase: 6
// spec: docs/spec/15-source-language.md §5
module test.array_arguments
fn first(a: ref<[u64; 2]>) -> u64 effects [] { return a[0]; }
fn main() -> u64 effects [] { let a = [1u8; 2]; return first(ref a); }
@@ -0,0 +1,6 @@
// expect: reject E7005
// phase: 6
// spec: docs/spec/15-source-language.md §5
module test.array_arguments
fn first(a: ref<[u64; 2]>) -> u64 effects [] { return a[0]; }
fn main() -> u64 effects [] { let a = [1u64; 3]; return first(ref a); }
@@ -0,0 +1,6 @@
// expect: reject E7005
// phase: 6
// spec: docs/spec/15-source-language.md §5
module test.array_arguments
fn set(a: mut<[u64; 2]>) -> void effects [] { a[0] = 42; }
fn main() -> void effects [] { var a: [u64; 2] = zero; set(ref a); }
@@ -0,0 +1,6 @@
// expect: reject E7005
// phase: 6
// spec: docs/spec/15-source-language.md §5
module test.array_arguments
fn first(a: ref<[u64; 2]>) -> u64 effects [] { return a[0]; }
fn main() -> u64 effects [] { var a: [u64; 2] = zero; return first(mut a); }
+86
View File
@@ -0,0 +1,86 @@
#!/usr/bin/env python3
"""Whole-array arguments: CLI/AIR parity, temporary cleanup and package provenance."""
import json
from pathlib import Path
import subprocess
import tempfile
REPO = Path(__file__).resolve().parents[2]
AJI = REPO / 'zig-out/bin/aji'
CORPUS = REPO / 'tests/conformance/source/array_arguments'
checks = 0
def cli(*args, code=0):
global checks
result = subprocess.run([str(AJI), *map(str, args)], capture_output=True, text=True)
assert result.returncode == code, (args, result.returncode, result.stdout, result.stderr)
checks += 1
return result.stdout
with tempfile.TemporaryDirectory(prefix='aji-array-arguments-') as directory:
root = Path(directory)
raw = root / 'lowered.air'
for name, value in {'direct': 42, 'projections': 84, 'reborrow': 43,
'shared_forward': 42, 'generic': 42, 'static': 42,
'bytes_explicit': 104, 'retained_other_temporary': 42,
'unchecked': 42, 'slices': 42}.items():
source = CORPUS / f'{name}.aji'
cli('check', source)
air = cli('lower', source)
assert cli('lower', source) == air
raw.write_text(air)
cli('check', raw)
result = json.loads(cli('run', '--fuel', '100000', source))
assert result['value'] == value, (name, result)
assert json.loads(cli('run', '--fuel', '100000', raw)) == result
hosted = root / 'hosted.json'
assert cli('run', '--profile', 'hosted', '--fuel', '100000',
'--result-file', hosted, source) == ''
assert json.loads(hosted.read_text())['value'] == value
if name == 'slices':
assert 'slice.make' in air
else:
# The unrelated retained origin is intentionally still a slice.
main = air.split('fn @main(', 1)[1]
if name != 'retained_other_temporary':
assert 'slice.make' not in main, (name, main)
# Real package linking, including a specialized imported signature.
entry, library, manifest = (root / name for name in ('main.aji', 'arrays.aji', 'aji.toml'))
entry.write_text((CORPUS / 'imported.aji').read_text())
library.write_text((CORPUS / 'arrays.aji.inc').read_text())
manifest.write_text('[package]\nname = "arrays"\nversion = "1"\nentry = "main.aji"\n'
'[modules]\ntest.arrays = "arrays.aji"\n')
cli('check', '--manifest', manifest)
assert json.loads(cli('run', '--fuel', '100000', '--manifest', manifest))['value'] == 42
air = cli('lower', '--manifest', manifest)
assert cli('lower', '--manifest', manifest) == air
raw.write_text(air)
cli('check', raw)
assert json.loads(cli('run', '--fuel', '100000', raw))['value'] == 42
cli('report', '--manifest', manifest)
cli('explore', '--max-runs', '8', '--manifest', manifest)
# Type mismatch comes from the imported body with its original source position.
library.write_text(library.read_text().replace('return a[0];', 'return first::<T>(ref a[0]);'))
error = json.loads(cli('check', '--manifest', manifest, code=1))
assert error['code'] == 'E7005', error
assert error['file'] == 'arrays.aji' and error['line'] == 2, error
assert error['expected'] == 'ref<array<u64, 2>>' and error['found'] == 'ref<u64>', error
for command in ('run', 'report', 'explore'):
assert json.loads(cli(command, '--manifest', manifest, code=1)) == error
# Aliasing failures retain the AIR verifier code and a useful argument position.
source = CORPUS / 'conflict.aji'
errors = [json.loads(line) for line in cli('check', source, code=1).splitlines()]
assert len(errors) == 2 and all(e['code'] == 'E2101' for e in errors), errors
assert all(e['file'] == str(source) and e['line'] == 7 and e['col'] > 1 for e in errors), errors
raw.write_text(cli('lower', source))
air_errors = [json.loads(line) for line in cli('check', raw, code=1).splitlines()]
# Source deduplication collapses the two conflicting call operands at
# the same source position; raw AIR retains both factual identities.
assert len(air_errors) == 3 and all(e['code'] == 'E2101' for e in air_errors), air_errors
print(f'array argument CLI integration passed ({checks} checks)')