Skip to content

Trim Analyzer doesn't handle annotations on inferred generic arguments #86032

Closed

Description

@vitek-karas
public class TestType
{
    [RequiresUnreferencedCode("Dangerous")]
    public void Dangerous() {}
}

public static void InvokeDangerous<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicMethods)] T>(T instance)
{
    typeof(T).GetMethod("Dangerous").Invoke(instance, Array.Empty<object>());
}

// IL2026 - Dangerous
InvokeDangerous<TestType>(new TestType());

// No warning
InvokeDangerous(new TestType());

When the generic argument is inferred, the analyzer doesn't react to it in any way and doesn't produce any diagnostics. See the above sample, calling a generic method with the argument explicitly specified produces diagnostics, but when it's inferred no diagnostics are produced.

Activity

  1. added
    area-Tools-ILLink
    on May 10, 2023
  2. ghost added
    untriaged
    on May 10, 2023
  3. added a commit that references this issue on May 10, 2023
  4. vitek-karas commented on May 10, 2023

    @vitek-karas
    MemberAuthor

    The reason for this bug is that we use the syntax nodes to detect generic parameter usage in the analyzed code. But the inferred usage will not show up in the syntax tree (since it's missing). Not sure what is the correct way to handle this in the analyzer. Maybe we can detect most cases during our method body pass where we will see the inferred symbol.

  5. agocke commented on May 10, 2023

    @agocke
    Member

    We could look for generic instantiations on invocations when we have an IOperation tree in hand.

  6. vitek-karas commented on May 10, 2023

    @vitek-karas
    MemberAuthor

    We could look for generic instantiations on invocations when we have an IOperation tree in hand.

    That's what I was trying to say with my above comment, unfortunately I used the wrong terms :-(.

  7. agocke commented on May 10, 2023

    @agocke
    Member

    FYI you could also look at symbols using the semantic model, but I thought it might be easier to just use the IOperation visitor

  8. ghost added
    in-pr
    on Jun 5, 2023
  9. ghost removed
    in-pr
    on Jun 12, 2023
  10. ghost removed
    untriaged
    on Jun 12, 2023
  11. ghost locked as resolved and limited conversation to collaborators on Jul 13, 2023
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

    area-Tools-ILLink

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions