-
Notifications
You must be signed in to change notification settings - Fork 5.3k
Fix trimming analyzer missing diagnostics for method calls in field access expressions #123040
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
…s for method calls Co-authored-by: sbomer <[email protected]>
Co-authored-by: sbomer <[email protected]>
|
@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 <[email protected]>
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 <[email protected]>
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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) |
Fix for Type.GetInterfaceMap Trimming Analyzer Issue (#117849)
Problem
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).
Changes
Visit(fieldRef.Instance, state);at the start ofVisitFieldReference()typeof(TestType)instead ofo.GetType()to avoid unrelated IL2075 warnings[0]instead of LINQ for test framework compatibilityTesting
Impact
This is a targeted fix that only affects field reference analysis. The change ensures that:
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.