Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

std.testing.refAllDeclsRecursive: Stop recursive loops #19814

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 7 additions & 1 deletion lib/std/testing.zig
Original file line number Diff line number Diff line change
Expand Up @@ -1120,10 +1120,16 @@ pub fn refAllDecls(comptime T: type) void {
/// For deep types, you may use `@setEvalBranchQuota`.
pub fn refAllDeclsRecursive(comptime T: type) void {
if (!builtin.is_test) return;
comptime refAllDeclsRecursiveInterior(T, &[0]type{});
}

/// helper function to do the actual recursion for refAllDeclsRecursive
fn refAllDeclsRecursiveInterior(comptime T: type, comptime type_record: []const type) void {
inline for (comptime type_record) |r| if (T == r) return;
inline for (comptime std.meta.declarations(T)) |decl| {
if (@TypeOf(@field(T, decl.name)) == type) {
switch (@typeInfo(@field(T, decl.name))) {
.Struct, .Enum, .Union, .Opaque => refAllDeclsRecursive(@field(T, decl.name)),
.Struct, .Enum, .Union, .Opaque => refAllDeclsRecursiveInterior(@field(T, decl.name), type_record ++ .{T}),
else => {},
}
}
Expand Down