未验证 提交 665a0b11 编写于 作者: M Michal Strehovský 提交者: GitHub

Don't produce unnecessary default interface methods (#67409)

Found this because it was asserting in one of the Pri-0 tests. We were trying to generate a special instantiating stub for an interface type that was not a dynamic interface castable implementation. I'm adding a regression test too because it has a visible outside behavior besides the assert.
上级 9b2e2a83
......@@ -315,8 +315,16 @@ public sealed override IEnumerable<CombinedDependencyListEntry> GetConditionalSt
DefType defType = _type.GetClosestDefType();
// Interfaces don't have vtables and we don't need to track their slot use.
// The only exception are those interfaces that provide IDynamicInterfaceCastable implementations;
// those have slots and we dispatch on them.
bool needsDependenciesForVirtualMethodImpls = !defType.IsInterface
|| ((MetadataType)defType).IsDynamicInterfaceCastableImplementation();
// If we're producing a full vtable, none of the dependencies are conditional.
if (!factory.VTable(defType).HasFixedSlots)
needsDependenciesForVirtualMethodImpls &= !factory.VTable(defType).HasFixedSlots;
if (needsDependenciesForVirtualMethodImpls)
{
foreach (MethodDesc decl in defType.EnumAllVirtualSlots())
{
......
......@@ -16,6 +16,7 @@ static int Main()
TestAbstractTypeNeverDerivedVirtualsOptimization.Run();
TestAbstractNeverDerivedWithDevirtualizedCall.Run();
TestAbstractDerivedByUnrelatedTypeWithDevirtualizedCall.Run();
TestUnusedDefaultInterfaceMethod.Run();
return 100;
}
......@@ -187,6 +188,38 @@ public static void Run()
}
}
class TestUnusedDefaultInterfaceMethod
{
interface IFoo<T>
{
void DoSomething();
}
interface IBar<T> : IFoo<T>
{
void IFoo<T>.DoSomething()
{
Activator.CreateInstance(typeof(NeverReferenced));
}
}
class NeverReferenced { }
class SomeInstance : IBar<object>
{
void IFoo<object>.DoSomething() { }
}
static IFoo<object> s_instance = new SomeInstance();
public static void Run()
{
s_instance.DoSomething();
ThrowIfPresent(typeof(TestUnusedDefaultInterfaceMethod), nameof(NeverReferenced));
}
}
[UnconditionalSuppressMessage("ReflectionAnalysis", "IL2070:UnrecognizedReflectionPattern",
Justification = "That's the point")]
private static bool IsTypePresent(Type testType, string typeName) => testType.GetNestedType(typeName, BindingFlags.NonPublic | BindingFlags.Public) != null;
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册