Fix trimming analyzer missing diagnostics for method calls in field access expressions#123040
Fix trimming analyzer missing diagnostics for method calls in field access expressions#123040
Conversation
…s for method calls Co-authored-by: sbomer <787361+sbomer@users.noreply.github.com>
Co-authored-by: sbomer <787361+sbomer@users.noreply.github.com>
|
@copilot fix error: '/__w/1/s/src/tools/illink/test/Mono.Linker.Tests.Cases.Expectations/Support/DynamicallyAccessedMembersAttribute.cs'.\n/__w/1/s/artifacts/bin/ILLink.testcases/linker_tests/DataFlow/GetInterfaceDataFlow/GetInterfaceDataFlow.cs(197,55): error CS1061: 'Type[]' does not contain a definition for 'First' and no accessible extension method 'First' accepting a first argument of type 'Type[]' could be found (are you missing a using directive or an assembly reference?)\n/__w/1/s/artifacts/bin/ILLink.testcases/linker_tests/DataFlow/GetInterfaceDataFlow/GetInterfaceDataFlow.cs(209,55): error CS1061: 'Type[]' does not contain a definition for 'First' and no accessible extension method 'First' accepting a first argument of type 'Type[]' could be found (are you missing a using directive or an assembly reference?)\n/__w/1/s/artifacts/bin/ILLink.testcases/linker_tests/DataFlow/GetInterfaceDataFlow/GetInterfaceDataFlow.cs(219,55): error CS1061: 'Type[]' does not contain a definition for 'First' and no accessible extension method 'First' accepting a first argument of type 'Type[]' could be found (are you missing a using directive or an assembly reference?)\n)\n---- System.Exception : Roslyn compilation |
… [0] Co-authored-by: sbomer <787361+sbomer@users.noreply.github.com>
Fixed in 8b353ad. Replaced |
|
@copilot fix this error: Unexpected warning found: ILLink: Trim analysis warning IL2075: Mono.Linker.Tests.Cases.DataFlow.GetInterfaceDataFlow.GetInterfaceMap_DataFlow.TestDirectCall(): 'this' argument does not satisfy 'DynamicallyAccessedMemberTypes.Interfaces' in call to 'System.Type.GetInterfaces()'. The return value of method 'System.Object.GetType()' does not have matching annotations. The source value must declare at least the same requirements as those declared on the target location it is assigned to. Be sure to run this test before committing. |
…id IL2075 warnings Co-authored-by: sbomer <787361+sbomer@users.noreply.github.com>
Fixed in 025264e. Replaced |
Extends the fix for visiting field instances to also cover field assignments. Previously only field reads were handled; now field writes (simple assignments) also visit the instance expression to ensure method calls used as the instance are analyzed. This resolves the remaining cases from dotnet/linker#2832.
There was a problem hiding this comment.
Pull request overview
This PR fixes a trimming analyzer bug where diagnostics were not reported when method calls appear in field access expressions. For example, type.GetInterfaceMap(@interface).TargetMethods was not warning even though @interface lacks required annotations, while type.GetInterfaceMap(@interface) correctly reports IL2072.
Changes:
- Added instance visiting in
VisitFieldReferenceto ensure method calls in field access chains are analyzed - Updated field assignment handling to explicitly visit instances before processing field values
- Added comprehensive test coverage for the GetInterfaceMap scenario with field access patterns
Reviewed changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
| TrimAnalysisVisitor.cs | Added Visit(fieldRef.Instance, state) in VisitFieldReference to analyze method calls used as field instances, matching the pattern used by property and event references |
| LocalDataFlowVisitor.cs | Refactored assignment handling to explicitly visit field reference instances, ensuring consistency for both read and write operations |
| GetInterfaceDataFlow.cs | Added test class GetInterfaceMap_DataFlow with comprehensive test cases covering direct calls, field access, multiple field access, and properly annotated scenarios |
| FieldDataFlow.cs | Updated test expectations in AccessReturnedInstanceField to expect IL2072 warnings for all tools (removed tool-specific exclusions) |
|
Tagging subscribers to this area: @agocke, @dotnet/illink |
This comment has been minimized.
This comment has been minimized.
🤖 Copilot Code Review — PR #123040Note This review was generated by Copilot using multi-model analysis (Claude Opus 4.6 primary, Claude Sonnet 4 and GPT-5.3-codex sub-agents). Holistic AssessmentMotivation: The PR fixes a confirmed bug (#108114) where the trimming analyzer silently drops diagnostics when a method call's return value is used as the instance of a field access (e.g., Approach: The fix adds Summary: ✅ LGTM. The change is correct, targeted, and well-tested. All three models independently verified the same conclusion with no blocking or advisory issues found. Detailed Findings✅ Correctness — Fix addresses root cause
✅ Null safety — Static fields handled correctlyFor static fields, ✅ State ordering — Instance visited before state capture
✅ Double-visiting — Safe by designIn compound operations (e.g., ✅ Test coverage — Comprehensive
|
Description
The trimming analyzer fails to report diagnostics when method calls are used in field access expressions. For example,
type.GetInterfaceMap(@interface).TargetMethodsdoes not warn, whiletype.GetInterfaceMap(@interface)correctly reports IL2072.Root cause:
TrimAnalysisVisitor.VisitFieldReferencedoes not visit the field's instance operation before processing the field itself.Fix: Visit the instance before processing the field, consistent with how other visitor methods handle instances (property references, event references). The fix applies to both field reads and field writes.
Changes Made
Visit(fieldRef.Instance, state);at the start ofVisitFieldReference()to ensure method calls used as field instances are properly analyzed for diagnosticsProcessSingleTargetAssignmentforIFieldReferenceOperationcases, ensuring both field reads and writes analyze method calls in the instance expressiontypeof(TestType)instead ofo.GetType()to avoid unrelated IL2075 warnings[0]instead of LINQ for test framework compatibilityAccessReturnedInstanceFieldtest to remove tool-specific exclusions, as the fix now ensures consistent warning reporting across all tools (Analyzer, Trimmer, and NativeAot)Testing
Risk Assessment
Low risk. This is a targeted fix that only affects field reference analysis. The change ensures that:
This resolves the remaining cases from dotnet/linker#2832.
Original prompt
Type.GetInterfaceMap(Type)is treated oddly by the trimming analyzers #108114💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.