Skip to content

AOT linker can be smarter about custom attributes #126278

@DoctorKrolic

Description

@DoctorKrolic

Compile this code with NativeAOT and check linker report:

using System.ComponentModel;
using System.Reflection;
using System.Runtime.CompilerServices;

Console.WriteLine(GetDisplayName<MyClass>());

[MethodImpl(MethodImplOptions.NoInlining)]
static string? GetDisplayName<T>()
{
    var attr = typeof(T).GetCustomAttribute<DisplayNameAttribute>();
    return attr?.DisplayName;
}

[DisplayName("Test"), Dummy]
public class MyClass
{
}

public class DummyAttribute : Attribute
{
}

Expected:
DymmyAttribute is not included into the final executable since it is "never" read (I guess GetCustomAttribute<T> enumerates all attributes to find the requested one, but this is an implementation detail and linker can be taught to be smarter about it)

Actual:
DymmyAttribute is included into the final executable

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    Status

    No status

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions