Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -1228,10 +1228,9 @@ internal static unsafe bool AreTypesAssignableInternalUncached(MethodTable* pSou
}

//
// Update the cache. We only consider type-based conversion rules here.
// Therefore a negative result cannot rule out convertibility for IDynamicInterfaceCastable.
// Update the cache
//
if (retObj != null || !(pTargetType->IsInterface && pSourceType->IsIDynamicInterfaceCastable))
if (!pSourceType->IsIDynamicInterfaceCastable || !pTargetType->IsInterface)
{
nuint sourceAndVariation = (nuint)pSourceType + (uint)AssignmentVariation.BoxedSource;
s_castCache.TrySet(sourceAndVariation, (nuint)pTargetType, retObj != null);
Expand Down Expand Up @@ -1270,8 +1269,11 @@ private static unsafe object CheckCastAny_NoCacheLookup(MethodTable* pTargetType
//
// Update the cache
//
nuint sourceAndVariation = (nuint)pSourceType + (uint)AssignmentVariation.BoxedSource;
s_castCache.TrySet(sourceAndVariation, (nuint)pTargetType, true);
if (!pSourceType->IsIDynamicInterfaceCastable || !pTargetType->IsInterface)
{
nuint sourceAndVariation = (nuint)pSourceType + (uint)AssignmentVariation.BoxedSource;
s_castCache.TrySet(sourceAndVariation, (nuint)pTargetType, true);
}

return obj;
}
Expand Down
27 changes: 27 additions & 0 deletions src/tests/nativeaot/SmokeTests/UnitTests/Interfaces.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ public static int Run()
TestDynamicStaticGenericVirtualMethods.Run();
TestRuntime109496Regression.Run();
TestRuntime113664Regression.Run();
TestRuntime125577Regression.Run();

return Pass;
}
Expand Down Expand Up @@ -2046,4 +2047,30 @@ static string Frob<T, U, V>() where T : class, IFoo<U> where U : class where V :
return T.Frob<V>();
}
}

class TestRuntime125577Regression
{
class Shapeshifter : IDynamicInterfaceCastable
{
bool _result;
public Shapeshifter(bool result) => _result = result;

public RuntimeTypeHandle GetInterfaceImplementation(RuntimeTypeHandle interfaceType) => throw new NotImplementedException();
public bool IsInterfaceImplemented(RuntimeTypeHandle interfaceType, bool throwIfNotImplemented) => _result;
}

[MethodImpl(MethodImplOptions.NoInlining)]
static bool Is(object o, bool expected) => o is IEnumerable<object> == expected;

public static void Run()
{
// Call multiple times in case we just flushed the cast cache (when we flush we don't store).
if (!Is(new Shapeshifter(true), true)
|| !Is(new Shapeshifter(false), false)
|| !Is(new Shapeshifter(true), true)
|| !Is(new Shapeshifter(false), false))
throw new Exception();
}
}

}
Loading