Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -69,15 +69,6 @@ public override IEnumerable<DependencyListEntry> 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);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<SymbolAnalysisContext> 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<DiagnosticDescriptor> 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;

Expand Down Expand Up @@ -99,8 +75,6 @@ protected override bool CreateSpecialIncompatibleMembersDiagnostic(

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

protected override bool VerifyAttributeArguments(AttributeData attribute) =>
RequiresUnreferencedCodeUtils.VerifyRequiresUnreferencedCodeAttributeArguments(attribute);
Expand Down
2 changes: 1 addition & 1 deletion src/tools/illink/src/ILLink.Shared/DiagnosticId.cs
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ public enum DiagnosticId
DynamicallyAccessedMembersOnMethodReturnValueCanOnlyApplyToTypesOrStrings = 2106,
MethodsAreAssociatedWithStateMachine = 2107,
InvalidScopeInUnconditionalSuppressMessage = 2108,
RequiresUnreferencedCodeOnBaseClass = 2109,
_unused_RequiresUnreferencedCodeOnBaseClass = 2109,
DynamicallyAccessedMembersFieldAccessedViaReflection = 2110,
DynamicallyAccessedMembersMethodAccessedViaReflection = 2111,
DynamicallyAccessedMembersOnTypeReferencesMemberWithRequiresUnreferencedCode = 2112,
Expand Down
11 changes: 0 additions & 11 deletions src/tools/illink/src/linker/Linker.Steps/MarkStep.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<RequiresUnreferencedCodeAttribute>(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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,12 @@ public Task RequiresOnAttributeCtor()
return RunTest(nameof(RequiresOnAttributeCtor));
}

[Fact]
public Task RequiresOnBaseClass()
{
return RunTest(nameof(RequiresOnBaseClass));
}

[Fact]
public Task RequiresOnClass()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,6 @@ static void TestDerivedTypeWithOpenGenericOnBaseWithRUCOnBase()
new DerivedTypeWithOpenGenericOnBaseWithRUCOnBase<TestType>();
}

[ExpectedWarning("IL2109", nameof(BaseTypeWithOpenGenericDAMTAndRUC<T>))]
[ExpectedWarning("IL2091", nameof(BaseTypeWithOpenGenericDAMTAndRUC<T>))]
[ExpectedWarning("IL2091", nameof(IGenericInterfaceTypeWithRequirements<T>))]
class DerivedTypeWithOpenGenericOnBaseWithRUCOnBase<T> : BaseTypeWithOpenGenericDAMTAndRUC<T>, IGenericInterfaceTypeWithRequirements<T>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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--")]
Expand Down
Original file line number Diff line number Diff line change
@@ -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 { }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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() { }
Expand Down Expand Up @@ -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")]
Expand Down Expand Up @@ -340,7 +339,6 @@ class BaseWithRequiresOnType
public virtual void Method() { }
}

[ExpectedWarning("IL2109", nameof(BaseWithRequiresOnType))]
class DerivedWithoutRequiresOnType : BaseWithRequiresOnType
{
public override void Method() { }
Expand Down Expand Up @@ -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--")]
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -696,7 +694,6 @@ class WithRequiresOnlyInstanceFields
public int InstanceField;
}

[ExpectedWarning("IL2109", "ReflectionAccessOnField.DerivedWithoutRequires", "ReflectionAccessOnField.WithRequires")]
class DerivedWithoutRequires : WithRequires
{
public static int DerivedStaticField;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,6 @@
<argument>IL2026</argument>
</attribute>
</method>
<attribute fullname="System.Diagnostics.CodeAnalysis.UnconditionalSuppressMessageAttribute">
<argument>ILLink</argument>
<argument>IL2109</argument>
</attribute>
</type>
</assembly>
</linker>
Loading