Files
aji/tools/source_coverage.py
T
dev 1eaf968f4f cli: add explicit built-in error values
Add the error_values feature and error.const through specification, AIR,
verification, interpretation and source lowering. Error values are Copy data
with no allocation, effects or authority. Keep enum.make semantics frozen.

Cover all registered tags, feature negotiation, payload/type errors and
authority rejection in AIR/source; verify canonical and hosted CLI behavior.
Record reference-bundle costs and the map preflight-validation prerequisite.
2026-10-07 08:09:49 +03:00

156 lines
9.7 KiB
Python

#!/usr/bin/env python3
"""Source construct coverage (Phase 6A freeze criterion).
Every construct of `.aji` 0.1 (spec 15) must appear in at least one `.aji`
conformance test that is accepted or run, and, where the construct has
rules that reject programs, in at least one reject test. The checklist is
below; each construct is a regular expression over the test's source (header
comments removed).
python3 tools/source_coverage.py # table; exit 1 if a row fails
"""
import glob
import os
import re
import sys
ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
TESTS = os.path.join(ROOT, "tests", "conformance", "source")
# (area, construct, regex, needs a reject test)
CHECKLIST = [
("failure", "explicit built-in error value", r"(?:return|=) (alloc_error|io_error|cap_error)::", True),
("hash", "pure FNV/CRC byte hashing", r"std\.hash\.(fnv1a64|crc32c)\(", True),
("hash", "continuation from explicit state", r"std\.hash\.(fnv1a64_extend|crc32c_extend)\(", True),
("deque", "bounded owning ring ends", r"std\.deque\.(push_front|push_back|pop_front|pop_back)", True),
("deque", "explicit growth, replacement and drain", r"std\.deque\.(reserve|replace|free)", True),
("buffers", "initialized buffer allocation and drain", r"buffer\.(new|push|pop|free)", True),
("buffers", "explicit reserve and borrowed views", r"buffer\.(reserve|view)", True),
("buffers", "safe owning Vec", r"std\.vec\.", True),
("linear aggregates", "complete struct unpack", r"struct\.unpack\(", True),
("linear aggregates", "complete array unpack", r"array\.unpack\(", True),
("linear aggregates", "ordered array literal", r"\[move \w+,", True),
("linear aggregates", "consuming enum match", r"match move ", True),
("linear aggregates", "borrowed complete exchange", r"value\.swap\(", True),
("linear aggregates", "ownership-preserving heap allocation", r"own\.pack\(", True),
("generics", "generic function declaration", r"fn \w+<", True),
("generics", "generic struct/enum declaration", r"(?:struct|enum) \w+<", True),
("generics", "explicit function/worker application", r"[a-z]\w*::<", True),
("generics", "applied generic type", r": (?:[a-z]\w*\.)*[A-Z]\w*<", True),
("generics", "generic constructor/pattern", r"[A-Z]\w*::<", True),
("generics", "structural property constraint", r"[A-Z]\w*: (?:Copy|Send|Share|Freeze|Local)", True),
("modules", "package module import", r"^import app\.", True),
("modules", "qualified package call", r"app\.\w+\.[a-z]\w*\(", True),
("modules", "qualified struct literal", r"app\.\w+\.[A-Z]\w* \{", True),
("modules", "qualified task worker", r"task\.spawn\(\w+, app\.", True),
("stdlib", "qualified standard enum type", r": std\.(?:bytes|fmt|math|utf8|encoding\.(?:hex|base64))\.[A-Z]\w*\b", True),
("stdlib", "qualified standard enum constructor", r"std\.(?:bytes|fmt|math|utf8|encoding\.(?:hex|base64))\.[A-Z]\w*::\w+(?:\(|;)", True),
("stdlib", "qualified standard enum match", r"std\.(?:bytes|fmt|math|utf8|encoding\.(?:hex|base64))\.[A-Z]\w*::\w+(?:\([^)]*\))? =>", True),
("stdlib", "built-in module import", r"^import std\.", True),
("stdlib", "qualified standard call", r"std\.(?:bytes|fmt|math|utf8|encoding\.(?:hex|base64))\.\w+\(", True),
("scalars", "checked arithmetic", r"[^%]\+ [a-z0-9]", False),
("scalars", "wrapping arithmetic `+%`", r"\+%", False),
("scalars", "`as` (lossless widening)", r"\bas [ui]\d", True),
("scalars", "`as!` (checked conversion)", r"\bas! ", False),
("scalars", "`as%` (wrapping conversion)", r"\bas% ", False),
("scalars", "bitwise and shifts", r"<<|>>| & | \^ ", False),
("control flow", "`let`", r"\blet ", False),
("control flow", "`var`", r"\bvar ", False),
("control flow", "assignment", r"^\s*[a-z_][a-z0-9_]* = ", False),
("control flow", "`if`/`else`", r"\bif .*\{.*\belse\b|\} else \{", False),
("control flow", "`while`", r"\bwhile ", False),
("control flow", "`for … in a..b`", r"\bfor \w+ in ", False),
("control flow", "`loop`", r"\bloop \{", False),
("control flow", "`break` / `continue`", r"\bbreak;|\bcontinue;", True),
("control flow", "`return`", r"\breturn\b", True),
("control flow", "`assert`", r"\bassert\(", False),
("struct/enum/match", "struct declaration and literal", r"\bstruct \w+ \{", True),
("struct/enum/match", "enum declaration and `Enum::Variant`", r"\w+::\w+", True),
("struct/enum/match", "enum `match`", r"match .*\{[^}]*\w+::\w+", True),
("struct/enum/match", "integer `match` with `_`", r"\b_ =>", True),
("struct/enum/match", "`match` as an expression", r"= match ", False),
("arrays/slices", "`[e; N]`", r"\[[^\]]+; \d+\]", False),
("arrays/slices", "indexing `s[i]`", r"\w\[[a-z0-9_]+\]", True),
("arrays/slices", "ranges `s[a..b]`", r"\[[^\]]*\.\.[^\]]*\]", False),
("arrays/slices", "`split_at`", r"\.split_at\(", False),
("arrays/slices", "`slice.copy`", r"slice\.copy\(", True),
("arrays/slices", "byte strings", r'b"', False),
("borrow/move", "`ref r = …;` / `mut r = …;`", r"^\s*(ref|mut) \w+ = ", True),
("borrow/move", "`ref`/`mut` arguments", r"\((ref|mut) \w+|, (ref|mut) \w+", True),
("borrow/move", "`*r` load and store", r"\*\w+ = |= \*\w+", True),
("borrow/move", "`move`", r"\bmove \w+", True),
("own/region", "`own.new` / `own.take` / `own.free`", r"own\.(new|take|free)\(", True),
("own/region", "`own.array`", r"own\.array\(", False),
("own/region", "regions", r"region\.(create|new|array|reset|destroy)\(", True),
("own/region", "`cap.narrow.budget`", r"cap\.narrow\.budget\(", False),
("failure", "outcome `match`", r"\bok\(\w*\)? =>|\bok =>", True),
("failure", "`or trap`", r"\bor trap\b", False),
("failure", "built-in error enums", r"(alloc_error|io_error|cap_error)::", False),
("resources/I/O", "stdin/stdout", r"io\.(stdin_read|stdout_write)\(", True),
("resources/I/O", "files", r"fs\.(open_read|create|read|write_all|close)\(", True),
("resources/I/O", "TCP", r"net\.(listen|connect|accept|recv|send_all)\(", False),
("resources/I/O", "`clock`/`random`", r"(clock\.monotonic|random\.fill)\(", True),
("resources/I/O", "`cap.narrow.prefix`", r"cap\.narrow\.prefix\(", False),
("tasks/channels", "`scope` and `task.spawn`/`join`", r"\bscope \w+ \{", True),
("tasks/channels", "`task.cancel` / `task.cancelled`", r"task\.cancel(led)?\(", False),
("tasks/channels", "`channel.make` / `send` / `recv`", r"channel\.(make|send|recv)", True),
("tasks/channels", "`channel.dup`", r"channel\.dup\(", False),
("tasks/channels", "`close_receiver` and `pending`", r"close_receiver\(", True),
("mutex/select", "`lock … cancelled`", r"\block ref \w+ as \w+", True),
("mutex/select", "`mutex.make` / `mutex.take`", r"mutex\.(make|take)\(", True),
("mutex/select", "`select`", r"\bselect \{", True),
("cancellation", "`shield`", r"\bshield \{", False),
("cancellation", "`task.check_cancel`", r"task\.check_cancel\(", False),
("cancellation", "`task.join_cancel`", r"task\.join_cancel\(", True),
("graphs", "`island.create` / `island.alloc` / `island.at`", r"island\.(create|alloc|at)\(", True),
("graphs", "`island.freeze` / `frozen.at` / `frozen.release`", r"island\.freeze\(|frozen\.(at|release)\(", True),
("graphs", "`arc.from_frozen` / `arc.clone` / `arc.release` / `arc.at`", r"arc\.(from_frozen|clone|release|at)\(", True),
("graphs", "`iref` equality", r"(?s)(?:island|frozen|arc)\.root\(.*\b\w+ (?:==|!=) (?:\w+|arc\.root\(\w+\)) \{", False),
("graphs", "graphs through channels and tasks", r"channel\.make<(island|arc)<", True),
("atomics", "`atomic.new` / `atomic.take`", r"atomic\.(new|take)\(", True),
("atomics", "`atomic.load` / `atomic.store` with an ordering", r"atomic\.(load|store)\([^)]*(relaxed|acquire|release|seq_cst)\)", True),
("atomics", "read-modify-write (`swap`, `add.wrap`, `sub.wrap`, `and`, `or`, `xor`)", r"atomic\.(swap|add\.wrap|sub\.wrap|and|or|xor)\(", False),
("atomics", "`let (old, exchanged) = atomic.compare_exchange(…)`", r"let \(\w+, \w+\) = atomic\.compare_exchange\(", True),
("atomics", "`atomic.fence`", r"atomic\.fence\(", False),
("atomics", "cells lent to tasks (`ref c = cell;`)", r"(?s)ref \w+ = \w+;.*task\.spawn\(.*atomic\.", True),
("unsafe", "`ref unchecked` / `assume`", r"\bunchecked\b|\bassume\(", True),
("unsafe", "`trusted` rejected", r"\btrusted\b", True),
]
def load():
runs, rejects = [], []
for path in sorted(glob.glob(os.path.join(TESTS, "**", "*.aji"), recursive=True)):
with open(path, encoding="utf-8") as f:
text = f.read()
m = re.search(r"^// expect: (\w+)", text, re.M)
if not m:
continue
body = "\n".join(l for l in text.splitlines() if not l.lstrip().startswith("//"))
(rejects if m.group(1) == "reject" else runs).append((os.path.relpath(path, TESTS), body))
return runs, rejects
def main():
runs, rejects = load()
failed = 0
print("| area | construct | run/accept tests | reject tests | status |")
print("|---|---|---:|---:|---|")
for area, name, rx, need_reject in CHECKLIST:
pat = re.compile(rx, re.M)
nr = sum(1 for _, b in runs if pat.search(b))
nj = sum(1 for _, b in rejects if pat.search(b))
ok = nr > 0 and (nj > 0 or not need_reject)
if name == "`trusted` rejected":
ok = nj > 0
failed += not ok
print(f"| {area} | {name} | {nr} | {nj}{'' if need_reject else ' (not required)'} | {'covered' if ok else 'MISSING'} |")
print(f"\n{len(CHECKLIST) - failed}/{len(CHECKLIST)} constructs covered "
f"({len(runs)} run/accept and {len(rejects)} reject `.aji` tests)")
sys.exit(1 if failed else 0)
if __name__ == "__main__":
main()