Files
aji/tests/hosted/source_generics.py
T
dev 7eea31518d cli: add explicit source generics and Copy option/result
Specialize explicit function, struct and enum templates into deterministic
ordinary AIR, with structural constraints and bounded expansion. Preserve
source provenance and readable names in specialization diagnostics.

Add Copy-only std.option/std.result and vertical specification, grammar,
Quick Reference, RFC and source coverage updates. Keep linear aggregates
and existing unsupported source from-returns explicit follow-up work.

Repair the finite-layout check through arrays under the existing E1003
contract, with positive and rejected source/AIR fixtures.

Validation: build, unit tests, 640 conformance cases, 82/82 source constructs,
32-seed generic task stress, agent-reference, formatting, hosted smoke,
20 generic and 36 package CLI checks. Record three-encoding documentation
budgets; AIR reference is unchanged.
2026-10-06 19:53:26 +03:00

59 lines
3.0 KiB
Python

#!/usr/bin/env python3
"""Source generic CLI integration; finite programs use interpreter fuel."""
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
example = REPO / "examples/std/generics.aji"
assert json.loads(cli("run", "--fuel", "1000000", example))["value"] == 42
cli("check", example)
cli("report", example)
cli("explore", "--max-runs", "8", example)
canonical = cli("lower", example)
assert cli("lower", example) == canonical
with tempfile.TemporaryDirectory(prefix="aji-generics-") as directory:
root = Path(directory)
air = root / "result.air"
air.write_text(canonical)
cli("check", air)
assert json.loads(cli("run", "--fuel", "1000000", air))["value"] == 42
entry = root / "main.aji"
lib = root / "lib.aji"
manifest = root / "aji.toml"
manifest.write_text('[package]\nname = "fixture"\nversion = "1"\nentry = "main.aji"\n[modules]\napp.lib = "lib.aji"\n')
lib.write_text("module app.lib\nfn id<T: Copy>(value: T) -> T effects [] { return value; }\n")
entry.write_text("module app.entry\nimport app.lib;\nstruct Pair { value: u64 }\nfn main() -> u64 effects [] { return app.lib.id::<Pair>(Pair { value: 42 }).value; }\n")
assert json.loads(cli("run", "--fuel", "1000000", "--manifest", manifest))["value"] == 42
cli("report", "--manifest", manifest)
cli("explore", "--max-runs", "8", "--manifest", manifest)
assert "__aji_g_" in cli("lower", "--manifest", manifest)
lib.write_text("module app.lib\nfn id<T: Copy>(value: T) -> T effects [] { assert(false); return value; }\n")
record = json.loads(cli("run", "--fuel", "1000000", "--manifest", manifest, code=4))
assert record["source_location"]["file"] == "lib.aji"
lib.write_text("module app.lib\nfn id<T: Copy>(value: T) -> T effects [] { return missing(value); }\n")
error = json.loads(cli("check", "--manifest", manifest, code=1))
assert error["code"] == "E7002" and error["file"] == "lib.aji"
lib.write_text("module app.lib\nfn id<T: Copy>(value: T) -> T effects [] { return value; }\n")
entry.write_text("module app.entry\nimport app.lib;\nfn main() -> u64 effects [] { return app.lib.id::<own<u64>>(0); }\n")
for command in ("check", "lower", "report", "run", "explore"):
error = json.loads(cli(command, "--manifest", manifest, code=1))
assert error["code"] == "E7005" and error["constraint"] == "Copy" and error["parameter"] == "T"
entry.write_text("module app.entry\nimport app.lib;\nfn main() -> u64 effects [] { return app.lib.id::<u64>(); }\n")
error = json.loads(cli("check", "--manifest", manifest, code=1))
assert error["function"] == "app.lib.id<u64>"
print(f"source generic CLI integration passed ({checks} checks)")