cli: add safe owning general-key hash sets
Add std.hashset with explicit pure hash/equality policies and complete owning keys. Duplicate/full insert returns the original input, while remove/pop and nonempty free preserve explicit resource ownership. Reuse safe HashMap storage and failure/peak/refund accounting. Integrate normative contracts, RFC, references, complexity measurements, CLI/profile checks and independent Copy/owner set traces. Validation: 973 conformance cases; runtime set GC and schedule stress; unit/Quick Reference, formatting, hosted smoke, 53 CLI checks, 106 source coverage constructs and bundle generation.
This commit is contained in:
1 parent
7743f515c5
commit
4716cda0f1
43 files changed
+2696
-7
No files matched your search
@@ -0,0 +1,83 @@
|
||||
#!/usr/bin/env python3
|
||||
"""General owning sets: duplicate ownership, source/AIR, packages and profiles."""
|
||||
import json
|
||||
from pathlib import Path
|
||||
import subprocess
|
||||
import tempfile
|
||||
|
||||
REPO = Path(__file__).resolve().parents[2]
|
||||
AJI = REPO / "zig-out/bin/aji"
|
||||
CASES = REPO / "tests/conformance/source/hashset"
|
||||
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, *grants):
|
||||
args = [item for grant in grants for item in ("--grant", grant)]
|
||||
record = json.loads(cli("run", "--fuel", "2000000", *args, path))
|
||||
assert record["status"] == "returned" and record["value"] == 42, record
|
||||
return record
|
||||
|
||||
|
||||
example = REPO / "examples/std/hashset.aji"
|
||||
returned(example, "heap=mem.alloc:67")
|
||||
cli("check", example)
|
||||
report = json.loads(cli("report", example))
|
||||
assert report["unsafe"]["operations"] == 0 and report["unsafe"]["trusted_functions"] == [], report
|
||||
canonical = cli("lower", example)
|
||||
assert canonical == cli("lower", example)
|
||||
assert "trusted" not in canonical and "unsafe" not in canonical
|
||||
for name in ("copy", "owner", "byte_keys", "duplicate_state", "reserve_failure", "reserve_peak",
|
||||
"reserve_authority", "zero", "rollback", "model", "model_owner", "free_returns", "shared"):
|
||||
path = CASES / (name + ".aji")
|
||||
grants = [line.removeprefix("// grant: ") for line in path.read_text().splitlines() if line.startswith("// grant: ")]
|
||||
returned(path, *grants)
|
||||
for name, expected in (("present_leak", "E3002"), ("full_leak", "E3002"), ("free_leak", "E3002"),
|
||||
("missing_move", "E7003"), ("policy_mismatch", "E7005"), ("not_share", "E4002")):
|
||||
commands = ("check", "report", "run", "explore") if expected in ("E3002", "E4002") else ("check", "lower", "report", "run", "explore")
|
||||
for command in commands:
|
||||
records = [json.loads(line) for line in cli(command, CASES / (name + ".aji"), code=1).splitlines()]
|
||||
assert records and {error["code"] for error in records} == {expected}, records
|
||||
|
||||
with tempfile.TemporaryDirectory(prefix="aji-hashset-") as directory:
|
||||
root = Path(directory)
|
||||
air = root / "lowered.air"
|
||||
air.write_text(canonical)
|
||||
cli("check", air)
|
||||
assert returned(air, "heap=mem.alloc:67") == returned(example, "heap=mem.alloc:67")
|
||||
result = root / "hosted.json"
|
||||
stdout = cli("run", "--profile", "hosted", "--fuel", "2000000", "--grant", "heap=mem.alloc:67", "--result-file", result, example)
|
||||
assert stdout == "" and json.loads(result.read_text())["value"] == 42
|
||||
policy = root / "policy.aji"
|
||||
policy.write_text('''module app.policy
|
||||
struct Key { id: u64, owner: own<u64> }
|
||||
fn hash(key: ref<Key>) -> u64 effects [] { return key.id; }
|
||||
fn equal(a: ref<Key>, b: ref<Key>) -> bool effects [] { return a.id == b.id; }
|
||||
''')
|
||||
entry, manifest = root / "main.aji", root / "aji.toml"
|
||||
manifest.write_text('[package]\nname = "hashset"\nversion = "1"\nentry = "main.aji"\n[modules]\napp.policy = "policy.aji"\n[capabilities]\nheap = "mem.alloc:67"\n')
|
||||
program = example.read_text().replace("module demo.hashset", "module app.entry")
|
||||
start, end = program.index("struct Key"), program.index("fn main(")
|
||||
program = program[:start].replace("import std.option;", "import std.option;\nimport app.policy;") + program[end:]
|
||||
program = program.replace("<Key,", "<app.policy.Key,").replace("<Key>", "<app.policy.Key>").replace("fn hash", "fn app.policy.hash").replace("fn equal", "fn app.policy.equal").replace("Key {", "app.policy.Key {")
|
||||
entry.write_text(program)
|
||||
record = json.loads(cli("run", "--fuel", "2000000", "--manifest", manifest))
|
||||
assert record["status"] == "returned" and record["value"] == 42, record
|
||||
package_air = cli("lower", "--manifest", manifest)
|
||||
cli("report", "--manifest", manifest)
|
||||
imports = "import std.hashset;\nimport std.option;\nimport app.policy;"
|
||||
entry.write_text(program.replace(imports, "import app.policy;\nimport std.option;\nimport std.hashset;"))
|
||||
assert cli("lower", "--manifest", manifest) == package_air
|
||||
entry.write_text(program)
|
||||
policy.write_text(policy.read_text().replace("return key.id;", "assert(false); return key.id;"))
|
||||
record = json.loads(cli("run", "--fuel", "2000000", "--manifest", manifest, code=4))
|
||||
assert Path(record["source_location"]["file"]).name == "policy.aji", record
|
||||
|
||||
print(f"general hash-set CLI integration passed ({checks} checks)")
|
||||
Reference in new issue
Block a user