Annotate more Mono reflection for trimming (#38431)
authorMichal Strehovský <MichalStrehovsky@users.noreply.github.com>
Fri, 3 Jul 2020 13:10:59 +0000 (15:10 +0200)
committerGitHub <noreply@github.com>
Fri, 3 Jul 2020 13:10:59 +0000 (15:10 +0200)
src/mono/netcore/System.Private.CoreLib/src/System/Reflection/Emit/ConstructorBuilder.Mono.cs
src/mono/netcore/System.Private.CoreLib/src/System/Reflection/Emit/ConstructorOnTypeBuilderInst.cs
src/mono/netcore/System.Private.CoreLib/src/System/Reflection/Emit/CustomAttributeBuilder.Mono.cs
src/mono/netcore/System.Private.CoreLib/src/System/Reflection/Emit/EnumBuilder.Mono.cs
src/mono/netcore/System.Private.CoreLib/src/System/Reflection/Emit/MethodOnTypeBuilderInst.cs
src/mono/netcore/System.Private.CoreLib/src/System/Reflection/Emit/ModuleBuilder.Mono.cs
src/mono/netcore/System.Private.CoreLib/src/System/Reflection/Emit/TypeBuilder.Mono.cs
src/mono/netcore/System.Private.CoreLib/src/System/Reflection/Emit/TypeBuilderInstantiation.cs
src/mono/netcore/System.Private.CoreLib/src/System/Reflection/RuntimeAssembly.cs
src/mono/netcore/System.Private.CoreLib/src/System/Reflection/RuntimePropertyInfo.cs
src/mono/netcore/System.Private.CoreLib/src/System/RuntimeType.Mono.cs

index 53faf4a..879a987 100644 (file)
@@ -32,6 +32,7 @@
 
 #if MONO_FEATURE_SRE
 using System.Globalization;
+using System.Diagnostics.CodeAnalysis;
 using System.Runtime.InteropServices;
 
 namespace System.Reflection.Emit
@@ -146,6 +147,8 @@ namespace System.Reflection.Emit
             return parameters![pos];
         }
 
+        [UnconditionalSuppressMessage("ReflectionAnalysis", "IL2006:UnrecognizedReflectionPattern",
+            Justification = "Linker doesn't analyze RuntimeResolve but it's an identity function")]
         internal MethodBase RuntimeResolve()
         {
             return type.RuntimeResolve().GetConstructor(this);
index 2608f0c..e2be93d 100644 (file)
@@ -28,6 +28,7 @@
 //
 
 #if MONO_FEATURE_SRE
+using System.Diagnostics.CodeAnalysis;
 using System.Globalization;
 using System.Runtime.InteropServices;
 
@@ -163,6 +164,8 @@ namespace System.Reflection.Emit
         }
 
         // Called from the runtime to return the corresponding finished ConstructorInfo object
+        [UnconditionalSuppressMessage("ReflectionAnalysis", "IL2006:UnrecognizedReflectionPattern",
+            Justification = "Reflection.Emit is not subject to trimming")]
         internal ConstructorInfo RuntimeResolve()
         {
             Type type = instantiation.InternalResolve();
index d5974a0..b3d6a6f 100644 (file)
@@ -31,6 +31,7 @@
 //
 
 #if MONO_FEATURE_SRE
+using System.Diagnostics.CodeAnalysis;
 using System.Runtime.CompilerServices;
 using System.Runtime.InteropServices;
 
@@ -317,6 +318,8 @@ namespace System.Reflection.Emit
             return decode_string(data, pos, out pos);
         }
 
+        [UnconditionalSuppressMessage("ReflectionAnalysis", "IL2006:UnrecognizedReflectionPattern",
+            Justification = "Types referenced from custom attributes are preserved")]
         internal static UnmanagedMarshal get_umarshal(CustomAttributeBuilder customBuilder, bool is_field)
         {
             byte[] data = customBuilder.Data;
@@ -493,6 +496,8 @@ namespace System.Reflection.Emit
             public object?[] namedParamValues;
         }
 
+        [UnconditionalSuppressMessage("ReflectionAnalysis", "IL2006:UnrecognizedReflectionPattern",
+            Justification = "Types referenced from custom attributes are preserved")]
         internal static CustomAttributeInfo decode_cattr(CustomAttributeBuilder customBuilder)
         {
             byte[] data = customBuilder.Data;
index 60811a6..ed9960a 100644 (file)
@@ -46,6 +46,8 @@ namespace System.Reflection.Emit
         private FieldBuilder _underlyingField;
         private Type _underlyingType;
 
+        [UnconditionalSuppressMessage("ReflectionAnalysis", "IL2006:UnrecognizedReflectionPattern",
+            Justification = "Reflection.Emit is not subject to trimming")]
         internal EnumBuilder(ModuleBuilder mb, string name, TypeAttributes visibility, Type underlyingType)
         {
             if ((visibility & ~TypeAttributes.VisibilityMask) != 0)
index aa85ae9..ee4d1cd 100644 (file)
@@ -30,6 +30,7 @@
 #if MONO_FEATURE_SRE
 using System.Globalization;
 using System.Text;
+using System.Diagnostics.CodeAnalysis;
 using System.Runtime.InteropServices;
 
 
@@ -73,6 +74,8 @@ namespace System.Reflection.Emit
                 this.generic_method_definition = method;
         }
 
+        [UnconditionalSuppressMessage("ReflectionAnalysis", "IL2026:RequiresUnreferencedCode",
+            Justification = "Reflection.Emit is not subject to trimming")]
         private static MethodInfo ExtractBaseMethod(MethodInfo info)
         {
             if (info is MethodBuilder)
index f334eec..e119567 100644 (file)
@@ -151,6 +151,8 @@ namespace System.Reflection.Emit
             return DefineDataImpl(name, size, attributes & ~FieldAttributes.ReservedMask);
         }
 
+        [UnconditionalSuppressMessage("ReflectionAnalysis", "IL2026:RequiresUnreferencedCode",
+            Justification = "Reflection.Emit is not subject to trimming")]
         private FieldBuilder DefineDataImpl(string name, int size, FieldAttributes attributes)
         {
             if (name == null)
@@ -288,6 +290,8 @@ namespace System.Reflection.Emit
             num_types++;
         }
 
+        [UnconditionalSuppressMessage("ReflectionAnalysis", "IL2006:UnrecognizedReflectionPattern",
+            Justification = "Reflection.Emit is not subject to trimming")]
         private TypeBuilder DefineType(string name, TypeAttributes attr, Type? parent, Type[]? interfaces, PackingSize packingSize, int typesize)
         {
             if (name == null)
index 79c17af..2703706 100644 (file)
@@ -101,6 +101,8 @@ namespace System.Reflection.Emit
             pmodule = mb;
         }
 
+        [UnconditionalSuppressMessage("ReflectionAnalysis", "IL2006:UnrecognizedReflectionPattern",
+            Justification = "Linker doesn't analyze ResolveUserType but it's an identity function")]
         internal TypeBuilder(ModuleBuilder mb, string fullname, TypeAttributes attr, [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)]Type? parent, Type[]? interfaces, PackingSize packing_size, int type_size, Type? nesting_type)
         {
             int sep_index;
@@ -777,11 +779,17 @@ namespace System.Reflection.Emit
             return false;
         }
 
+        // We require emitted types to have all members on their bases to be accessible.
+        // This is basically an identity function for `this`.
+        [return: DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)]
         public Type? CreateType()
         {
             return CreateTypeInfo();
         }
 
+        // We require emitted types to have all members on their bases to be accessible.
+        // This is basically an identity function for `this`.
+        [return: DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)]
         public
         TypeInfo? CreateTypeInfo()
         {
index 28097da..2f78472 100644 (file)
@@ -36,6 +36,7 @@ using System.Collections.Generic;
 using System.Globalization;
 using System.Text;
 using System.Runtime.InteropServices;
+using System.Diagnostics.CodeAnalysis;
 
 namespace System.Reflection.Emit
 {
@@ -69,6 +70,8 @@ namespace System.Reflection.Emit
             this.type_arguments = args;
         }
 
+        [UnconditionalSuppressMessage("ReflectionAnalysis", "IL2006:UnrecognizedReflectionPattern",
+            Justification = "Reflection.Emit is not subject to trimming")]
         internal override Type InternalResolve()
         {
             Type gtd = generic_type.InternalResolve();
index 1cde489..517af80 100644 (file)
@@ -24,6 +24,7 @@
 using System.IO;
 using System.Globalization;
 using System.Collections.Generic;
+using System.Diagnostics.CodeAnalysis;
 using System.Runtime.CompilerServices;
 using System.Runtime.InteropServices;
 using System.Runtime.Loader;
@@ -153,6 +154,7 @@ namespace System.Reflection
         [MethodImplAttribute(MethodImplOptions.InternalCall)]
         internal extern Type[] GetTopLevelForwardedTypes();
 
+        [RequiresUnreferencedCode("Types might be removed")]
         public override Type[] GetForwardedTypes()
         {
             Type[] topLevelTypes = GetTopLevelForwardedTypes();
@@ -172,6 +174,7 @@ namespace System.Reflection
             return forwardedTypes.ToArray();
         }
 
+        [RequiresUnreferencedCode("Types might be removed")]
         private static void AddPublicNestedTypes(Type type, List<Type> types, List<Exception> exceptions)
         {
             Type[] nestedTypes;
index 7bb7fbf..a00faee 100644 (file)
@@ -25,6 +25,7 @@
 
 using System.Collections.Generic;
 using System.Globalization;
+using System.Diagnostics.CodeAnalysis;
 using System.Runtime.CompilerServices;
 using System.Runtime.InteropServices;
 using System.Text;
@@ -366,6 +367,8 @@ namespace System.Reflection
          * The idea behing this optimization is to use a pair of delegates to simulate the same effect of doing a reflection call.
          * The first delegate cast the this argument to the right type and the second does points to the target method.
          */
+        [UnconditionalSuppressMessage("ReflectionAnalysis", "IL2006:UnrecognizedReflectionPattern",
+            Justification = "MethodInfo used with MakeGenericMethod doesn't have DynamicallyAccessedMembers generic parameters")]
         private static GetterAdapter CreateGetterDelegate(MethodInfo method)
         {
             Type[] typeVector;
index ca347c8..6f8f2d2 100644 (file)
@@ -1885,6 +1885,8 @@ namespace System
             return GetCorrespondingInflatedConstructor(fromNoninstanciated);
         }
 
+        [UnconditionalSuppressMessage("ReflectionAnalysis", "IL2006:UnrecognizedReflectionPattern",
+            Justification = "We already have a FieldInfo so this will succeed")]
         internal override FieldInfo GetField(FieldInfo fromNoninstanciated)
         {
             /* create sensible flags from given FieldInfo */