Commit d4e22e3eb6

Tau <jonathan.haehne@hotmail.com>
2021-07-21 16:12:01
Correct hasUniqueRepresentation for vectors
Closes #9333.
1 parent d99f55b
Changed files (2)
lib
lib/std/hash/auto_hash.zig
@@ -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);
lib/std/meta/trait.zig
@@ -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)));
 }