diff options
| author | Sean <69403556+SeanTheGleaming@users.noreply.github.com> | 2024-06-22 23:33:45 -0400 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-06-23 03:33:45 +0000 |
| commit | f1b6f1aeb38452fce2c1185326556566cacee2ca (patch) | |
| tree | 0e1414b6a39236c84bfd3532c65c6adfa7dbf324 /lib/std | |
| parent | 642093e04bf10f6a9a7c23dfcf219bbbc7d51b54 (diff) | |
| download | zig-f1b6f1aeb38452fce2c1185326556566cacee2ca.tar.gz zig-f1b6f1aeb38452fce2c1185326556566cacee2ca.zip | |
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.
Diffstat (limited to 'lib/std')
| -rw-r--r-- | lib/std/meta.zig | 15 |
1 files changed, 15 insertions, 0 deletions
diff --git a/lib/std/meta.zig b/lib/std/meta.zig index 85eb6cd4d3..676d7423c6 100644 --- a/lib/std/meta.zig +++ b/lib/std/meta.zig @@ -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))); |
