Skip to content

Commit dd284e6

Browse files
authored
Fix analyzer nullref on assembly attribute (dotnet/linker#2534)
The analyzer runs for property assignment operations, including those in attributes. To check whether the property assignment should warn, we look for RUC on the containing symbol. However, assembly-level attributes are contained in the global namespace, which has a null containing type. Commit migrated from dotnet/linker@58a0c60
1 parent 29f119a commit dd284e6

File tree

3 files changed

+67
-4
lines changed

3 files changed

+67
-4
lines changed

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,8 @@ private static bool IsInRequiresScope (this ISymbol member, string requiresAttri
5454
if (member is ISymbol containingSymbol) {
5555
if (containingSymbol.HasAttribute (requiresAttribute)
5656
|| (containingSymbol is not ITypeSymbol &&
57-
containingSymbol.ContainingType.HasAttribute (requiresAttribute))) {
57+
containingSymbol.ContainingType is ITypeSymbol containingType &&
58+
containingType.HasAttribute (requiresAttribute))) {
5859
return true;
5960
}
6061
}

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -423,5 +423,20 @@ class C
423423

424424
return VerifyRequiresUnreferencedCodeAnalyzer (source);
425425
}
426+
427+
[Fact]
428+
public Task TestPropertyAssignmentInAssemblyAttribute ()
429+
{
430+
var source = @"
431+
using System;
432+
[assembly: MyAttribute (Value = 5)]
433+
434+
class MyAttribute : Attribute
435+
{
436+
public int Value { get; set; }
437+
}
438+
";
439+
return VerifyRequiresUnreferencedCodeAnalyzer (source);
440+
}
426441
}
427442
}

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

Lines changed: 50 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Licensed to the .NET Foundation under one or more agreements.
1+
// Licensed to the .NET Foundation under one or more agreements.
22
// The .NET Foundation licenses this file to you under the MIT license.
33

44
using System;
@@ -15,7 +15,8 @@ namespace Mono.Linker.Tests.Cases.RequiresCapability
1515
[ExpectedNoWarnings]
1616
public class RequiresOnAttributeCtor
1717
{
18-
[ExpectedWarning ("IL2026", "Message from attribute's ctor.")]
18+
[ExpectedWarning ("IL2026", "RUC on MethodAnnotatedWithRequires")]
19+
[ExpectedWarning ("IL2026", "RUC on TestTypeWithRequires")]
1920
public static void Main ()
2021
{
2122
var type = new Type ();
@@ -27,6 +28,17 @@ public static void Main ()
2728
type.EventAdd -= (sender, e) => { };
2829
type.EventRemove += (sender, e) => { };
2930
Type.Interface annotatedInterface = new Type.NestedType ();
31+
32+
TestTypeWithRequires ();
33+
}
34+
35+
[RequiresUnreferencedCode ("RUC on TestTypeWithRequires")]
36+
public static void TestTypeWithRequires ()
37+
{
38+
var typeWithRequires = new TypeWithRequires ();
39+
typeWithRequires.Method ();
40+
TypeWithRequires.StaticMethod ();
41+
TypeWithRequires.Interface annotatedInterface = new TypeWithRequires.NestedType ();
3042
}
3143

3244
[ExpectedWarning ("IL2026", "Message from attribute's ctor.")]
@@ -40,7 +52,7 @@ public void Method ()
4052
{
4153
}
4254

43-
[RequiresUnreferencedCode ("Message from attribute's ctor.")]
55+
[RequiresUnreferencedCode ("RUC on MethodAnnotatedWithRequires")]
4456
[RequiresOnAttributeCtor]
4557
public void MethodAnnotatedWithRequires ()
4658
{
@@ -82,6 +94,41 @@ public interface Interface
8294
{
8395
}
8496

97+
[ExpectedWarning ("IL2026", "Message from attribute's ctor.")]
98+
[RequiresOnAttributeCtor]
99+
public class NestedType : Interface
100+
{
101+
}
102+
}
103+
104+
// https://github.com/dotnet/linker/issues/2529
105+
[ExpectedWarning ("IL2026", "Message from attribute's ctor.", ProducedBy = ProducedBy.Trimmer)]
106+
[RequiresUnreferencedCode ("RUC on TypeWithRequires")]
107+
[RequiresOnAttributeCtor]
108+
public class TypeWithRequires
109+
{
110+
// https://github.com/dotnet/linker/issues/2529
111+
[ExpectedWarning ("IL2026", "Message from attribute's ctor.", ProducedBy = ProducedBy.Analyzer)]
112+
[RequiresOnAttributeCtor]
113+
public void Method ()
114+
{
115+
}
116+
117+
// https://github.com/dotnet/linker/issues/2529
118+
[ExpectedWarning ("IL2026", "Message from attribute's ctor.", ProducedBy = ProducedBy.Analyzer)]
119+
[RequiresOnAttributeCtor]
120+
public static void StaticMethod ()
121+
{
122+
}
123+
124+
[ExpectedWarning ("IL2026", "Message from attribute's ctor.")]
125+
[RequiresOnAttributeCtor]
126+
public interface Interface
127+
{
128+
}
129+
130+
[ExpectedWarning ("IL2026", "Message from attribute's ctor.")]
131+
[RequiresOnAttributeCtor]
85132
public class NestedType : Interface
86133
{
87134
}

0 commit comments

Comments
 (0)