verifier: account finite anonymous type sizes exactly

This commit is contained in:
dev committed 2026-10-09 13:26:17 +03:00
1 parent 03614c9bef
commit 111a3861f9
16 files changed
+226 -8

No files matched your search

+42 -6
View File
@@ -34,8 +34,8 @@ pub const SizeMemo = struct {
values: air_module.SizeCache = .{},
evaluations: usize = 0,
// Preserve the legacy depth cutoff exactly: at most V * (V + 2)
// nominal/depth states, rather than exponentially many shared paths.
// Count nominal member edges, not finite anonymous wrappers: at most
// V * (V + 2) nominal/depth states, rather than shared-path expansion.
pub fn size(cache: *SizeMemo, m: air.Module, t: air.Type, depth: usize) std.mem.Allocator.Error!?u64 {
return sizeQuery(m, t, depth, cache);
@@ -43,6 +43,8 @@ pub const SizeMemo = struct {
};
fn sizeQuery(m: air.Module, t: air.Type, depth: usize, memo: ?*SizeMemo) std.mem.Allocator.Error!?u64 {
// Only nominal members advance this cycle guard. Array/atomic/mutex
// nesting is finite syntax and must never truncate a valid value's size.
if (depth > m.types.len + 1) return 0;
const key: air_module.SizeKey = .{ .index = if (t == .named) t.named else 0, .depth = depth };
if (t == .named) {
@@ -64,13 +66,13 @@ fn sizeValue(m: air.Module, t: air.Type, depth: usize, memo: ?*SizeMemo) std.mem
.bool => 1,
.ref, .mut, .cap, .resource, .own, .region, .scope, .task, .shield, .sender, .receiver, .guard, .island, .frozen, .arc => 8,
.iref => 16,
.atomic => |inner| (try sizeQuery(m, inner.*, depth + 1, memo)),
.mutex => |inner| std.math.add(u64, 8, (try sizeQuery(m, inner.*, depth + 1, memo)) orelse return null) catch null,
.atomic => |inner| (try sizeQuery(m, inner.*, depth, memo)),
.mutex => |inner| std.math.add(u64, 8, (try sizeQuery(m, inner.*, depth, memo)) orelse return null) catch null,
.own_array => 16,
.buffer => 24,
.slice, .slice_mut => 16,
.builtin => |b| tagSize(b.variants().len),
.array => |a| std.math.mul(u64, a.len, (try sizeQuery(m, a.elem, depth + 1, memo)) orelse return null) catch null,
.array => |a| std.math.mul(u64, a.len, (try sizeQuery(m, a.elem, depth, memo)) orelse return null) catch null,
.named => |i| switch (m.types[i].kind) {
.@"struct" => |fs| blk: {
var sum: u64 = 0;
@@ -222,7 +224,7 @@ test "accounted sizes memoize shared diamonds and cyclic depth states" {
m.accounted_sizes = &memo.values;
for (0..65) |i| try std.testing.expectEqual(@as(?u64, 0), accountedSize(m, .{ .named = @intCast(i) }, 0));
try std.testing.expectEqual(before, memo.evaluations);
// A cyclic, branching size query keeps the legacy depth cutoff exactly.
// A cyclic, branching size query keeps the nominal depth guard bounded.
ds[64].kind = .{ .@"struct" = try a.dupe(air_module.Field, &.{ .{ .name = "a", .ty = .{ .named = 0 } }, .{ .name = "b", .ty = .{ .named = 0 } } }) };
m.accounted_sizes = null;
var cyclic: SizeMemo = .{ .arena = a };
@@ -262,3 +264,37 @@ test "accounted sizes" {
try std.testing.expectEqual(@as(u64, 1), tagSize(256));
try std.testing.expectEqual(@as(u64, 2), tagSize(257));
}
test "anonymous wrapper sizes do not depend on nominal declaration count" {
var scratch = std.heap.ArenaAllocator.init(std.testing.allocator);
defer scratch.deinit();
const a = scratch.allocator();
const leaf: air.Type = .{ .int = .u64 };
const row: air.Type.Array = .{ .elem = leaf, .len = 2 };
const rows: air.Type.Array = .{ .elem = .{ .array = &row }, .len = 2 };
const matrix: air.Type = .{ .array = &rows };
const cell: air.Type = .{ .atomic = &leaf };
const cells: air.Type.Array = .{ .elem = cell, .len = 4 };
const wrapped: air.Type = .{ .mutex = &matrix };
const ds = try a.alloc(air.TypeDecl, 4);
for (ds) |*d| d.* = .{ .name = "Unused", .span = .{ .line = 1, .col = 1 }, .kind = .{ .@"struct" = &.{} } };
for (0..5) |n| {
const m: air.Module = .{ .name = "t", .format_version = 0, .lang = .{ 0, 1 }, .features = &.{}, .types = ds[0..n], .functions = &.{} };
var memo: SizeMemo = .{ .arena = a };
for ([_]air.Type{ matrix, .{ .array = &cells }, wrapped }, [_]u64{ 32, 32, 40 }) |t, want| {
try std.testing.expectEqual(@as(?u64, want), accountedSize(m, t, 0));
try std.testing.expectEqual(@as(?u64, want), try memo.size(m, t, 0));
}
}
}
test "cyclic nominal layouts through anonymous wrappers stay bounded" {
var scratch = std.heap.ArenaAllocator.init(std.testing.allocator);
defer scratch.deinit();
const a = scratch.allocator();
const row: air.Type.Array = .{ .elem = .{ .named = 0 }, .len = 2 };
const m: air.Module = .{ .name = "t", .format_version = 0, .lang = .{ 0, 1 }, .features = &.{}, .types = &.{.{ .name = "Cycle", .span = .{ .line = 1, .col = 1 }, .kind = .{ .@"struct" = &.{.{ .name = "children", .ty = .{ .array = &row } }} } }}, .functions = &.{} };
var memo: SizeMemo = .{ .arena = a };
try std.testing.expectEqual(@as(?u64, 0), try memo.size(m, .{ .named = 0 }, 0));
try std.testing.expect(memo.evaluations <= m.types.len * (m.types.len + 2));
}