Skip to content

Commit

Permalink
Fix load exception on generic covariant return type (#54790)
Browse files Browse the repository at this point in the history
  • Loading branch information
wzchua committed Jun 28, 2021
1 parent af18e93 commit 291e284
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 3 deletions.
7 changes: 4 additions & 3 deletions src/coreclr/vm/class.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1170,12 +1170,13 @@ void ClassLoader::ValidateMethodsWithCovariantReturnTypes(MethodTable* pMT)
continue;

// Locate the MethodTable defining the pParentMD.
while (pParentMT->GetCanonicalMethodTable() != pParentMD->GetMethodTable())
MethodTable* pDefinitionParentMT = pParentMT;
while (pDefinitionParentMT->GetCanonicalMethodTable() != pParentMD->GetMethodTable())
{
pParentMT = pParentMT->GetParentMethodTable();
pDefinitionParentMT = pDefinitionParentMT->GetParentMethodTable();
}

SigTypeContext context1(pParentMT->GetInstantiation(), pMD->GetMethodInstantiation());
SigTypeContext context1(pDefinitionParentMT->GetInstantiation(), pMD->GetMethodInstantiation());
MetaSig methodSig1(pParentMD);
TypeHandle hType1 = methodSig1.GetReturnProps().GetTypeHandleThrowing(pParentMD->GetModule(), &context1, ClassLoader::LoadTypesFlag::LoadTypes, CLASS_LOAD_EXACTPARENTS);

Expand Down
32 changes: 32 additions & 0 deletions src/tests/Regressions/coreclr/GitHub_54719/test54719.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System;

public class IA { }
public class IB { }

public abstract class Base
{
public abstract IA Key { get; }
public abstract IB Value { get; }
}
public sealed class Derived : Base<IB>
{
public class A : IA { }
public sealed override A Key => default;
}
public abstract class Base<B> : Base where B : IB
{
public sealed override B Value => null;
}

class Program
{
static int Main()
{
new Derived();

return 100;
}
}
10 changes: 10 additions & 0 deletions src/tests/Regressions/coreclr/GitHub_54719/test54719.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<CLRTestKind>BuildAndRun</CLRTestKind>
<CLRTestPriority>1</CLRTestPriority>
</PropertyGroup>
<ItemGroup>
<Compile Include="test54719.cs" />
</ItemGroup>
</Project>

0 comments on commit 291e284

Please sign in to comment.