diff --git a/src/ILLink.RoslynAnalyzer/RequiresISymbolExtensions.cs b/src/ILLink.RoslynAnalyzer/RequiresISymbolExtensions.cs index 1c54e3af971b..5d9b221c52d3 100644 --- a/src/ILLink.RoslynAnalyzer/RequiresISymbolExtensions.cs +++ b/src/ILLink.RoslynAnalyzer/RequiresISymbolExtensions.cs @@ -54,7 +54,8 @@ private static bool IsInRequiresScope (this ISymbol member, string requiresAttri if (member is ISymbol containingSymbol) { if (containingSymbol.HasAttribute (requiresAttribute) || (containingSymbol is not ITypeSymbol && - containingSymbol.ContainingType.HasAttribute (requiresAttribute))) { + containingSymbol.ContainingType is ITypeSymbol containingType && + containingType.HasAttribute (requiresAttribute))) { return true; } } diff --git a/test/ILLink.RoslynAnalyzer.Tests/RequiresUnreferencedCodeAnalyzerTests.cs b/test/ILLink.RoslynAnalyzer.Tests/RequiresUnreferencedCodeAnalyzerTests.cs index 501ba16f9bb2..bdd6db8ca1aa 100644 --- a/test/ILLink.RoslynAnalyzer.Tests/RequiresUnreferencedCodeAnalyzerTests.cs +++ b/test/ILLink.RoslynAnalyzer.Tests/RequiresUnreferencedCodeAnalyzerTests.cs @@ -423,5 +423,20 @@ class C return VerifyRequiresUnreferencedCodeAnalyzer (source); } + + [Fact] + public Task TestPropertyAssignmentInAssemblyAttribute () + { + var source = @" +using System; +[assembly: MyAttribute (Value = 5)] + +class MyAttribute : Attribute +{ + public int Value { get; set; } +} +"; + return VerifyRequiresUnreferencedCodeAnalyzer (source); + } } } diff --git a/test/Mono.Linker.Tests.Cases/RequiresCapability/RequiresOnAttributeCtor.cs b/test/Mono.Linker.Tests.Cases/RequiresCapability/RequiresOnAttributeCtor.cs index d2c6ec8ffd9f..41733e6c929e 100644 --- a/test/Mono.Linker.Tests.Cases/RequiresCapability/RequiresOnAttributeCtor.cs +++ b/test/Mono.Linker.Tests.Cases/RequiresCapability/RequiresOnAttributeCtor.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; @@ -15,7 +15,8 @@ namespace Mono.Linker.Tests.Cases.RequiresCapability [ExpectedNoWarnings] public class RequiresOnAttributeCtor { - [ExpectedWarning ("IL2026", "Message from attribute's ctor.")] + [ExpectedWarning ("IL2026", "RUC on MethodAnnotatedWithRequires")] + [ExpectedWarning ("IL2026", "RUC on TestTypeWithRequires")] public static void Main () { var type = new Type (); @@ -27,6 +28,17 @@ public static void Main () type.EventAdd -= (sender, e) => { }; type.EventRemove += (sender, e) => { }; Type.Interface annotatedInterface = new Type.NestedType (); + + TestTypeWithRequires (); + } + + [RequiresUnreferencedCode ("RUC on TestTypeWithRequires")] + public static void TestTypeWithRequires () + { + var typeWithRequires = new TypeWithRequires (); + typeWithRequires.Method (); + TypeWithRequires.StaticMethod (); + TypeWithRequires.Interface annotatedInterface = new TypeWithRequires.NestedType (); } [ExpectedWarning ("IL2026", "Message from attribute's ctor.")] @@ -40,7 +52,7 @@ public void Method () { } - [RequiresUnreferencedCode ("Message from attribute's ctor.")] + [RequiresUnreferencedCode ("RUC on MethodAnnotatedWithRequires")] [RequiresOnAttributeCtor] public void MethodAnnotatedWithRequires () { @@ -82,6 +94,41 @@ public interface Interface { } + [ExpectedWarning ("IL2026", "Message from attribute's ctor.")] + [RequiresOnAttributeCtor] + public class NestedType : Interface + { + } + } + + // https://github.com/dotnet/linker/issues/2529 + [ExpectedWarning ("IL2026", "Message from attribute's ctor.", ProducedBy = ProducedBy.Trimmer)] + [RequiresUnreferencedCode ("RUC on TypeWithRequires")] + [RequiresOnAttributeCtor] + public class TypeWithRequires + { + // https://github.com/dotnet/linker/issues/2529 + [ExpectedWarning ("IL2026", "Message from attribute's ctor.", ProducedBy = ProducedBy.Analyzer)] + [RequiresOnAttributeCtor] + public void Method () + { + } + + // https://github.com/dotnet/linker/issues/2529 + [ExpectedWarning ("IL2026", "Message from attribute's ctor.", ProducedBy = ProducedBy.Analyzer)] + [RequiresOnAttributeCtor] + public static void StaticMethod () + { + } + + [ExpectedWarning ("IL2026", "Message from attribute's ctor.")] + [RequiresOnAttributeCtor] + public interface Interface + { + } + + [ExpectedWarning ("IL2026", "Message from attribute's ctor.")] + [RequiresOnAttributeCtor] public class NestedType : Interface { }