Skip to content

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

Open
@eerhardt

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

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions