dotnet / runtime

.NET is a cross-platform runtime for cloud, mobile, desktop, and IoT apps.
https://docs.microsoft.com/dotnet/core/
MIT License
14.98k stars 4.66k forks source link

ILLink: RUC on derived virtual should not warn if base class has RUC #108090

Open sbomer opened 3 hours ago

sbomer commented 3 hours ago

Consider a type Base that has RequiresUnreferencedCode:

[RequiresUnreferencedCode("Base")]
class Base {
    public Base() {}

    public virtual void Instance() {}
}

It should be possible to annotate a derived class, without necessarily marking all static methods in the derived class as RUC:

class Derived : Base {
    [RequiresUnreferencedCode("Base")]
    public Derived() {}

    [RequiresUnreferencedCode("Instance")] // warning: annotation mismatch
    public override void Instance() => Helper.RUC();

    public static void Static() {}
}

[RequiresUnreferencedCode("Helper")]
class Helper {
    public static void RUC() {}
}

Today ILLink warns about mismatching annotations on Derived.Instance and Base.Instance:

Trim analysis warning IL2046: Derived.Instance(): Member 'Derived.Instance()' with 'RequiresUnreferencedCodeAttribute' overrides base member 'Base.Instance()' without 'RequiresUnreferencedCodeAttribute'. 'RequiresUnreferencedCodeAttribute' annotations must match across all interface implementations or overrides.

This should not happen. The analyzer and ILC don't produce the same warning.

(note that this scenario also produces a separate warning about Derived deriving from Base, which should also be removed, see https://github.com/dotnet/runtime/issues/107660.)

dotnet-policy-service[bot] commented 3 hours ago

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