From 7ef273a57f8e1b2f31792e9702bd1a8d6e9d5519 Mon Sep 17 00:00:00 2001 From: Sven Boemer Date: Thu, 24 Mar 2022 08:34:48 -0700 Subject: [PATCH] Don't let RUC on cctor silence warnings (#2705) * Don't let RUC on cctor silence warnings --- .../RequiresISymbolExtensions.cs | 9 +++++---- src/linker/Linker/Annotations.cs | 6 ++++-- .../RequiresOnStaticConstructor.cs | 13 ++++++++++++- 3 files changed, 21 insertions(+), 7 deletions(-) diff --git a/src/ILLink.RoslynAnalyzer/RequiresISymbolExtensions.cs b/src/ILLink.RoslynAnalyzer/RequiresISymbolExtensions.cs index d536b456972b..aaf80b72536e 100644 --- a/src/ILLink.RoslynAnalyzer/RequiresISymbolExtensions.cs +++ b/src/ILLink.RoslynAnalyzer/RequiresISymbolExtensions.cs @@ -57,11 +57,12 @@ private static bool IsInRequiresScope (this ISymbol member, string requiresAttri if (member is ITypeSymbol) return false; - if (member.HasAttribute (requiresAttribute) - || (member.ContainingType is ITypeSymbol containingType && - containingType.HasAttribute (requiresAttribute))) { + if (member.HasAttribute (requiresAttribute) && !member.IsStaticConstructor ()) return true; - } + + if (member.ContainingType is ITypeSymbol containingType && containingType.HasAttribute (requiresAttribute)) + return true; + // Only check associated symbol if not override or virtual method if (checkAssociatedSymbol && member is IMethodSymbol { AssociatedSymbol: { } associated } && associated.HasAttribute (requiresAttribute)) return true; diff --git a/src/linker/Linker/Annotations.cs b/src/linker/Linker/Annotations.cs index 7e8e16e43862..d51c4ea484dd 100644 --- a/src/linker/Linker/Annotations.cs +++ b/src/linker/Linker/Annotations.cs @@ -618,8 +618,10 @@ internal bool DoesMethodRequireUnreferencedCode (MethodDefinition method, [NotNu /// instance methods (not just statics and .ctors). internal bool IsMethodInRequiresUnreferencedCodeScope (MethodDefinition method) { - if (HasLinkerAttribute (method) || - (method.DeclaringType is not null && HasLinkerAttribute (method.DeclaringType))) + if (HasLinkerAttribute (method) && !method.IsStaticConstructor ()) + return true; + + if (method.DeclaringType is not null && HasLinkerAttribute (method.DeclaringType)) return true; return false; diff --git a/test/Mono.Linker.Tests.Cases/RequiresCapability/RequiresOnStaticConstructor.cs b/test/Mono.Linker.Tests.Cases/RequiresCapability/RequiresOnStaticConstructor.cs index 9b4007a5b34d..a07ce5219482 100644 --- a/test/Mono.Linker.Tests.Cases/RequiresCapability/RequiresOnStaticConstructor.cs +++ b/test/Mono.Linker.Tests.Cases/RequiresCapability/RequiresOnStaticConstructor.cs @@ -1,4 +1,4 @@ -// Licensed to the .NET Foundation under one or more agreements. +// Licensed to the .NET Foundation under one or more agreements. // The .NET Foundation licenses this file to you under the MIT license. using System; @@ -28,10 +28,14 @@ public static void Main () class StaticCtor { + [ExpectedWarning ("IL2026", "--MethodWithRequires--")] + [ExpectedWarning ("IL3002", "--MethodWithRequires--", ProducedBy = ProducedBy.Analyzer)] + [ExpectedWarning ("IL3050", "--MethodWithRequires--", ProducedBy = ProducedBy.Analyzer)] [ExpectedWarning ("IL2116", "StaticCtor..cctor()")] [RequiresUnreferencedCode ("Message for --TestStaticCtor--")] static StaticCtor () { + MethodWithRequires (); } } @@ -123,5 +127,12 @@ static void TestTypeIsBeforeFieldInit () { var x = TypeIsBeforeFieldInit.field + 42; } + + [RequiresUnreferencedCode ("--MethodWithRequires--")] + [RequiresAssemblyFiles ("--MethodWithRequires--")] + [RequiresDynamicCode ("--MethodWithRequires--")] + static void MethodWithRequires () + { + } } }