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 class as a target for RequiresUnreferencedCodeAttribute #50122

Closed
tlakollo opened this issue Mar 23, 2021 · 5 comments
Closed

Add class as a target for RequiresUnreferencedCodeAttribute #50122

tlakollo opened this issue Mar 23, 2021 · 5 comments
Assignees
Labels
api-approved API was approved in API review, it can be implemented area-AssemblyLoader-coreclr blocking Marks issues that we want to fast track in order to unblock other important work linkable-framework Issues associated with delivering a linker friendly framework

Comments

@tlakollo
Copy link
Contributor

tlakollo commented Mar 23, 2021

Background and Motivation

As explained in dotnet/linker#1742 some types are considered not trim compatible leading to mark all its member methods with RequiresUnreferencedCode. This could lead to hundreds of members annotated with RequiresUnreferencedCode. The proposal is to only annotate the type with RequiresUnreferencedCode, then the linker will treat this as if we annotated constructors and static methods with RequiresUnreferencedCode.

Proposed API

 namespace System.Diagnostics.CodeAnalysis
 {
     [AttributeUsage(
+        AttributeTargets.Class |
         AttributeTargets.Constructor |
         AttributeTargets.Method,
         Inherited = false)]
     public sealed class RequiresUnreferencedCodeAttribute : Attribute
     {
     }
 }

Usage Examples

[RequiresUnreferencedCode ("Class MyType has been marked as dangerous for trimming", Url = "https://helpurl/MyType")]
public class MyType {
  public static void M1 () {}
  public static void M2 () {}
}

public class OtherType {
  public void Main () {
    M1();     -> This creates a warning IL2026
    M2();     -> This creates a warning IL2026
  }
}

Alternative Designs

Apply to other AttributeTargets like struct and interface. As per @MichalStrehovsky comments, Structs are problematic because they can be default-instantiated. We would like this to be considered unsafe (and warn) only if a constructor or a static method on the type is called to limit the amount of noise (and additional suppressions needed) when a type is annotated as such.
If we allow this on structs, we would have to warn on instance methods too, plus it would introduce problems when unsafe instance methods get called indirectly through interfaces (the tool would not be able to see that at all).

Since this enhancement is meant to help people to annotate fewer methods seems to be a good option to start just by enabling it in classes, but this issue is meant to discuss which targets we want to enable.

Risks

Related PR

Related to #50064

@tlakollo tlakollo added api-suggestion Early API idea and discussion, it is NOT ready for implementation linkable-framework Issues associated with delivering a linker friendly framework labels Mar 23, 2021
@tlakollo tlakollo self-assigned this Mar 23, 2021
@ghost
Copy link

ghost commented Mar 23, 2021

Tagging subscribers to 'linkable-framework': @eerhardt, @vitek-karas, @LakshanF, @tannergooding, @sbomer
See info in area-owners.md if you want to be subscribed.

Issue Details

Background and Motivation

As explained in dotnet/linker#1742 some types are considered not trim compatible leading to mark all its member methods with RequiresUnreferencedCode. This could lead to hundreds of members annotated with RequiresUnreferencedCode. The proposal is to only annotate the type with RequiresUnreferencedCode, then the linker will treat this as if we annotated constructors and static methods with RequiresUnreferencedCode.

Proposed API

- [System.AttributeUsageAttribute(System.AttributeTargets.Constructor | System.AttributeTargets.Method, Inherited=false)]
+ [System.AttributeUsageAttribute(System.AttributeTargets.Class | System.AttributeTargets.Constructor | System.AttributeTargets.Method, Inherited=false)]
#if SYSTEM_PRIVATE_CORELIB
    public
#else
    internal
#endif
        sealed class RequiresUnreferencedCodeAttribute : Attribute

Usage Examples

[RequiresUnreferencedCode ("Class MyType has been marked as dangerous for trimming", Url = "https://helpurl/MyType")]
public class MyType {
  public static void M1 () {}
  public static void M2 () {}
}

public class OtherType {
  public void Main () {
    M1();     -> This creates a warning IL2026
    M2();     -> This creates a warning IL2026
  }
}

Alternative Designs

Apply to other AttributeTargets like struct and interface. As per @MichalStrehovsky comments, Structs are problematic because they can be default-instantiated. We would like this to be considered unsafe (and warn) only if a constructor or a static method on the type is called to limit the amount of noise (and additional suppressions needed) when a type is annotated as such.
If we allow this on structs, we would have to warn on instance methods too, plus it would introduce problems when unsafe instance methods get called indirectly through interfaces (the tool would not be able to see that at all).

Since this enhancement is meant to help people to annotate fewer methods seems to be a good option to start just by enabling it in classes, but this issue is meant to discuss which targets we want to enable.

Risks

Related to #50064

Author: tlakollo
Assignees: tlakollo
Labels:

api-suggestion, linkable-framework

Milestone: -

@dotnet-issue-labeler
Copy link

I couldn't figure out the best area label to add to this issue. If you have write-permissions please help me learn by adding exactly one area label.

@ghost
Copy link

ghost commented Mar 24, 2021

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

Issue Details

Background and Motivation

As explained in dotnet/linker#1742 some types are considered not trim compatible leading to mark all its member methods with RequiresUnreferencedCode. This could lead to hundreds of members annotated with RequiresUnreferencedCode. The proposal is to only annotate the type with RequiresUnreferencedCode, then the linker will treat this as if we annotated constructors and static methods with RequiresUnreferencedCode.

Proposed API

- [System.AttributeUsageAttribute(System.AttributeTargets.Constructor | System.AttributeTargets.Method, Inherited=false)]
+ [System.AttributeUsageAttribute(System.AttributeTargets.Class | System.AttributeTargets.Constructor | System.AttributeTargets.Method, Inherited=false)]
#if SYSTEM_PRIVATE_CORELIB
    public
#else
    internal
#endif
        sealed class RequiresUnreferencedCodeAttribute : Attribute

Usage Examples

[RequiresUnreferencedCode ("Class MyType has been marked as dangerous for trimming", Url = "https://helpurl/MyType")]
public class MyType {
  public static void M1 () {}
  public static void M2 () {}
}

public class OtherType {
  public void Main () {
    M1();     -> This creates a warning IL2026
    M2();     -> This creates a warning IL2026
  }
}

Alternative Designs

Apply to other AttributeTargets like struct and interface. As per @MichalStrehovsky comments, Structs are problematic because they can be default-instantiated. We would like this to be considered unsafe (and warn) only if a constructor or a static method on the type is called to limit the amount of noise (and additional suppressions needed) when a type is annotated as such.
If we allow this on structs, we would have to warn on instance methods too, plus it would introduce problems when unsafe instance methods get called indirectly through interfaces (the tool would not be able to see that at all).

Since this enhancement is meant to help people to annotate fewer methods seems to be a good option to start just by enabling it in classes, but this issue is meant to discuss which targets we want to enable.

Risks

Related PR

Related to #50064

Author: tlakollo
Assignees: tlakollo
Labels:

api-suggestion, area-AssemblyLoader-coreclr, linkable-framework, untriaged

Milestone: -

@terrajobst terrajobst added api-ready-for-review API is ready for review, it is NOT ready for implementation blocking Marks issues that we want to fast track in order to unblock other important work and removed api-suggestion Early API idea and discussion, it is NOT ready for implementation untriaged New issue has not been triaged by the area owner labels Mar 29, 2021
@bartonjs
Copy link
Member

bartonjs commented Mar 30, 2021

Video

  • Looks good as proposed
  • We discussed the Struct and Interface targets, and felt that while they'll eventually show up we shouldn't add the usage until we have an implementation backing them up.
 namespace System.Diagnostics.CodeAnalysis
 {
     [AttributeUsage(
+        AttributeTargets.Class |
         AttributeTargets.Constructor |
         AttributeTargets.Method,
         Inherited = false)]
     public sealed class RequiresUnreferencedCodeAttribute : Attribute
     {
     }
 }

@bartonjs bartonjs added api-approved API was approved in API review, it can be implemented and removed api-ready-for-review API is ready for review, it is NOT ready for implementation labels Mar 30, 2021
@tlakollo
Copy link
Contributor Author

tlakollo commented Apr 1, 2021

Closing via #50064

@tlakollo tlakollo closed this as completed Apr 1, 2021
@ghost ghost locked as resolved and limited conversation to collaborators May 1, 2021
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-AssemblyLoader-coreclr blocking Marks issues that we want to fast track in order to unblock other important work linkable-framework Issues associated with delivering a linker friendly framework
Projects
None yet
Development

No branches or pull requests

4 participants