cli: implement explicit source borrow returns

This commit is contained in:
dev committed 2026-10-07 18:20:45 +03:00
1 parent 715a8be491
commit a9a0d63f27
78 files changed
+970 -48

No files matched your search

+81
View File
@@ -0,0 +1,81 @@
#!/usr/bin/env python3
"""Borrow-return CLI, packages, canonical AIR and profile parity; use fuel."""
import json
from pathlib import Path
import subprocess
import tempfile
REPO = Path(__file__).resolve().parents[2]
AJI = REPO / "zig-out/bin/aji"
FIXTURES = REPO / "tests/conformance/source/borrow_returns"
checks = 0
def cli(*args, code=0):
global checks
run = subprocess.run([str(AJI), *map(str, args)], capture_output=True, text=True)
assert run.returncode == code, (args, run.returncode, run.stdout, run.stderr)
checks += 1
return run.stdout
with tempfile.TemporaryDirectory(prefix="aji-borrow-returns-") as directory:
root = Path(directory)
for name in ("first", "forward", "ancestors", "mutable", "mutable_slice", "generic",
"other_temporary", "split", "region", "region_match", "buffer_shared_from_mut",
"returned_task_loan", "structured_exits"):
source = FIXTURES / f"{name}.aji"
grants = ["--grant", "heap=mem.alloc:256"] if "heap: cap" in source.read_text() else []
canonical = cli("lower", source)
assert cli("lower", source) == canonical
air = root / f"{name}.air"
air.write_text(canonical)
cli("check", air)
record = json.loads(cli("run", "--fuel", "1000000", *grants, source))
assert record["value"] == 42
assert json.loads(cli("run", "--fuel", "1000000", *grants, air)) == record
output = root / "hosted.json"
cli("run", "--profile", "hosted", "--result-file", output,
"--fuel", "1000000", *grants, source)
assert json.loads(output.read_text())["value"] == 42
cli("report", FIXTURES / "forward.aji")
cli("explore", "--max-runs", "8", FIXTURES / "returned_task_loan.aji")
for name, code in (("frame_escape", "E2108"), ("wrong_origin", "E2102"),
("inline_origin", "E7006"), ("parent_access", "E2101"), ("lock_escape", "E2102"),
("zero_array_origin", "E7005"), ("oversized_repeat_origin", "E7005"),
("spawn_return", "E7006"), ("unused_generic_zero_array", "E7005"),
("unused_generic_oversized_array", "E7005")):
commands = ("check", "lower", "run", "report", "explore") if code.startswith("E7") else ("check", "run", "report", "explore")
for command in commands:
errors = [json.loads(line) for line in cli(command, FIXTURES / f"{name}.aji", code=1).splitlines()]
assert errors[0]["code"] == code and errors[0]["file"].endswith(f"{name}.aji")
if code.startswith("E21"):
assert "air_line" in errors[0] and "air_col" in errors[0]
if name == "spawn_return":
source = FIXTURES / f"{name}.aji"
spawn_line = next(i for i, line in enumerate(source.read_text().splitlines(), 1)
if "let t = task.spawn" in line)
assert errors[0]["function"] == "first" and errors[0]["line"] == spawn_line
entry = root / "main.aji"
library = root / "views.aji"
manifest = root / "aji.toml"
manifest.write_text('[package]\nname = "returned-views"\nversion = "1"\nentry = "main.aji"\n[modules]\napp.views = "views.aji"\n')
library.write_text('module app.views\nfn first<T>(s: slice<T>) -> ref<T> from s effects [] { ref item = s[0]; return item; }\n')
entry.write_text('module app.entry\nimport app.views;\nfn main() -> u64 effects [] { let data = [42u64, 7u64]; ref s = data; ref item = app.views.first::<u64>(s); return *item; }\n')
canonical = cli("lower", "--manifest", manifest)
assert cli("lower", "--manifest", manifest) == canonical and " from %s" in canonical
assert json.loads(cli("run", "--fuel", "1000000", "--manifest", manifest))["value"] == 42
output = root / "package-hosted.json"
cli("run", "--profile", "hosted", "--result-file", output, "--fuel", "1000000", "--manifest", manifest)
assert json.loads(output.read_text())["value"] == 42
# An ordinary qualified function takes the moduleCall normalization path.
library.write_text('module app.views\nfn first(s: slice<u64>) -> ref<u64> from s effects [] { ref item = s[0]; return item; }\n')
entry.write_text(entry.read_text().replace('first::<u64>', 'first'))
assert json.loads(cli("run", "--fuel", "1000000", "--manifest", manifest))["value"] == 42
library.write_text(library.read_text().replace('ref item = s[0]', 'let n = 1u64; ref item = n'))
error = json.loads(cli("check", "--manifest", manifest, code=1))
assert error["code"] == "E2108" and error["file"] == "views.aji"
library.write_text('module app.views\nfn first(s: slice<u64>) -> ref<u64> from s effects [] { ref item = s[2]; return item; }\n')
record = json.loads(cli("run", "--fuel", "1000000", "--manifest", manifest, code=4))
assert record["code"] == "T0008" and record["source_location"]["file"] == "views.aji"
print(f"source borrow-return CLI integration passed ({checks} checks)")