Files
aji/tests/hosted/source_generics.py
T

119 lines
6.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>"
# Composed arguments can exceed one ty() traversal's depth while the active
# specialization stack is still bounded. Every CLI entry rejects identically.
for width in (31, 63):
source = REPO / f"tests/conformance/source/generics/composed_identity_growth_{width}.aji"
lines = source.read_text().splitlines()
line = next(i for i, text in enumerate(lines, 1) if text.startswith("fn grow<"))
col = lines[line - 1].index("grow::<") + 1
expected = cli("check", source, code=1)
diagnostics = [json.loads(text) for text in expected.splitlines()]
assert len(diagnostics) == 1, diagnostics
error = diagnostics[0]
assert (error["code"], error["reason"], error["name"]) == ("E7006", "implementation_limit", "grow"), error
assert (error["file"], error["line"], error["col"]) == (str(source), line, col), error
for command in ("lower", "report", "run", "explore"):
assert cli(command, source, code=1) == expected
print(f"source generic CLI integration passed ({checks} checks)")