Skip to content

Trimmer hint - Type.AssumeAssignableTo #49448

Closed

Description

@LakshanF

Background and Motivation

[See this issue and specifically the conversation from here to get more context]. The API enhancement proposal #49465 will work well with this proposal to provide validation and enforcement to a problematic coding pattern for the trimmer to reason about correctly as described below.

There are many scenarios in .NET where code uses a type in a polymorphic manner that makes it harder for the trimmer, as a static analysis tool, to determine the exact type. This is very common where an interface or a base class type is used in the code to refer to a derived type. A helper function similar to IsAssignableTo but one that returns a type will help the trimmer to apply parent type annotation to the full inheritance graph of the parent type. This method can be added as an extension method in the System.Diagnostic.CodeAnalysis namespace with other Trimmer helper types.

Proposed API

namespace System.Diagnostics.CodeAnalysis
{
+   public static class TypeExensions {
+             public static Type AssumeAssignableTo(this Type type, Type? targetType) {
     }
}

The API should be similar to Type.IsAssignable except return the same type if assignable to targetType or throw otherwise

Usage Examples

This API will be used by developers who want to trim their applications but need to use problematic code patterns that are difficult for the trimmer to reason about at trim time. By using this API, the trimmer will preserve all methods of all derived types of the targetType.

// New code
Type someType = GetTypeFromSomewhere().AssumeAssignableTo(typeof(EventSource));
// AssumeAssignableTo validated at runtime that the type passed to it implements the base type or an interface
// It also now allows the trimmer to validate that the type in question implements the base type or an interface

// Trimmer sees we're calling GetMethods on something that derives from EventSource. It needs to
// go over all EventSource-implementing types that were marked and keep their methods.
foreach (var method in someType.GetMethods())
{
    // ...
}

Alternative Designs

Option 1: Use the existing Type API, IsAssignableTo : The trimmer does not track branch level analysis for types, and even with such support, the usage scenario is going to be very challenging.

Option 2: Proposal #49465 on its own will help solve a subset of the problem

Option 3: The long-term recommendation is to use source generators in such scenarios but the support to make it easier for developers to use source generators are not planned in the near term.

Risks

This API might cause more code to be preserved than intended since it applies to all derived types of the targetType. This can be mitigated through requiring an attribute to be also applied to the parent type.

Activity

  1. added
    api-suggestion
    on Mar 10, 2021
  2. ghost added
    untriaged
    on Mar 10, 2021
  3. LakshanF commented on Mar 10, 2021

    @LakshanF
    ContributorAuthor
  4. LakshanF commented on Mar 22, 2021

    @LakshanF
    ContributorAuthor

    With #49465 approved and mostly unblocks the basic scenarios related to object.GetType(). Once the trimmer support is added (currently in progress) to recognize the attribute usage on classes, this API request is not a high-priority for the trimmer team in 6.0.

  5. eerhardt commented on May 10, 2021

    @eerhardt
    Member

    Can we close this issue now that #49465 has been fixed?

  6. MichalStrehovsky commented on May 10, 2021

    @MichalStrehovsky
    Member

    I'm not aware of a scenario for this right now - we addressed what we had with #49465. This one is kind of more powerful, but if there's no scenario to validate it on, we should close. (Reopen if anyone has one.)

  7. ghost locked as resolved and limited conversation to collaborators on Jun 9, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    api-suggestionarea-TypeSystem-coreclruntriaged

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions