authorgravatar for mail@isaacfreund.comIsaac Freund <mail@isaacfreund.com> 2021-06-14 04:45:44+00:00
committergravatar for git@vexu.euVeikka Tuominen <git@vexu.eu> 2021-06-14 12:14:04+03:00
log2d4c4396524d7572d2869d79a209e5772a33a59b
treefdc3785d36784af4aff24fd91f627b9a526cc34d
parent86ebd4b975d2f1e01f468e06daab6e28c06f9719

std: fix auto hash of tagged union with void field


1 files changed, 10 insertions(+), 2 deletions(-)

lib/std/hash/auto_hash.zig+10-2
...@@ -146,10 +146,12 @@ pub fn hash(hasher: anytype, key: anytype, comptime strat: HashStrategy) void {...@@ -146,10 +146,12 @@ pub fn hash(hasher: anytype, key: anytype, comptime strat: HashStrategy) void {
146 .Union => |info| {146 .Union => |info| {
147 if (info.tag_type) |tag_type| {147 if (info.tag_type) |tag_type| {
148 const tag = meta.activeTag(key);148 const tag = meta.activeTag(key);
149 const s = hash(hasher, tag, strat);149 hash(hasher, tag, strat);
150 inline for (info.fields) |field| {150 inline for (info.fields) |field| {
151 if (@field(tag_type, field.name) == tag) {151 if (@field(tag_type, field.name) == tag) {
152 hash(hasher, @field(key, field.name), strat);152 if (field.field_type != void) {
153 hash(hasher, @field(key, field.name), strat);
154 }
153 // TODO use a labelled break when it does not crash the compiler. cf #2908155 // TODO use a labelled break when it does not crash the compiler. cf #2908
154 // break :blk;156 // break :blk;
155 return;157 return;
...@@ -385,17 +387,23 @@ test "testHash union" {...@@ -385,17 +387,23 @@ test "testHash union" {
385 A: u32,387 A: u32,
386 B: bool,388 B: bool,
387 C: u32,389 C: u32,
390 D: void,
388 };391 };
389392
390 const a = Foo{ .A = 18 };393 const a = Foo{ .A = 18 };
391 var b = Foo{ .B = true };394 var b = Foo{ .B = true };
392 const c = Foo{ .C = 18 };395 const c = Foo{ .C = 18 };
396 const d: Foo = .D;
393 try testing.expect(testHash(a) == testHash(a));397 try testing.expect(testHash(a) == testHash(a));
394 try testing.expect(testHash(a) != testHash(b));398 try testing.expect(testHash(a) != testHash(b));
395 try testing.expect(testHash(a) != testHash(c));399 try testing.expect(testHash(a) != testHash(c));
400 try testing.expect(testHash(a) != testHash(d));
396401
397 b = Foo{ .A = 18 };402 b = Foo{ .A = 18 };
398 try testing.expect(testHash(a) == testHash(b));403 try testing.expect(testHash(a) == testHash(b));
404
405 b = .D;
406 try testing.expect(testHash(d) == testHash(b));
399}407}
400408
401test "testHash vector" {409test "testHash vector" {