This repository was archived by the owner on Sep 25, 2026. It is now read-only.
Repository navigation
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
Copy link
Copy link
Open
Labels
Description
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.
Activity
mateoatr commentedon Jul 2, 2021
We parse the argument of
Targetagainst this format so I think you could use eitherM:ConsoleApp19.Program.<>c__1`1.<Suppressed>b__1_0(ConsoleApp19.IFoo{`0})(or maybe suppress warnings on the generated type?).Related to this, I opened #2129 (and a follow-up issue that is related to how scopes are treated: #2130).
MichalStrehovsky commentedon Jul 8, 2021
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).