Files
aji/tests/hosted/source_generics.py
T
dev 1495650587 cli: resolve struct len fields by receiver type
Use ordinary field lowering and declared field types for nominal receivers while preserving array and slice lengths. Make array field type hints available to chained access. Missing struct len now reports E7002 instead of the builtin-length E7005.

Add 15 source regressions and imported generic CLI checks. Unit tests, all 1201 conformance cases, per-instruction collection for new cases, formatter corpus and original public check/lower compatibility pass.
2026-10-07 20:50:48 +03:00

104 lines
5.1 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)
# Nominal `len` fields keep their declared types across module and
# generic boundaries, including diagnostic source positions.
lib.write_text("""module app.lib
struct Field<T: Copy> { len: T }
fn read<T: Copy>(value: ref<Field<T>>) -> T effects [] { return value.len; }
fn make() -> Field<u32> effects [] { return Field::<u32> { len: 42u32 }; }
""")
entry.write_text("""module app.entry
import app.lib;
fn main() -> u32 effects [] {
var value: app.lib.Field<u32> = app.lib.Field::<u32> { len: 41u32 };
var flag: app.lib.Field<bool> = app.lib.Field::<bool> { len: true };
assert(app.lib.read::<bool>(ref flag));
assert(app.lib.make().len == 42u32);
let one = 1;
return one + app.lib.read::<u32>(ref value);
}
""")
cli("check", "--manifest", manifest)
canonical = cli("lower", "--manifest", manifest)
assert canonical == cli("lower", "--manifest", manifest)
assert "struct.get" in canonical and "borrow.field" in canonical
assert "slice.len" not in canonical
air.write_text(canonical)
source_record = json.loads(cli("run", "--fuel", "100000", "--manifest", manifest))
assert source_record["value"] == 42
assert json.loads(cli("run", "--fuel", "100000", air)) == source_record
cli("report", "--manifest", manifest)
cli("explore", "--max-runs", "8", "--manifest", manifest)
lib.write_text("""module app.lib
struct Field<T: Copy> { count: T }
fn read<T: Copy>(value: ref<Field<T>>) -> T effects [] { return value.len; }
""")
entry.write_text("""module app.entry
import app.lib;
fn main() -> u32 effects [] {
var value: app.lib.Field<u32> = app.lib.Field::<u32> { count: 42u32 };
return app.lib.read::<u32>(ref value);
}
""")
for command in ("check", "lower", "report", "run", "explore"):
error = json.loads(cli(command, "--manifest", manifest, code=1))
assert error["code"] == "E7002" and error["field"] == "len"
assert error["file"] == "lib.aji" and error["line"] == 3 and error["col"] == 65, error
lib.write_text("module app.lib\nfn id<T: Copy>(value: T) -> T effects [] { assert(false); 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")
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)")