authorgravatar for 69403556+SeanTheGleaming@users.noreply.github.comSean <69403556+SeanTheGleaming@users.noreply.github.com> 2024-06-22 23:33:45-04:00
committergravatar for noreply@github.comGitHub <noreply@github.com> 2024-06-23 03:33:45+00:00
logf1b6f1aeb38452fce2c1185326556566cacee2ca
tree0e1414b6a39236c84bfd3532c65c6adfa7dbf324
parent642093e04bf10f6a9a7c23dfcf219bbbc7d51b54
signaturebadge-check Signed by PGP key B5690EEEBB952194

std.meta.hasUniqueRepresentation: Handle optional pointers correctly (#20366)

std.meta.hasUniqueRepresentation should now return true for non-slice optional pointers. Additional checks were added to the test to reflect this.

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

lib/std/meta.zig+15
...@@ -1203,6 +1203,14 @@ pub inline fn hasUniqueRepresentation(comptime T: type) bool {...@@ -1203,6 +1203,14 @@ pub inline fn hasUniqueRepresentation(comptime T: type) bool {
12031203
1204 .Pointer => |info| info.size != .Slice,1204 .Pointer => |info| info.size != .Slice,
12051205
1206 .Optional => |info| switch (@typeInfo(info.child)) {
1207 .Pointer => |ptr| !ptr.is_allowzero and switch (ptr.size) {
1208 .Slice, .C => false,
1209 .One, .Many => true,
1210 },
1211 else => false,
1212 },
1213
1206 .Array => |info| hasUniqueRepresentation(info.child),1214 .Array => |info| hasUniqueRepresentation(info.child),
12071215
1208 .Struct => |info| {1216 .Struct => |info| {
...@@ -1301,8 +1309,15 @@ test hasUniqueRepresentation {...@@ -1301,8 +1309,15 @@ test hasUniqueRepresentation {
1301 try testing.expect(!hasUniqueRepresentation(T));1309 try testing.expect(!hasUniqueRepresentation(T));
1302 }1310 }
13031311
1312 try testing.expect(hasUniqueRepresentation(*u8));
1313 try testing.expect(hasUniqueRepresentation(*const u8));
1314 try testing.expect(hasUniqueRepresentation(?*u8));
1315 try testing.expect(hasUniqueRepresentation(?*const u8));
1316
1304 try testing.expect(!hasUniqueRepresentation([]u8));1317 try testing.expect(!hasUniqueRepresentation([]u8));
1305 try testing.expect(!hasUniqueRepresentation([]const u8));1318 try testing.expect(!hasUniqueRepresentation([]const u8));
1319 try testing.expect(!hasUniqueRepresentation(?[]u8));
1320 try testing.expect(!hasUniqueRepresentation(?[]const u8));
13061321
1307 try testing.expect(hasUniqueRepresentation(@Vector(std.simd.suggestVectorLength(u8) orelse 1, u8)));1322 try testing.expect(hasUniqueRepresentation(@Vector(std.simd.suggestVectorLength(u8) orelse 1, u8)));
1308 try testing.expect(@sizeOf(@Vector(3, u8)) == 3 or !hasUniqueRepresentation(@Vector(3, u8)));1323 try testing.expect(@sizeOf(@Vector(3, u8)) == 3 or !hasUniqueRepresentation(@Vector(3, u8)));