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.
This commit is contained in:
Sean 2024-06-22 23:33:45 -04:00 committed by GitHub
parent 642093e04b
commit f1b6f1aeb3
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -1203,6 +1203,14 @@ pub inline fn hasUniqueRepresentation(comptime T: type) bool {
.Pointer => |info| info.size != .Slice,
.Optional => |info| switch (@typeInfo(info.child)) {
.Pointer => |ptr| !ptr.is_allowzero and switch (ptr.size) {
.Slice, .C => false,
.One, .Many => true,
},
else => false,
},
.Array => |info| hasUniqueRepresentation(info.child),
.Struct => |info| {
@ -1301,8 +1309,15 @@ test hasUniqueRepresentation {
try testing.expect(!hasUniqueRepresentation(T));
}
try testing.expect(hasUniqueRepresentation(*u8));
try testing.expect(hasUniqueRepresentation(*const u8));
try testing.expect(hasUniqueRepresentation(?*u8));
try testing.expect(hasUniqueRepresentation(?*const u8));
try testing.expect(!hasUniqueRepresentation([]u8));
try testing.expect(!hasUniqueRepresentation([]const u8));
try testing.expect(!hasUniqueRepresentation(?[]u8));
try testing.expect(!hasUniqueRepresentation(?[]const u8));
try testing.expect(hasUniqueRepresentation(@Vector(std.simd.suggestVectorLength(u8) orelse 1, u8)));
try testing.expect(@sizeOf(@Vector(3, u8)) == 3 or !hasUniqueRepresentation(@Vector(3, u8)));