report: collect capability kinds without recursive type walks

This commit is contained in:
dev committed 2026-10-09 20:25:47 +03:00
1 parent 53030efcea
commit 8a714ba6de
5 files changed
+62 -3

No files matched your search

+24 -3
View File
@@ -206,9 +206,12 @@ pub fn write(arena: std.mem.Allocator, m: air.Module, w: *Writer) !void {
// Capability kinds appearing in any type of the module, sorted.
var kinds: std.ArrayList([]const u8) = .empty;
const visit = struct {
fn go(arena_: std.mem.Allocator, list: *std.ArrayList([]const u8), t: air.Type) !void {
if (t == .cap) _ = try addOnce(arena_, list, air.authority.kinds[t.cap].name);
if (t.inner()) |inner| try go(arena_, list, inner);
fn go(arena_: std.mem.Allocator, list: *std.ArrayList([]const u8), initial: air.Type) !void {
var t = initial;
while (true) {
if (t == .cap) _ = try addOnce(arena_, list, air.authority.kinds[t.cap].name);
t = t.inner() orelse return;
}
}
}.go;
for (m.types) |d| switch (d.kind) {
@@ -269,3 +272,21 @@ test "audit report" {
\\{"report":"audit","air":0,"module":"demo","features":["arrays","capabilities","effects","ownership","resources","unsafe"],"entry":{"function":"main","capabilities":[{"parameter":"out","kind":"io.write"},{"parameter":"fs","kind":"fs.read"}]},"functions":[{"name":"main","trusted":null,"effects_declared":["fs.read","io.write"],"effects_used":[],"effects_unused":["fs.read","io.write"],"calls":["peek"],"intrinsics":[],"allocations":0,"runtime_checks":{"overflow":1,"division":0,"shift":0,"cast":0,"bounds":0,"assert":0,"guard":0},"proven_checks":{"bounds":0},"unsafe_operations":[],"atomic_operations":[]},{"name":"peek","trusted":"only reads","effects_declared":[],"effects_used":["unsafe"],"effects_unused":[],"calls":[],"intrinsics":[],"allocations":0,"runtime_checks":{"overflow":0,"division":0,"shift":0,"cast":0,"bounds":0,"assert":0,"guard":0},"proven_checks":{"bounds":0},"unsafe_operations":[{"block":"entry","inst":1,"op":"assume"}],"atomic_operations":[]}],"capability_kinds":["fs.read","io.write"],"unsafe":{"trusted_functions":["peek"],"unsafe_functions":[],"operations":1}}
, out.written());
}
test "audit report handles deep capability syntax and retains sorted deduplication" {
var scratch = std.heap.ArenaAllocator.init(std.testing.allocator);
defer scratch.deinit();
const arena = scratch.allocator();
var source: Writer.Allocating = .init(arena);
try source.writer.writeAll("module deep.report\nair 0\nlang 0.1\nfeatures [arrays, capabilities, effects, ownership]\ntype @Read = struct { x: cap<fs.read> }\ntype @Write = enum { Empty, Some(cap<io.write>) }\nfn @f(%x: ");
for (0..65_536) |_| try source.writer.writeAll("array<");
try source.writer.writeAll("cap<io.write>");
for (0..65_536) |_| try source.writer.writeAll(",1>");
try source.writer.writeAll(", %y: cap<fs.read>) -> cap<io.write>\neffects []\nentry:\n br @end(%y)\nend(%z: cap<fs.read>):\n trap\n");
const checked = try @import("check.zig").checkAir(arena, "deep.air", source.written());
try std.testing.expectEqual(@import("check.zig").Status.accepted, checked.status);
var out: Writer.Allocating = .init(arena);
try write(arena, checked.module.?, &out.writer);
try std.testing.expect(std.mem.indexOf(u8, out.written(), "\"capability_kinds\":[\"fs.read\",\"io.write\"]") != null);
try std.testing.expect(std.mem.indexOf(u8, out.written(), "\"entry\":null") != null);
}