Skip to content

Commit 18440cb

Browse files
authored
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 parent 44c321c commit 18440cb

File tree

1 file changed

+4
-1
lines changed

1 file changed

+4
-1
lines changed

lib/std/mem.zig

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -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.

0 commit comments

Comments
 (0)