From 84a3ffad8ace7c541b1a718e016d743441f4d814 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michal=20Strehovsk=C3=BD?= Date: Tue, 14 Jun 2022 15:18:19 +0900 Subject: [PATCH] Fix invalid suppression in System.Attribute The suppression on System.Attribute is wrong. Ran into this in #70201 where `EditorBrowsableAttributeTests.EqualsTest` is failing because we didn't generate any reflection metadata for the field. There's a difference between "a field is kept" and "a field is accessible from reflection without issues". The issue is more pronounced in Native AOT (where the field can be completely erased from reflection). The implementation of Attribute.Equals didn't see any fields on the attribute type because the static analysis didn't deem any of them necessary. This fixes the suppression and replaces it with a more correct DAM on type. --- src/libraries/System.Private.CoreLib/src/System/Attribute.cs | 5 +---- src/libraries/System.Runtime/ref/System.Runtime.cs | 1 + 2 files changed, 2 insertions(+), 4 deletions(-) diff --git a/src/libraries/System.Private.CoreLib/src/System/Attribute.cs b/src/libraries/System.Private.CoreLib/src/System/Attribute.cs index 2bc115b9f686d..045edd8b1f644 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Attribute.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Attribute.cs @@ -10,12 +10,11 @@ namespace System [AttributeUsage(AttributeTargets.All, Inherited = true, AllowMultiple = false)] [Serializable] [System.Runtime.CompilerServices.TypeForwardedFrom("mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089")] + [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.NonPublicFields | DynamicallyAccessedMemberTypes.PublicFields)] public abstract partial class Attribute { protected Attribute() { } - [UnconditionalSuppressMessage("ReflectionAnalysis", "IL2075:UnrecognizedReflectionPattern", - Justification = "Unused fields don't make a difference for equality")] public override bool Equals([NotNullWhen(true)] object? obj) { if (obj == null) @@ -48,8 +47,6 @@ public override bool Equals([NotNullWhen(true)] object? obj) return true; } - [UnconditionalSuppressMessage("ReflectionAnalysis", "IL2075: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.Runtime/ref/System.Runtime.cs b/src/libraries/System.Runtime/ref/System.Runtime.cs index 680a79f0102a2..15819f3413301 100644 --- a/src/libraries/System.Runtime/ref/System.Runtime.cs +++ b/src/libraries/System.Runtime/ref/System.Runtime.cs @@ -498,6 +498,7 @@ public AssemblyLoadEventArgs(System.Reflection.Assembly loadedAssembly) { } public delegate void AssemblyLoadEventHandler(object? sender, System.AssemblyLoadEventArgs args); public delegate void AsyncCallback(System.IAsyncResult ar); [System.AttributeUsageAttribute(System.AttributeTargets.All, Inherited=true, AllowMultiple=false)] + [System.Diagnostics.CodeAnalysis.DynamicallyAccessedMembers(System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes.NonPublicFields | System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes.PublicFields)] public abstract partial class Attribute { protected Attribute() { }