diff options
| author | mlugg <mlugg@mlugg.co.uk> | 2023-05-29 05:07:17 +0100 |
|---|---|---|
| committer | mlugg <mlugg@mlugg.co.uk> | 2023-05-29 23:06:08 +0100 |
| commit | 4976b58ab16069f8d3267b69ed030f29685c1abe (patch) | |
| tree | 400f632d11eec4f3c330bee15d59f8fd6219c20f /lib/std/testing.zig | |
| parent | b5fad3a40a86eb379903d6a803bdbe66dcaa5487 (diff) | |
| download | zig-4976b58ab16069f8d3267b69ed030f29685c1abe.tar.gz zig-4976b58ab16069f8d3267b69ed030f29685c1abe.zip | |
Prevent analysis of functions only referenced at comptime
The idea here is that there are two ways we can reference a function at runtime:
* Through a direct call, i.e. where the function is comptime-known
* Through a function pointer
This means we can easily perform a form of rudimentary escape analysis
on functions. If we ever see a `decl_ref` or `ref` of a function, we
have a function pointer, which could "leak" into runtime code, so we
emit the function; but for a plain `decl_val`, there's no need to.
This change means that `comptime { _ = f; }` no longer forces a function
to be emitted, which was used for some things (mainly tests). These use
sites have been replaced with `_ = &f;`, which still triggers analysis
of the function body, since you're taking a pointer to the function.
Resolves: #6256
Resolves: #15353
Diffstat (limited to 'lib/std/testing.zig')
| -rw-r--r-- | lib/std/testing.zig | 4 |
1 files changed, 2 insertions, 2 deletions
diff --git a/lib/std/testing.zig b/lib/std/testing.zig index 7986c50eaf..fa131122bb 100644 --- a/lib/std/testing.zig +++ b/lib/std/testing.zig @@ -1116,7 +1116,7 @@ pub fn checkAllAllocationFailures(backing_allocator: std.mem.Allocator, comptime pub fn refAllDecls(comptime T: type) void { if (!builtin.is_test) return; inline for (comptime std.meta.declarations(T)) |decl| { - if (decl.is_pub) _ = @field(T, decl.name); + if (decl.is_pub) _ = &@field(T, decl.name); } } @@ -1132,7 +1132,7 @@ pub fn refAllDeclsRecursive(comptime T: type) void { else => {}, } } - _ = @field(T, decl.name); + _ = &@field(T, decl.name); } } } |
