authorgravatar for me@hannesbredberg.seN00byEdge <me@hannesbredberg.se> 2022-08-05 14:47:52+02:00
committergravatar for noreply@github.comGitHub <noreply@github.com> 2022-08-05 15:47:52+03:00
log18440cb239755c983f672b4902147c12e819e029
tree41c49d99b3fc18ea76d23007cc77a62e15f82a92
parent44c321c05e0f5755bcb9f4b1a33dc23aeef880aa
signaturebadge-question-mark Signed by PGP key 4AEE18F83AFDEB23

std.mem.zeroes: Zero sized structs with uninitialized members (#12246)

`std.mem.zeroes(struct{handle: void})` Failed with the following error before: ``` /nix/store/l6v4359wc9xrxxmvvp3rynsb5s3d78xf-zig-0.9.1/lib/zig/std/mem.zig:270:42: error: missing field: 'handle' if (@sizeOf(T) == 0) return T{}; ^ ```

1 files changed, 4 insertions(+), 1 deletions(-)

lib/std/mem.zig+4-1
......@@ -267,7 +267,7 @@ pub fn zeroes(comptime T: type) T {
267267 return null;
268268 },
269269 .Struct => |struct_info| {
270 if (@sizeOf(T) == 0) return T{};
270 if (@sizeOf(T) == 0) return undefined;
271271 if (struct_info.layout == .Extern) {
272272 var item: T = undefined;
273273 set(u8, asBytes(&item), 0);
......@@ -424,6 +424,9 @@ test "zeroes" {
424424
425425 comptime var comptime_union = zeroes(C_union);
426426 try testing.expectEqual(@as(u8, 0), comptime_union.a);
427
428 // Ensure zero sized struct with fields is initialized correctly.
429 _ = zeroes(struct { handle: void });
427430}
428431
429432/// Initializes all fields of the struct with their default value, or zero values if no default value is present.