From 1d36201c8e246cfccea21d113e1317154ee04eb0 Mon Sep 17 00:00:00 2001 From: Nathan Sashihara <21227491+n8sh@users.noreply.github.com> Date: Wed, 2 Mar 2022 18:35:04 -0500 Subject: [PATCH] Fix Issue 22136 - [REG 2.097.1] hashOf failed to compile because of different inheritance order Replacement for https://github.com/dlang/dmd/pull/13404 --- src/core/internal/hash.d | 10 ++++++++-- test/hash/src/test_hash.d | 16 ++++++++++++++++ 2 files changed, 24 insertions(+), 2 deletions(-) diff --git a/src/core/internal/hash.d b/src/core/internal/hash.d index ef9f1e525c..568f78eaa8 100644 --- a/src/core/internal/hash.d +++ b/src/core/internal/hash.d @@ -608,7 +608,10 @@ if (!is(T == enum) && (is(T == interface) || is(T == class)) static if (__traits(compiles, {size_t h = val.toHash();})) { static if (is(__traits(parent, val.toHash) P) && !is(immutable T* : immutable P*) - && is(typeof((ref P p) => p is null))) + && is(typeof((ref P p) => p is null)) + // https://issues.dlang.org/show_bug.cgi?id=22136 + && __traits(getAliasThis, T).length + && is(P == typeof(__traits(getMember, val, __traits(getAliasThis, T))))) return val ? hashOf(__traits(getMember, val, __traits(getAliasThis, T))) : 0; else return val ? val.toHash() : 0; @@ -625,7 +628,10 @@ if (!is(T == enum) && (is(T == interface) || is(T == class)) static if (__traits(compiles, {size_t h = val.toHash();})) { static if (is(__traits(parent, val.toHash) P) && !is(immutable T* : immutable P*) - && is(typeof((ref P p) => p is null))) + && is(typeof((ref P p) => p is null)) + // https://issues.dlang.org/show_bug.cgi?id=22136 + && __traits(getAliasThis, T).length + && is(P == typeof(__traits(getMember, val, __traits(getAliasThis, T))))) return hashOf(val ? hashOf(__traits(getMember, val, __traits(getAliasThis, T))) : size_t(0), seed); else diff --git a/test/hash/src/test_hash.d b/test/hash/src/test_hash.d index b2f1ca0efe..b347db392d 100644 --- a/test/hash/src/test_hash.d +++ b/test/hash/src/test_hash.d @@ -16,6 +16,7 @@ void main() issue21642(); issue22024(); issue22076(); + issue22136(); testTypeInfoArrayGetHash1(); testTypeInfoArrayGetHash2(); pr2243(); @@ -312,6 +313,21 @@ void issue22076() cast(void) hashOf(c1, 0); } +void issue22136() +{ + interface IObject { + size_t toHash() @trusted nothrow; + } + interface Dummy {} + interface Bug : Dummy, IObject {} + interface Ok : IObject, Dummy {} + + Bug bug; + Ok ok; + assert(hashOf(bug) == hashOf(Object.init)); // Regression: failed to compile. + assert(hashOf(ok) == hashOf(Object.init)); +} + /// Tests ensure TypeInfo_Array.getHash uses element hash functions instead /// of hashing array data. void testTypeInfoArrayGetHash1()