authorgravatar for jonathan.haehne@hotmail.comTau <jonathan.haehne@hotmail.com> 2021-07-21 16:12:01+02:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2021-07-21 12:30:56-07:00
logd4e22e3eb668968fa30f25c382b97629902f0bf8
treefa0239b1e16609be866077790b115684ceb39130
parentd99f55b7cf3b3b15ccbef224f157725b91e40bea

Correct hasUniqueRepresentation for vectors

Closes #9333.

2 files changed, 5 insertions(+), 5 deletions(-)

lib/std/hash/auto_hash.zig+1-4
......@@ -122,12 +122,9 @@ pub fn hash(hasher: anytype, key: anytype, comptime strat: HashStrategy) void {
122122 .Array => hashArray(hasher, key, strat),
123123
124124 .Vector => |info| {
125 if (std.meta.bitCount(info.child) % 8 == 0) {
126 // If there's no unused bits in the child type, we can just hash
127 // this as an array of bytes.
125 if (comptime meta.trait.hasUniqueRepresentation(Key)) {
128126 hasher.update(mem.asBytes(&key));
129127 } else {
130 // Otherwise, hash every element.
131128 comptime var i = 0;
132129 inline while (i < info.len) : (i += 1) {
133130 hash(hasher, key[i], strat);
lib/std/meta/trait.zig+4-1
......@@ -582,7 +582,7 @@ pub fn hasUniqueRepresentation(comptime T: type) bool {
582582 return @sizeOf(T) == sum_size;
583583 },
584584
585 .Vector => |info| return comptime hasUniqueRepresentation(info.child),
585 .Vector => |info| return comptime hasUniqueRepresentation(info.child) and @sizeOf(T) == @sizeOf(info.child) * info.len,
586586 }
587587}
588588
......@@ -653,4 +653,7 @@ test "std.meta.trait.hasUniqueRepresentation" {
653653
654654 try testing.expect(!hasUniqueRepresentation([]u8));
655655 try testing.expect(!hasUniqueRepresentation([]const u8));
656
657 try testing.expect(hasUniqueRepresentation(@Vector(4, u16)));
658 try testing.expect(!hasUniqueRepresentation(@Vector(3, u16)));
656659}