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

Open
#108,454 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
4/5
Estimated time
3-5 days
Newbie friendliness
42/100
Issue type
Bug
Clarity
Mostly clear
Activity status
Stale
Tech stack
csharp
Domain
compilers

Research direction

Start by reproducing the supplied C# example and review the trim-analysis paths used by the analyzer, ILC, and ILLink. Use the referenced ProviderSettings.cs case as an additional example. Done means property and event attributes with RequiresUnreferencedCode no longer produce unactionable warnings, while the existing method behavior remains covered.

Written by the indexing model from the issue text.

Description

area-Tools-ILLink

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

Dominant language
C#
Stars
18.3k
Forks
5.6k
PR merge metrics
PR metrics pending

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from dotnet/runtime

All issues in dotnet/runtime

Similar issues

More C# issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.