cli: inspect generic uses with an explicit worklist

This commit is contained in:
dev committed 2026-10-10 13:04:58 +03:00
1 parent b2a0251376
commit 24a3876003
4 files changed
+183 -7

No files matched your search

+165 -7
View File
@@ -320,7 +320,7 @@ const Generator = struct {
}
generic = generic or f.generics.len != 0;
}
if (!generic and !usesGenerics(ast.Module, g.m)) return g.m;
if (!generic and !try usesGenerics(g.arena, ast.Module, &g.m)) return g.m;
var types: std.ArrayList(ast.TypeDecl) = .empty;
var functions: std.ArrayList(ast.FnDecl) = .empty;
for (g.m.types) |t| if (t.generics.len == 0) try types.append(g.arena, try g.walk(ast.TypeDecl, t, .{}));
@@ -661,7 +661,72 @@ fn path(arena: A, e: ast.Expr) A.Error!?[]const u8 {
};
}
fn usesGenerics(comptime T: type, value: T) bool {
// Each entry borrows immutable AST storage. Reflective children point into
// that storage, never into a copied union payload or a queue entry.
const GenericUseWork = struct {
pointer: *const anyopaque,
visit: *const fn (A, *std.ArrayList(GenericUseWork), *const anyopaque) A.Error!bool,
};
fn queueGenericUse(arena: A, queue: *std.ArrayList(GenericUseWork), comptime T: type, value: *const T) A.Error!void {
switch (@typeInfo(T)) {
.@"struct", .@"union", .optional => {},
.pointer => |info| if (info.child == u8) return,
else => return,
}
try queue.append(arena, .{ .pointer = @ptrCast(value), .visit = GenericUseVisitor(T).visit });
}
fn GenericUseVisitor(comptime T: type) type {
return struct {
fn visit(arena: A, queue: *std.ArrayList(GenericUseWork), raw: *const anyopaque) A.Error!bool {
const value: *const T = @ptrCast(@alignCast(raw));
if (T == ast.Type) if (value.kind == .applied or value.kind == .function_ref) return true;
if (T == ast.Expr) switch (value.kind) {
.specialize => return true,
.variant => |x| if (x.type_args.len != 0) return true,
.struct_lit => |x| if (x.type_args.len != 0) return true,
else => {},
};
if (T == ast.Pattern) if (value.kind == .ctor and value.kind.ctor.type_args.len != 0) return true;
switch (@typeInfo(T)) {
.@"struct" => |info| {
inline for (0..info.field_names.len) |offset| {
const i = info.field_names.len - 1 - offset;
try queueGenericUse(arena, queue, info.field_types[i], &@field(value.*, info.field_names[i]));
}
},
.@"union" => switch (value.*) {
inline else => |*payload| try queueGenericUse(arena, queue, @TypeOf(payload.*), payload),
},
.optional => |info| if (value.*) |*child| try queueGenericUse(arena, queue, info.child, child),
.pointer => |info| {
if (info.child == u8) return false;
if (info.size == .slice) {
var i = value.*.len;
while (i != 0) {
i -= 1;
try queueGenericUse(arena, queue, info.child, &value.*[i]);
}
} else try queueGenericUse(arena, queue, info.child, value.*);
},
else => {},
}
return false;
}
};
}
fn usesGenerics(arena: A, comptime T: type, value: *const T) A.Error!bool {
var queue: std.ArrayList(GenericUseWork) = .empty;
defer queue.deinit(arena);
try queueGenericUse(arena, &queue, T, value);
while (queue.pop()) |work| if (try work.visit(arena, &queue, work.pointer)) return true;
return false;
}
// Independent pre-repair derivation for shallow, finite test ASTs.
fn referenceUsesGenerics(comptime T: type, value: T) bool {
if (T == ast.Type) if (value.kind == .applied or value.kind == .function_ref) return true;
if (T == ast.Expr) switch (value.kind) {
.specialize => return true,
@@ -672,17 +737,17 @@ fn usesGenerics(comptime T: type, value: T) bool {
if (T == ast.Pattern) if (value.kind == .ctor and value.kind.ctor.type_args.len != 0) return true;
switch (@typeInfo(T)) {
.@"struct" => |info| {
inline for (info.field_names, info.field_types) |name, FT| if (usesGenerics(FT, @field(value, name))) return true;
inline for (info.field_names, info.field_types) |name, FT| if (referenceUsesGenerics(FT, @field(value, name))) return true;
},
.@"union" => switch (value) {
inline else => |payload| return usesGenerics(@TypeOf(payload), payload),
inline else => |payload| return referenceUsesGenerics(@TypeOf(payload), payload),
},
.optional => |info| return if (value) |v| usesGenerics(info.child, v) else false,
.optional => |info| return if (value) |v| referenceUsesGenerics(info.child, v) else false,
.pointer => |info| {
if (info.child == u8) return false;
if (info.size == .slice) {
for (value) |v| if (usesGenerics(info.child, v)) return true;
} else return usesGenerics(info.child, value.*);
for (value) |v| if (referenceUsesGenerics(info.child, v)) return true;
} else return referenceUsesGenerics(info.child, value.*);
},
else => {},
}
@@ -704,3 +769,96 @@ fn usedAir(instances: []const Instance, name: []const u8) bool {
for (instances) |instance| if (std.mem.eql(u8, instance.air_name, name)) return true;
return false;
}
test "generic use worklist matches the independent shallow AST derivation" {
var scratch: std.heap.ArenaAllocator = .init(std.testing.allocator);
defer scratch.deinit();
const a = scratch.allocator();
const span: ast.Span = .{ .start = .{ .line = 1, .col = 1 }, .end = .{ .line = 1, .col = 2 }, .file = "Box<T>", .namespace = "generic.name" };
const byte: ast.Type = .{ .span = span, .kind = .{ .name = "u8" } };
const application: ast.Type = .{ .span = span, .kind = .{ .applied = .{ .name = "Box", .args = &.{byte} } } };
const static_arg: ast.Type = .{ .span = span, .kind = .{ .function_ref = .{ .name = "operation" } } };
const plain: ast.Expr = .{ .span = span, .kind = .{ .name = "value" } };
const types = [_]ast.Type{ byte, application, static_arg };
for (&types) |*ty| {
try std.testing.expectEqual(referenceUsesGenerics(ast.Type, ty.*), try usesGenerics(a, ast.Type, ty));
for (0..8) |mode| {
const argument_types: []const ast.Type = if (mode % 2 == 0) &.{} else &.{ty.*};
const expressions = [_]ast.Expr{
.{ .span = span, .kind = .{ .variant = .{ .@"enum" = "E", .name = "A", .type_args = argument_types } } },
.{ .span = span, .kind = .{ .struct_lit = .{ .name = "S", .fields = &.{.{ .name = "x", .span = span, .e = plain }}, .type_args = argument_types } } },
.{ .span = span, .kind = .{ .cast = .{ .mode = .widen, .e = &plain, .ty = ty.* } } },
.{ .span = span, .kind = .{ .call = .{ .callee = &plain, .type_arg = if (mode % 3 == 0) null else ty.*, .args = &.{plain} } } },
.{ .span = span, .kind = .{ .specialize = .{ .base = &plain, .args = argument_types } } },
};
const pattern: ast.Pattern = .{ .span = span, .kind = .{ .ctor = .{ .qualifier = null, .name = "ok", .binds = &.{"x"}, .type_args = argument_types } } };
try std.testing.expectEqual(referenceUsesGenerics(ast.Pattern, pattern), try usesGenerics(a, ast.Pattern, &pattern));
for (&expressions) |*expression| {
try std.testing.expectEqual(referenceUsesGenerics(ast.Expr, expression.*), try usesGenerics(a, ast.Expr, expression));
const arm: ast.Arm = .{ .pattern = pattern, .body = expression.* };
const match: ast.Match = .{ .span = span, .scrutinee = plain, .arms = &.{arm}, .kind = .outcome };
const matched: ast.Expr = .{ .span = span, .kind = .{ .match_expr = &match } };
const statements = [_]ast.Stmt{ .{ .span = span, .kind = .{ .@"var" = .{ .name = "v", .ty = ty.*, .init = expression.* } } }, .{ .span = span, .kind = .{ .expr = matched } } };
const block: ast.Block = .{ .span = span, .stmts = &statements, .tail = if (mode % 4 == 0) null else expression };
const module: ast.Module = .{ .name = "m", .types = &.{.{ .name = "S", .span = span, .kind = .{ .@"enum" = &.{.{ .name = "A", .span = span, .payload = &.{ty.*} }} } }}, .functions = &.{.{ .name = "f", .span = span, .params = &.{.{ .name = "p", .span = span, .ty = byte }}, .ret = byte, .from = null, .effects = &.{}, .trusted = null, .body = block }} };
try std.testing.expectEqual(referenceUsesGenerics(ast.Block, block), try usesGenerics(a, ast.Block, &block));
try std.testing.expectEqual(referenceUsesGenerics(ast.Module, module), try usesGenerics(a, ast.Module, &module));
}
}
}
}
test "generic use detection visits deep types expressions and statement blocks iteratively" {
var scratch: std.heap.ArenaAllocator = .init(std.testing.allocator);
defer scratch.deinit();
const a = scratch.allocator();
const span: ast.Span = .{ .start = .{ .line = 1, .col = 1 }, .end = .{ .line = 1, .col = 2 } };
const types = try a.alloc(ast.Type, 32_769);
for (types[0 .. types.len - 1], 0..) |*t, i| t.* = .{ .span = span, .kind = if (i % 2 == 0) .{ .array = .{ .elem = &types[i + 1], .len = 1 } } else .{ .wrapped = .{ .wrapper = "ref", .inner = &types[i + 1] } } };
types[types.len - 1] = .{ .span = span, .kind = .{ .name = "u8" } };
try std.testing.expect(!try usesGenerics(a, ast.Type, &types[0]));
types[types.len - 1].kind = .{ .applied = .{ .name = "Box", .args = &.{} } };
try std.testing.expect(try usesGenerics(a, ast.Type, &types[0]));
const expressions = try a.alloc(ast.Expr, 32_769);
for (expressions[0 .. expressions.len - 1], 0..) |*e, i| e.* = .{ .span = span, .kind = .{ .field = .{ .name = "x", .e = &expressions[i + 1] } } };
expressions[expressions.len - 1] = .{ .span = span, .kind = .{ .name = "value" } };
try std.testing.expect(!try usesGenerics(a, ast.Expr, &expressions[0]));
expressions[expressions.len - 1].kind = .{ .variant = .{ .@"enum" = "E", .name = "A", .type_args = types[types.len - 1 ..] } };
try std.testing.expect(try usesGenerics(a, ast.Expr, &expressions[0]));
const blocks = try a.alloc(ast.Block, 32_769);
const statements = try a.alloc(ast.Stmt, blocks.len - 1);
blocks[blocks.len - 1] = .{ .span = span, .stmts = &.{}, .tail = null };
var i = statements.len;
while (i != 0) {
i -= 1;
statements[i] = .{ .span = span, .kind = .{ .loop = blocks[i + 1] } };
blocks[i] = .{ .span = span, .stmts = statements[i .. i + 1], .tail = null };
}
try std.testing.expect(!try usesGenerics(a, ast.Block, &blocks[0]));
blocks[0].tail = &expressions[0];
try std.testing.expect(try usesGenerics(a, ast.Block, &blocks[0]));
}
test "generic use work metadata errors propagate and temporary entries are released" {
var scratch: std.heap.ArenaAllocator = .init(std.testing.allocator);
defer scratch.deinit();
const a = scratch.allocator();
const span: ast.Span = .{ .start = .{ .line = 1, .col = 1 }, .end = .{ .line = 1, .col = 2 } };
const fields = try a.alloc(ast.Field, 256);
for (fields) |*f| f.* = .{ .span = span, .name = "x", .ty = .{ .span = span, .kind = .{ .name = "u8" } } };
var declaration: ast.TypeDecl = .{ .span = span, .name = "S", .kind = .{ .@"struct" = fields } };
for ([_]bool{ false, true }) |generic| {
if (generic) fields[fields.len - 1].ty.kind = .{ .function_ref = .{ .name = "operation" } };
var fail_index: usize = 0;
while (true) : (fail_index += 1) {
try std.testing.expect(fail_index < 100);
var failing: std.testing.FailingAllocator = .init(std.testing.allocator, .{ .fail_index = fail_index });
const result = usesGenerics(failing.allocator(), ast.TypeDecl, &declaration);
if (failing.has_induced_failure) {
try std.testing.expectError(error.OutOfMemory, result);
} else try std.testing.expectEqual(generic, try result);
try std.testing.expectEqual(failing.allocated_bytes, failing.freed_bytes);
if (!failing.has_induced_failure) break;
}
}
}