authorgravatar for 102751849+Fri3dNstuff@users.noreply.github.comFri3dNstuff <102751849+Fri3dNstuff@users.noreply.github.com> 2025-01-26 20:23:34+02:00
committergravatar for noreply@github.comGitHub <noreply@github.com> 2025-01-26 18:23:34+00:00
log9c6d728b0c9350b0661c8da1134d4fd623b88713
tree121b627bf886d1cd6097363c2accc7c6e58f9f8d
parent317996185499c484d1b27b444ea68c3ab18db5ea
signaturebadge-check Signed by PGP key B5690EEEBB952194

std.meta: handle `comptime` fields in `hasUniqueRepresentation` (#22132)


1 files changed, 9 insertions(+), 0 deletions(-)

lib/std/meta.zig+9
...@@ -1208,6 +1208,7 @@ pub inline fn hasUniqueRepresentation(comptime T: type) bool {...@@ -1208,6 +1208,7 @@ pub inline fn hasUniqueRepresentation(comptime T: type) bool {
1208 var sum_size = @as(usize, 0);1208 var sum_size = @as(usize, 0);
12091209
1210 inline for (info.fields) |field| {1210 inline for (info.fields) |field| {
1211 if (field.is_comptime) continue;
1211 if (!hasUniqueRepresentation(field.type)) return false;1212 if (!hasUniqueRepresentation(field.type)) return false;
1212 sum_size += @sizeOf(field.type);1213 sum_size += @sizeOf(field.type);
1213 }1214 }
...@@ -1310,4 +1311,12 @@ test hasUniqueRepresentation {...@@ -1310,4 +1311,12 @@ test hasUniqueRepresentation {
13101311
1311 try testing.expect(hasUniqueRepresentation(@Vector(std.simd.suggestVectorLength(u8) orelse 1, u8)));1312 try testing.expect(hasUniqueRepresentation(@Vector(std.simd.suggestVectorLength(u8) orelse 1, u8)));
1312 try testing.expect(@sizeOf(@Vector(3, u8)) == 3 or !hasUniqueRepresentation(@Vector(3, u8)));1313 try testing.expect(@sizeOf(@Vector(3, u8)) == 3 or !hasUniqueRepresentation(@Vector(3, u8)));
1314
1315 const StructWithComptimeFields = struct {
1316 comptime should_be_ignored: u64 = 42,
1317 comptime should_also_be_ignored: [*:0]const u8 = "hope you're having a good day :)",
1318 field: u32,
1319 };
1320
1321 try testing.expect(hasUniqueRepresentation(StructWithComptimeFields));
1313}1322}