From 5cc28431b92d6849ef7758d200f714f5aee996f3 Mon Sep 17 00:00:00 2001 From: Denis Shelomovskii Date: Thu, 15 Dec 2016 19:21:41 +0300 Subject: [PATCH 1/2] Fix Issue 16974 - Equal associative arrays with associative array keys are considered unequal Use correct overload of `hashOf` in `_aaGetHash`. This incorrect usage is possible because of Issue 16973 - `hashOf` has error-prone signature as `(T, seed)` may be confused with `(ptr, length)` [1]. [1] https://issues.dlang.org/show_bug.cgi?id=16973 --- src/rt/aaA.d | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/src/rt/aaA.d b/src/rt/aaA.d index d51843d45c..37dcaab6f5 100644 --- a/src/rt/aaA.d +++ b/src/rt/aaA.d @@ -662,7 +662,7 @@ extern (C) hash_t _aaGetHash(in AA* aa, in TypeInfo tiRaw) nothrow continue; size_t[2] h2 = [b.hash, valHash(b.entry + off)]; // use XOR here, so that hash is independent of element order - h ^= hashOf(h2.ptr, h2.length * h2[0].sizeof); + h ^= hashOf(h2); } return h; } @@ -1001,3 +1001,15 @@ pure nothrow unittest assert(aa.length == 1); assert(aa[5] == 6); } + +// test AA as key (Issue 16974) +unittest +{ + int[int] a = [1 : 2], a2 = [1 : 2]; + + assert([a : 3] == [a : 3]); + assert([a : 3] == [a2 : 3]); + + assert(typeid(a).getHash(&a) == typeid(a).getHash(&a)); + assert(typeid(a).getHash(&a) == typeid(a).getHash(&a2)); +} From df2308b8d251602f22e615e88b94f4e4f19af9fa Mon Sep 17 00:00:00 2001 From: Martin Nowak Date: Sun, 4 Dec 2016 17:51:42 +0100 Subject: [PATCH 2/2] Merge pull request #1707 from WalterWaldron/fixHashOf [REG 2.072] Revert object.hashOf changes from "use array interface to hashOf()" --- src/object.d | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/src/object.d b/src/object.d index b5f265e9bf..99718cba41 100644 --- a/src/object.d +++ b/src/object.d @@ -3176,7 +3176,15 @@ struct Test size_t hashOf(T)(auto ref T arg, size_t seed = 0) { import core.internal.hash; - return core.internal.hash.hashOf((cast(void*)&arg)[0 .. T.sizeof], seed); + return core.internal.hash.hashOf(arg, seed); +} + +unittest +{ + // Issue # 16654 / 16764 + auto a = [1]; + auto b = a.dup; + assert(hashOf(a) == hashOf(b)); } bool _xopEquals(in void*, in void*)