mirror of
https://github.com/ziglang/zig.git
synced 2025-12-06 06:13:07 +00:00
Fix #12423: auto_hash not hashing arrays of slices uniquely
This commit is contained in:
parent
d2342370fe
commit
2279f27e0f
@ -30,13 +30,15 @@ pub fn hashPointer(hasher: anytype, key: anytype, comptime strat: HashStrategy)
|
|||||||
.DeepRecursive => hash(hasher, key.*, .DeepRecursive),
|
.DeepRecursive => hash(hasher, key.*, .DeepRecursive),
|
||||||
},
|
},
|
||||||
|
|
||||||
.Slice => switch (strat) {
|
.Slice => {
|
||||||
.Shallow => {
|
switch (strat) {
|
||||||
hashPointer(hasher, key.ptr, .Shallow);
|
.Shallow => {
|
||||||
hash(hasher, key.len, .Shallow);
|
hashPointer(hasher, key.ptr, .Shallow);
|
||||||
},
|
},
|
||||||
.Deep => hashArray(hasher, key, .Shallow),
|
.Deep => hashArray(hasher, key, .Shallow),
|
||||||
.DeepRecursive => hashArray(hasher, key, .DeepRecursive),
|
.DeepRecursive => hashArray(hasher, key, .DeepRecursive),
|
||||||
|
}
|
||||||
|
hash(hasher, key.len, .Shallow);
|
||||||
},
|
},
|
||||||
|
|
||||||
.Many,
|
.Many,
|
||||||
@ -53,17 +55,8 @@ pub fn hashPointer(hasher: anytype, key: anytype, comptime strat: HashStrategy)
|
|||||||
|
|
||||||
/// Helper function to hash a set of contiguous objects, from an array or slice.
|
/// 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 {
|
pub fn hashArray(hasher: anytype, key: anytype, comptime strat: HashStrategy) void {
|
||||||
switch (strat) {
|
for (key) |element| {
|
||||||
.Shallow => {
|
hash(hasher, element, strat);
|
||||||
for (key) |element| {
|
|
||||||
hash(hasher, element, .Shallow);
|
|
||||||
}
|
|
||||||
},
|
|
||||||
else => {
|
|
||||||
for (key) |element| {
|
|
||||||
hash(hasher, element, strat);
|
|
||||||
}
|
|
||||||
},
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -359,6 +352,12 @@ test "testHash array" {
|
|||||||
try testing.expectEqual(h, hasher.final());
|
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" {
|
test "testHash struct" {
|
||||||
const Foo = struct {
|
const Foo = struct {
|
||||||
a: u32 = 1,
|
a: u32 = 1,
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user