From d4e22e3eb668968fa30f25c382b97629902f0bf8 Mon Sep 17 00:00:00 2001 From: Tau Date: Wed, 21 Jul 2021 16:12:01 +0200 Subject: [PATCH] Correct hasUniqueRepresentation for vectors Closes #9333. --- lib/std/hash/auto_hash.zig | 5 +---- lib/std/meta/trait.zig | 5 ++++- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/lib/std/hash/auto_hash.zig b/lib/std/hash/auto_hash.zig index f9bb0ecae1..3375066c12 100644 --- a/lib/std/hash/auto_hash.zig +++ b/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); diff --git a/lib/std/meta/trait.zig b/lib/std/meta/trait.zig index c6e5a9c512..26330aebc4 100644 --- a/lib/std/meta/trait.zig +++ b/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))); }