zig

fork of https://codeberg.org/ziglang/zig
Log | Files | Refs | README | LICENSE

commit f1b6f1aeb38452fce2c1185326556566cacee2ca (tree)
parent 642093e04bf10f6a9a7c23dfcf219bbbc7d51b54
Author: Sean <69403556+SeanTheGleaming@users.noreply.github.com>
Date:   Sat, 22 Jun 2024 23:33:45 -0400

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:
Mlib/std/meta.zig | 15+++++++++++++++
1 file changed, 15 insertions(+), 0 deletions(-)

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