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 noreply@github.comGitHub <noreply@github.com> 2021-12-27 23:33:11-05:00
log042b770d6272391a9c25f3444991b439ae07a1b5
tree79fce7488a46a9e11224a0ed051732fc42ec9f0b
parent6ed7850972c5d74912e6802474564de332ecf0d9
signaturebadge-question-mark Signed by PGP key 4AEE18F83AFDEB23

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);