Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add DynamicallyAccessedMembersAttribute #33861

Closed
MichalStrehovsky opened this issue Mar 20, 2020 · 7 comments · Fixed by #35387
Closed

Add DynamicallyAccessedMembersAttribute #33861

MichalStrehovsky opened this issue Mar 20, 2020 · 7 comments · Fixed by #35387
Assignees
Labels
api-approved API was approved in API review, it can be implemented area-System.Runtime.CompilerServices linkable-framework Issues associated with delivering a linker friendly framework
Milestone

Comments

@MichalStrehovsky
Copy link
Member

MichalStrehovsky commented Mar 20, 2020

This is in support of dotnet/linker#988 - see the spec there for motivation.

namespace System.Runtime.CompilerServices
{
    [AttributeUsage(
        AttributeTargets.Field |
        AttributeTargets.ReturnValue |
        AttributeTargets.GenericParameter |
        AttributeTargets.Parameter |
        AttributeTargets.Property)]
    public sealed class DynamicallyAccessedMembersAttribute : Attribute
    {
        public DynamicallyAccessedMembersAttribute(DynamicallyAccessedMemberKinds memberKinds)
        {
            MemberKinds = memberKinds;
        }

        public DynamicallyAccessedMemberKinds MemberKinds { get; }
    }

    [Flags]
    public enum DynamicallyAccessedMemberKinds
    {
        DefaultConstructor  = 0b00000000_00000001,
        PublicConstructors  = 0b00000000_00000011,
        Constructors        = 0b00000000_00000111,
        PublicMethods       = 0b00000000_00001000,
        Methods             = 0b00000000_00011000,
        PublicFields        = 0b00000000_00100000,
        Fields              = 0b00000000_01100000,
        PublicNestedTypes   = 0b00000000_10000000,
        NestedTypes         = 0b00000001_10000000,
        PublicProperties    = 0b00000010_00000000,
        Properties          = 0b00000110_00000000,
        PublicEvents        = 0b00001000_00000000,
        Events              = 0b00011000_00000000,
    }
}
@MichalStrehovsky MichalStrehovsky added api-suggestion Early API idea and discussion, it is NOT ready for implementation area-System.Runtime.CompilerServices labels Mar 20, 2020
@MichalStrehovsky MichalStrehovsky added this to the 5.0 milestone Mar 20, 2020
@MichalStrehovsky MichalStrehovsky self-assigned this Mar 20, 2020
@Dotnet-GitSync-Bot Dotnet-GitSync-Bot added the untriaged New issue has not been triaged by the area owner label Mar 20, 2020
@MichalStrehovsky MichalStrehovsky added linkable-framework Issues associated with delivering a linker friendly framework and removed untriaged New issue has not been triaged by the area owner labels Mar 20, 2020
@vitek-karas
Copy link
Member

Looks good - I would add an overload with a message. Being able to customize the UX when this is reported as potential issue by the linker will be very valuable I think.

@vitek-karas
Copy link
Member

/cc @eerhardt

@MichalStrehovsky MichalStrehovsky added api-ready-for-review and removed api-suggestion Early API idea and discussion, it is NOT ready for implementation labels Apr 2, 2020
@vitek-karas
Copy link
Member

/cc @marek-safar

@terrajobst terrajobst added blocking Marks issues that we want to fast track in order to unblock other important work api-approved API was approved in API review, it can be implemented and removed api-ready-for-review blocking Marks issues that we want to fast track in order to unblock other important work labels Apr 22, 2020
@terrajobst
Copy link
Member

terrajobst commented Apr 23, 2020

Video

  • What about MetadataLoadContext?
    • Will need to make use of supressions, there is a separate attribute for that
  • It feels the defaults are flipped
    • We should have PublicMethods and NonPublicMethods to make it clear what's being returned
    • The semantics should match BindingFlags
  • Are attributes kept?
    • Depends, but there is a discussion on trimming those too
  • Should the enum have an "All" member?
  • We should have a None member with value 0
  • We should make sure the attribute can only be applied once is not inherited
  • The linker tooling should warn if the attribute is applied to things that aren't System.Type or System.String, the linker should warn
namespace System.Diagnostics.CodeAnalysis
{
    [AttributeUsage(
        AttributeTargets.Field |
        AttributeTargets.ReturnValue |
        AttributeTargets.GenericParameter |
        AttributeTargets.Parameter |
        AttributeTargets.Property)]
    public sealed class DynamicallyAccessedMembersAttribute : Attribute
    {
        public DynamicallyAccessedMembersAttribute(DynamicallyAccessedMemberKinds memberKinds)
        {
            MemberKinds = memberKinds;
        }

        public DynamicallyAccessedMemberKinds MemberKinds { get; }
    }

    [Flags]
    public enum DynamicallyAccessedMemberKinds
    {
        None                         = 0b00000000_00000000,
        DefaultConstructor           = 0b00000000_00000001,
        PublicConstructors           = 0b00000000_00000011,
        NonPublicConstructors        = 0b00000000_00000100,
        PublicMethods                = 0b00000000_00001000,
        NonPublicMethods             = 0b00000000_00010000,
        PublicFields                 = 0b00000000_00100000,
        NonPublicFields              = 0b00000000_01000000,
        PublicNestedTypes            = 0b00000000_10000000,
        NonPublicNestedTypes         = 0b00000001_00000000,
        PublicProperties             = 0b00000010_00000000,
        NonPublicProperties          = 0b00000100_00000000,
        PublicEvents                 = 0b00001000_00000000,
        NonPublicEvents              = 0b00010000_00000000,
    }
}

@eerhardt
Copy link
Member

@terrajobst - I made some edits to your pasted API from the review.

  1. Updated the namespace
  2. Updated the names of the enum to be Public and NonPublic.
  3. Update the values of the enum to match the names.

@eerhardt eerhardt self-assigned this Apr 23, 2020
@eerhardt
Copy link
Member

One tiny naming question.

We already have System.Reflection.MemberTypes. Would it make sense to name this enum DynamicallyAccessedMemberTypes instead of DynamicallyAccessedMemberKinds to align with the existing enum?

@terrajobst @stephentoub @bartonjs @GrabYourPitchforks @MichalStrehovsky @vitek-karas ?

@stephentoub
Copy link
Member

Would it make sense to name this enum DynamicallyAccessedMemberTypes instead of DynamicallyAccessedMemberKinds to align with the existing enum?

Sounds fine to me.

eerhardt added a commit to eerhardt/runtime that referenced this issue Apr 24, 2020
DynamicallyAccessedMembersAttribute
DynamicDependencyAttribute
UnconditionalSuppressMessageAttribute

Fix dotnet#33861, dotnet#35339, dotnet#30902
eerhardt added a commit that referenced this issue Apr 28, 2020
* Add new attributes for ILLink analysis.

DynamicallyAccessedMembersAttribute
DynamicDependencyAttribute
UnconditionalSuppressMessageAttribute

Fix #33861, #35339, #30902
@ghost ghost locked as resolved and limited conversation to collaborators Dec 10, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
api-approved API was approved in API review, it can be implemented area-System.Runtime.CompilerServices linkable-framework Issues associated with delivering a linker friendly framework
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants