How to force analysis of a function without calling it?

What’s the idiomatic way to force an analysis of a body of a function without calling it?

If I do just _ = f, it seems the function is not analyzed. I can force analysis with casting it to a runtime function pointer:

_ = @as(*const fn() anyerror!void, f);

Is there a better way to do this?

XY: what I am trying to do is making zig test actually run all the relevant tests via

test {
    _ = @as(*const fn () anyerror!void, &main);
}

Ideally, I’d love to say “analyze anything reachable from main, but don’t actually codegen main”, but I think there’s no such thing?

Would std.testing.refAllDecls/std.testing.refAllDeclsRecursive do the trick?

2 Likes

Looking at how that’s implemented, _ = &main is actually enough!

And, yeah, std.testing.refAllDeclsRecursive is what I need, didn’t realize that recursive variant exist, will add

test {
    testing.refAllDecls(@This());
}

To my main.zig, thanks!

2 Likes

I still use it, and will for as long as I can, but it seems refAllDecls is slated for the chopping block at some point in the future.

refAllDecls is a hack that will almost certainly be removed from the standard library, and reflection will never inspect private declarations.

Source

Ideally, I hope that the this decision is reconsidered and it is kept in, but it would be nice to have an alternative idiomatic way to do this if it’s not, and hopefully not one that is the drudgery of referencing every function manually.

I do my best to write tests for every function as I go, but it is easy to get on a roll and just want a quick way to ensure there are not any major issues going on that are being optimized away in the codes current state, one that doesn’t require manually adding/removing temporary references to be discarded.

1 Like