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 {...@@ -122,12 +122,9 @@ pub fn hash(hasher: anytype, key: anytype, comptime strat: HashStrategy) void {
122 .Array => hashArray(hasher, key, strat),122 .Array => hashArray(hasher, key, strat),
123123
124 .Vector => |info| {124 .Vector => |info| {
125 if (std.meta.bitCount(info.child) % 8 == 0) {125 if (comptime meta.trait.hasUniqueRepresentation(Key)) {
126 // If there's no unused bits in the child type, we can just hash
127 // this as an array of bytes.
128 hasher.update(mem.asBytes(&key));126 hasher.update(mem.asBytes(&key));
129 } else {127 } else {
130 // Otherwise, hash every element.
131 comptime var i = 0;128 comptime var i = 0;
132 inline while (i < info.len) : (i += 1) {129 inline while (i < info.len) : (i += 1) {
133 hash(hasher, key[i], strat);130 hash(hasher, key[i], strat);
lib/std/meta/trait.zig+4-1
...@@ -582,7 +582,7 @@ pub fn hasUniqueRepresentation(comptime T: type) bool {...@@ -582,7 +582,7 @@ pub fn hasUniqueRepresentation(comptime T: type) bool {
582 return @sizeOf(T) == sum_size;582 return @sizeOf(T) == sum_size;
583 },583 },
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,
586 }586 }
587}587}
588588
...@@ -653,4 +653,7 @@ test "std.meta.trait.hasUniqueRepresentation" {...@@ -653,4 +653,7 @@ test "std.meta.trait.hasUniqueRepresentation" {
653653
654 try testing.expect(!hasUniqueRepresentation([]u8));654 try testing.expect(!hasUniqueRepresentation([]u8));
655 try testing.expect(!hasUniqueRepresentation([]const u8));655 try testing.expect(!hasUniqueRepresentation([]const u8));
656
657 try testing.expect(hasUniqueRepresentation(@Vector(4, u16)));
658 try testing.expect(!hasUniqueRepresentation(@Vector(3, u16)));
656}659}