diff options
| author | Evan Haas <evan@lagerdata.com> | 2022-02-05 22:18:31 -0800 |
|---|---|---|
| committer | Veikka Tuominen <git@vexu.eu> | 2022-02-07 11:34:20 +0200 |
| commit | e382e7be2b7a4c40a8db78dca0de38141dad8c67 (patch) | |
| tree | ce716c94b22473d51781ce929625fff4ecd49236 /lib | |
| parent | 435beb4e1dec117136ef9541530ebd3e70c2319d (diff) | |
| download | zig-e382e7be2b7a4c40a8db78dca0de38141dad8c67.tar.gz zig-e382e7be2b7a4c40a8db78dca0de38141dad8c67.zip | |
std: Allow `mem.zeroes` to work at comptime with extern union
Fixes #10797
Diffstat (limited to 'lib')
| -rw-r--r-- | lib/std/mem.zig | 7 |
1 files changed, 4 insertions, 3 deletions
diff --git a/lib/std/mem.zig b/lib/std/mem.zig index 71de42aad7..5ca7faf90b 100644 --- a/lib/std/mem.zig +++ b/lib/std/mem.zig @@ -309,9 +309,7 @@ pub fn zeroes(comptime T: type) T { if (comptime meta.containerLayout(T) == .Extern) { // The C language specification states that (global) unions // should be zero initialized to the first named member. - var item: T = undefined; - @field(item, info.fields[0].name) = zeroes(@TypeOf(@field(item, info.fields[0].name))); - return item; + return @unionInit(T, info.fields[0].name, zeroes(info.fields[0].field_type)); } @compileError("Can't set a " ++ @typeName(T) ++ " to zero."); @@ -417,6 +415,9 @@ test "mem.zeroes" { var c = zeroes(C_union); try testing.expectEqual(@as(u8, 0), c.a); + + comptime var comptime_union = zeroes(C_union); + try testing.expectEqual(@as(u8, 0), comptime_union.a); } /// Initializes all fields of the struct with their default value, or zero values if no default value is present. |
