cli: query field annotations iteratively and close type-depth audit

This commit is contained in:
dev committed 2026-10-10 15:46:46 +03:00
1 parent d98ddbc073
commit 46bd29953f
6 files changed
+291 -10

No files matched your search

+158 -9
View File
@@ -1702,17 +1702,35 @@ const Lowerer = struct {
};
}
fn typeOfNoFail(l: *Lowerer, t: ast.Type) ?Ty {
return switch (t.kind) {
fn typeOfNoFail(l: *Lowerer, annotation: ast.Type) ?Ty {
// Validate before allocating, as the old postorder query did. This
// best-effort lookup recognizes only names and fixed array chains.
var t = annotation;
while (t.kind == .array) {
const a = t.kind.array;
if (a.len == 0 or a.len > (@as(u64, 1) << 32)) return null;
t = a.elem.*;
}
const known: ?Ty = switch (t.kind) {
.name => |n| if (isIntName(n)) Ty{ .int = n } else if (std.mem.eql(u8, n, "bool")) .bool else if (std.meta.stringToEnum(authority.Builtin, n)) |b| Ty{ .builtin = b } else l.enumOrStruct(n, t.span),
.array => |a| blk: {
if (a.len == 0 or a.len > (@as(u64, 1) << 32)) break :blk null;
const elem = l.typeOfNoFail(a.elem.*) orelse break :blk null;
if (elem == .void or elem == .never) break :blk null;
break :blk l.arrayOf(elem, a.len) catch null;
},
else => null,
};
const leaf = known orelse return null;
var result: Ty = undefined;
var target = &result;
t = annotation;
while (t.kind == .array) {
const a = t.kind.array;
// Only fresh output nodes are writable; no source AST is borrowed
// or changed. The caller arena retains partial nodes on OOM.
const node = l.arena.create(Ty.Arr) catch return null;
node.len = a.len;
target.* = .{ .array = node };
target = &node.elem;
t = a.elem.*;
}
target.* = leaf;
return result;
}
fn enumOrStruct(l: *Lowerer, name: []const u8, span: Span) ?Ty {
@@ -4881,7 +4899,7 @@ const AirConversionAllocations = struct {
return null;
}
fn free(_: *anyopaque, _: []u8, _: std.mem.Alignment, _: usize) void {
unreachable; // Conversion transfers nodes to the caller, never frees them.
unreachable; // Type construction transfers nodes to the caller.
}
fn deinit(self: *@This()) void {
for (self.nodes[0..self.count]) |node| if (node.pointer) |ptr| self.backing.rawFree(ptr[0..node.length], node.alignment, @returnAddress());
@@ -4889,6 +4907,137 @@ const AirConversionAllocations = struct {
}
};
// Independent pre-repair best-effort lookup, only for shallow test trees.
fn referenceTypeHint(l: *Lowerer, t: ast.Type) ?Ty {
return switch (t.kind) {
.name => |n| if (isIntName(n)) Ty{ .int = n } else if (std.mem.eql(u8, n, "bool")) .bool else if (std.meta.stringToEnum(authority.Builtin, n)) |b| Ty{ .builtin = b } else l.enumOrStruct(n, t.span),
.array => |a| blk: {
if (a.len == 0 or a.len > (@as(u64, 1) << 32)) break :blk null;
const elem = referenceTypeHint(l, a.elem.*) orelse break :blk null;
if (elem == .void or elem == .never) break :blk null;
break :blk l.arrayOf(elem, a.len) catch null;
},
else => null,
};
}
fn expectTypeHintMatches(l: *Lowerer, t: ast.Type) !void {
const before = referenceTypeHint(l, t);
const after = l.typeOfNoFail(t);
try std.testing.expectEqual(before == null, after == null);
if (before) |b| try std.testing.expect(b.eql(after.?));
try std.testing.expect(l.problem == null and l.unimpl == null);
}
test "best-effort source type lookup matches the shallow independent query" {
var scratch: std.heap.ArenaAllocator = .init(std.testing.allocator);
defer scratch.deinit();
const a = scratch.allocator();
const m = (try parse_mod.parse(a, "module hints\nstruct S { x: u8 }\nenum E { Some, None }\nfn main() -> void effects [] {}\n")).ok;
var l: Lowerer = .{ .arena = a, .m = m };
var leaves: std.ArrayList(ast.Type) = .empty;
const span: Span = .{ .start = .{ .line = 2, .col = 7 }, .end = .{ .line = 2, .col = 8 } };
for (int_types ++ [_][]const u8{ "bool", "S", "E", "alloc_error", "cap_error", "io_error", "void", "never", "region", "stream", "listener", "missing", "f32", "f64" }) |name| {
try leaves.append(a, .{ .span = span, .kind = .{ .name = name } });
}
const u8_type: ast.Type = .{ .span = span, .kind = .{ .name = "u8" } };
const wrappers = comptime std.meta.fieldNames(Lowerer.TypeWrapper) ++ [_][]const u8{"unknown"};
for (wrappers) |wrapper| {
try leaves.append(a, .{ .span = span, .kind = .{ .wrapped = .{ .wrapper = wrapper, .inner = &u8_type } } });
}
try leaves.appendSlice(a, &.{
.{ .span = span, .kind = .{ .cap = "mem.alloc" } },
.{ .span = span, .kind = .{ .file = "read" } },
.{ .span = span, .kind = .{ .applied = .{ .name = "S", .args = &.{u8_type} } } },
.{ .span = span, .kind = .{ .function_ref = .{ .name = "main" } } },
});
const lengths = [_]u64{ 0, 1, 7, @as(u64, 1) << 32, (@as(u64, 1) << 32) + 1, std.math.maxInt(u64) };
for (leaves.items) |*leaf| {
try expectTypeHintMatches(&l, leaf.*);
for (lengths) |length| {
const inner: ast.Type = .{ .span = span, .kind = .{ .array = .{ .elem = leaf, .len = length } } };
try expectTypeHintMatches(&l, inner);
for (lengths) |outer_length| {
try expectTypeHintMatches(&l, .{ .span = span, .kind = .{ .array = .{ .elem = &inner, .len = outer_length } } });
}
}
}
}
test "deep best-effort source type lookup owns output and rejects without allocation" {
var scratch: std.heap.ArenaAllocator = .init(std.testing.allocator);
defer scratch.deinit();
const a = scratch.allocator();
const m: ast.Module = .{ .name = "hints", .types = &.{}, .functions = &.{} };
const count = 65_536;
const nodes = try a.alloc(ast.Type, count + 1);
const span: Span = .{ .start = .{ .line = 1, .col = 1 }, .end = .{ .line = 1, .col = 2 } };
nodes[0] = .{ .span = span, .kind = .{ .name = "u8" } };
for (nodes[1..], 1..) |*node, i| node.* = .{ .span = span, .kind = .{ .array = .{ .elem = &nodes[i - 1], .len = i } } };
var allocations: std.testing.FailingAllocator = .init(std.testing.allocator, .{});
var l: Lowerer = .{ .arena = allocations.allocator(), .m = m };
var current = l.typeOfNoFail(nodes[count]).?;
var remaining: usize = count;
while (remaining != 0) : (remaining -= 1) {
try std.testing.expect(current == .array);
const node = current.array;
try std.testing.expectEqual(@as(u64, remaining), node.len);
current = node.elem;
// Only tests individually free nodes; production owns a caller arena.
allocations.allocator().destroy(node);
}
try std.testing.expectEqualStrings("u8", current.int);
try std.testing.expectEqual(@as(usize, count), allocations.allocations);
try std.testing.expectEqual(allocations.allocated_bytes, allocations.freed_bytes);
var no_alloc: std.testing.FailingAllocator = .init(std.testing.allocator, .{ .fail_index = 0 });
var invalid: Lowerer = .{ .arena = no_alloc.allocator(), .m = m };
for ([_][]const u8{ "missing", "void", "never" }) |name| {
nodes[0].kind = .{ .name = name };
try std.testing.expect(invalid.typeOfNoFail(nodes[count]) == null);
}
nodes[0].kind = .{ .name = "u8" };
nodes[1].kind.array.len = 0;
try std.testing.expect(invalid.typeOfNoFail(nodes[count]) == null);
nodes[1].kind.array.len = (@as(u64, 1) << 32) + 1;
try std.testing.expect(invalid.typeOfNoFail(nodes[count]) == null);
try std.testing.expect(!no_alloc.has_induced_failure);
try std.testing.expect(invalid.problem == null and invalid.unimpl == null);
}
test "best-effort source type lookup preserves null on every allocation failure" {
var scratch: std.heap.ArenaAllocator = .init(std.testing.allocator);
defer scratch.deinit();
const a = scratch.allocator();
const m: ast.Module = .{ .name = "hints", .types = &.{}, .functions = &.{} };
const nodes = try a.alloc(ast.Type, 65);
const span: Span = .{ .start = .{ .line = 1, .col = 1 }, .end = .{ .line = 1, .col = 2 } };
nodes[0] = .{ .span = span, .kind = .{ .name = "u8" } };
for (nodes[1..], 1..) |*node, i| node.* = .{ .span = span, .kind = .{ .array = .{ .elem = &nodes[i - 1], .len = i } } };
for (0..65) |fail_index| {
var before_fail: std.testing.FailingAllocator = .init(std.testing.allocator, .{ .fail_index = fail_index });
var after_fail: std.testing.FailingAllocator = .init(std.testing.allocator, .{ .fail_index = fail_index });
var before_nodes: AirConversionAllocations = .{ .backing = before_fail.allocator() };
var after_nodes: AirConversionAllocations = .{ .backing = after_fail.allocator() };
var before: Lowerer = .{ .arena = before_nodes.allocator(), .m = m };
var after: Lowerer = .{ .arena = after_nodes.allocator(), .m = m };
const original = referenceTypeHint(&before, nodes[64]);
const current = after.typeOfNoFail(nodes[64]);
try std.testing.expectEqual(original == null, current == null);
try std.testing.expectEqual(fail_index < 64, current == null);
if (original) |ty| try std.testing.expect(ty.eql(current.?));
try std.testing.expectEqual(before_nodes.count, after_nodes.count);
for (before_nodes.nodes[0..before_nodes.count], after_nodes.nodes[0..after_nodes.count]) |old, new| {
try std.testing.expectEqual(old.length, new.length);
try std.testing.expectEqual(old.alignment, new.alignment);
}
try std.testing.expect(after.problem == null and after.unimpl == null);
before_nodes.deinit();
after_nodes.deinit();
try std.testing.expectEqual(before_fail.allocated_bytes, before_fail.freed_bytes);
try std.testing.expectEqual(after_fail.allocated_bytes, after_fail.freed_bytes);
}
}
// Independent pre-repair comparison, restricted to shallow test trees.
fn referenceSourceEquality(a: Ty, b: Ty) bool {
if (std.meta.activeTag(a) != std.meta.activeTag(b)) return false;