Skip to content

Commit

Permalink
std: fix auto hash of tagged union with void field
Browse files Browse the repository at this point in the history
  • Loading branch information
ifreund authored and kubkon committed Jun 15, 2021
1 parent 0063f64 commit 7611b80
Showing 1 changed file with 10 additions and 2 deletions.
12 changes: 10 additions & 2 deletions lib/std/hash/auto_hash.zig
Original file line number Diff line number Diff line change
Expand Up @@ -146,10 +146,12 @@ pub fn hash(hasher: anytype, key: anytype, comptime strat: HashStrategy) void {
.Union => |info| {
if (info.tag_type) |tag_type| {
const tag = meta.activeTag(key);
const s = hash(hasher, tag, strat);
hash(hasher, tag, strat);
inline for (info.fields) |field| {
if (@field(tag_type, field.name) == tag) {
hash(hasher, @field(key, field.name), strat);
if (field.field_type != void) {
hash(hasher, @field(key, field.name), strat);
}
// TODO use a labelled break when it does not crash the compiler. cf #2908
// break :blk;
return;
Expand Down Expand Up @@ -385,17 +387,23 @@ test "testHash union" {
A: u32,
B: bool,
C: u32,
D: void,
};

const a = Foo{ .A = 18 };
var b = Foo{ .B = true };
const c = Foo{ .C = 18 };
const d: Foo = .D;
try testing.expect(testHash(a) == testHash(a));
try testing.expect(testHash(a) != testHash(b));
try testing.expect(testHash(a) != testHash(c));
try testing.expect(testHash(a) != testHash(d));

b = Foo{ .A = 18 };
try testing.expect(testHash(a) == testHash(b));

b = .D;
try testing.expect(testHash(d) == testHash(b));
}

test "testHash vector" {
Expand Down

0 comments on commit 7611b80

Please sign in to comment.