From 291e28426c8810aa2e0b3952a4f824371099c4c4 Mon Sep 17 00:00:00 2001 From: Wei Zheng <13881045+wzchua@users.noreply.github.com> Date: Tue, 29 Jun 2021 01:51:46 +0800 Subject: [PATCH] Fix load exception on generic covariant return type (#54790) --- src/coreclr/vm/class.cpp | 7 ++-- .../coreclr/GitHub_54719/test54719.cs | 32 +++++++++++++++++++ .../coreclr/GitHub_54719/test54719.csproj | 10 ++++++ 3 files changed, 46 insertions(+), 3 deletions(-) create mode 100644 src/tests/Regressions/coreclr/GitHub_54719/test54719.cs create mode 100644 src/tests/Regressions/coreclr/GitHub_54719/test54719.csproj diff --git a/src/coreclr/vm/class.cpp b/src/coreclr/vm/class.cpp index 6f2da9db3c29d..7fdf25133c12d 100644 --- a/src/coreclr/vm/class.cpp +++ b/src/coreclr/vm/class.cpp @@ -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); diff --git a/src/tests/Regressions/coreclr/GitHub_54719/test54719.cs b/src/tests/Regressions/coreclr/GitHub_54719/test54719.cs new file mode 100644 index 0000000000000..64e60e5980085 --- /dev/null +++ b/src/tests/Regressions/coreclr/GitHub_54719/test54719.cs @@ -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 +{ + public class A : IA { } + public sealed override A Key => default; +} +public abstract class Base : Base where B : IB +{ + public sealed override B Value => null; +} + +class Program +{ + static int Main() + { + new Derived(); + + return 100; + } +} diff --git a/src/tests/Regressions/coreclr/GitHub_54719/test54719.csproj b/src/tests/Regressions/coreclr/GitHub_54719/test54719.csproj new file mode 100644 index 0000000000000..543d7356bbbea --- /dev/null +++ b/src/tests/Regressions/coreclr/GitHub_54719/test54719.csproj @@ -0,0 +1,10 @@ + + + Exe + BuildAndRun + 1 + + + + +