authorgravatar for sahnvour@pm.meSahnvour <sahnvour@pm.me> 2020-07-26 22:04:10+02:00
committergravatar for sahnvour@pm.meSahnvour <sahnvour@pm.me> 2020-07-26 22:04:10+02:00
log345cb3200c353d6fb7aeb0e058986d8ca59ced1e
treeca458b60eb26e7ec73f29cc592fb579ff2c602a6
parent7ae1b3a6b348a24f07bacb6e19f1aa6d7b3ba32d

improve autoHash type switch

floats shouldn't be autoHash'd as they have multiple representations for some values, preventing it by default is safer

1 files changed, 9 insertions(+), 11 deletions(-)

lib/std/hash/auto_hash.zig+9-11
......@@ -81,24 +81,22 @@ pub fn hash(hasher: anytype, key: anytype, comptime strat: HashStrategy) void {
8181 .Undefined,
8282 .Void,
8383 .Null,
84 .BoundFn,
8584 .ComptimeFloat,
8685 .ComptimeInt,
8786 .Type,
8887 .EnumLiteral,
8988 .Frame,
89 .Float,
9090 => @compileError("cannot hash this type"),
9191
9292 // Help the optimizer see that hashing an int is easy by inlining!
9393 // TODO Check if the situation is better after #561 is resolved.
9494 .Int => @call(.{ .modifier = .always_inline }, hasher.update, .{std.mem.asBytes(&key)}),
9595
96 .Float => |info| hash(hasher, @bitCast(std.meta.Int(false, info.bits), key), strat),
97
9896 .Bool => hash(hasher, @boolToInt(key), strat),
9997 .Enum => hash(hasher, @enumToInt(key), strat),
10098 .ErrorSet => hash(hasher, @errorToInt(key), strat),
101 .AnyFrame, .Fn => hash(hasher, @ptrToInt(key), strat),
99 .AnyFrame, .BoundFn, .Fn => hash(hasher, @ptrToInt(key), strat),
102100
103101 .Pointer => @call(.{ .modifier = .always_inline }, hashPointer, .{ hasher, key, strat }),
104102
......@@ -266,12 +264,12 @@ test "hash slice deep" {
266264test "hash struct deep" {
267265 const Foo = struct {
268266 a: u32,
269 b: f64,
267 b: u16,
270268 c: *bool,
271269
272270 const Self = @This();
273271
274 pub fn init(allocator: *mem.Allocator, a_: u32, b_: f64, c_: bool) !Self {
272 pub fn init(allocator: *mem.Allocator, a_: u32, b_: u16, c_: bool) !Self {
275273 const ptr = try allocator.create(bool);
276274 ptr.* = c_;
277275 return Self{ .a = a_, .b = b_, .c = ptr };
......@@ -279,9 +277,9 @@ test "hash struct deep" {
279277 };
280278
281279 const allocator = std.testing.allocator;
282 const foo = try Foo.init(allocator, 123, 1.0, true);
283 const bar = try Foo.init(allocator, 123, 1.0, true);
284 const baz = try Foo.init(allocator, 123, 1.0, false);
280 const foo = try Foo.init(allocator, 123, 10, true);
281 const bar = try Foo.init(allocator, 123, 10, true);
282 const baz = try Foo.init(allocator, 123, 10, false);
285283 defer allocator.destroy(foo.c);
286284 defer allocator.destroy(bar.c);
287285 defer allocator.destroy(baz.c);
......@@ -338,12 +336,12 @@ test "testHash struct" {
338336test "testHash union" {
339337 const Foo = union(enum) {
340338 A: u32,
341 B: f32,
339 B: bool,
342340 C: u32,
343341 };
344342
345343 const a = Foo{ .A = 18 };
346 var b = Foo{ .B = 12.34 };
344 var b = Foo{ .B = true };
347345 const c = Foo{ .C = 18 };
348346 testing.expect(testHash(a) == testHash(a));
349347 testing.expect(testHash(a) != testHash(b));