Bug 13045 – TypeInfo.getHash should return consistent result with object equality by default

Status
RESOLVED
Resolution
FIXED
Severity
blocker
Priority
P1
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2014-07-04T16:29:00Z
Last change time
2014-07-06T01:13:53Z
Keywords
pull, wrong-code
Assigned to
nobody
Creator
k.hara.pg

Comments

Comment #0 by k.hara.pg — 2014-07-04T16:29:51Z
This test case should succeed to run, but doesn't. struct S { int[] a; } void main() { auto s1 = S([1,2]); auto s2 = S([1,2]); assert(s1 !is s2); assert(s1 == s2); assert(typeid(S).getHash(&s1) == typeid(S).getHash(&s2)); // -> assert should pass, but doesn't } And, TypeInfo.getHash should also support composed hash calculation. struct S { size_t toHash() const nothrow @safe { assert(0); // all getHash call should reach here } } struct T { S s; } void main() { import std.exception : assertThrown; S s; assertThrown!Error(typeid(S).getHash(&s)); // OK S[1] ssa; assertThrown!Error(typeid(S[1]).getHash(&ssa)); // OK S[] sda = [S(), S()]; assertThrown!Error(typeid(S[]).getHash(&sda)); // OK T t; assertThrown!Error(typeid(T).getHash(&t)); // should pass T[1] tsa; assertThrown!Error(typeid(T[1]).getHash(&tsa)); // should pass T[] tda = [T(), T()]; assertThrown!Error(typeid(T[]).getHash(&tda)); // should pass }
Comment #1 by k.hara.pg — 2014-07-04T17:16:40Z
Comment #2 by k.hara.pg — 2014-07-04T17:25:21Z
*** Issue 11025 has been marked as a duplicate of this issue. ***
Comment #3 by k.hara.pg — 2014-07-04T17:35:41Z
*** Issue 10525 has been marked as a duplicate of this issue. ***
Comment #4 by github-bugzilla — 2014-07-06T01:13:53Z
Commit pushed to master at https://github.com/D-Programming-Language/dmd https://github.com/D-Programming-Language/dmd/commit/a5c8c3afc84cad8ef62c119c4101acb8e6de3140 fix Issue 13045 - TypeInfo.getHash should return consistent result with object equality by default If struct member field has toHash method, or has non-bitwise equality, generate __xtoHash implicitly to guarantee `a != b || typeid(S).getHash(&a) == typeid(S).getHash(&b)`.