Annotate and adjust more of CoreLib for trimming (#38865)
authorMichal Strehovský <MichalStrehovsky@users.noreply.github.com>
Sat, 18 Jul 2020 17:07:43 +0000 (19:07 +0200)
committerGitHub <noreply@github.com>
Sat, 18 Jul 2020 17:07:43 +0000 (10:07 -0700)
* Annotate and adjust more of CoreLib for trimming

* Update CustomAttribute.cs

* Update TypeBuilder.Mono.cs

src/libraries/System.Private.CoreLib/src/System/Reflection/SignatureTypeExtensions.cs
src/libraries/System.Private.CoreLib/src/System/Reflection/TypeInfo.cs
src/libraries/System.Private.CoreLib/src/System/Resources/ResourceManager.cs
src/libraries/System.Private.CoreLib/src/System/Resources/ResourceSet.cs
src/mono/netcore/System.Private.CoreLib/src/System/Reflection/CustomAttribute.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

index f79b42ea5fd05eade807cf52115da68b9069479e..06b391696060e8c396ae8e2bc8bab423d59eb2d2 100644 (file)
@@ -1,6 +1,8 @@
 // Licensed to the .NET Foundation under one or more agreements.
 // The .NET Foundation licenses this file to you under the MIT license.
 
+using System.Diagnostics.CodeAnalysis;
+
 namespace System.Reflection
 {
 #if CORERT
@@ -108,6 +110,8 @@ namespace System.Reflection
             return signatureType.TryResolve(genericMethod.GetGenericArguments());
         }
 
+        [UnconditionalSuppressMessage("ReflectionAnalysis", "IL2006:UnrecognizedReflectionPattern",
+            Justification = "Used to find matching method overloads. Only used for assignability checks.")]
         private static Type? TryResolve(this SignatureType signatureType, Type[] genericMethodParameters)
         {
             if (signatureType.IsSZArray)
index 5786aa4d9d02cc9ae934aa0279312fa9617f3495..3487f2aab00c4cd87c247d5abf9d4712bc1e5a4f 100644 (file)
@@ -33,11 +33,16 @@ namespace System.Reflection
         [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicMethods | DynamicallyAccessedMemberTypes.NonPublicMethods)]
         public virtual IEnumerable<MethodInfo> GetDeclaredMethods(string name)
         {
-            foreach (MethodInfo method in GetMethods(TypeInfo.DeclaredOnlyLookup))
+            foreach (MethodInfo method in GetDeclaredOnlyMethods(this))
             {
                 if (method.Name == name)
                     yield return method;
             }
+
+            [UnconditionalSuppressMessage("ReflectionAnalysis", "IL2006:UnrecognizedReflectionPattern",
+                Justification = "The yield return state machine doesn't propagate annotations")]
+            static MethodInfo[] GetDeclaredOnlyMethods(
+                Type type) => type.GetMethods(TypeInfo.DeclaredOnlyLookup);
         }
 
         public virtual IEnumerable<ConstructorInfo> DeclaredConstructors
@@ -75,10 +80,15 @@ namespace System.Reflection
             [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicNestedTypes | DynamicallyAccessedMemberTypes.NonPublicNestedTypes)]
             get
             {
-                foreach (Type t in GetNestedTypes(TypeInfo.DeclaredOnlyLookup))
+                foreach (Type t in GetDeclaredOnlyNestedTypes(this))
                 {
                     yield return t.GetTypeInfo();
                 }
+
+                [UnconditionalSuppressMessage("ReflectionAnalysis", "IL2006:UnrecognizedReflectionPattern",
+                    Justification = "The yield return state machine doesn't propagate annotations")]
+                static Type[] GetDeclaredOnlyNestedTypes(
+                    Type type) => type.GetNestedTypes(TypeInfo.DeclaredOnlyLookup);
             }
         }
 
index 2334103d055ff5b29857c7e7bfc3e9dc75e6c733..16c585d4fe63451475cb2eecc0dcbe7f22547a36 100644 (file)
@@ -306,7 +306,9 @@ namespace System.Resources
             }
         }
 
-        public static ResourceManager CreateFileBasedResourceManager(string baseName, string resourceDir, Type? usingResourceSet)
+        public static ResourceManager CreateFileBasedResourceManager(string baseName, string resourceDir,
+            [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors | DynamicallyAccessedMemberTypes.NonPublicConstructors)]
+            Type? usingResourceSet)
         {
             return new ResourceManager(baseName, resourceDir, usingResourceSet);
         }
index 0a42533351b55ef67712aa4cbc42c79691a84d21..dbb3342b94f09d1df5cb0a756db2983ac96870ca 100644 (file)
@@ -14,6 +14,7 @@
 
 using System.Collections;
 using System.Diagnostics;
+using System.Diagnostics.CodeAnalysis;
 using System.IO;
 using System.Reflection;
 
@@ -114,10 +115,11 @@ namespace System.Resources
         // Returns the preferred IResourceWriter class for this kind of ResourceSet.
         // Subclasses of ResourceSet using their own Readers &; should override
         // GetDefaultReader and GetDefaultWriter.
+        // TODO: https://github.com/mono/linker/issues/943
+        [DynamicDependency(DynamicallyAccessedMemberTypes.PublicConstructors, "System.Resources.ResourceWriter", "System.Resources.Writer")]
         public virtual Type GetDefaultWriter()
         {
-            Assembly resourceWriterAssembly = Assembly.Load("System.Resources.Writer, Version=4.0.1.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a");
-            return resourceWriterAssembly.GetType("System.Resources.ResourceWriter", throwOnError: true)!;
+            return Type.GetType("System.Resources.ResourceWriter, System.Resources.Writer", throwOnError: true)!;
         }
 
         public virtual IDictionaryEnumerator GetEnumerator()
index 3c9d4ccb82b4339e64b96d65984ebd98d7394b3f..b8af9e8f172c19843a2b2bb38bfad3a2c5db212f 100644 (file)
@@ -593,6 +593,8 @@ namespace System.Reflection
         [MethodImplAttribute(MethodImplOptions.InternalCall)]
         private static extern bool IsDefinedInternal(ICustomAttributeProvider obj, Type AttributeType);
 
+        [UnconditionalSuppressMessage("ReflectionAnalysis", "IL2006:UnrecognizedReflectionPattern",
+            Justification = "Linker analyzes base properties and marks them")]
         private static PropertyInfo? GetBasePropertyDefinition(RuntimePropertyInfo property)
         {
             MethodInfo? method = property.GetGetMethod(true);
@@ -622,6 +624,8 @@ namespace System.Reflection
 
         }
 
+        [UnconditionalSuppressMessage("ReflectionAnalysis", "IL2006:UnrecognizedReflectionPattern",
+            Justification = "Linker analyzes base events and marks them")]
         private static EventInfo? GetBaseEventDefinition(RuntimeEventInfo evt)
         {
             MethodInfo? method = evt.GetAddMethod(true);
index 35731ec326543ba76b9237311f16a4a72afd38b0..197a76f40e34d9519d3608416586a7d60c14e514 100644 (file)
@@ -794,6 +794,8 @@ namespace System.Reflection.Emit
 
         // We require emitted types to have all members on their bases to be accessible.
         // This is basically an identity function for `this`.
+        [UnconditionalSuppressMessage("ReflectionAnalysis", "IL2006:UnrecognizedReflectionPattern",
+            Justification = "Reflection emitted types have all of their members")]
         [return: DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)]
         public
         TypeInfo? CreateTypeInfo()
@@ -1821,6 +1823,8 @@ namespace System.Reflection.Emit
             return generic_params;
         }
 
+        [UnconditionalSuppressMessage("ReflectionAnalysis", "IL2006:UnrecognizedReflectionPattern",
+            Justification = "Linker thinks Type.GetConstructor(ConstructorInfo) is one of the public APIs because it doesn't analyze method signatures. We already have ConstructorInfo.")]
         public static ConstructorInfo GetConstructor(Type type, ConstructorInfo constructor)
         {
             /*FIXME I would expect the same checks of GetMethod here*/
index 167347af4e300000383a6051622819e2b323bbca..88204f50ada93607e7eed0c14ad17e2121b986ba 100644 (file)
@@ -122,6 +122,8 @@ namespace System.Reflection.Emit
             return InflateType(type, type_arguments, method_args);
         }
 
+        [UnconditionalSuppressMessage("ReflectionAnalysis", "IL2006:UnrecognizedReflectionPattern",
+            Justification = "Reflection emitted types have all of their members")]
         internal static Type? InflateType(Type? type, Type[]? type_args, Type[]? method_args)
         {
             if (type == null)