diff --git a/src/coreclr/tools/aot/ILCompiler.Compiler/Compiler/DependencyAnalysis/DataflowAnalyzedTypeDefinitionNode.cs b/src/coreclr/tools/aot/ILCompiler.Compiler/Compiler/DependencyAnalysis/DataflowAnalyzedTypeDefinitionNode.cs index 2fb798452850f2..4b283cb22c3542 100644 --- a/src/coreclr/tools/aot/ILCompiler.Compiler/Compiler/DependencyAnalysis/DataflowAnalyzedTypeDefinitionNode.cs +++ b/src/coreclr/tools/aot/ILCompiler.Compiler/Compiler/DependencyAnalysis/DataflowAnalyzedTypeDefinitionNode.cs @@ -69,15 +69,6 @@ public override IEnumerable GetStaticDependencies(NodeFacto if (_typeDefinition.HasBaseType) { - if (_typeDefinition.BaseType.DoesTypeRequire(DiagnosticUtilities.RequiresUnreferencedCodeAttribute, out var requiresAttribute) && - !_typeDefinition.DoesTypeRequire(DiagnosticUtilities.RequiresUnreferencedCodeAttribute, out _)) - { - UsageBasedMetadataManager metadataManager = (UsageBasedMetadataManager)factory.MetadataManager; - string arg1 = MessageFormat.FormatRequiresAttributeMessageArg(DiagnosticUtilities.GetRequiresAttributeMessage(requiresAttribute.Value)); - string arg2 = MessageFormat.FormatRequiresAttributeUrlArg(DiagnosticUtilities.GetRequiresAttributeUrl(requiresAttribute.Value)); - metadataManager.Logger.LogWarning(new MessageOrigin(_typeDefinition), DiagnosticId.RequiresUnreferencedCodeOnBaseClass, _typeDefinition.GetDisplayName(), _typeDefinition.BaseType.GetDisplayName(), arg1, arg2); - } - GenericArgumentDataFlow.ProcessGenericArgumentDataFlow(ref dependencies, factory, new MessageOrigin(_typeDefinition), _typeDefinition.BaseType, _typeDefinition); } diff --git a/src/tools/illink/src/ILLink.RoslynAnalyzer/RequiresUnreferencedCodeAnalyzer.cs b/src/tools/illink/src/ILLink.RoslynAnalyzer/RequiresUnreferencedCodeAnalyzer.cs index be50cb92f5a66b..e60eb128484b0d 100644 --- a/src/tools/illink/src/ILLink.RoslynAnalyzer/RequiresUnreferencedCodeAnalyzer.cs +++ b/src/tools/illink/src/ILLink.RoslynAnalyzer/RequiresUnreferencedCodeAnalyzer.cs @@ -25,32 +25,8 @@ public sealed class RequiresUnreferencedCodeAnalyzer : RequiresAnalyzerBase private static readonly DiagnosticDescriptor s_requiresUnreferencedCodeOnStaticCtor = DiagnosticDescriptors.GetDiagnosticDescriptor(DiagnosticId.RequiresUnreferencedCodeOnStaticConstructor); private static readonly DiagnosticDescriptor s_requiresUnreferencedCodeOnEntryPoint = DiagnosticDescriptors.GetDiagnosticDescriptor(DiagnosticId.RequiresUnreferencedCodeOnEntryPoint); - private static readonly DiagnosticDescriptor s_typeDerivesFromRucClassRule = DiagnosticDescriptors.GetDiagnosticDescriptor(DiagnosticId.RequiresUnreferencedCodeOnBaseClass); - - private Action typeDerivesFromRucBase - { - get - { - return symbolAnalysisContext => - { - if (symbolAnalysisContext.Symbol is INamedTypeSymbol typeSymbol && !typeSymbol.HasAttribute(RequiresUnreferencedCodeAttribute) - && typeSymbol.BaseType is INamedTypeSymbol baseType - && baseType.TryGetAttribute(RequiresUnreferencedCodeAttribute, out var requiresUnreferencedCodeAttribute)) - { - var diag = Diagnostic.Create(s_typeDerivesFromRucClassRule, - typeSymbol.Locations[0], - typeSymbol, - baseType.GetDisplayName(), - GetMessageFromAttribute(requiresUnreferencedCodeAttribute), - GetUrlFromAttribute(requiresUnreferencedCodeAttribute)); - symbolAnalysisContext.ReportDiagnostic(diag); - } - }; - } - } - public override ImmutableArray SupportedDiagnostics => - ImmutableArray.Create(s_makeGenericMethodRule, s_makeGenericTypeRule, s_requiresUnreferencedCodeRule, s_requiresUnreferencedCodeAttributeMismatch, s_typeDerivesFromRucClassRule, s_requiresUnreferencedCodeOnStaticCtor, s_requiresUnreferencedCodeOnEntryPoint); + ImmutableArray.Create(s_makeGenericMethodRule, s_makeGenericTypeRule, s_requiresUnreferencedCodeRule, s_requiresUnreferencedCodeAttributeMismatch, s_requiresUnreferencedCodeOnStaticCtor, s_requiresUnreferencedCodeOnEntryPoint); private protected override string RequiresAttributeName => RequiresUnreferencedCodeAttribute; @@ -99,8 +75,6 @@ protected override bool CreateSpecialIncompatibleMembersDiagnostic( return false; } - private protected override ImmutableArray<(Action Action, SymbolKind[] SymbolKind)> ExtraSymbolActions => - ImmutableArray.Create<(Action Action, SymbolKind[] SymbolKind)>((typeDerivesFromRucBase, new SymbolKind[] { SymbolKind.NamedType })); protected override bool VerifyAttributeArguments(AttributeData attribute) => RequiresUnreferencedCodeUtils.VerifyRequiresUnreferencedCodeAttributeArguments(attribute); diff --git a/src/tools/illink/src/ILLink.Shared/DiagnosticId.cs b/src/tools/illink/src/ILLink.Shared/DiagnosticId.cs index 68f96275ff718c..415f7b6812c56d 100644 --- a/src/tools/illink/src/ILLink.Shared/DiagnosticId.cs +++ b/src/tools/illink/src/ILLink.Shared/DiagnosticId.cs @@ -173,7 +173,7 @@ public enum DiagnosticId DynamicallyAccessedMembersOnMethodReturnValueCanOnlyApplyToTypesOrStrings = 2106, MethodsAreAssociatedWithStateMachine = 2107, InvalidScopeInUnconditionalSuppressMessage = 2108, - RequiresUnreferencedCodeOnBaseClass = 2109, + _unused_RequiresUnreferencedCodeOnBaseClass = 2109, DynamicallyAccessedMembersFieldAccessedViaReflection = 2110, DynamicallyAccessedMembersMethodAccessedViaReflection = 2111, DynamicallyAccessedMembersOnTypeReferencesMemberWithRequiresUnreferencedCode = 2112, diff --git a/src/tools/illink/src/linker/Linker.Steps/MarkStep.cs b/src/tools/illink/src/linker/Linker.Steps/MarkStep.cs index ffdd348619827e..6eda4e165992f0 100644 --- a/src/tools/illink/src/linker/Linker.Steps/MarkStep.cs +++ b/src/tools/illink/src/linker/Linker.Steps/MarkStep.cs @@ -2152,17 +2152,6 @@ internal void MarkStaticConstructorVisibleToReflection(TypeDefinition type, in D MarkCustomAttributes(type, new DependencyInfo(DependencyKind.CustomAttribute, type), typeOrigin); MarkSecurityDeclarations(type, new DependencyInfo(DependencyKind.CustomAttribute, type), typeOrigin); - if (Context.TryResolve(type.BaseType) is TypeDefinition baseType && - !Annotations.HasLinkerAttribute(type) && - Annotations.TryGetLinkerAttribute(baseType, out RequiresUnreferencedCodeAttribute? effectiveRequiresUnreferencedCode)) - { - - string arg1 = MessageFormat.FormatRequiresAttributeMessageArg(effectiveRequiresUnreferencedCode.Message); - string arg2 = MessageFormat.FormatRequiresAttributeUrlArg(effectiveRequiresUnreferencedCode.Url); - Context.LogWarning(typeOrigin, DiagnosticId.RequiresUnreferencedCodeOnBaseClass, type.GetDisplayName(), type.BaseType.GetDisplayName(), arg1, arg2); - } - - if (type.IsMulticastDelegate()) { MarkMulticastDelegate(type, typeOrigin); diff --git a/src/tools/illink/test/ILLink.RoslynAnalyzer.Tests/RequiresCapabilityTests.cs b/src/tools/illink/test/ILLink.RoslynAnalyzer.Tests/RequiresCapabilityTests.cs index da78b488d0c643..581e022524762e 100644 --- a/src/tools/illink/test/ILLink.RoslynAnalyzer.Tests/RequiresCapabilityTests.cs +++ b/src/tools/illink/test/ILLink.RoslynAnalyzer.Tests/RequiresCapabilityTests.cs @@ -88,6 +88,12 @@ public Task RequiresOnAttributeCtor() return RunTest(nameof(RequiresOnAttributeCtor)); } + [Fact] + public Task RequiresOnBaseClass() + { + return RunTest(nameof(RequiresOnBaseClass)); + } + [Fact] public Task RequiresOnClass() { diff --git a/src/tools/illink/test/Mono.Linker.Tests.Cases/DataFlow/GenericParameterDataFlow.cs b/src/tools/illink/test/Mono.Linker.Tests.Cases/DataFlow/GenericParameterDataFlow.cs index 1a39904824fde1..0452fbde0a4857 100644 --- a/src/tools/illink/test/Mono.Linker.Tests.Cases/DataFlow/GenericParameterDataFlow.cs +++ b/src/tools/illink/test/Mono.Linker.Tests.Cases/DataFlow/GenericParameterDataFlow.cs @@ -214,7 +214,6 @@ static void TestDerivedTypeWithOpenGenericOnBaseWithRUCOnBase() new DerivedTypeWithOpenGenericOnBaseWithRUCOnBase(); } - [ExpectedWarning("IL2109", nameof(BaseTypeWithOpenGenericDAMTAndRUC))] [ExpectedWarning("IL2091", nameof(BaseTypeWithOpenGenericDAMTAndRUC))] [ExpectedWarning("IL2091", nameof(IGenericInterfaceTypeWithRequirements))] class DerivedTypeWithOpenGenericOnBaseWithRUCOnBase : BaseTypeWithOpenGenericDAMTAndRUC, IGenericInterfaceTypeWithRequirements diff --git a/src/tools/illink/test/Mono.Linker.Tests.Cases/RequiresCapability/RequiresExcludeStatics.cs b/src/tools/illink/test/Mono.Linker.Tests.Cases/RequiresCapability/RequiresExcludeStatics.cs index a7651b044b7903..524a0c0f3a2866 100644 --- a/src/tools/illink/test/Mono.Linker.Tests.Cases/RequiresCapability/RequiresExcludeStatics.cs +++ b/src/tools/illink/test/Mono.Linker.Tests.Cases/RequiresCapability/RequiresExcludeStatics.cs @@ -145,7 +145,6 @@ public static void Test() } } - [UnexpectedWarning("IL2109", "DerivedWithoutRequires", Tool.Analyzer | Tool.Trimmer, "https://github.com/dotnet/runtime/issues/107660")] class DerivedWithoutRequires : BaseWithRequires { [ExpectedWarning("IL2026", "--Requires--")] diff --git a/src/tools/illink/test/Mono.Linker.Tests.Cases/RequiresCapability/RequiresOnBaseClass.cs b/src/tools/illink/test/Mono.Linker.Tests.Cases/RequiresCapability/RequiresOnBaseClass.cs new file mode 100644 index 00000000000000..3c35f02e8c60de --- /dev/null +++ b/src/tools/illink/test/Mono.Linker.Tests.Cases/RequiresCapability/RequiresOnBaseClass.cs @@ -0,0 +1,51 @@ +// Copyright (c) .NET Foundation and contributors. All rights reserved. +// Licensed under the MIT license. See LICENSE file in the project root for full license information. + +using System; +using System.Diagnostics.CodeAnalysis; +using System.Reflection; +using Mono.Linker.Tests.Cases.Expectations.Assertions; +using Mono.Linker.Tests.Cases.Expectations.Helpers; + +namespace Mono.Linker.Tests.Cases.RequiresCapability +{ + [SkipKeptItemsValidation] + [ExpectedNoWarnings] + class RequiresOnBaseClass + { + + public static void Main() + { + DerivedFromBaseWithRUC.StaticMethod(); + DerivedFromBaseWithRDC.StaticMethod(); + + new DerivedFromBaseWithRDC(); + new DerivedFromBaseWithRUC(); + } + + class DerivedFromBaseWithRUC : BaseWithRUC + { + [ExpectedWarning("IL2026")] + public DerivedFromBaseWithRUC() + { + } + + public static void StaticMethod() { } + } + + [RequiresUnreferencedCode(nameof(BaseWithRUC))] + class BaseWithRUC { } + + class DerivedFromBaseWithRDC : BaseWithRDC + { + [ExpectedWarning("IL3050", Tool.Analyzer | Tool.NativeAot, "NativeAOT Specific Warning")] + public DerivedFromBaseWithRDC() + { } + + public static void StaticMethod() { } + } + + [RequiresDynamicCode(nameof(BaseWithRDC))] + class BaseWithRDC { } + } +} diff --git a/src/tools/illink/test/Mono.Linker.Tests.Cases/RequiresCapability/RequiresOnClass.cs b/src/tools/illink/test/Mono.Linker.Tests.Cases/RequiresCapability/RequiresOnClass.cs index 967f9b2c6eed39..3c067185bb6576 100644 --- a/src/tools/illink/test/Mono.Linker.Tests.Cases/RequiresCapability/RequiresOnClass.cs +++ b/src/tools/illink/test/Mono.Linker.Tests.Cases/RequiresCapability/RequiresOnClass.cs @@ -98,7 +98,6 @@ class RequiresOnMethod public static void MethodWithRequires() { } } - [ExpectedWarning("IL2109", "RequiresOnClass.DerivedWithoutRequires", "RequiresOnClass.ClassWithRequires", "--ClassWithRequires--")] private class DerivedWithoutRequires : ClassWithRequires { // This method contains implicit call to ClassWithRequires.ctor() @@ -120,14 +119,15 @@ public static void ShouldntWarn(object objectToCast) } } - // In order to generate IL2109 the nested class would also need to be annotated with Requires - // otherwise we threat the nested class as safe - private class DerivedWithoutRequires2 : ClassWithRequires.NestedClass + private class DerivedFromNestedInRequiresClass : ClassWithRequires.NestedClass { + // In order to generate IL2026 the nested class would also need to be annotated with Requires + // otherwise we treat the nested class as safe + public DerivedFromNestedInRequiresClass() { } + public static void StaticMethod() { } } - [UnconditionalSuppressMessage("trim", "IL2109")] class TestUnconditionalSuppressMessage : ClassWithRequires { public static void StaticMethodInTestSuppressionClass() { } @@ -305,11 +305,10 @@ private class DerivedWithRequires2 : ClassWithRequires { public static void StaticMethodInInheritedClass() { } - // A nested class is not considered a static method nor constructor therefore RequiresUnreferencedCode doesn't apply - // and this warning is not suppressed - [ExpectedWarning("IL2109", "RequiresOnClass.DerivedWithRequires2.DerivedNestedClass", "--ClassWithRequires--")] public class DerivedNestedClass : ClassWithRequires { + // A nested class is not considered a static method nor constructor therefore RequiresUnreferencedCode doesn't apply + // and this warning is not suppressed // This method contains implicit call to ClassWithRequires.ctor() [ExpectedWarning("IL2026")] [ExpectedWarning("IL3050", Tool.Analyzer | Tool.NativeAot, "NativeAOT Specific Warnings")] @@ -340,7 +339,6 @@ class BaseWithRequiresOnType public virtual void Method() { } } - [ExpectedWarning("IL2109", nameof(BaseWithRequiresOnType))] class DerivedWithoutRequiresOnType : BaseWithRequiresOnType { public override void Method() { } @@ -427,7 +425,8 @@ static void TestRequiresOnBaseButNotOnDerived() DerivedWithoutRequires.NestedClass.CallMethodWithRequires(); DerivedWithoutRequires.ShouldntWarn(null); DerivedWithoutRequires.Instance.ToString(); - DerivedWithoutRequires2.StaticMethod(); + DerivedFromNestedInRequiresClass.StaticMethod(); + new DerivedFromNestedInRequiresClass(); } [ExpectedWarning("IL2026", "RequiresOnClass.DerivedWithRequires.StaticMethodInInheritedClass()", "--DerivedWithRequires--")] @@ -607,7 +606,6 @@ class BaseWithRequires public BaseWithRequires() { } } - [ExpectedWarning("IL2109", "ReflectionAccessOnCtor.DerivedWithoutRequires", "ReflectionAccessOnCtor.BaseWithRequires")] class DerivedWithoutRequires : BaseWithRequires { [ExpectedWarning("IL2026", "--BaseWithRequires--")] // The body has direct call to the base.ctor() @@ -696,7 +694,6 @@ class WithRequiresOnlyInstanceFields public int InstanceField; } - [ExpectedWarning("IL2109", "ReflectionAccessOnField.DerivedWithoutRequires", "ReflectionAccessOnField.WithRequires")] class DerivedWithoutRequires : WithRequires { public static int DerivedStaticField; @@ -857,7 +854,6 @@ class DerivedRequires : WithRequires private event EventHandler DerivedPrivateInstanceEvent; } - [ExpectedWarning("IL2109", "ReflectionAccessOnEvents.DerivedWithoutRequires", "ReflectionAccessOnEvents.WithRequires")] class DerivedWithoutRequires : WithRequires { public static event EventHandler DerivedStaticEvent; @@ -1054,7 +1050,6 @@ class WithRequiresOnlyInstanceProperties public int InstanceProperty { get; set; } } - [ExpectedWarning("IL2109", "ReflectionAccessOnProperties.DerivedWithoutRequires", "ReflectionAccessOnProperties.WithRequires")] class DerivedWithoutRequires : WithRequires { public static int DerivedStaticProperty { get; set; } diff --git a/src/tools/illink/test/Mono.Linker.Tests.Cases/Warnings/WarningSuppression/SuppressWarningsViaXml.xml b/src/tools/illink/test/Mono.Linker.Tests.Cases/Warnings/WarningSuppression/SuppressWarningsViaXml.xml index 00227a178475bd..e7d95da2da4289 100644 --- a/src/tools/illink/test/Mono.Linker.Tests.Cases/Warnings/WarningSuppression/SuppressWarningsViaXml.xml +++ b/src/tools/illink/test/Mono.Linker.Tests.Cases/Warnings/WarningSuppression/SuppressWarningsViaXml.xml @@ -8,10 +8,6 @@ IL2026 - - ILLink - IL2109 -