Skip to content

Fix analyzer nullref on assembly attribute #2534

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 21, 2022
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
3 changes: 2 additions & 1 deletion src/ILLink.RoslynAnalyzer/RequiresISymbolExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
}
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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 ();
Expand All @@ -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.")]
Expand All @@ -40,7 +52,7 @@ public void Method ()
{
}

[RequiresUnreferencedCode ("Message from attribute's ctor.")]
[RequiresUnreferencedCode ("RUC on MethodAnnotatedWithRequires")]
[RequiresOnAttributeCtor]
public void MethodAnnotatedWithRequires ()
{
Expand Down Expand Up @@ -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
{
}
Expand Down