diff --git a/src/coreclr/tools/Common/JitInterface/CorInfoImpl.cs b/src/coreclr/tools/Common/JitInterface/CorInfoImpl.cs index d21377ed7d80da..e3dea4bbdc585a 100644 --- a/src/coreclr/tools/Common/JitInterface/CorInfoImpl.cs +++ b/src/coreclr/tools/Common/JitInterface/CorInfoImpl.cs @@ -1546,7 +1546,7 @@ static CORINFO_RESOLVED_TOKEN CreateResolvedTokenFromMethod(CorInfoImpl jitInter { method = method.GetTargetOfAsyncVariant(); } - else if (method.Signature.ReturnsTaskOrValueTask()) + else if (HasCallableAsyncVariant(method)) { method = method.GetAsyncVariant(); } @@ -1874,15 +1874,7 @@ private void resolveToken(ref CORINFO_RESOLVED_TOKEN pResolvedToken) if (pResolvedToken.tokenType is CorInfoTokenKind.CORINFO_TOKENKIND_Await) { - // in rare cases a method that returns Task is not actually TaskReturning (i.e. returns T). - // we cannot resolve to an Async variant in such case. - // return NULL, so that caller would re-resolve as a regular method call - bool allowAsyncVariant = method.GetTypicalMethodDefinition().Signature.ReturnsTaskOrValueTask(); - - // Don't get async variant of Delegate.Invoke method; the pointed to method is not an async variant either. - allowAsyncVariant = allowAsyncVariant && !method.OwningType.IsDelegate; - - method = allowAsyncVariant + method = HasCallableAsyncVariant(method) ? _compilation.TypeSystemContext.GetAsyncVariantMethod(method) : null; } @@ -1955,6 +1947,33 @@ private void resolveToken(ref CORINFO_RESOLVED_TOKEN pResolvedToken) pResolvedToken.cbMethodSpec = 0; } + private static bool HasCallableAsyncVariant(MethodDesc method) + { + // In some cases a method that returns Task is not actually + // TaskReturning (i.e. it might return T). We cannot resolve to an + // async variant in such case. + if (!method.GetTypicalMethodDefinition().Signature.ReturnsTaskOrValueTask()) + { + return false; + } + + // Don't get async variant of Delegate.Invoke method; the pointed + // to method is not an async variant either. + if (method.OwningType.IsDelegate) + { + return false; + } + + // Don't get async variant of ComImport methods since we do not + // generate any runtime async entry points for them. + if (method.OwningType.IsComImport) + { + return false; + } + + return true; + } + private void findSig(CORINFO_MODULE_STRUCT_* module, uint sigTOK, CORINFO_CONTEXT_STRUCT* context, CORINFO_SIG_INFO* sig) { var methodIL = HandleToObject(module);