| ... | ... | @@ -308,9 +308,7 @@ pub fn zeroes(comptime T: type) T { |
| 308 | 308 | if (comptime meta.containerLayout(T) == .Extern) { |
| 309 | 309 | // The C language specification states that (global) unions |
| 310 | 310 | // should be zero initialized to the first named member. |
| 311 | | var item: T = undefined; |
| 312 | | @field(item, info.fields[0].name) = zeroes(@TypeOf(@field(item, info.fields[0].name))); |
| 313 | | return item; |
| 311 | return @unionInit(T, info.fields[0].name, zeroes(info.fields[0].field_type)); |
| 314 | 312 | } |
| 315 | 313 | |
| 316 | 314 | @compileError("Can't set a " ++ @typeName(T) ++ " to zero."); |
| ... | ... | @@ -416,6 +414,9 @@ test "mem.zeroes" { |
| 416 | 414 | |
| 417 | 415 | var c = zeroes(C_union); |
| 418 | 416 | try testing.expectEqual(@as(u8, 0), c.a); |
| 417 | |
| 418 | comptime var comptime_union = zeroes(C_union); |
| 419 | try testing.expectEqual(@as(u8, 0), comptime_union.a); |
| 419 | 420 | } |
| 420 | 421 | |
| 421 | 422 | /// Initializes all fields of the struct with their default value, or zero values if no default value is present. |