You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Trim Analyzer doesn't handle annotations on inferred generic arguments #86032
publicclassTestType{[RequiresUnreferencedCode("Dangerous")]publicvoidDangerous(){}}publicstaticvoidInvokeDangerous<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicMethods)]T>(Tinstance){typeof(T).GetMethod("Dangerous").Invoke(instance,Array.Empty<object>());}// IL2026 - DangerousInvokeDangerous<TestType>(newTestType());// No warningInvokeDangerous(newTestType());
When the generic argument is inferred, the analyzer doesn't react to it in any way and doesn't produce any diagnostics. See the above sample, calling a generic method with the argument explicitly specified produces diagnostics, but when it's inferred no diagnostics are produced.
The reason for this bug is that we use the syntax nodes to detect generic parameter usage in the analyzed code. But the inferred usage will not show up in the syntax tree (since it's missing). Not sure what is the correct way to handle this in the analyzer. Maybe we can detect most cases during our method body pass where we will see the inferred symbol.
Activity
Add a test for dotnet#86032
vitek-karas commentedon May 10, 2023
The reason for this bug is that we use the syntax nodes to detect generic parameter usage in the analyzed code. But the inferred usage will not show up in the syntax tree (since it's missing). Not sure what is the correct way to handle this in the analyzer. Maybe we can detect most cases during our method body pass where we will see the inferred symbol.
agocke commentedon May 10, 2023
We could look for generic instantiations on invocations when we have an IOperation tree in hand.
vitek-karas commentedon May 10, 2023
That's what I was trying to say with my above comment, unfortunately I used the wrong terms :-(.
agocke commentedon May 10, 2023
FYI you could also look at symbols using the semantic model, but I thought it might be easier to just use the IOperation visitor
Add tests for RUC warnings on override methods (#86015)
Fix trim analyzer warning for inferred type arguments (#87156)