Skip to content
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

Unactionable trim warnings on property/event with attribute that has RUC ctor #108454

Open
sbomer opened this issue Oct 1, 2024 · 1 comment
Open
Labels
area-Tools-ILLink .NET linker development as well as trimming analyzers
Milestone

Comments

@sbomer
Copy link
Member

sbomer commented Oct 1, 2024

The trim analysis tooling warns on members annotated with attributes whose ctors have RequiresUnreferencedCode:

using System.Diagnostics.CodeAnalysis;

var m = typeof(C).GetMethod("M");
var p = typeof(C).GetProperty("P");
var f = typeof(C).GetField("f");
var e = typeof(C).GetEvent("E");

class C {
    [AttributeWithRUC]
    public static void M() {}

    [AttributeWithRUC]
    public static int f;

    [AttributeWithRUC]
    public static int P { get; set; }

    [AttributeWithRUC]
    public static event Action E;
}

[RequiresUnreferencedCode("AttributeWithRUC")]
class AttributeWithRUCAttribute : Attribute {}

analyzer:

Program.cs(12,6): warning IL2026: Using member 'AttributeWithRUCAttribute.AttributeWithRUCAttribute()' which has 'RequiresUnreferencedCodeAttribute' can break functionality when trimming application code. AttributeWithRUC.
Program.cs(18,6): warning IL2026: Using member 'AttributeWithRUCAttribute.AttributeWithRUCAttribute()' which has 'RequiresUnreferencedCodeAttribute' can break functionality when trimming application code. AttributeWithRUC.
Program.cs(15,6): warning IL2026: Using member 'AttributeWithRUCAttribute.AttributeWithRUCAttribute()' which has 'RequiresUnreferencedCodeAttribute' can break functionality when trimming application code. AttributeWithRUC.
Program.cs(9,6): warning IL2026: Using member 'AttributeWithRUCAttribute.AttributeWithRUCAttribute()' which has 'RequiresUnreferencedCodeAttribute' can break functionality when trimming application code. AttributeWithRUC.

ILC:

ILC : Trim analysis warning IL2026: C.E: Using member 'AttributeWithRUCAttribute.AttributeWithRUCAttribute()' which has 'RequiresUnreferencedCodeAttribute' can break functionality when trimming application code. AttributeWithRUC.
ILC : Trim analysis warning IL2026: C.E: Using member 'AttributeWithRUCAttribute.AttributeWithRUCAttribute()' which has 'RequiresUnreferencedCodeAttribute' can break functionality when trimming application code. AttributeWithRUC.
ILC : Trim analysis warning IL2026: C.f: Using member 'AttributeWithRUCAttribute.AttributeWithRUCAttribute()' which has 'RequiresUnreferencedCodeAttribute' can break functionality when trimming application code. AttributeWithRUC.
ILC : Trim analysis warning IL2026: C.P: Using member 'AttributeWithRUCAttribute.AttributeWithRUCAttribute()' which has 'RequiresUnreferencedCodeAttribute' can break functionality when trimming application code. AttributeWithRUC.
ILC : Trim analysis warning IL2026: C.P: Using member 'AttributeWithRUCAttribute.AttributeWithRUCAttribute()' which has 'RequiresUnreferencedCodeAttribute' can break functionality when trimming application code. AttributeWithRUC.
ILC : Trim analysis warning IL2026: C.M(): Using member 'AttributeWithRUCAttribute.AttributeWithRUCAttribute()' which has 'RequiresUnreferencedCodeAttribute' can break functionality when trimming application code. AttributeWithRUC.

(similar for ILLink - except ILLink warns for all kept members, whereas ILC only warns for reflectable members)

The warning on the method M can be bubbled up by annotating it with RequiresUnreferencedCode - then there will be a warning on the reference to M instead.

Fields/properties/events aren't supported attribute targets of RequiresUnreferencedCode, so they can't be annotated directly. If you instead add RequiresUnreferencedCode at the class level, this produces warnings at the access to the field/property/event. However, the warnings from the annotated property/event are still present, making it impossible to annotate this without suppressions.

This came up while annotating System.Configuration.ConfigurationManager - for example, if ConfigurationPropertyAttribute is annotated with RequiresUnreferencedCode, it warns here:

[ConfigurationProperty("name", IsRequired = true, IsKey = true)]
public string Name

@dotnet-policy-service dotnet-policy-service bot added the untriaged New issue has not been triaged by the area owner label Oct 1, 2024
@sbomer sbomer added area-Tools-ILLink .NET linker development as well as trimming analyzers and removed area-System.ComponentModel.DataAnnotations untriaged New issue has not been triaged by the area owner labels Oct 1, 2024
@sbomer sbomer added this to the 10.0.0 milestone Oct 1, 2024
Copy link
Contributor

Tagging subscribers to this area: @agocke, @sbomer, @vitek-karas
See info in area-owners.md if you want to be subscribed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-Tools-ILLink .NET linker development as well as trimming analyzers
Projects
Status: No status
Development

No branches or pull requests

1 participant