Skip to content

NativeAot produces warning for generic instantiation which can't be suppressed #77455

@sbomer

Description

@sbomer

Description

Instantiating a generic which has DynamicallyAccessedMemberTypes requirements with a type that has members marked RequiresUnreferencedCode correctly produces a warning. However, the warning is not suppressible by RequiresUnreferencedCodeAttribute or UnconditionalSuppressMessageAttribute.

The problem is that the message origin for the warning is an InstantiatedMethod, a type system entity that isn't associated with the calling context. Encountered while porting compiler-generated code tests from the linker as part of #68786.

Reproduction Steps

Publish the following with PublishAot:

using RUC = System.Diagnostics.CodeAnalysis.RequiresUnreferencedCodeAttribute;
using DAM = System.Diagnostics.CodeAnalysis.DynamicallyAccessedMembersAttribute;
using DAMT = System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes;
using USM = System.Diagnostics.CodeAnalysis.UnconditionalSuppressMessageAttribute;

class Program {
    static void Main() {
        Test();
    }
    [USM("", "IL2026:RequiresUnreferencedCode")]
    [RUC("")]
    static void Test() {
        MethodWithGenericWhichRequiresMethods<TypeWithMethodWithRequires>();
    }
    static void MethodWithGenericWhichRequiresMethods<[DAM(DAMT.All)] T>() {}
}

class TypeWithMethodWithRequires {
    [RUC("")]
 
    static void MethodWithRequires() {}
}

Expected behavior

RequiresUnreferencedCodeAttribute or UnconditionalSuppressMessageAttribute on Test should suppress the warning.

Actual behavior

ILC : Trim analysis warning IL2026: Program.MethodWithGenericWhichRequiresMethods<TypeWithMethodWithRequires>(): Using member 'TypeWithMethodWithRequires.MethodWithRequires()' which has 'RequiresUnreferencedCodeAttribute' can break functionality when trimming application code.

Regression?

No response

Known Workarounds

No response

Configuration

No response

Other information

No response

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions