Skip to content

Commit 8ea8809

Browse files
authored
Remove warning for base type with RequiresUnreferencedCode (#117944)
Removes the IL2109 warning about a type without RUC derived from a type with RUC. This existed for RequiresUnreferencedCode but not RequiresDynamicCode. The warning on the class should not be necessary because there will be a warning for the base ctor call.
1 parent be3060c commit 8ea8809

File tree

10 files changed

+68
-68
lines changed

10 files changed

+68
-68
lines changed

src/coreclr/tools/aot/ILCompiler.Compiler/Compiler/DependencyAnalysis/DataflowAnalyzedTypeDefinitionNode.cs

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -69,15 +69,6 @@ public override IEnumerable<DependencyListEntry> GetStaticDependencies(NodeFacto
6969

7070
if (_typeDefinition.HasBaseType)
7171
{
72-
if (_typeDefinition.BaseType.DoesTypeRequire(DiagnosticUtilities.RequiresUnreferencedCodeAttribute, out var requiresAttribute) &&
73-
!_typeDefinition.DoesTypeRequire(DiagnosticUtilities.RequiresUnreferencedCodeAttribute, out _))
74-
{
75-
UsageBasedMetadataManager metadataManager = (UsageBasedMetadataManager)factory.MetadataManager;
76-
string arg1 = MessageFormat.FormatRequiresAttributeMessageArg(DiagnosticUtilities.GetRequiresAttributeMessage(requiresAttribute.Value));
77-
string arg2 = MessageFormat.FormatRequiresAttributeUrlArg(DiagnosticUtilities.GetRequiresAttributeUrl(requiresAttribute.Value));
78-
metadataManager.Logger.LogWarning(new MessageOrigin(_typeDefinition), DiagnosticId.RequiresUnreferencedCodeOnBaseClass, _typeDefinition.GetDisplayName(), _typeDefinition.BaseType.GetDisplayName(), arg1, arg2);
79-
}
80-
8172
GenericArgumentDataFlow.ProcessGenericArgumentDataFlow(ref dependencies, factory, new MessageOrigin(_typeDefinition), _typeDefinition.BaseType, _typeDefinition);
8273
}
8374

src/tools/illink/src/ILLink.RoslynAnalyzer/RequiresUnreferencedCodeAnalyzer.cs

Lines changed: 1 addition & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -25,32 +25,8 @@ public sealed class RequiresUnreferencedCodeAnalyzer : RequiresAnalyzerBase
2525
private static readonly DiagnosticDescriptor s_requiresUnreferencedCodeOnStaticCtor = DiagnosticDescriptors.GetDiagnosticDescriptor(DiagnosticId.RequiresUnreferencedCodeOnStaticConstructor);
2626
private static readonly DiagnosticDescriptor s_requiresUnreferencedCodeOnEntryPoint = DiagnosticDescriptors.GetDiagnosticDescriptor(DiagnosticId.RequiresUnreferencedCodeOnEntryPoint);
2727

28-
private static readonly DiagnosticDescriptor s_typeDerivesFromRucClassRule = DiagnosticDescriptors.GetDiagnosticDescriptor(DiagnosticId.RequiresUnreferencedCodeOnBaseClass);
29-
30-
private Action<SymbolAnalysisContext> typeDerivesFromRucBase
31-
{
32-
get
33-
{
34-
return symbolAnalysisContext =>
35-
{
36-
if (symbolAnalysisContext.Symbol is INamedTypeSymbol typeSymbol && !typeSymbol.HasAttribute(RequiresUnreferencedCodeAttribute)
37-
&& typeSymbol.BaseType is INamedTypeSymbol baseType
38-
&& baseType.TryGetAttribute(RequiresUnreferencedCodeAttribute, out var requiresUnreferencedCodeAttribute))
39-
{
40-
var diag = Diagnostic.Create(s_typeDerivesFromRucClassRule,
41-
typeSymbol.Locations[0],
42-
typeSymbol,
43-
baseType.GetDisplayName(),
44-
GetMessageFromAttribute(requiresUnreferencedCodeAttribute),
45-
GetUrlFromAttribute(requiresUnreferencedCodeAttribute));
46-
symbolAnalysisContext.ReportDiagnostic(diag);
47-
}
48-
};
49-
}
50-
}
51-
5228
public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics =>
53-
ImmutableArray.Create(s_makeGenericMethodRule, s_makeGenericTypeRule, s_requiresUnreferencedCodeRule, s_requiresUnreferencedCodeAttributeMismatch, s_typeDerivesFromRucClassRule, s_requiresUnreferencedCodeOnStaticCtor, s_requiresUnreferencedCodeOnEntryPoint);
29+
ImmutableArray.Create(s_makeGenericMethodRule, s_makeGenericTypeRule, s_requiresUnreferencedCodeRule, s_requiresUnreferencedCodeAttributeMismatch, s_requiresUnreferencedCodeOnStaticCtor, s_requiresUnreferencedCodeOnEntryPoint);
5430

5531
private protected override string RequiresAttributeName => RequiresUnreferencedCodeAttribute;
5632

@@ -99,8 +75,6 @@ protected override bool CreateSpecialIncompatibleMembersDiagnostic(
9975

10076
return false;
10177
}
102-
private protected override ImmutableArray<(Action<SymbolAnalysisContext> Action, SymbolKind[] SymbolKind)> ExtraSymbolActions =>
103-
ImmutableArray.Create<(Action<SymbolAnalysisContext> Action, SymbolKind[] SymbolKind)>((typeDerivesFromRucBase, new SymbolKind[] { SymbolKind.NamedType }));
10478

10579
protected override bool VerifyAttributeArguments(AttributeData attribute) =>
10680
RequiresUnreferencedCodeUtils.VerifyRequiresUnreferencedCodeAttributeArguments(attribute);

src/tools/illink/src/ILLink.Shared/DiagnosticId.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ public enum DiagnosticId
173173
DynamicallyAccessedMembersOnMethodReturnValueCanOnlyApplyToTypesOrStrings = 2106,
174174
MethodsAreAssociatedWithStateMachine = 2107,
175175
InvalidScopeInUnconditionalSuppressMessage = 2108,
176-
RequiresUnreferencedCodeOnBaseClass = 2109,
176+
_unused_RequiresUnreferencedCodeOnBaseClass = 2109,
177177
DynamicallyAccessedMembersFieldAccessedViaReflection = 2110,
178178
DynamicallyAccessedMembersMethodAccessedViaReflection = 2111,
179179
DynamicallyAccessedMembersOnTypeReferencesMemberWithRequiresUnreferencedCode = 2112,

src/tools/illink/src/linker/Linker.Steps/MarkStep.cs

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2152,17 +2152,6 @@ internal void MarkStaticConstructorVisibleToReflection(TypeDefinition type, in D
21522152
MarkCustomAttributes(type, new DependencyInfo(DependencyKind.CustomAttribute, type), typeOrigin);
21532153
MarkSecurityDeclarations(type, new DependencyInfo(DependencyKind.CustomAttribute, type), typeOrigin);
21542154

2155-
if (Context.TryResolve(type.BaseType) is TypeDefinition baseType &&
2156-
!Annotations.HasLinkerAttribute<RequiresUnreferencedCodeAttribute>(type) &&
2157-
Annotations.TryGetLinkerAttribute(baseType, out RequiresUnreferencedCodeAttribute? effectiveRequiresUnreferencedCode))
2158-
{
2159-
2160-
string arg1 = MessageFormat.FormatRequiresAttributeMessageArg(effectiveRequiresUnreferencedCode.Message);
2161-
string arg2 = MessageFormat.FormatRequiresAttributeUrlArg(effectiveRequiresUnreferencedCode.Url);
2162-
Context.LogWarning(typeOrigin, DiagnosticId.RequiresUnreferencedCodeOnBaseClass, type.GetDisplayName(), type.BaseType.GetDisplayName(), arg1, arg2);
2163-
}
2164-
2165-
21662155
if (type.IsMulticastDelegate())
21672156
{
21682157
MarkMulticastDelegate(type, typeOrigin);

src/tools/illink/test/ILLink.RoslynAnalyzer.Tests/RequiresCapabilityTests.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,12 @@ public Task RequiresOnAttributeCtor()
8888
return RunTest(nameof(RequiresOnAttributeCtor));
8989
}
9090

91+
[Fact]
92+
public Task RequiresOnBaseClass()
93+
{
94+
return RunTest(nameof(RequiresOnBaseClass));
95+
}
96+
9197
[Fact]
9298
public Task RequiresOnClass()
9399
{

src/tools/illink/test/Mono.Linker.Tests.Cases/DataFlow/GenericParameterDataFlow.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,6 @@ static void TestDerivedTypeWithOpenGenericOnBaseWithRUCOnBase()
214214
new DerivedTypeWithOpenGenericOnBaseWithRUCOnBase<TestType>();
215215
}
216216

217-
[ExpectedWarning("IL2109", nameof(BaseTypeWithOpenGenericDAMTAndRUC<T>))]
218217
[ExpectedWarning("IL2091", nameof(BaseTypeWithOpenGenericDAMTAndRUC<T>))]
219218
[ExpectedWarning("IL2091", nameof(IGenericInterfaceTypeWithRequirements<T>))]
220219
class DerivedTypeWithOpenGenericOnBaseWithRUCOnBase<T> : BaseTypeWithOpenGenericDAMTAndRUC<T>, IGenericInterfaceTypeWithRequirements<T>

src/tools/illink/test/Mono.Linker.Tests.Cases/RequiresCapability/RequiresExcludeStatics.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,6 @@ public static void Test()
145145
}
146146
}
147147

148-
[UnexpectedWarning("IL2109", "DerivedWithoutRequires", Tool.Analyzer | Tool.Trimmer, "https://github.com/dotnet/runtime/issues/107660")]
149148
class DerivedWithoutRequires : BaseWithRequires
150149
{
151150
[ExpectedWarning("IL2026", "--Requires--")]
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
// Copyright (c) .NET Foundation and contributors. All rights reserved.
2+
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
3+
4+
using System;
5+
using System.Diagnostics.CodeAnalysis;
6+
using System.Reflection;
7+
using Mono.Linker.Tests.Cases.Expectations.Assertions;
8+
using Mono.Linker.Tests.Cases.Expectations.Helpers;
9+
10+
namespace Mono.Linker.Tests.Cases.RequiresCapability
11+
{
12+
[SkipKeptItemsValidation]
13+
[ExpectedNoWarnings]
14+
class RequiresOnBaseClass
15+
{
16+
17+
public static void Main()
18+
{
19+
DerivedFromBaseWithRUC.StaticMethod();
20+
DerivedFromBaseWithRDC.StaticMethod();
21+
22+
new DerivedFromBaseWithRDC();
23+
new DerivedFromBaseWithRUC();
24+
}
25+
26+
class DerivedFromBaseWithRUC : BaseWithRUC
27+
{
28+
[ExpectedWarning("IL2026")]
29+
public DerivedFromBaseWithRUC()
30+
{
31+
}
32+
33+
public static void StaticMethod() { }
34+
}
35+
36+
[RequiresUnreferencedCode(nameof(BaseWithRUC))]
37+
class BaseWithRUC { }
38+
39+
class DerivedFromBaseWithRDC : BaseWithRDC
40+
{
41+
[ExpectedWarning("IL3050", Tool.Analyzer | Tool.NativeAot, "NativeAOT Specific Warning")]
42+
public DerivedFromBaseWithRDC()
43+
{ }
44+
45+
public static void StaticMethod() { }
46+
}
47+
48+
[RequiresDynamicCode(nameof(BaseWithRDC))]
49+
class BaseWithRDC { }
50+
}
51+
}

src/tools/illink/test/Mono.Linker.Tests.Cases/RequiresCapability/RequiresOnClass.cs

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,6 @@ class RequiresOnMethod
9898
public static void MethodWithRequires() { }
9999
}
100100

101-
[ExpectedWarning("IL2109", "RequiresOnClass.DerivedWithoutRequires", "RequiresOnClass.ClassWithRequires", "--ClassWithRequires--")]
102101
private class DerivedWithoutRequires : ClassWithRequires
103102
{
104103
// This method contains implicit call to ClassWithRequires.ctor()
@@ -120,14 +119,15 @@ public static void ShouldntWarn(object objectToCast)
120119
}
121120
}
122121

123-
// In order to generate IL2109 the nested class would also need to be annotated with Requires
124-
// otherwise we threat the nested class as safe
125-
private class DerivedWithoutRequires2 : ClassWithRequires.NestedClass
122+
private class DerivedFromNestedInRequiresClass : ClassWithRequires.NestedClass
126123
{
124+
// In order to generate IL2026 the nested class would also need to be annotated with Requires
125+
// otherwise we treat the nested class as safe
126+
public DerivedFromNestedInRequiresClass() { }
127+
127128
public static void StaticMethod() { }
128129
}
129130

130-
[UnconditionalSuppressMessage("trim", "IL2109")]
131131
class TestUnconditionalSuppressMessage : ClassWithRequires
132132
{
133133
public static void StaticMethodInTestSuppressionClass() { }
@@ -305,11 +305,10 @@ private class DerivedWithRequires2 : ClassWithRequires
305305
{
306306
public static void StaticMethodInInheritedClass() { }
307307

308-
// A nested class is not considered a static method nor constructor therefore RequiresUnreferencedCode doesn't apply
309-
// and this warning is not suppressed
310-
[ExpectedWarning("IL2109", "RequiresOnClass.DerivedWithRequires2.DerivedNestedClass", "--ClassWithRequires--")]
311308
public class DerivedNestedClass : ClassWithRequires
312309
{
310+
// A nested class is not considered a static method nor constructor therefore RequiresUnreferencedCode doesn't apply
311+
// and this warning is not suppressed
313312
// This method contains implicit call to ClassWithRequires.ctor()
314313
[ExpectedWarning("IL2026")]
315314
[ExpectedWarning("IL3050", Tool.Analyzer | Tool.NativeAot, "NativeAOT Specific Warnings")]
@@ -340,7 +339,6 @@ class BaseWithRequiresOnType
340339
public virtual void Method() { }
341340
}
342341

343-
[ExpectedWarning("IL2109", nameof(BaseWithRequiresOnType))]
344342
class DerivedWithoutRequiresOnType : BaseWithRequiresOnType
345343
{
346344
public override void Method() { }
@@ -427,7 +425,8 @@ static void TestRequiresOnBaseButNotOnDerived()
427425
DerivedWithoutRequires.NestedClass.CallMethodWithRequires();
428426
DerivedWithoutRequires.ShouldntWarn(null);
429427
DerivedWithoutRequires.Instance.ToString();
430-
DerivedWithoutRequires2.StaticMethod();
428+
DerivedFromNestedInRequiresClass.StaticMethod();
429+
new DerivedFromNestedInRequiresClass();
431430
}
432431

433432
[ExpectedWarning("IL2026", "RequiresOnClass.DerivedWithRequires.StaticMethodInInheritedClass()", "--DerivedWithRequires--")]
@@ -607,7 +606,6 @@ class BaseWithRequires
607606
public BaseWithRequires() { }
608607
}
609608

610-
[ExpectedWarning("IL2109", "ReflectionAccessOnCtor.DerivedWithoutRequires", "ReflectionAccessOnCtor.BaseWithRequires")]
611609
class DerivedWithoutRequires : BaseWithRequires
612610
{
613611
[ExpectedWarning("IL2026", "--BaseWithRequires--")] // The body has direct call to the base.ctor()
@@ -696,7 +694,6 @@ class WithRequiresOnlyInstanceFields
696694
public int InstanceField;
697695
}
698696

699-
[ExpectedWarning("IL2109", "ReflectionAccessOnField.DerivedWithoutRequires", "ReflectionAccessOnField.WithRequires")]
700697
class DerivedWithoutRequires : WithRequires
701698
{
702699
public static int DerivedStaticField;
@@ -857,7 +854,6 @@ class DerivedRequires : WithRequires
857854
private event EventHandler DerivedPrivateInstanceEvent;
858855
}
859856

860-
[ExpectedWarning("IL2109", "ReflectionAccessOnEvents.DerivedWithoutRequires", "ReflectionAccessOnEvents.WithRequires")]
861857
class DerivedWithoutRequires : WithRequires
862858
{
863859
public static event EventHandler DerivedStaticEvent;
@@ -1054,7 +1050,6 @@ class WithRequiresOnlyInstanceProperties
10541050
public int InstanceProperty { get; set; }
10551051
}
10561052

1057-
[ExpectedWarning("IL2109", "ReflectionAccessOnProperties.DerivedWithoutRequires", "ReflectionAccessOnProperties.WithRequires")]
10581053
class DerivedWithoutRequires : WithRequires
10591054
{
10601055
public static int DerivedStaticProperty { get; set; }

src/tools/illink/test/Mono.Linker.Tests.Cases/Warnings/WarningSuppression/SuppressWarningsViaXml.xml

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,6 @@
88
<argument>IL2026</argument>
99
</attribute>
1010
</method>
11-
<attribute fullname="System.Diagnostics.CodeAnalysis.UnconditionalSuppressMessageAttribute">
12-
<argument>ILLink</argument>
13-
<argument>IL2109</argument>
14-
</attribute>
1511
</type>
1612
</assembly>
1713
</linker>

0 commit comments

Comments
 (0)