dotnet / runtime

.NET is a cross-platform runtime for cloud, mobile, desktop, and IoT apps.
https://docs.microsoft.com/dotnet/core/
MIT License
15.37k stars 4.75k forks source link

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

Open sbomer opened 1 month ago

sbomer commented 1 month ago

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: https://github.com/dotnet/runtime/blob/82f8ac64b3e369411f4f1168da616fa9ab3b79d3/src/libraries/System.Configuration.ConfigurationManager/src/System/Configuration/ProviderSettings.cs#L47-L48

dotnet-policy-service[bot] commented 1 month ago

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