Skip to content

Commit

Permalink
Don't let RUC on cctor silence warnings (#2705)
Browse files Browse the repository at this point in the history
* Don't let RUC on cctor silence warnings
  • Loading branch information
sbomer authored Mar 24, 2022
1 parent 04c49c9 commit 7ef273a
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 7 deletions.
9 changes: 5 additions & 4 deletions src/ILLink.RoslynAnalyzer/RequiresISymbolExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
6 changes: 4 additions & 2 deletions src/linker/Linker/Annotations.cs
Original file line number Diff line number Diff line change
Expand Up @@ -618,8 +618,10 @@ internal bool DoesMethodRequireUnreferencedCode (MethodDefinition method, [NotNu
/// instance methods (not just statics and .ctors).</remarks>
internal bool IsMethodInRequiresUnreferencedCodeScope (MethodDefinition method)
{
if (HasLinkerAttribute<RequiresUnreferencedCodeAttribute> (method) ||
(method.DeclaringType is not null && HasLinkerAttribute<RequiresUnreferencedCodeAttribute> (method.DeclaringType)))
if (HasLinkerAttribute<RequiresUnreferencedCodeAttribute> (method) && !method.IsStaticConstructor ())
return true;

if (method.DeclaringType is not null && HasLinkerAttribute<RequiresUnreferencedCodeAttribute> (method.DeclaringType))
return true;

return false;
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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 ();
}
}

Expand Down Expand Up @@ -123,5 +127,12 @@ static void TestTypeIsBeforeFieldInit ()
{
var x = TypeIsBeforeFieldInit.field + 42;
}

[RequiresUnreferencedCode ("--MethodWithRequires--")]
[RequiresAssemblyFiles ("--MethodWithRequires--")]
[RequiresDynamicCode ("--MethodWithRequires--")]
static void MethodWithRequires ()
{
}
}
}

0 comments on commit 7ef273a

Please sign in to comment.