Files
aji/tests/hosted/source_linear.py
dev 4f084decce cli: add whole-value linear aggregates
Implement STD-5 through source, AIR, verifier and deterministic interpreter.
Constructors consume nested owners; exhaustive unpack/take, explicit swap and
own.pack preserve ownership on success and failure. Support owning Option and
Result while keeping classification helpers Copy-only. Preserve borrow identity
across whole replacement and collection; distinguish individual owned arrays
from repeated heap arrays.

Specify feature contracts, invariants, diagnostics, compatibility and measured
source/AIR budgets in RFC 0023 and normative docs. Add canonical round trips,
AIR/source acceptance and rejection fixtures, CLI integration and CI checks.

Validate build, unit/extended tests, full conformance, forced-collection schedule
stress (32 seeds per quantum), hosted smoke, CLI suites, source coverage (88/88),
formatting and reference bundle generation. Source bundle grows 3.2%; Quick
Reference grows 4.0%. No MCC improvement is claimed.

Dynamic initialized linear buffers and explicit drain remain Q-036/STD-6;
frame/region/repeated heap storage is Copy-only. RFC remains Proposed.
2026-10-06 21:23:52 +03:00

72 lines
3.6 KiB
Python

#!/usr/bin/env python3
"""Whole-value ownership through source, canonical AIR, and CLI entry points."""
import json
from pathlib import Path
import subprocess
import tempfile
REPO = Path(__file__).resolve().parents[2]
AJI = REPO / "zig-out/bin/aji"
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
def returned(path, *args):
record = json.loads(cli("run", "--fuel", "1000000", *args, path))
assert record["status"] == "returned" and record["value"] == 42, record
features = json.loads(cli("features"))
assert "linear_aggregates" in features["features"]
source = REPO / "examples/std/owning-option.aji"
returned(source, "--grant", "heap=mem.alloc:64")
cli("check", source)
canonical = cli("lower", source)
assert cli("lower", source) == canonical
assert "enum.take" in canonical and "value.swap" in canonical
with tempfile.TemporaryDirectory(prefix="aji-linear-") as directory:
root = Path(directory)
air = root / "result.air"
air.write_text(canonical)
cli("check", air)
returned(air, "--grant", "heap=mem.alloc:64")
for fixture, grant in (("pack", 64), ("pack_failure", 8), ("swap_loop", 8), ("heap_array_borrow", 64)):
path = REPO / f"tests/conformance/source/linear/{fixture}.aji"
lowered = cli("lower", path)
assert cli("lower", path) == lowered
air.write_text(lowered)
cli("check", air)
returned(air, "--grant", f"heap=mem.alloc:{grant}")
# Resource-bearing nominal types imported across a package boundary.
(root / "lib.aji").write_text("module app.lib\nstruct Box<T> { value: T }\nfn extract<T>(box: Box<T>) -> T effects [] { let (value) = struct.unpack(move box); return move value; }\n")
(root / "main.aji").write_text("module app.main\nimport app.lib;\nfn main(heap: cap<mem.alloc>) -> u64 effects [mem.alloc] { let owner = own.new(42u64, heap) or trap; let box = app.lib.Box::<own<u64>> { value: move owner }; let value = app.lib.extract::<own<u64>>(move box); return own.take(move value); }\n")
manifest = root / "aji.toml"
manifest.write_text('[package]\nname = "fixture"\nversion = "1"\nentry = "main.aji"\n[modules]\napp.lib = "lib.aji"\n')
assert json.loads(cli("run", "--fuel", "1000000", "--manifest", manifest, "--grant", "heap=mem.alloc:64"))["value"] == 42
cli("check", "--manifest", manifest)
cli("report", "--manifest", manifest)
cli("lower", "--manifest", manifest)
cli("explore", "--max-runs", "8", "--manifest", manifest, "--grant", "heap=mem.alloc:64")
for fixture, expected in (("missing_move", "E7003"), ("ignored_field", "E3002"), ("borrow_conflict", "E2101"), ("recursive_ownership", "E1003")):
path = REPO / f"tests/conformance/source/linear/{fixture}.aji"
commands = ("check", "lower", "report", "run", "explore") if fixture in ("missing_move", "recursive_ownership") else ("check", "report", "run", "explore")
for command in commands:
diagnostics = [json.loads(line) for line in cli(command, path, code=1).splitlines()]
assert diagnostics[0]["code"] == expected, diagnostics
pack = REPO / "tests/conformance/source/linear/pack.aji"
report = json.loads(cli("report", pack))
assert sum(function["allocations"] for function in report["functions"]) == 2, report
for fixture in ("task", "channel"):
path = REPO / f"tests/conformance/source/linear/{fixture}.aji"
cli("explore", "--max-runs", "8", "--grant", "heap=mem.alloc:64", path)
print(f"source linear CLI integration passed ({checks} checks)")