dotnet / runtime

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

Inconsistent warning code for array parameter passed to annotated location in ILLink/analyzer #96646

Open sbomer opened 6 months ago

sbomer commented 6 months ago
static void Method(Type[] interfaces) {
    foreach (Type i in interfaces)
        i.GetMethods(); // warning here. IL2075 for analyzer, IL2065 for ILLink
}

The ILLink Roslyn analyzer produces IL2075 (unannotated method return value flows to 'this' parameter with requirements), because it sees the return value of Current.get used as the receiver for GetMethods.

ILLink produces IL2065 ('this' parameter cannot be statically determined), because when it sees ldelem.ref of the array method parameter, it turns this into an "unknown" value, which has a different warning code.

Fundamentally this is due to the analyzer seeing a different lowering of the foreach loop than illink. Fixing this seems tricky.

Found while investigating https://github.com/dotnet/runtime/pull/96462. There's an existing testcase similar to this:

https://github.com/dotnet/runtime/blob/0451127421fe7862f0e8407eafd6052d7ea3b175/src/tools/illink/test/Mono.Linker.Tests.Cases/DataFlow/LocalDataFlow.cs#L443-L453

ghost commented 6 months ago

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

Issue Details
```csharp static void Method(Type[] interfaces) { foreach (Type i in interfaces) i.GetMethods(); // warning here. IL2075 for analyzer, IL2065 for ILLink } ``` The ILLink Roslyn analyzer produces IL2075 (unannotated method return value flows to 'this' parameter with requirements), because it sees the return value of `Current.get` used as the receiver for `GetMethods`. ILLink produces IL2065 ('this' parameter cannot be statically determined), because when it sees `ldelem.ref` of the array method parameter, it turns this into an "unknown" value, which has a different warning code. Fundamentally this is due to the analyzer seeing a different lowering of the foreach loop than illink. Fixing this seems tricky.
Author: sbomer
Assignees: -
Labels: `untriaged`, `area-Tools-ILLink`, `needs-area-label`
Milestone: -