Skip to content
This repository was archived by the owner on Sep 25, 2026. It is now read-only.
This repository was archived by the owner on Sep 25, 2026. It is now read-only.

assembly level UnconditionalSuppressMessage is suppressing all warnings in the assembly, even if Scope and Target are set #2127

Open

Description

@eerhardt

dotnet publish -r win-x64 -p:PublishTrimmed=true the following app both with the assembly level suppress attribute, and without:

using System;
using System.Diagnostics.CodeAnalysis;

[assembly: UnconditionalSuppressMessage("ReflectionAnalysis", "IL2091:UnrecognizedReflectionPattern",
    Target = "M:ConsoleApp19.Program.<>c__1<TOptions>",
    Scope = "member",
    Justification = "Workaround for https://github.com/mono/linker/issues/1416. Outer method has been annotated with DynamicallyAccessedMembers.")]

namespace ConsoleApp19
{
    interface IFoo<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicFields)] TFoo> { }

    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Hello World!");
        }

        public static void Suppressed<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicFields)] TOptions>()
        {
            Configure<TOptions>(a => { Console.WriteLine("This should be suppressed"); });
        }

        public static void Unsuppressed<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicFields)] TOptions>()
        {
            Configure<TOptions>(a => { Console.WriteLine("This should not be suppressed"); });
        }

        public static void Configure<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicFields)] TFoo>(Action<IFoo<TFoo>> a) => a(null);
    }
}

When I publish without the UnconditionalSuppressMessage, I get 4 warnings (2 for each call to Configure):

ILLink : Trim analysis warning IL2091: System.Action`1<ConsoleApp19.IFoo`1<TOptions>> ConsoleApp19.Program/<>c__1`1::<>9__1_0: 'TFoo' generic argument does not satisfy 'DynamicallyAccessedMemberTypes.PublicFields' in 'ConsoleApp19.IFoo<TFoo>'. The generic parameter 'TOptions' of 'ConsoleApp19.Program.<>c__1<TOptions>' does not have matching annotations. The source value must declare at least the same requirements as those declared on the target location it is assigned to. [C:\Users\eerhardt\source\repos\ConsoleApp19\ConsoleApp19\ConsoleApp19.csproj]
C:\Users\eerhardt\source\repos\ConsoleApp19\ConsoleApp19\Program.cs(22,38): Trim analysis warning IL2091: ConsoleApp19.Program.<>c__1<TOptions>.<Suppressed>b__1_0(IFoo<Program.<>c__1<TOptions>.TOptions>): 'TFoo' generic argument does not satisfy 'DynamicallyAccessedMemberTypes.PublicFields' in 'ConsoleApp19.IFoo<TFoo>'. The generic parameter 'TOptions' of 'ConsoleApp19.Program.<>c__1<TOptions>' does not have matching annotations. The source value must declare at least the same requirements as those declared on the target location it is assigned to. [C:\Users\eerhardt\source\repos\ConsoleApp19\ConsoleApp19\ConsoleApp19.csproj]
ILLink : Trim analysis warning IL2091: System.Action`1<ConsoleApp19.IFoo`1<TOptions>> ConsoleApp19.Program/<>c__2`1::<>9__2_0: 'TFoo' generic argument does not satisfy 'DynamicallyAccessedMemberTypes.PublicFields' in 'ConsoleApp19.IFoo<TFoo>'. The generic parameter 'TOptions' of 'ConsoleApp19.Program.<>c__2<TOptions>' does not have matching annotations. The source value must declare at least the same requirements as those declared on the target location it is assigned to. [C:\Users\eerhardt\source\repos\ConsoleApp19\ConsoleApp19\ConsoleApp19.csproj]
C:\Users\eerhardt\source\repos\ConsoleApp19\ConsoleApp19\Program.cs(27,38): Trim analysis warning IL2091: ConsoleApp19.Program.<>c__2<TOptions>.<Unsuppressed>b__2_0(IFoo<Program.<>c__2<TOptions>.TOptions>): 'TFoo' generic argument does not satisfy 'DynamicallyAccessedMemberTypes.PublicFields' in 'ConsoleApp19.IFoo<TFoo>'. The generic parameter 'TOptions' of 'ConsoleApp19.Program.<>c__2<TOptions>' does not have matching annotations. The source value must declare at least the same requirements as those declared on the target location it is assigned to. [C:\Users\eerhardt\source\repos\ConsoleApp19\ConsoleApp19\ConsoleApp19.csproj]

However, when I publish with the assembly level suppression attribute, which should only be suppressing 1 of those warnings, all 4 of them disappear, and I don't get any warnings.

Since I am only targeting 1 of the 4 warnings, I should still see 3 warnings.

Side-note: It doesn't appear to matter what I put in for Scope. I put Scope = "eric" and the linker happily kept suppressing all 4 warnings just fine.

cc @mateoatr @sbomer @vitek-karas

Activity

  1. self-assigned this
    on Jul 2, 2021
  2. mateoatr commented on Jul 2, 2021

    @mateoatr
    Contributor

    We parse the argument of Target against this format so I think you could use either M:ConsoleApp19.Program.<>c__1`1.<Suppressed>b__1_0(ConsoleApp19.IFoo{`0}) (or maybe suppress warnings on the generated type?).

    Side-note: It doesn't appear to matter what I put in for Scope. I put Scope = "eric" and the linker happily kept suppressing all 4 warnings just fine.

    Related to this, I opened #2129 (and a follow-up issue that is related to how scopes are treated: #2130).

  3. MichalStrehovsky commented on Jul 8, 2021

    @MichalStrehovsky
    Member

    We parse the argument of Target against this format so I think you could use either M:ConsoleApp19.Program.<>c__1`1.<Suppressed>b__1_0(ConsoleApp19.IFoo{`0}) (or maybe suppress warnings on the generated type?).

    Sidenote: this kind of suppressions will probably be problematic in the context of the Roslyn analyzer - I don't think such suppression is going to work there.

    This is probably in the general category of "warnings in compiler generated code should be translated to warnings in user code" (we should not warn about the compiler generated thing in the first place).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions