dotnet / runtime

.NET is a cross-platform runtime for cloud, mobile, desktop, and IoT apps.
https://docs.microsoft.com/dotnet/core/
MIT License
14.97k stars 4.66k forks source link

[API Proposal]: Add the ability to trim custom attributes #102802

Open vyacheslav-volkov opened 3 months ago

vyacheslav-volkov commented 3 months ago

Background and motivation

I'm using JetBrains.Annotations as internal classes in my library and since trimmer doesn't know about these attributes they will be preserved in the final build, but since these attributes are only for code annotations I think it would be nice to have some sort of mechanism, allowing you to mark them as safe for trimming. I suggest adding a TrimmableAttributeAttribute which we can use to mark custom code annotation attributes and the linker will know that this attribute can be safely trimmed.

API Proposal

namespace System.Diagnostics.CodeAnalysis;

[AttributeUsage(AttributeTargets.Class)]
public sealed class TrimmableAttributeAttribute : Attribute;

API Usage

[AttributeUsage(AttributeTargets.Parameter)]
[TrimmableAttribute]
public class RequireStaticDelegateAttribute : Attribute
{
    public bool IsError { get; set; }
}

Alternative Designs

No response

Risks

No response

dotnet-policy-service[bot] commented 3 months ago

Tagging subscribers to this area: @dotnet/area-system-componentmodel-dataannotations See info in area-owners.md if you want to be subscribed.

dotnet-policy-service[bot] commented 3 months ago

Tagging subscribers to this area: @agocke, @sbomer, @vitek-karas See info in area-owners.md if you want to be subscribed.

hez2010 commented 3 months ago

Why not just add a property to AttributeUsageAttribute?

[AttributeUsage(AttributeTargets.Parameter, Trimmable = true)]
public class RequireStaticDelegateAttribute : Attribute
{
    public bool IsError { get; set; }
}
colejohnson66 commented 3 months ago

JetBrains.Annotations attributes are marked with [Conditional("JETBRAINS_ANNOTATIONS")]. They will not be in your final build. You can also mark your own attributes as conditional.

vyacheslav-volkov commented 3 months ago

Why not just add a property to AttributeUsageAttribute?

@hez2010 If this can be added to the base class it would be great.

JetBrains.Annotations attributes are marked with [Conditional("JETBRAINS_ANNOTATIONS")]. They will not be in your final build. You can also mark your own attributes as conditional.

@colejohnson66 If you want to ship your library to other developers, you will have to include them as internal classes or compile with JETBRAINS_ANNOTATIONS flag set.

When building a library that is intended to be shipped to others, we can include the JetBrains Annotations source code directly in our project (or compile with the JETBRAINS_ANNOTATIONS flag set). https://blog.jetbrains.com/dotnet/2018/05/03/what-are-jetbrains-annotations/

Another example: many source generators use their own marker attributes just to generate sources, and these can be safely trimmed, the solution with ConditionalAttribute does not solve all cases.

jkotas commented 3 months ago

Many attributes are trimmable in some scenarios and not in other scenarios. The nullability attributes are a good example. They are trimmable unless the app uses NullabilityInfoContextSupport. (#88805 tracks the problems around this.)