diff --git a/src/coreclr/vm/method.cpp b/src/coreclr/vm/method.cpp index 3d27905122ce01..df61e7b785a331 100644 --- a/src/coreclr/vm/method.cpp +++ b/src/coreclr/vm/method.cpp @@ -2273,10 +2273,12 @@ bool IsTypeDefOrRefImplementedInSystemModule(Module* pModule, mdToken tk) mdToken tkTypeDef; Module* pModuleOfTypeDef; - ClassLoader::ResolveTokenToTypeDefThrowing(pModule, tk, &pModuleOfTypeDef, &tkTypeDef); - if (pModuleOfTypeDef->IsSystem()) + if (ClassLoader::ResolveTokenToTypeDefThrowing(pModule, tk, &pModuleOfTypeDef, &tkTypeDef)) { - return true; + if (pModuleOfTypeDef->IsSystem()) + { + return true; + } } } diff --git a/src/coreclr/vm/methodtablebuilder.cpp b/src/coreclr/vm/methodtablebuilder.cpp index 1e8b5c87893c2c..075b944d531715 100644 --- a/src/coreclr/vm/methodtablebuilder.cpp +++ b/src/coreclr/vm/methodtablebuilder.cpp @@ -3873,7 +3873,12 @@ CorElementType MethodTableBuilder::GetCorElementTypeOfTypeDefOrRefForStaticField Module *pModuleOfTypeDef; mdTypeDef tkTypeDef; - ClassLoader::ResolveTokenToTypeDefThrowing(module, typeDefOrRef, &pModuleOfTypeDef, &tkTypeDef); + if (!ClassLoader::ResolveTokenToTypeDefThrowing(module, typeDefOrRef, &pModuleOfTypeDef, &tkTypeDef)) + { + // Returning ELEMENT_TYPE_VALUETYPE will cause the type to be fully resolved and proper type load exception + // to be thrown later in MethodTable::DoFullyLoad + return ELEMENT_TYPE_VALUETYPE; + } // First check to see if the type is byref-like if (pModuleOfTypeDef->GetCustomAttribute(tkTypeDef, @@ -3908,11 +3913,12 @@ CorElementType MethodTableBuilder::GetCorElementTypeOfTypeDefOrRefForStaticField Module *pModuleOfSystemEnumType; mdTypeDef tkTypeDefOfSystemEnumType; - ClassLoader::ResolveTokenToTypeDefThrowing(pModuleOfTypeDef, tkTypeDefExtends, &pModuleOfSystemEnumType, &tkTypeDefOfSystemEnumType); - - if (pModuleOfSystemEnumType != NULL && pModuleOfSystemEnumType->IsSystem()) + if (ClassLoader::ResolveTokenToTypeDefThrowing(pModuleOfTypeDef, tkTypeDefExtends, &pModuleOfSystemEnumType, &tkTypeDefOfSystemEnumType)) { - thisIsAnEnum = true; + if (pModuleOfSystemEnumType != NULL && pModuleOfSystemEnumType->IsSystem()) + { + thisIsAnEnum = true; + } } } } diff --git a/src/tests/Regressions/coreclr/GitHub_121791/test121791.il b/src/tests/Regressions/coreclr/GitHub_121791/test121791.il new file mode 100644 index 00000000000000..a84e4b77d9439d --- /dev/null +++ b/src/tests/Regressions/coreclr/GitHub_121791/test121791.il @@ -0,0 +1,56 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. + +.assembly extern System.Runtime { .publickeytoken = (B0 3F 5F 7F 11 D5 0A 3A ) } +.assembly extern xunit.core {} + +.assembly test121791{} + +.class public auto ansi test121791 + extends [System.Runtime]System.Object +{ + .method public hidebysig static int32 + Main() cil managed + { + .custom instance void [xunit.core]Xunit.FactAttribute::.ctor() = ( + 01 00 00 00 + ) + .entrypoint + + .maxstack 1 + .try + { + call object test121791::M() + pop + leave.s Failure + } + catch [System.Runtime]System.Object + { + pop + leave.s Success + } + + Success: + ldc.i4 100 + ret + + Failure: + ldc.i4 10 + ret + } + + .method private hidebysig static object + M() cil managed noinlining + { + .maxstack 8 + IL_0000: ldsfld valuetype [System.Runtime]MissingType FieldOfMissingTypeInExistingAssembly::f + IL_0005: box [System.Runtime]MissingType + IL_000a: ret + } +} + +.class private auto ansi beforefieldinit FieldOfMissingTypeInExistingAssembly + extends [System.Runtime]System.Object +{ + .field public static valuetype [System.Runtime]MissingType f +} diff --git a/src/tests/Regressions/coreclr/GitHub_121791/test121791.ilproj b/src/tests/Regressions/coreclr/GitHub_121791/test121791.ilproj new file mode 100644 index 00000000000000..e8bc2779377048 --- /dev/null +++ b/src/tests/Regressions/coreclr/GitHub_121791/test121791.ilproj @@ -0,0 +1,8 @@ + + + 1 + + + + +