Sample code: ---- import std.stdio; class Foo{ hash_t toHash(){ writeln("Foo.toHash"); return 0; } } void main(){ Foo[] arr = [new Foo(), new Foo()]; typeid(arr).getHash(&arr); // doesn't call Foo.toHash() } ---- The typeid(arr) returns instance of TypeInfo_Array, but its getHash is not call Object.toHash of each element. The internal TypeInfo class rt.typeinfo.ti_AC.TypeInfo_AC supports it, but this is only used in array/associative-array operations.
Patches: https://github.com/9rnsr/dmd/compare/master...aa_hash_fix https://github.com/9rnsr/druntime/compare/master...aa_hash_fix Changes: - TypeInfo_AC is derived from TypeInfo_Array. - Class array's TypeInfo is instance of TypeInfo_AC.
Sorry, this patch has disabling optimizing problem. I'll fix it.
Changed. - Removing getInternalTypeInfo is too strict. - Changed internal TypeInfo of class array -> typeid(Object[]) - TypeInfo_AC is only used for TypeInfo class of typeid(user class array).
Are these patches still valid? Maybe worth turning them into pull requests.
Simplified original testcase: --- class S { int i; this(in int i) { this.i = i; } override hash_t toHash() { return 0; } } void main() { S[] a1 = [new S(11)], a2 = [new S(12)]; assert(typeid(S[]).getHash(&a1) == typeid(S[]).getHash(&a2)); // fails } --- With structs: --- struct S { int i; hash_t toHash() const @safe nothrow { return 0; } } void main() { S[] a1 = [S(11)], a2 = [S(12)]; assert(typeid(S[]).getHash(&a1) != typeid(S[]).getHash(&a2)); // fails } --- Associative arrays are affected too because of this: --- struct S { int i; hash_t toHash() const @safe nothrow { return 0; } bool opEquals(ref const S el) const { return true; } int opCmp(ref const S el) const { return 0; } } void main() { int[S[]] aa = [[S(11)] : 13]; assert([S(12)] in aa); // fails } ---
Will fix soon.
(In reply to comment #5) > ... > With structs: > ... > assert(typeid(S[]).getHash(&a1) != typeid(S[]).getHash(&a2)); // fails > ... Of course `==` was meant. A fixing pull: https://github.com/D-Programming-Language/druntime/pull/735
Commits pushed to master at https://github.com/D-Programming-Language/druntime https://github.com/D-Programming-Language/druntime/commit/1a3f6796270752ba0d770539392f2815f57e8181 Fix Issue 5835 - `TypeInfo_Array.getHash` creates raw data hash instead using array element hash function Issue URL: https://d.puremagic.com/issues/show_bug.cgi?id=5835 https://github.com/D-Programming-Language/druntime/commit/dc3e69a24622331722978158cfef3df10204dcf9 Merge pull request #735 from denis-sh/fix-Issue-5835 Fix Issue 5835