[release/8.0] Fix Type.ContainsGenericParameters for function pointers (#90963)
authorgithub-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
Wed, 23 Aug 2023 21:44:09 +0000 (14:44 -0700)
committerGitHub <noreply@github.com>
Wed, 23 Aug 2023 21:44:09 +0000 (14:44 -0700)
* Fix Type.ContainsGenericParameters for function pointers

Fixes #84916

* Fix System.Reflection.MetadataLoadContext

* Fix Mono

* Update test

---------

Co-authored-by: Jan Kotas <jkotas@microsoft.com>
src/coreclr/vm/typedesc.cpp
src/coreclr/vm/typedesc.h
src/coreclr/vm/typehandle.cpp
src/libraries/Common/tests/System/FunctionPointerTests.cs
src/libraries/System.Reflection.MetadataLoadContext/src/System/Reflection/TypeLoading/Types/RoFunctionPointerType.cs
src/mono/System.Private.CoreLib/src/System/RuntimeType.Mono.cs

index 6c32268..95e86cc 100644 (file)
@@ -104,6 +104,31 @@ BOOL TypeDesc::IsSharedByGenericInstantiations()
     return FALSE;
 }
 
+BOOL TypeDesc::ContainsGenericVariables(BOOL methodOnly)
+{
+    if (IsGenericVariable())
+    {
+        if (!methodOnly)
+            return TRUE;
+
+        PTR_TypeVarTypeDesc pTyVar = dac_cast<PTR_TypeVarTypeDesc>(this);
+        return TypeFromToken(pTyVar->GetTypeOrMethodDef()) == mdtMethodDef;
+    }
+
+    if (HasTypeParam())
+    {
+        return GetRootTypeParam().ContainsGenericVariables(methodOnly);
+    }
+
+    if (IsFnPtr())
+    {
+        return dac_cast<PTR_FnPtrTypeDesc>(this)->ContainsGenericVariables(methodOnly);
+    }
+
+    return FALSE;
+}
+
+
 PTR_BaseDomain TypeDesc::GetDomain()
 {
     CONTRACTL
@@ -1670,6 +1695,21 @@ FnPtrTypeDesc::IsSharedByGenericInstantiations()
     return FALSE;
 } // FnPtrTypeDesc::IsSharedByGenericInstantiations
 
+BOOL
+FnPtrTypeDesc::ContainsGenericVariables(BOOL methodOnly)
+{
+    LIMITED_METHOD_DAC_CONTRACT;
+
+    for (DWORD i = 0; i <= m_NumArgs; i++)
+    {
+        if (m_RetAndArgTypes[i].ContainsGenericVariables(methodOnly))
+        {
+            return TRUE;
+        }
+    }
+    return FALSE;
+} // FnPtrTypeDesc::ContainsGenericVariables
+
 #ifndef DACCESS_COMPILE
 
 // Returns TRUE if all return and argument types are externally visible.
index 51614c3..b86845c 100644 (file)
@@ -182,6 +182,8 @@ public:
 
     BOOL IsSharedByGenericInstantiations();
 
+    BOOL ContainsGenericVariables(BOOL methodOnly);
+
  protected:
     // See methodtable.h for details of the flags with the same name there
     enum
@@ -527,6 +529,8 @@ public:
 
     BOOL IsSharedByGenericInstantiations();
 
+    BOOL ContainsGenericVariables(BOOL methodOnly);
+
 #ifndef DACCESS_COMPILE
     // Returns TRUE if all return and argument types are externally visible.
     BOOL IsExternallyVisible() const;
index 59eed8b..053cc75 100644 (file)
@@ -138,26 +138,10 @@ BOOL TypeHandle::ContainsGenericVariables(BOOL methodOnly /*=FALSE*/) const
     STATIC_CONTRACT_NOTHROW;
     SUPPORTS_DAC;
 
-    if (HasTypeParam())
-    {
-        return GetTypeParam().ContainsGenericVariables(methodOnly);
-    }
-
-    if (IsGenericVariable())
-    {
-        if (!methodOnly)
-            return TRUE;
-
-        PTR_TypeVarTypeDesc pTyVar = dac_cast<PTR_TypeVarTypeDesc>(AsTypeDesc());
-        return TypeFromToken(pTyVar->GetTypeOrMethodDef()) == mdtMethodDef;
-    }
-    else if (HasInstantiation())
-    {
-        if (GetMethodTable()->ContainsGenericVariables(methodOnly))
-            return TRUE;
-    }
-
-    return FALSE;
+    if (IsTypeDesc())
+        return AsTypeDesc()->ContainsGenericVariables(methodOnly);
+    else
+        return AsMethodTable()->ContainsGenericVariables(methodOnly);
 }
 
 //@GENERICS:
index 925c868..8a344e0 100644 (file)
@@ -173,6 +173,22 @@ namespace System.Tests.Types
             Assert.Equal(typeof(Runtime.InteropServices.OutAttribute).Project(), parameters[1].GetRequiredCustomModifiers()[0]);
         }
 
+        [Fact]
+        public static unsafe void GenericFunctionPointer()
+        {
+            Type t = typeof(FunctionPointerHolder).Project();
+
+            MethodInfo m1 = t.GetMethod(nameof(FunctionPointerHolder.GenericReturnValue), Bindings);
+            Type fcnPtr1 = m1.ReturnType;
+            Assert.True(fcnPtr1.IsFunctionPointer);
+            Assert.True(fcnPtr1.ContainsGenericParameters);
+
+            MethodInfo m2 = t.GetMethod(nameof(FunctionPointerHolder.GenericArgument), Bindings);
+            Type fcnPtr2 = m2.GetParameters()[1].ParameterType;
+            Assert.True(fcnPtr2.IsFunctionPointer);
+            Assert.True(fcnPtr2.ContainsGenericParameters);
+        }
+
         [Theory]
         [InlineData(nameof(FunctionPointerHolder.MethodReturnValue1),
             "MethodReturnValue1()",
@@ -278,6 +294,9 @@ namespace System.Tests.Types
             public delegate* unmanaged[Stdcall, MemberFunction]<string, ref bool*, MyClass, in MyStruct, double> SeveralArguments() => default;
             public delegate*<in int, out int, void> RequiredModifiers() => default;
 
+            public delegate*<T> GenericReturnValue<T>() => default;
+            public bool GenericArgument<T>(int x, delegate*<T[], void> fptr) => default;
+
             public class MyClass { }
             public struct MyStruct { }
         }
index 73cd874..426ab70 100644 (file)
@@ -147,7 +147,23 @@ namespace System.Reflection.TypeLoading
         public sealed override bool IsGenericParameter => false;
         public sealed override bool IsGenericTypeParameter => false;
         public sealed override bool IsGenericMethodParameter => false;
-        public sealed override bool ContainsGenericParameters => IsGenericTypeDefinition;
+
+        public sealed override bool ContainsGenericParameters
+        {
+            get
+            {
+                if (_returnType.ContainsGenericParameters)
+                    return true;
+
+                foreach (Type parameterType in _parameterTypes)
+                {
+                    if (parameterType.ContainsGenericParameters)
+                        return true;
+                }
+
+                return false;
+            }
+        }
 
         protected sealed override TypeCode GetTypeCodeImpl() => TypeCode.Object;
 
index e7feb14..5924d5c 100644 (file)
@@ -2048,6 +2048,16 @@ namespace System
                 if (HasElementType)
                     return GetElementType().ContainsGenericParameters;
 
+                if (IsFunctionPointer)
+                {
+                    if (GetFunctionPointerReturnType().ContainsGenericParameters)
+                        return true;
+
+                    foreach (Type arg in GetFunctionPointerParameterTypes())
+                        if (arg.ContainsGenericParameters)
+                            return true;
+                }
+
                 return false;
             }
         }