Skip to content
This repository was archived by the owner on Sep 25, 2026. It is now read-only.
This repository was archived by the owner on Sep 25, 2026. It is now read-only.

Analyzer: RUC/RDC on type triggers override mismatch warnings #2763

Closed

Description

@vitek-karas
public class Base
{
    public virtual void Method() {}
}

[RequiresUnreferencedCode("RUC")]
[RequiresDynamicCode("RDC")]
public class Derived : Base
{
    // IL2046 - Analyzer only
    // IL3051
    public override void Method() {}
}

RUC on type should mean:

  • RUC on all statics and .ctors
  • Suppress trim analysis warnings in all method bodies

But the analyzer in this case treats it as if it has RUC on virtual methods as well.
The IL2046 is expected if the derived method as RUC on it, but the class as a whole should not trigger this warning since it doesn't imply any changes to instance methods as seen from outside (calling instance methods on RUC annotated type does not produce warnings, only .ctors do).

This prevents usage of the RUC/RDC on type feature in runtime repo. There are quite a few examples in System.Text.Json specifically - for example: https://github.com/dotnet/runtime/blob/main/src/libraries/System.Text.Json/src/System/Text/Json/Nodes/JsonValueNotTrimmable.cs. Instead of having a RUC on .ctor and suppression on virtual method, this should be done with RUC on type alone.

Activity

  1. vitek-karas commented on Apr 26, 2022

    @vitek-karas
    MemberAuthor

    PR #2764 adds a test for this case.

  2. vitek-karas commented on Apr 26, 2022

    @vitek-karas
    MemberAuthor
  3. tlakollo commented on Apr 26, 2022

    @tlakollo
    Contributor

    What about the following:

    class Base
    {
    	[RequiresUnreferencedCode ("RUC")]
    	[RequiresDynamicCode ("RDC")]
    	public virtual void Method () { }
    }
    
    [RequiresUnreferencedCode ("RUC")]
    [RequiresDynamicCode ("RDC")]
    class Derived : Base
    	public override void Method () { }
    }

    Base has the attribute in the method directly, which means that it will look to have the same annotation in the Derived. Derived uses a type annotation but doesn't cover the method because is not static. Is this something we want?

  4. sbomer commented on Apr 26, 2022

    @sbomer
    Member

    I would think that the example you gave @tlakollo would not produce any warnings - effectively, RUC on type does mean that the virtual methods are "unsafe" to execute, but we just don't warn for calls to them, because we are blaming the constructor instead. Similarly in the original issue, there is effectively a mismatch, but there's no way to execute the override without calling the constructor, so blaming it means we don't need to produce warnings about the "mismatch".

  5. tlakollo commented on Apr 26, 2022

    @tlakollo
    Contributor

    I think the two scenarios collide with each other. The question to be answered is: Is the method in the derived class in the Requires scope? In the first scenario, we want to answer no because the method is not static. In the second example, the answer should be the same, the difference being that Base is in the Requires Scope which should create a mismatch warning.

  6. sbomer commented on Apr 26, 2022

    @sbomer
    Member

    I realize it's implemented that way currently, but I think we need to add some logic that checks for RUC on type. If the derived class has RUC on the type it shouldn't produce this warning. I think the same is probably true of the base type - since we already produce a warning at the type level.

  7. vitek-karas commented on Apr 26, 2022

    @vitek-karas
    MemberAuthor

    I think the logic is split:

    • A method can create a "RUC" requirement - that is "I the method am dangerous, anybody who can call me needs to warn"
    • A method can fulfill a "RUC" requirement - that is "You can't get to me without triggering a warning"

    The creation of RUC requirement is:

    • Method which has a RUC on it creates a requirement
    • Static method on a RUC annotated type creates a requirement
    • .ctor on a RUC annotated type creates a requirement

    The fulfillment of RUC requirement is:

    • Method which has a RUC on it fulfills a requirement
    • Any method on a RUC annotated type fulfills requirement

    Notice the different, only static/.ctor methods on RUC types create requirements, but all methods on RUC types fulfill them.
    So for the override it means - only a method with RUC directly on it creates a requirement and needs to check that its base/overrides fulfill the requirement.

    (We will have to think carefully about static abstract methods on interfaces - as those are static but also virtual... so weird - but that's and edge case for now).

  8. added a commit that references this issue on Apr 26, 2022
    6ffb647
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions