Resolve easy ILLink warnings from InteropServices, ObjectModel, Drawing.Primitives...
authorEric Erhardt <eric.erhardt@microsoft.com>
Fri, 8 Jan 2021 00:10:46 +0000 (18:10 -0600)
committerGitHub <noreply@github.com>
Fri, 8 Jan 2021 00:10:46 +0000 (18:10 -0600)
* Resolve ILLink warnings in System.Runtime.InteropServices
* Resolve ILLink warnings in System.ObjectModel
* Resolve ILLink warnings in System.Drawing.Primitives

Contributes to #45623

* Remove suppression no longer needed with the latest ILLinker.

* Annotate ComEventInterfaceAttribute.EventProvider to make it trim compatible.

12 files changed:
src/coreclr/System.Private.CoreLib/src/System/__ComObject.cs
src/libraries/Common/src/System/Drawing/ColorTable.cs
src/libraries/System.ComponentModel.TypeConverter/src/ILLink/ILLink.Suppressions.xml
src/libraries/System.Drawing.Primitives/src/ILLink/ILLink.Suppressions.xml [deleted file]
src/libraries/System.ObjectModel/ref/System.ObjectModel.cs
src/libraries/System.ObjectModel/src/ILLink/ILLink.Suppressions.xml [deleted file]
src/libraries/System.ObjectModel/src/System/Windows/Markup/ValueSerializerAttribute.cs
src/libraries/System.Private.CoreLib/src/ILLink/ILLink.Suppressions.Shared.xml
src/libraries/System.Private.CoreLib/src/System/Runtime/InteropServices/ComEventInterfaceAttribute.cs
src/libraries/System.Runtime.InteropServices/ref/System.Runtime.InteropServices.cs
src/libraries/System.Runtime.InteropServices/src/ILLink/ILLink.Suppressions.xml [deleted file]
src/libraries/System.Runtime.InteropServices/src/System/Runtime/InteropServices/ComAwareEventInfo.cs

index be3294f..85a2ff6 100644 (file)
@@ -2,8 +2,9 @@
 // The .NET Foundation licenses this file to you under the MIT license.
 
 using System.Collections;
-using System.Runtime.InteropServices;
+using System.Diagnostics.CodeAnalysis;
 using System.Reflection;
+using System.Runtime.InteropServices;
 using System.Runtime.Versioning;
 
 namespace System
@@ -103,7 +104,8 @@ namespace System
         /// <summary>
         /// Called from within the EE and is used to handle calls on methods of event interfaces.
         /// </summary>
-        internal object GetEventProvider(RuntimeType t)
+        internal object GetEventProvider(
+            [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors | DynamicallyAccessedMemberTypes.NonPublicConstructors)] RuntimeType t)
         {
             // Check to see if we already have a cached event provider for this type.
             object? provider = GetData(t);
@@ -120,7 +122,8 @@ namespace System
 
         internal void FinalReleaseSelf() => Marshal.InternalFinalReleaseComObject(this);
 
-        private object CreateEventProvider(RuntimeType t)
+        private object CreateEventProvider(
+            [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors | DynamicallyAccessedMemberTypes.NonPublicConstructors)] RuntimeType t)
         {
             // Create the event provider for the specified type.
             object EvProvider = Activator.CreateInstance(t, BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.CreateInstance, null, new object[] { this }, null)!;
index 3d96a69..6e560bb 100644 (file)
@@ -3,6 +3,7 @@
 
 #nullable enable
 using System.Collections.Generic;
+using System.Diagnostics.CodeAnalysis;
 using System.Reflection;
 
 namespace System.Drawing
@@ -19,7 +20,9 @@ namespace System.Drawing
             return colors;
         }
 
-        private static void FillWithProperties(Dictionary<string, Color> dictionary, Type typeWithColors)
+        private static void FillWithProperties(
+            Dictionary<string, Color> dictionary,
+            [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicProperties | DynamicallyAccessedMemberTypes.NonPublicProperties)] Type typeWithColors) // can remove NonPublicProperties once https://github.com/mono/linker/issues/1724 is fixed
         {
             foreach (PropertyInfo prop in typeWithColors.GetProperties(BindingFlags.Public | BindingFlags.Static))
             {
index e5159bf..63a84c9 100644 (file)
     </attribute>
     <attribute fullname="System.Diagnostics.CodeAnalysis.UnconditionalSuppressMessageAttribute">
       <argument>ILLink</argument>
-      <argument>IL2070</argument>
-      <property name="Scope">member</property>
-      <property name="Target">M:System.Drawing.ColorTable.FillWithProperties(System.Collections.Generic.Dictionary{System.String,System.Drawing.Color},System.Type)</property>
-    </attribute>
-    <attribute fullname="System.Diagnostics.CodeAnalysis.UnconditionalSuppressMessageAttribute">
-      <argument>ILLink</argument>
       <argument>IL2072</argument>
       <property name="Scope">member</property>
       <property name="Target">M:System.ComponentModel.LicenseManager.ValidateInternalRecursive(System.ComponentModel.LicenseContext,System.Type,System.Object,System.Boolean,System.ComponentModel.License@,System.String@)</property>
       <property name="Target">M:System.ComponentModel.BindingList`1.get_ItemTypeHasDefaultConstructor</property>
     </attribute>
   </assembly>
-</linker>
\ No newline at end of file
+</linker>
diff --git a/src/libraries/System.Drawing.Primitives/src/ILLink/ILLink.Suppressions.xml b/src/libraries/System.Drawing.Primitives/src/ILLink/ILLink.Suppressions.xml
deleted file mode 100644 (file)
index f3c98e1..0000000
+++ /dev/null
@@ -1,11 +0,0 @@
-<?xml version="1.0" encoding="utf-8"?>
-<linker>
-  <assembly fullname="System.Drawing.Primitives, PublicKeyToken=b03f5f7f11d50a3a">
-    <attribute fullname="System.Diagnostics.CodeAnalysis.UnconditionalSuppressMessageAttribute">
-      <argument>ILLink</argument>
-      <argument>IL2070</argument>
-      <property name="Scope">member</property>
-      <property name="Target">M:System.Drawing.ColorTable.FillWithProperties(System.Collections.Generic.Dictionary{System.String,System.Drawing.Color},System.Type)</property>
-    </attribute>
-  </assembly>
-</linker>
\ No newline at end of file
index 582af30..2358820 100644 (file)
@@ -237,9 +237,11 @@ namespace System.Windows.Markup
     [System.AttributeUsageAttribute(System.AttributeTargets.Class | System.AttributeTargets.Enum | System.AttributeTargets.Interface | System.AttributeTargets.Method | System.AttributeTargets.Property | System.AttributeTargets.Struct, AllowMultiple=false, Inherited=true)]
     public sealed partial class ValueSerializerAttribute : System.Attribute
     {
-        public ValueSerializerAttribute(string valueSerializerTypeName) { }
-        public ValueSerializerAttribute(System.Type valueSerializerType) { }
+        public ValueSerializerAttribute([System.Diagnostics.CodeAnalysis.DynamicallyAccessedMembersAttribute(System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes.PublicConstructors)] string valueSerializerTypeName) { }
+        public ValueSerializerAttribute([System.Diagnostics.CodeAnalysis.DynamicallyAccessedMembersAttribute(System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes.PublicConstructors)] System.Type valueSerializerType) { }
+        [System.Diagnostics.CodeAnalysis.DynamicallyAccessedMembersAttribute(System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes.PublicConstructors)]
         public System.Type ValueSerializerType { get { throw null; } }
+        [System.Diagnostics.CodeAnalysis.DynamicallyAccessedMembersAttribute(System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes.PublicConstructors)]
         public string ValueSerializerTypeName { get { throw null; } }
     }
 }
diff --git a/src/libraries/System.ObjectModel/src/ILLink/ILLink.Suppressions.xml b/src/libraries/System.ObjectModel/src/ILLink/ILLink.Suppressions.xml
deleted file mode 100644 (file)
index 4e660a5..0000000
+++ /dev/null
@@ -1,17 +0,0 @@
-<?xml version="1.0" encoding="utf-8"?>
-<linker>
-  <assembly fullname="System.ObjectModel, PublicKeyToken=b03f5f7f11d50a3a">
-    <attribute fullname="System.Diagnostics.CodeAnalysis.UnconditionalSuppressMessageAttribute">
-      <argument>ILLink</argument>
-      <argument>IL2057</argument>
-      <property name="Scope">member</property>
-      <property name="Target">M:System.Windows.Markup.ValueSerializerAttribute.get_ValueSerializerType</property>
-    </attribute>
-    <attribute fullname="System.Diagnostics.CodeAnalysis.UnconditionalSuppressMessageAttribute">
-      <argument>ILLink</argument>
-      <argument>IL2079</argument>
-      <property name="Scope">member</property>
-      <property name="Target">M:System.ComponentModel.TypeConverterAttribute.#ctor</property>
-    </attribute>
-  </assembly>
-</linker>
\ No newline at end of file
index f7e9897..45fe3b8 100644 (file)
@@ -1,6 +1,7 @@
 // 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;
 using System.Runtime.CompilerServices;
 
 namespace System.Windows.Markup
@@ -15,14 +16,16 @@ namespace System.Windows.Markup
     [TypeForwardedFrom("WindowsBase, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35")]
     public sealed class ValueSerializerAttribute : Attribute
     {
+        [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors)]
         private Type? _valueSerializerType;
+        [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors)]
         private readonly string? _valueSerializerTypeName;
 
         /// <summary>
         /// Constructor for the ValueSerializerAttribute
         /// </summary>
         /// <param name="valueSerializerType">Type of the value serializer being associated with a type or property</param>
-        public ValueSerializerAttribute(Type valueSerializerType)
+        public ValueSerializerAttribute([DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors)] Type valueSerializerType)
         {
             _valueSerializerType = valueSerializerType;
         }
@@ -31,7 +34,7 @@ namespace System.Windows.Markup
         /// Constructor for the ValueSerializerAttribute
         /// </summary>
         /// <param name="valueSerializerTypeName">Fully qualified type name of the value serializer being associated with a type or property</param>
-        public ValueSerializerAttribute(string valueSerializerTypeName)
+        public ValueSerializerAttribute([DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors)] string valueSerializerTypeName)
         {
             _valueSerializerTypeName = valueSerializerTypeName;
         }
@@ -39,6 +42,7 @@ namespace System.Windows.Markup
         /// <summary>
         /// The type of the value serializer to create for this type or property.
         /// </summary>
+        [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors)]
         public Type ValueSerializerType
         {
             get
@@ -55,6 +59,7 @@ namespace System.Windows.Markup
         /// <summary>
         /// The assembly qualified name of the value serializer type for this type or property.
         /// </summary>
+        [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors)]
         public string ValueSerializerTypeName
         {
             get
index fb533a5..6a6025a 100644 (file)
     </attribute>
     <attribute fullname="System.Diagnostics.CodeAnalysis.UnconditionalSuppressMessageAttribute">
       <argument>ILLink</argument>
-      <argument>IL2067</argument>
-      <property name="Scope">member</property>
-      <property name="Target">M:System.__ComObject.CreateEventProvider(System.RuntimeType)</property>
-    </attribute>
-    <attribute fullname="System.Diagnostics.CodeAnalysis.UnconditionalSuppressMessageAttribute">
-      <argument>ILLink</argument>
       <argument>IL2070</argument>
       <property name="Scope">member</property>
       <property name="Target">M:System.Diagnostics.Tracing.NullableTypeInfo.#ctor(System.Type,System.Collections.Generic.List{System.Type})</property>
index 0a3e518..a23c62d 100644 (file)
@@ -1,18 +1,28 @@
 // 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.Runtime.InteropServices
 {
     [AttributeUsage(AttributeTargets.Interface, Inherited = false)]
     public sealed class ComEventInterfaceAttribute : Attribute
     {
-        public ComEventInterfaceAttribute(Type SourceInterface, Type EventProvider)
+        private const DynamicallyAccessedMemberTypes EventProviderAccessedMemberTypes =
+            DynamicallyAccessedMemberTypes.PublicConstructors | DynamicallyAccessedMemberTypes.NonPublicConstructors |
+            DynamicallyAccessedMemberTypes.PublicEvents | DynamicallyAccessedMemberTypes.PublicMethods | DynamicallyAccessedMemberTypes.PublicProperties | DynamicallyAccessedMemberTypes.PublicFields;
+
+        public ComEventInterfaceAttribute(
+            [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicMethods)] Type SourceInterface,
+            [DynamicallyAccessedMembers(EventProviderAccessedMemberTypes)] Type EventProvider)
         {
             this.SourceInterface = SourceInterface;
             this.EventProvider = EventProvider;
         }
 
+        [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicMethods)]
         public Type SourceInterface { get; }
+        [DynamicallyAccessedMembers(EventProviderAccessedMemberTypes)]
         public Type EventProvider { get; }
     }
 }
index 9bbb8e8..7f49168 100644 (file)
@@ -175,7 +175,7 @@ namespace System.Runtime.InteropServices
     [System.ComponentModel.EditorBrowsableAttribute(System.ComponentModel.EditorBrowsableState.Never)]
     public partial class ComAwareEventInfo : System.Reflection.EventInfo
     {
-        public ComAwareEventInfo(System.Type type, string eventName) { }
+        public ComAwareEventInfo([System.Diagnostics.CodeAnalysis.DynamicallyAccessedMembersAttribute(System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes.PublicEvents)] System.Type type, string eventName) { }
         public override System.Reflection.EventAttributes Attributes { get { throw null; } }
         public override System.Type? DeclaringType { get { throw null; } }
         public override int MetadataToken { get { throw null; } }
@@ -219,8 +219,10 @@ namespace System.Runtime.InteropServices
     [System.ComponentModel.EditorBrowsableAttribute(System.ComponentModel.EditorBrowsableState.Never)]
     public sealed partial class ComEventInterfaceAttribute : System.Attribute
     {
-        public ComEventInterfaceAttribute(System.Type SourceInterface, System.Type EventProvider) { }
+        public ComEventInterfaceAttribute([System.Diagnostics.CodeAnalysis.DynamicallyAccessedMembersAttribute(System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes.PublicMethods)] System.Type SourceInterface, [System.Diagnostics.CodeAnalysis.DynamicallyAccessedMembersAttribute(System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes.NonPublicConstructors | System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes.PublicConstructors | System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes.PublicEvents | System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes.PublicFields | System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes.PublicMethods | System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes.PublicProperties)] System.Type EventProvider) { }
+        [System.Diagnostics.CodeAnalysis.DynamicallyAccessedMembersAttribute(System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes.NonPublicConstructors | System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes.PublicConstructors | System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes.PublicEvents | System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes.PublicFields | System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes.PublicMethods | System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes.PublicProperties)]
         public System.Type EventProvider { get { throw null; } }
+        [System.Diagnostics.CodeAnalysis.DynamicallyAccessedMembersAttribute(System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes.PublicMethods)]
         public System.Type SourceInterface { get { throw null; } }
     }
     [System.Runtime.Versioning.SupportedOSPlatformAttribute("windows")]
diff --git a/src/libraries/System.Runtime.InteropServices/src/ILLink/ILLink.Suppressions.xml b/src/libraries/System.Runtime.InteropServices/src/ILLink/ILLink.Suppressions.xml
deleted file mode 100644 (file)
index 39f87c6..0000000
+++ /dev/null
@@ -1,17 +0,0 @@
-<?xml version="1.0" encoding="utf-8"?>
-<linker>
-  <assembly fullname="System.Runtime.InteropServices, PublicKeyToken=b03f5f7f11d50a3a">
-    <attribute fullname="System.Diagnostics.CodeAnalysis.UnconditionalSuppressMessageAttribute">
-      <argument>ILLink</argument>
-      <argument>IL2070</argument>
-      <property name="Scope">member</property>
-      <property name="Target">M:System.Runtime.InteropServices.ComAwareEventInfo.#ctor(System.Type,System.String)</property>
-    </attribute>
-    <attribute fullname="System.Diagnostics.CodeAnalysis.UnconditionalSuppressMessageAttribute">
-      <argument>ILLink</argument>
-      <argument>IL2075</argument>
-      <property name="Scope">member</property>
-      <property name="Target">M:System.Runtime.InteropServices.ComAwareEventInfo.GetDataForComInvocation(System.Reflection.EventInfo,System.Guid@,System.Int32@)</property>
-    </attribute>
-  </assembly>
-</linker>
\ No newline at end of file
index 197e853..56e40be 100644 (file)
@@ -2,6 +2,7 @@
 // The .NET Foundation licenses this file to you under the MIT license.
 
 using System.Collections.Generic;
+using System.Diagnostics.CodeAnalysis;
 using System.Reflection;
 using System.Runtime.Versioning;
 
@@ -15,7 +16,7 @@ namespace System.Runtime.InteropServices
     {
         private readonly EventInfo _innerEventInfo;
 
-        public ComAwareEventInfo(Type type, string eventName)
+        public ComAwareEventInfo([DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicEvents)] Type type, string eventName)
         {
             _innerEventInfo = type.GetEvent(eventName)!;
         }