Fix #12423: auto_hash not hashing arrays of slices uniquely

This commit is contained in:
Yujiri 2022-08-12 11:56:00 +00:00 committed by Veikka Tuominen
parent d2342370fe
commit 2279f27e0f

View File

@ -30,13 +30,15 @@ pub fn hashPointer(hasher: anytype, key: anytype, comptime strat: HashStrategy)
.DeepRecursive => hash(hasher, key.*, .DeepRecursive),
},
.Slice => switch (strat) {
.Slice => {
switch (strat) {
.Shallow => {
hashPointer(hasher, key.ptr, .Shallow);
hash(hasher, key.len, .Shallow);
},
.Deep => hashArray(hasher, key, .Shallow),
.DeepRecursive => hashArray(hasher, key, .DeepRecursive),
}
hash(hasher, key.len, .Shallow);
},
.Many,
@ -53,18 +55,9 @@ pub fn hashPointer(hasher: anytype, key: anytype, comptime strat: HashStrategy)
/// Helper function to hash a set of contiguous objects, from an array or slice.
pub fn hashArray(hasher: anytype, key: anytype, comptime strat: HashStrategy) void {
switch (strat) {
.Shallow => {
for (key) |element| {
hash(hasher, element, .Shallow);
}
},
else => {
for (key) |element| {
hash(hasher, element, strat);
}
},
}
}
/// Provides generic hashing for any eligible type.
@ -359,6 +352,12 @@ test "testHash array" {
try testing.expectEqual(h, hasher.final());
}
test "testHash multi-dimensional array" {
const a = [_][]const u32{ &.{ 1, 2, 3 }, &.{ 4, 5 } };
const b = [_][]const u32{ &.{ 1, 2 }, &.{ 3, 4, 5 } };
try testing.expect(testHash(a) != testHash(b));
}
test "testHash struct" {
const Foo = struct {
a: u32 = 1,