std: Skip comptime struct fields in mem.zeroes() (#10406)

closes #9934
This commit is contained in:
Ali Chraghi 2021-12-28 08:03:11 +03:30 committed by Andrew Kelley
parent 4d9377923d
commit b86aadfa38

View File

@ -275,7 +275,9 @@ pub fn zeroes(comptime T: type) T {
} else {
var structure: T = undefined;
inline for (struct_info.fields) |field| {
@field(structure, field.name) = zeroes(@TypeOf(@field(structure, field.name)));
if (!field.is_comptime) {
@field(structure, field.name) = zeroes(@TypeOf(@field(structure, field.name)));
}
}
return structure;
}
@ -342,6 +344,8 @@ test "mem.zeroes" {
try testing.expect(a.y == 10);
const ZigStruct = struct {
comptime comptime_field: u8 = 5,
integral_types: struct {
integer_0: i0,
integer_8: i8,
@ -376,6 +380,7 @@ test "mem.zeroes" {
};
const b = zeroes(ZigStruct);
try testing.expectEqual(@as(u8, 5), b.comptime_field);
try testing.expectEqual(@as(i8, 0), b.integral_types.integer_0);
try testing.expectEqual(@as(i8, 0), b.integral_types.integer_8);
try testing.expectEqual(@as(i16, 0), b.integral_types.integer_16);