Fix trim analyzer warning for inferred type arguments (#87156)
authorSven Boemer <sbomer@gmail.com>
Mon, 12 Jun 2023 16:51:49 +0000 (09:51 -0700)
committerGitHub <noreply@github.com>
Mon, 12 Jun 2023 16:51:49 +0000 (09:51 -0700)
commit91cc34c9d1d884eaf71e1f33b86094071d5f1404
treed1c5d61b08472760f3f468fd4aee8559cffdc367
parent7b25983451968a7edfeba78a3a16c050dba12a4f
Fix trim analyzer warning for inferred type arguments (#87156)

Fixes https://github.com/dotnet/runtime/issues/86032.

This replaces the syntax-based logic with:

- ISymbol-based logic that looks at generic instantiations in
  parameter types, return types, and base/interface types

- IOperation-based logic that looks at invocations

The syntax-based logic used to also warn for the types of local
variables, but `SymbolKind.Local` isn't supported for
`RegisterSymbolAction`. Since this falls into the category of
warnings that isn't strictly necessary (based on nativeaot logic
which only warns on generic instantiations that can actually lead
to code execution), I just left out local variables.

I'm slightly concerned that this could be missing some cases, but
at least it works on our existing testcases. This also adds tests
for delegate creation using generic methods, which was one such
missing case noticed during code review.

There are two slight differences in warning locations, which I
don't expect to cause issues for warning suppressions:

- For invocations, the warning location used to be the generic
  method name, without the argument list. Using the
  IOperation-based approach, we use the invocation's location
  that includes the argument list.

- For properties that use arrow syntax (`RequireMethods<TFields>
  Property => null`), the ISymbol-based approach warns on the
  underlying getter method, whose location seems to be the body
  of the accessor. We used to warn on the return type
  specifically, but I couldn't find a better way to get the
  location.
src/tools/illink/src/ILLink.RoslynAnalyzer/DynamicallyAccessedMembersAnalyzer.cs
src/tools/illink/src/ILLink.RoslynAnalyzer/README.md
src/tools/illink/test/ILLink.RoslynAnalyzer.Tests/DynamicallyAccessedMembersAnalyzerTests.cs
src/tools/illink/test/ILLink.RoslynAnalyzer.Tests/DynamicallyAccessedMembersCodeFixTests.cs
src/tools/illink/test/Mono.Linker.Tests.Cases/DataFlow/GenericParameterDataFlow.cs
src/tools/illink/test/Mono.Linker.Tests.Cases/RequiresCapability/RequiresViaDataflow.cs
src/tools/illink/test/Mono.Linker.Tests/TestCasesRunner/ResultChecker.cs