dotnet / runtime

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

Incorrect IL2026 for [RequiresUnreferencedCode] on type deriving from A<B> where B has new() in A<T> definition #108507

Open Sergio0694 opened 3 weeks ago

Sergio0694 commented 3 weeks ago

Description

Spotted what seems to be an incorrect trim warning that's not suppressed correctly.

Reproduction Steps

[RequiresUnreferencedCode("")]
class A;

class B<T> where T : new();

[RequiresUnreferencedCode("")]
class C : B<A>;

Expected behavior

No warnings.

Actual behavior

image

Regression?

No, can repro on .NET 8 as well.

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

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

sbomer commented 3 weeks ago

Related: https://github.com/dotnet/runtime/issues/108523

There are two problems here:

sbomer commented 3 weeks ago

An additional problem we might want to solve as part of this:

class C : B<A>
{
    public C()
    {
    }
}

In this case the analyzer misses the warning due to the implicit call to the base constructor. Note that the following example does produce a wartning:

[RequiresUnreferencedCode]
class Requires {}

class Derived : Requires
{
    public Derived // warning IL2026
    {
    }
}

The logic for checking the new constraint should be moved into the analyzer's dataflow logic where the implicit base constructor call is visible.