authorgravatar for 63465728+AliChraghi@users.noreply.github.comAli Chraghi <63465728+AliChraghi@users.noreply.github.com> 2021-12-28 08:03:11+03:30
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2021-12-31 14:22:38-07:00
logb86aadfa385ad2af8df601c0c69fa2e45eacd0ff
tree3bd9af83953d5ebccafa3d0cc7c048b6bb4bce9f
parent4d9377923d26ffa662612c75262849eda67d50fe

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

closes #9934

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

lib/std/mem.zig+6-1
...@@ -275,7 +275,9 @@ pub fn zeroes(comptime T: type) T {...@@ -275,7 +275,9 @@ pub fn zeroes(comptime T: type) T {
275 } else {275 } else {
276 var structure: T = undefined;276 var structure: T = undefined;
277 inline for (struct_info.fields) |field| {277 inline for (struct_info.fields) |field| {
278 @field(structure, field.name) = zeroes(@TypeOf(@field(structure, field.name)));278 if (!field.is_comptime) {
279 @field(structure, field.name) = zeroes(@TypeOf(@field(structure, field.name)));
280 }
279 }281 }
280 return structure;282 return structure;
281 }283 }
...@@ -342,6 +344,8 @@ test "mem.zeroes" {...@@ -342,6 +344,8 @@ test "mem.zeroes" {
342 try testing.expect(a.y == 10);344 try testing.expect(a.y == 10);
343345
344 const ZigStruct = struct {346 const ZigStruct = struct {
347 comptime comptime_field: u8 = 5,
348
345 integral_types: struct {349 integral_types: struct {
346 integer_0: i0,350 integer_0: i0,
347 integer_8: i8,351 integer_8: i8,
...@@ -376,6 +380,7 @@ test "mem.zeroes" {...@@ -376,6 +380,7 @@ test "mem.zeroes" {
376 };380 };
377381
378 const b = zeroes(ZigStruct);382 const b = zeroes(ZigStruct);
383 try testing.expectEqual(@as(u8, 5), b.comptime_field);
379 try testing.expectEqual(@as(i8, 0), b.integral_types.integer_0);384 try testing.expectEqual(@as(i8, 0), b.integral_types.integer_0);
380 try testing.expectEqual(@as(i8, 0), b.integral_types.integer_8);385 try testing.expectEqual(@as(i8, 0), b.integral_types.integer_8);
381 try testing.expectEqual(@as(i16, 0), b.integral_types.integer_16);386 try testing.expectEqual(@as(i16, 0), b.integral_types.integer_16);