Skip to content

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

@sbomer

Description

@sbomer

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    area-Tools-ILLink.NET linker development as well as trimming analyzers

    Type

    No type

    Projects

    Status

    No status

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions