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