From 54be9ed4ee6fb066522722fe60afe3993392831f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michal=20Strehovsk=C3=BD?= Date: Thu, 4 Jun 2020 16:03:04 +0200 Subject: [PATCH 1/5] Reflection annotate more of CoreLib --- .../InteropServices/Marshal.CoreCLR.cs | 2 ++ .../src/System/RuntimeType.CoreCLR.cs | 2 ++ .../src/System/ValueType.cs | 3 +++ .../src/System/Activator.RuntimeType.cs | 2 ++ .../src/System/Activator.cs | 6 +++++ .../src/System/AppDomain.cs | 25 +++++++++++++++++++ .../src/System/Attribute.cs | 5 ++++ .../System/Runtime/InteropServices/Marshal.cs | 6 ++++- .../src/System/Type.Enum.cs | 3 +++ .../System.Private.CoreLib/src/System/Type.cs | 24 ++++++++++++++++++ 10 files changed, 77 insertions(+), 1 deletion(-) diff --git a/src/coreclr/src/System.Private.CoreLib/src/System/Runtime/InteropServices/Marshal.CoreCLR.cs b/src/coreclr/src/System.Private.CoreLib/src/System/Runtime/InteropServices/Marshal.CoreCLR.cs index f3454c368dd369..82c9891fa1606e 100644 --- a/src/coreclr/src/System.Private.CoreLib/src/System/Runtime/InteropServices/Marshal.CoreCLR.cs +++ b/src/coreclr/src/System.Private.CoreLib/src/System/Runtime/InteropServices/Marshal.CoreCLR.cs @@ -26,6 +26,8 @@ public static partial class Marshal [MethodImpl(MethodImplOptions.InternalCall)] internal static extern int SizeOfHelper(Type t, bool throwIfNotMarshalable); + [UnconditionalSuppressMessage("ReflectionAnalysis", "IL2006:UnrecognizedReflectionPattern", + Justification = "Trimming doesn't affect types eligible for marshalling. Different exception for invalid inputs doesn't matter.")] public static IntPtr OffsetOf(Type t, string fieldName) { if (t is null) diff --git a/src/coreclr/src/System.Private.CoreLib/src/System/RuntimeType.CoreCLR.cs b/src/coreclr/src/System.Private.CoreLib/src/System/RuntimeType.CoreCLR.cs index 3d1da0e2dd7df5..42ae5348181473 100644 --- a/src/coreclr/src/System.Private.CoreLib/src/System/RuntimeType.CoreCLR.cs +++ b/src/coreclr/src/System.Private.CoreLib/src/System/RuntimeType.CoreCLR.cs @@ -3779,6 +3779,8 @@ private void CreateInstanceCheckThis() throw new NotSupportedException(SR.Acc_CreateVoid); } + [UnconditionalSuppressMessage("ReflectionAnalysis", "IL2006:UnrecognizedReflectionPattern", + Justification = "Implementation detail of Activator that linker intrinsically recognizes")] internal object? CreateInstanceImpl( BindingFlags bindingAttr, Binder? binder, object?[]? args, CultureInfo? culture) { diff --git a/src/coreclr/src/System.Private.CoreLib/src/System/ValueType.cs b/src/coreclr/src/System.Private.CoreLib/src/System/ValueType.cs index 636b3bc6842998..f8715551f01893 100644 --- a/src/coreclr/src/System.Private.CoreLib/src/System/ValueType.cs +++ b/src/coreclr/src/System.Private.CoreLib/src/System/ValueType.cs @@ -11,6 +11,7 @@ ** ===========================================================*/ +using System.Diagnostics.CodeAnalysis; using System.Reflection; using System.Runtime.CompilerServices; @@ -20,6 +21,8 @@ namespace System [System.Runtime.CompilerServices.TypeForwardedFrom("mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089")] public abstract class ValueType { + [UnconditionalSuppressMessage("ReflectionAnalysis", "IL2006:UnrecognizedReflectionPattern", + Justification = "Unused fields don't make a difference for equality")] public override bool Equals(object? obj) { if (null == obj) diff --git a/src/libraries/System.Private.CoreLib/src/System/Activator.RuntimeType.cs b/src/libraries/System.Private.CoreLib/src/System/Activator.RuntimeType.cs index 76957ef99db0e2..cb3bdf1c80640f 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Activator.RuntimeType.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Activator.RuntimeType.cs @@ -97,6 +97,8 @@ public static partial class Activator throw new ArgumentException(SR.Arg_MustBeType, nameof(type)); } + [UnconditionalSuppressMessage("ReflectionAnalysis", "IL2006:UnrecognizedReflectionPattern", + Justification = "Implementation detail of Activator that linker intrinsically recognizes")] private static ObjectHandle? CreateInstanceInternal(string assemblyString, string typeName, bool ignoreCase, diff --git a/src/libraries/System.Private.CoreLib/src/System/Activator.cs b/src/libraries/System.Private.CoreLib/src/System/Activator.cs index 3c6c24be6e6491..676b5dd73f0264 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Activator.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Activator.cs @@ -40,12 +40,18 @@ public static partial class Activator public static object? CreateInstance(Type type) => CreateInstance(type, nonPublic: false); + [UnconditionalSuppressMessage("ReflectionAnalysis", "IL2006:UnrecognizedReflectionPattern", + Justification = "Recognized as an intrinsic - no annotation possible")] public static ObjectHandle? CreateInstanceFrom(string assemblyFile, string typeName) => CreateInstanceFrom(assemblyFile, typeName, false, ConstructorDefault, null, null, null, null); + [UnconditionalSuppressMessage("ReflectionAnalysis", "IL2006:UnrecognizedReflectionPattern", + Justification = "Recognized as an intrinsic - no annotation possible")] public static ObjectHandle? CreateInstanceFrom(string assemblyFile, string typeName, object?[]? activationAttributes) => CreateInstanceFrom(assemblyFile, typeName, false, ConstructorDefault, null, null, null, activationAttributes); + [UnconditionalSuppressMessage("ReflectionAnalysis", "IL2006:UnrecognizedReflectionPattern", + Justification = "Recognized as an intrinsic - no annotation possible")] public static ObjectHandle? CreateInstanceFrom(string assemblyFile, string typeName, bool ignoreCase, BindingFlags bindingAttr, Binder? binder, object?[]? args, CultureInfo? culture, object?[]? activationAttributes) { Assembly assembly = Assembly.LoadFrom(assemblyFile); diff --git a/src/libraries/System.Private.CoreLib/src/System/AppDomain.cs b/src/libraries/System.Private.CoreLib/src/System/AppDomain.cs index 19164e5ce634c8..80bb2f0e968b88 100644 --- a/src/libraries/System.Private.CoreLib/src/System/AppDomain.cs +++ b/src/libraries/System.Private.CoreLib/src/System/AppDomain.cs @@ -5,6 +5,7 @@ #pragma warning disable CS0067 // events are declared but not used using System.Diagnostics; +using System.Diagnostics.CodeAnalysis; using System.IO; using System.Reflection; using System.Runtime.CompilerServices; @@ -278,6 +279,8 @@ public void SetThreadPrincipal(IPrincipal principal) } } + [UnconditionalSuppressMessage("ReflectionAnalysis", "IL2006:UnrecognizedReflectionPattern", + Justification = "Recognized as an intrinsic - no annotation possible")] public ObjectHandle? CreateInstance(string assemblyName, string typeName) { if (assemblyName == null) @@ -288,6 +291,8 @@ public void SetThreadPrincipal(IPrincipal principal) return Activator.CreateInstance(assemblyName, typeName); } + [UnconditionalSuppressMessage("ReflectionAnalysis", "IL2006:UnrecognizedReflectionPattern", + Justification = "Recognized as an intrinsic - no annotation possible")] public ObjectHandle? CreateInstance(string assemblyName, string typeName, bool ignoreCase, BindingFlags bindingAttr, Binder? binder, object?[]? args, System.Globalization.CultureInfo? culture, object?[]? activationAttributes) { if (assemblyName == null) @@ -305,6 +310,8 @@ public void SetThreadPrincipal(IPrincipal principal) activationAttributes); } + [UnconditionalSuppressMessage("ReflectionAnalysis", "IL2006:UnrecognizedReflectionPattern", + Justification = "Recognized as an intrinsic - no annotation possible")] public ObjectHandle? CreateInstance(string assemblyName, string typeName, object?[]? activationAttributes) { if (assemblyName == null) @@ -315,12 +322,16 @@ public void SetThreadPrincipal(IPrincipal principal) return Activator.CreateInstance(assemblyName, typeName, activationAttributes); } + [UnconditionalSuppressMessage("ReflectionAnalysis", "IL2006:UnrecognizedReflectionPattern", + Justification = "Recognized as an intrinsic - no annotation possible")] public object? CreateInstanceAndUnwrap(string assemblyName, string typeName) { ObjectHandle? oh = CreateInstance(assemblyName, typeName); return oh?.Unwrap(); } + [UnconditionalSuppressMessage("ReflectionAnalysis", "IL2006:UnrecognizedReflectionPattern", + Justification = "Recognized as an intrinsic - no annotation possible")] public object? CreateInstanceAndUnwrap(string assemblyName, string typeName, bool ignoreCase, BindingFlags bindingAttr, Binder? binder, object?[]? args, System.Globalization.CultureInfo? culture, object?[]? activationAttributes) { ObjectHandle? oh = CreateInstance(assemblyName, @@ -334,17 +345,23 @@ public void SetThreadPrincipal(IPrincipal principal) return oh?.Unwrap(); } + [UnconditionalSuppressMessage("ReflectionAnalysis", "IL2006:UnrecognizedReflectionPattern", + Justification = "Recognized as an intrinsic - no annotation possible")] public object? CreateInstanceAndUnwrap(string assemblyName, string typeName, object?[]? activationAttributes) { ObjectHandle? oh = CreateInstance(assemblyName, typeName, activationAttributes); return oh?.Unwrap(); } + [UnconditionalSuppressMessage("ReflectionAnalysis", "IL2006:UnrecognizedReflectionPattern", + Justification = "Recognized as an intrinsic - no annotation possible")] public ObjectHandle? CreateInstanceFrom(string assemblyFile, string typeName) { return Activator.CreateInstanceFrom(assemblyFile, typeName); } + [UnconditionalSuppressMessage("ReflectionAnalysis", "IL2006:UnrecognizedReflectionPattern", + Justification = "Recognized as an intrinsic - no annotation possible")] public ObjectHandle? CreateInstanceFrom(string assemblyFile, string typeName, bool ignoreCase, BindingFlags bindingAttr, Binder? binder, object?[]? args, System.Globalization.CultureInfo? culture, object?[]? activationAttributes) { return Activator.CreateInstanceFrom(assemblyFile, @@ -357,17 +374,23 @@ public void SetThreadPrincipal(IPrincipal principal) activationAttributes); } + [UnconditionalSuppressMessage("ReflectionAnalysis", "IL2006:UnrecognizedReflectionPattern", + Justification = "Recognized as an intrinsic - no annotation possible")] public ObjectHandle? CreateInstanceFrom(string assemblyFile, string typeName, object?[]? activationAttributes) { return Activator.CreateInstanceFrom(assemblyFile, typeName, activationAttributes); } + [UnconditionalSuppressMessage("ReflectionAnalysis", "IL2006:UnrecognizedReflectionPattern", + Justification = "Recognized as an intrinsic - no annotation possible")] public object? CreateInstanceFromAndUnwrap(string assemblyFile, string typeName) { ObjectHandle? oh = CreateInstanceFrom(assemblyFile, typeName); return oh?.Unwrap(); } + [UnconditionalSuppressMessage("ReflectionAnalysis", "IL2006:UnrecognizedReflectionPattern", + Justification = "Recognized as an intrinsic - no annotation possible")] public object? CreateInstanceFromAndUnwrap(string assemblyFile, string typeName, bool ignoreCase, BindingFlags bindingAttr, Binder? binder, object?[]? args, System.Globalization.CultureInfo? culture, object?[]? activationAttributes) { ObjectHandle? oh = CreateInstanceFrom(assemblyFile, @@ -381,6 +404,8 @@ public void SetThreadPrincipal(IPrincipal principal) return oh?.Unwrap(); } + [UnconditionalSuppressMessage("ReflectionAnalysis", "IL2006:UnrecognizedReflectionPattern", + Justification = "Recognized as an intrinsic - no annotation possible")] public object? CreateInstanceFromAndUnwrap(string assemblyFile, string typeName, object?[]? activationAttributes) { ObjectHandle? oh = CreateInstanceFrom(assemblyFile, typeName, activationAttributes); diff --git a/src/libraries/System.Private.CoreLib/src/System/Attribute.cs b/src/libraries/System.Private.CoreLib/src/System/Attribute.cs index 8f33c3e9df7c1a..796c7af4e07a16 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Attribute.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Attribute.cs @@ -3,6 +3,7 @@ // See the LICENSE file in the project root for more information. using System.Diagnostics; +using System.Diagnostics.CodeAnalysis; using System.Reflection; namespace System @@ -15,6 +16,8 @@ public abstract partial class Attribute protected Attribute() { } #if !CORERT + [UnconditionalSuppressMessage("ReflectionAnalysis", "IL2006:UnrecognizedReflectionPattern", + Justification = "Unused fields don't make a difference for equality")] public override bool Equals(object? obj) { if (obj == null) @@ -47,6 +50,8 @@ public override bool Equals(object? obj) return true; } + [UnconditionalSuppressMessage("ReflectionAnalysis", "IL2006:UnrecognizedReflectionPattern", + Justification = "Unused fields don't make a difference for hashcode quality")] public override int GetHashCode() { Type type = GetType(); diff --git a/src/libraries/System.Private.CoreLib/src/System/Runtime/InteropServices/Marshal.cs b/src/libraries/System.Private.CoreLib/src/System/Runtime/InteropServices/Marshal.cs index 76aab4ab2c4b18..8bc04a2e9a138b 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Runtime/InteropServices/Marshal.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Runtime/InteropServices/Marshal.cs @@ -509,6 +509,8 @@ public static void Prelink(MethodInfo m) PrelinkCore(m); } + [UnconditionalSuppressMessage("ReflectionAnalysis", "IL2006:UnrecognizedReflectionPattern", + Justification = "This only needs to prelink methods that are actually used")] public static void PrelinkAll(Type c) { if (c is null) @@ -533,7 +535,9 @@ public static void StructureToPtr([DisallowNull] T structure, IntPtr ptr, boo /// Creates a new instance of "structuretype" and marshals data from a /// native memory block to it. /// - public static object? PtrToStructure(IntPtr ptr, Type structureType) + public static object? PtrToStructure(IntPtr ptr, + [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicParameterlessConstructor)] + Type structureType) { if (ptr == IntPtr.Zero) { diff --git a/src/libraries/System.Private.CoreLib/src/System/Type.Enum.cs b/src/libraries/System.Private.CoreLib/src/System/Type.Enum.cs index d041c32b7b5349..369b9af562a02a 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Type.Enum.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Type.Enum.cs @@ -2,6 +2,7 @@ // The .NET Foundation licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. +using System.Diagnostics.CodeAnalysis; using System.Reflection; using System.Collections; using System.Collections.Generic; @@ -105,6 +106,8 @@ private Array GetEnumRawConstantValues() return values; } + [UnconditionalSuppressMessage("ReflectionAnalysis", "IL2006:UnrecognizedReflectionPattern", + Justification = "Literal fields on enums can never be trimmed")] // This will return enumValues and enumNames sorted by the values. private void GetEnumData(out string[] enumNames, out Array enumValues) { diff --git a/src/libraries/System.Private.CoreLib/src/System/Type.cs b/src/libraries/System.Private.CoreLib/src/System/Type.cs index 7c4be7b0a449b0..b08817701b5fa6 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Type.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Type.cs @@ -149,6 +149,8 @@ public ConstructorInfo? TypeInitializer protected abstract ConstructorInfo? GetConstructorImpl(BindingFlags bindingAttr, Binder? binder, CallingConventions callConvention, Type[] types, ParameterModifier[]? modifiers); [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors)] + [UnconditionalSuppressMessage("ReflectionAnalysis", "IL2006:UnrecognizedReflectionPattern", + Justification = "Linker doesn't recongnize GetConstructors(BindingFlags.Public) but this is safe")] public ConstructorInfo[] GetConstructors() => GetConstructors(BindingFlags.Public | BindingFlags.Instance); [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors | DynamicallyAccessedMemberTypes.NonPublicConstructors)] @@ -161,6 +163,8 @@ public ConstructorInfo? TypeInitializer public abstract EventInfo? GetEvent(string name, BindingFlags bindingAttr); [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicEvents)] + [UnconditionalSuppressMessage("ReflectionAnalysis", "IL2006:UnrecognizedReflectionPattern", + Justification = "Linker doesn't recongnize GetEvents(BindingFlags.Public) but this is safe")] public virtual EventInfo[] GetEvents() => GetEvents(Type.DefaultLookup); [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicEvents | DynamicallyAccessedMemberTypes.NonPublicEvents)] @@ -173,16 +177,26 @@ public ConstructorInfo? TypeInitializer public abstract FieldInfo? GetField(string name, BindingFlags bindingAttr); [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicFields)] + [UnconditionalSuppressMessage("ReflectionAnalysis", "IL2006:UnrecognizedReflectionPattern", + Justification = "Linker doesn't recongnize GetFields(BindingFlags.Public) but this is safe")] public FieldInfo[] GetFields() => GetFields(Type.DefaultLookup); [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicFields | DynamicallyAccessedMemberTypes.NonPublicFields)] public abstract FieldInfo[] GetFields(BindingFlags bindingAttr); + [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)] public MemberInfo[] GetMember(string name) => GetMember(name, Type.DefaultLookup); + + [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)] public virtual MemberInfo[] GetMember(string name, BindingFlags bindingAttr) => GetMember(name, MemberTypes.All, bindingAttr); + + [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)] public virtual MemberInfo[] GetMember(string name, MemberTypes type, BindingFlags bindingAttr) => throw new NotSupportedException(SR.NotSupported_SubclassOverride); + [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)] public MemberInfo[] GetMembers() => GetMembers(Type.DefaultLookup); + + [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)] public abstract MemberInfo[] GetMembers(BindingFlags bindingAttr); [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicMethods)] @@ -253,6 +267,8 @@ public ConstructorInfo? TypeInitializer protected virtual MethodInfo? GetMethodImpl(string name, int genericParameterCount, BindingFlags bindingAttr, Binder? binder, CallingConventions callConvention, Type[]? types, ParameterModifier[]? modifiers) => throw new NotSupportedException(); [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicMethods)] + [UnconditionalSuppressMessage("ReflectionAnalysis", "IL2006:UnrecognizedReflectionPattern", + Justification = "Linker doesn't recongnize GetMethods(BindingFlags.Public) but this is safe")] public MethodInfo[] GetMethods() => GetMethods(Type.DefaultLookup); [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicMethods | DynamicallyAccessedMemberTypes.NonPublicMethods)] @@ -265,6 +281,8 @@ public ConstructorInfo? TypeInitializer public abstract Type? GetNestedType(string name, BindingFlags bindingAttr); [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicNestedTypes)] + [UnconditionalSuppressMessage("ReflectionAnalysis", "IL2006:UnrecognizedReflectionPattern", + Justification = "Linker doesn't recongnize GetNestedTypes(BindingFlags.Public) but this is safe")] public Type[] GetNestedTypes() => GetNestedTypes(Type.DefaultLookup); [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicNestedTypes | DynamicallyAccessedMemberTypes.NonPublicNestedTypes)] @@ -282,6 +300,8 @@ public ConstructorInfo? TypeInitializer } [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicProperties)] + [UnconditionalSuppressMessage("ReflectionAnalysis", "IL2006:UnrecognizedReflectionPattern", + Justification = "Linker doesn't recongnize GetPropertyImpl(BindingFlags.Public) but this is safe")] public PropertyInfo? GetProperty(string name, Type? returnType) { if (name == null) @@ -312,6 +332,8 @@ public ConstructorInfo? TypeInitializer protected abstract PropertyInfo? GetPropertyImpl(string name, BindingFlags bindingAttr, Binder? binder, Type? returnType, Type[]? types, ParameterModifier[]? modifiers); [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicProperties)] + [UnconditionalSuppressMessage("ReflectionAnalysis", "IL2006:UnrecognizedReflectionPattern", + Justification = "Linker doesn't recongnize GetProperties(BindingFlags.Public) but this is safe")] public PropertyInfo[] GetProperties() => GetProperties(Type.DefaultLookup); [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicProperties | DynamicallyAccessedMemberTypes.NonPublicProperties)] @@ -392,6 +414,8 @@ protected virtual TypeCode GetTypeCodeImpl() public virtual bool IsInstanceOfType([NotNullWhen(true)] object? o) => o == null ? false : IsAssignableFrom(o.GetType()); public virtual bool IsEquivalentTo([NotNullWhen(true)] Type? other) => this == other; + [UnconditionalSuppressMessage("ReflectionAnalysis", "IL2006:UnrecognizedReflectionPattern", + Justification = "The single instance field on enum types is never trimmed")] public virtual Type GetEnumUnderlyingType() { if (!IsEnum) From 666c2730368426015960c91e7363ce8253e312e2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michal=20Strehovsk=C3=BD?= Date: Thu, 4 Jun 2020 19:53:36 +0200 Subject: [PATCH 2/5] Missed selecting a line in the diff --- .../System.Private.CoreLib/src/System/Activator.RuntimeType.cs | 1 + src/libraries/System.Private.CoreLib/src/System/Activator.cs | 1 + 2 files changed, 2 insertions(+) diff --git a/src/libraries/System.Private.CoreLib/src/System/Activator.RuntimeType.cs b/src/libraries/System.Private.CoreLib/src/System/Activator.RuntimeType.cs index cb3bdf1c80640f..262081328a0ceb 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Activator.RuntimeType.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Activator.RuntimeType.cs @@ -2,6 +2,7 @@ // The .NET Foundation licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. +using System.Diagnostics.CodeAnalysis; using System.Reflection; using System.Globalization; using System.Runtime.Loader; diff --git a/src/libraries/System.Private.CoreLib/src/System/Activator.cs b/src/libraries/System.Private.CoreLib/src/System/Activator.cs index 676b5dd73f0264..3e3b3c159f0de0 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Activator.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Activator.cs @@ -3,6 +3,7 @@ // See the LICENSE file in the project root for more information. using System.Diagnostics; +using System.Diagnostics.CodeAnalysis; using System.Globalization; using System.Reflection; using System.Runtime.Remoting; From b52ad4ab90b33f495d0d60a730cb732052543d56 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michal=20Strehovsk=C3=BD?= Date: Thu, 4 Jun 2020 19:58:46 +0200 Subject: [PATCH 3/5] Feedback --- .../System.Private.CoreLib/src/System/Type.cs | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/src/libraries/System.Private.CoreLib/src/System/Type.cs b/src/libraries/System.Private.CoreLib/src/System/Type.cs index b08817701b5fa6..a5086ff2ec26d9 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Type.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Type.cs @@ -150,7 +150,7 @@ public ConstructorInfo? TypeInitializer [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors)] [UnconditionalSuppressMessage("ReflectionAnalysis", "IL2006:UnrecognizedReflectionPattern", - Justification = "Linker doesn't recongnize GetConstructors(BindingFlags.Public) but this is safe")] + Justification = "Linker doesn't recongnize GetConstructors(BindingFlags.Public) but this is what the body is doing")] public ConstructorInfo[] GetConstructors() => GetConstructors(BindingFlags.Public | BindingFlags.Instance); [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors | DynamicallyAccessedMemberTypes.NonPublicConstructors)] @@ -164,7 +164,7 @@ public ConstructorInfo? TypeInitializer [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicEvents)] [UnconditionalSuppressMessage("ReflectionAnalysis", "IL2006:UnrecognizedReflectionPattern", - Justification = "Linker doesn't recongnize GetEvents(BindingFlags.Public) but this is safe")] + Justification = "Linker doesn't recongnize GetEvents(BindingFlags.Public) but this is what the body is doing")] public virtual EventInfo[] GetEvents() => GetEvents(Type.DefaultLookup); [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicEvents | DynamicallyAccessedMemberTypes.NonPublicEvents)] @@ -178,7 +178,7 @@ public ConstructorInfo? TypeInitializer [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicFields)] [UnconditionalSuppressMessage("ReflectionAnalysis", "IL2006:UnrecognizedReflectionPattern", - Justification = "Linker doesn't recongnize GetFields(BindingFlags.Public) but this is safe")] + Justification = "Linker doesn't recongnize GetFields(BindingFlags.Public) but this is what the body is doing")] public FieldInfo[] GetFields() => GetFields(Type.DefaultLookup); [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicFields | DynamicallyAccessedMemberTypes.NonPublicFields)] @@ -268,7 +268,7 @@ public ConstructorInfo? TypeInitializer [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicMethods)] [UnconditionalSuppressMessage("ReflectionAnalysis", "IL2006:UnrecognizedReflectionPattern", - Justification = "Linker doesn't recongnize GetMethods(BindingFlags.Public) but this is safe")] + Justification = "Linker doesn't recongnize GetMethods(BindingFlags.Public) but this is what the body is doing")] public MethodInfo[] GetMethods() => GetMethods(Type.DefaultLookup); [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicMethods | DynamicallyAccessedMemberTypes.NonPublicMethods)] @@ -282,7 +282,7 @@ public ConstructorInfo? TypeInitializer [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicNestedTypes)] [UnconditionalSuppressMessage("ReflectionAnalysis", "IL2006:UnrecognizedReflectionPattern", - Justification = "Linker doesn't recongnize GetNestedTypes(BindingFlags.Public) but this is safe")] + Justification = "Linker doesn't recongnize GetNestedTypes(BindingFlags.Public) but this is what the body is doing")] public Type[] GetNestedTypes() => GetNestedTypes(Type.DefaultLookup); [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicNestedTypes | DynamicallyAccessedMemberTypes.NonPublicNestedTypes)] @@ -301,7 +301,7 @@ public ConstructorInfo? TypeInitializer [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicProperties)] [UnconditionalSuppressMessage("ReflectionAnalysis", "IL2006:UnrecognizedReflectionPattern", - Justification = "Linker doesn't recongnize GetPropertyImpl(BindingFlags.Public) but this is safe")] + Justification = "Linker doesn't recongnize GetPropertyImpl(BindingFlags.Public) but this is what the body is doing")] public PropertyInfo? GetProperty(string name, Type? returnType) { if (name == null) @@ -333,7 +333,7 @@ public ConstructorInfo? TypeInitializer [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicProperties)] [UnconditionalSuppressMessage("ReflectionAnalysis", "IL2006:UnrecognizedReflectionPattern", - Justification = "Linker doesn't recongnize GetProperties(BindingFlags.Public) but this is safe")] + Justification = "Linker doesn't recongnize GetProperties(BindingFlags.Public) but this is what the body is doing")] public PropertyInfo[] GetProperties() => GetProperties(Type.DefaultLookup); [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicProperties | DynamicallyAccessedMemberTypes.NonPublicProperties)] From e0ed66f301e86a165f662c3b424f6dd071d67396 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michal=20Strehovsk=C3=BD?= Date: Fri, 5 Jun 2020 11:04:50 +0200 Subject: [PATCH 4/5] Update ValueType.cs --- src/coreclr/src/System.Private.CoreLib/src/System/ValueType.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/coreclr/src/System.Private.CoreLib/src/System/ValueType.cs b/src/coreclr/src/System.Private.CoreLib/src/System/ValueType.cs index f8715551f01893..48f18864759c77 100644 --- a/src/coreclr/src/System.Private.CoreLib/src/System/ValueType.cs +++ b/src/coreclr/src/System.Private.CoreLib/src/System/ValueType.cs @@ -22,7 +22,7 @@ namespace System public abstract class ValueType { [UnconditionalSuppressMessage("ReflectionAnalysis", "IL2006:UnrecognizedReflectionPattern", - Justification = "Unused fields don't make a difference for equality")] + Justification = "Trimmed fields don't make a difference for equality")] public override bool Equals(object? obj) { if (null == obj) From d99333a8f6b29e9ec3a9f32aac4991a553c07740 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michal=20Strehovsk=C3=BD?= Date: Fri, 5 Jun 2020 13:24:59 +0200 Subject: [PATCH 5/5] Review feedback --- .../src/System/Activator.cs | 9 ++--- .../src/System/AppDomain.cs | 36 +++++++------------ 2 files changed, 15 insertions(+), 30 deletions(-) diff --git a/src/libraries/System.Private.CoreLib/src/System/Activator.cs b/src/libraries/System.Private.CoreLib/src/System/Activator.cs index 3e3b3c159f0de0..c4f43c04e38e9d 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Activator.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Activator.cs @@ -41,18 +41,15 @@ public static partial class Activator public static object? CreateInstance(Type type) => CreateInstance(type, nonPublic: false); - [UnconditionalSuppressMessage("ReflectionAnalysis", "IL2006:UnrecognizedReflectionPattern", - Justification = "Recognized as an intrinsic - no annotation possible")] + [RequiresUnreferencedCode("Type and its constructor could be removed")] public static ObjectHandle? CreateInstanceFrom(string assemblyFile, string typeName) => CreateInstanceFrom(assemblyFile, typeName, false, ConstructorDefault, null, null, null, null); - [UnconditionalSuppressMessage("ReflectionAnalysis", "IL2006:UnrecognizedReflectionPattern", - Justification = "Recognized as an intrinsic - no annotation possible")] + [RequiresUnreferencedCode("Type and its constructor could be removed")] public static ObjectHandle? CreateInstanceFrom(string assemblyFile, string typeName, object?[]? activationAttributes) => CreateInstanceFrom(assemblyFile, typeName, false, ConstructorDefault, null, null, null, activationAttributes); - [UnconditionalSuppressMessage("ReflectionAnalysis", "IL2006:UnrecognizedReflectionPattern", - Justification = "Recognized as an intrinsic - no annotation possible")] + [RequiresUnreferencedCode("Type and its constructor could be removed")] public static ObjectHandle? CreateInstanceFrom(string assemblyFile, string typeName, bool ignoreCase, BindingFlags bindingAttr, Binder? binder, object?[]? args, CultureInfo? culture, object?[]? activationAttributes) { Assembly assembly = Assembly.LoadFrom(assemblyFile); diff --git a/src/libraries/System.Private.CoreLib/src/System/AppDomain.cs b/src/libraries/System.Private.CoreLib/src/System/AppDomain.cs index 80bb2f0e968b88..b6b8b123df76d3 100644 --- a/src/libraries/System.Private.CoreLib/src/System/AppDomain.cs +++ b/src/libraries/System.Private.CoreLib/src/System/AppDomain.cs @@ -279,8 +279,7 @@ public void SetThreadPrincipal(IPrincipal principal) } } - [UnconditionalSuppressMessage("ReflectionAnalysis", "IL2006:UnrecognizedReflectionPattern", - Justification = "Recognized as an intrinsic - no annotation possible")] + [RequiresUnreferencedCode("Type and its constructor could be removed")] public ObjectHandle? CreateInstance(string assemblyName, string typeName) { if (assemblyName == null) @@ -291,8 +290,7 @@ public void SetThreadPrincipal(IPrincipal principal) return Activator.CreateInstance(assemblyName, typeName); } - [UnconditionalSuppressMessage("ReflectionAnalysis", "IL2006:UnrecognizedReflectionPattern", - Justification = "Recognized as an intrinsic - no annotation possible")] + [RequiresUnreferencedCode("Type and its constructor could be removed")] public ObjectHandle? CreateInstance(string assemblyName, string typeName, bool ignoreCase, BindingFlags bindingAttr, Binder? binder, object?[]? args, System.Globalization.CultureInfo? culture, object?[]? activationAttributes) { if (assemblyName == null) @@ -310,8 +308,7 @@ public void SetThreadPrincipal(IPrincipal principal) activationAttributes); } - [UnconditionalSuppressMessage("ReflectionAnalysis", "IL2006:UnrecognizedReflectionPattern", - Justification = "Recognized as an intrinsic - no annotation possible")] + [RequiresUnreferencedCode("Type and its constructor could be removed")] public ObjectHandle? CreateInstance(string assemblyName, string typeName, object?[]? activationAttributes) { if (assemblyName == null) @@ -322,16 +319,14 @@ public void SetThreadPrincipal(IPrincipal principal) return Activator.CreateInstance(assemblyName, typeName, activationAttributes); } - [UnconditionalSuppressMessage("ReflectionAnalysis", "IL2006:UnrecognizedReflectionPattern", - Justification = "Recognized as an intrinsic - no annotation possible")] + [RequiresUnreferencedCode("Type and its constructor could be removed")] public object? CreateInstanceAndUnwrap(string assemblyName, string typeName) { ObjectHandle? oh = CreateInstance(assemblyName, typeName); return oh?.Unwrap(); } - [UnconditionalSuppressMessage("ReflectionAnalysis", "IL2006:UnrecognizedReflectionPattern", - Justification = "Recognized as an intrinsic - no annotation possible")] + [RequiresUnreferencedCode("Type and its constructor could be removed")] public object? CreateInstanceAndUnwrap(string assemblyName, string typeName, bool ignoreCase, BindingFlags bindingAttr, Binder? binder, object?[]? args, System.Globalization.CultureInfo? culture, object?[]? activationAttributes) { ObjectHandle? oh = CreateInstance(assemblyName, @@ -345,23 +340,20 @@ public void SetThreadPrincipal(IPrincipal principal) return oh?.Unwrap(); } - [UnconditionalSuppressMessage("ReflectionAnalysis", "IL2006:UnrecognizedReflectionPattern", - Justification = "Recognized as an intrinsic - no annotation possible")] + [RequiresUnreferencedCode("Type and its constructor could be removed")] public object? CreateInstanceAndUnwrap(string assemblyName, string typeName, object?[]? activationAttributes) { ObjectHandle? oh = CreateInstance(assemblyName, typeName, activationAttributes); return oh?.Unwrap(); } - [UnconditionalSuppressMessage("ReflectionAnalysis", "IL2006:UnrecognizedReflectionPattern", - Justification = "Recognized as an intrinsic - no annotation possible")] + [RequiresUnreferencedCode("Type and its constructor could be removed")] public ObjectHandle? CreateInstanceFrom(string assemblyFile, string typeName) { return Activator.CreateInstanceFrom(assemblyFile, typeName); } - [UnconditionalSuppressMessage("ReflectionAnalysis", "IL2006:UnrecognizedReflectionPattern", - Justification = "Recognized as an intrinsic - no annotation possible")] + [RequiresUnreferencedCode("Type and its constructor could be removed")] public ObjectHandle? CreateInstanceFrom(string assemblyFile, string typeName, bool ignoreCase, BindingFlags bindingAttr, Binder? binder, object?[]? args, System.Globalization.CultureInfo? culture, object?[]? activationAttributes) { return Activator.CreateInstanceFrom(assemblyFile, @@ -374,23 +366,20 @@ public void SetThreadPrincipal(IPrincipal principal) activationAttributes); } - [UnconditionalSuppressMessage("ReflectionAnalysis", "IL2006:UnrecognizedReflectionPattern", - Justification = "Recognized as an intrinsic - no annotation possible")] + [RequiresUnreferencedCode("Type and its constructor could be removed")] public ObjectHandle? CreateInstanceFrom(string assemblyFile, string typeName, object?[]? activationAttributes) { return Activator.CreateInstanceFrom(assemblyFile, typeName, activationAttributes); } - [UnconditionalSuppressMessage("ReflectionAnalysis", "IL2006:UnrecognizedReflectionPattern", - Justification = "Recognized as an intrinsic - no annotation possible")] + [RequiresUnreferencedCode("Type and its constructor could be removed")] public object? CreateInstanceFromAndUnwrap(string assemblyFile, string typeName) { ObjectHandle? oh = CreateInstanceFrom(assemblyFile, typeName); return oh?.Unwrap(); } - [UnconditionalSuppressMessage("ReflectionAnalysis", "IL2006:UnrecognizedReflectionPattern", - Justification = "Recognized as an intrinsic - no annotation possible")] + [RequiresUnreferencedCode("Type and its constructor could be removed")] public object? CreateInstanceFromAndUnwrap(string assemblyFile, string typeName, bool ignoreCase, BindingFlags bindingAttr, Binder? binder, object?[]? args, System.Globalization.CultureInfo? culture, object?[]? activationAttributes) { ObjectHandle? oh = CreateInstanceFrom(assemblyFile, @@ -404,8 +393,7 @@ public void SetThreadPrincipal(IPrincipal principal) return oh?.Unwrap(); } - [UnconditionalSuppressMessage("ReflectionAnalysis", "IL2006:UnrecognizedReflectionPattern", - Justification = "Recognized as an intrinsic - no annotation possible")] + [RequiresUnreferencedCode("Type and its constructor could be removed")] public object? CreateInstanceFromAndUnwrap(string assemblyFile, string typeName, object?[]? activationAttributes) { ObjectHandle? oh = CreateInstanceFrom(assemblyFile, typeName, activationAttributes);