Correct hasUniqueRepresentation for vectors

Closes #9333.
This commit is contained in:
Tau
2021-07-21 16:12:01 +02:00
committed by Andrew Kelley
parent d99f55b7cf
commit d4e22e3eb6
2 changed files with 5 additions and 5 deletions

View File

@@ -122,12 +122,9 @@ pub fn hash(hasher: anytype, key: anytype, comptime strat: HashStrategy) void {
.Array => hashArray(hasher, key, strat),
.Vector => |info| {
if (std.meta.bitCount(info.child) % 8 == 0) {
// If there's no unused bits in the child type, we can just hash
// this as an array of bytes.
if (comptime meta.trait.hasUniqueRepresentation(Key)) {
hasher.update(mem.asBytes(&key));
} else {
// Otherwise, hash every element.
comptime var i = 0;
inline while (i < info.len) : (i += 1) {
hash(hasher, key[i], strat);

View File

@@ -582,7 +582,7 @@ pub fn hasUniqueRepresentation(comptime T: type) bool {
return @sizeOf(T) == sum_size;
},
.Vector => |info| return comptime hasUniqueRepresentation(info.child),
.Vector => |info| return comptime hasUniqueRepresentation(info.child) and @sizeOf(T) == @sizeOf(info.child) * info.len,
}
}
@@ -653,4 +653,7 @@ test "std.meta.trait.hasUniqueRepresentation" {
try testing.expect(!hasUniqueRepresentation([]u8));
try testing.expect(!hasUniqueRepresentation([]const u8));
try testing.expect(hasUniqueRepresentation(@Vector(4, u16)));
try testing.expect(!hasUniqueRepresentation(@Vector(3, u16)));
}