Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Jan 9, 2026

Description

The trimming analyzer fails to report diagnostics when method calls are used in field access expressions. For example, type.GetInterfaceMap(@interface).TargetMethods does not warn, while type.GetInterfaceMap(@interface) correctly reports IL2072.

Root cause: TrimAnalysisVisitor.VisitFieldReference does 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).

// Before: method calls as field instances were never analyzed
_ = type.GetInterfaceMap(@interface).TargetMethods;  // Silent
_ = type.GetInterfaceMap(@interface);                // IL2072 ✓

// After: both correctly report IL2072

Customer Impact

Silent trimming failures. Code that should warn about missing annotations passes analysis when the return value is immediately used for field access, leading to runtime failures in trimmed applications.

Regression

No. This is a long-standing analyzer gap, not a recent regression.

Testing

  • Added regression tests in GetInterfaceDataFlow.cs covering direct calls, field access, and nested field access
  • Existing dataflow tests verify no unintended behavioral changes

Risk

Low. The change is minimal (4 lines) and aligns with established patterns in the codebase. It only affects diagnostic reporting—no changes to trimming behavior or false positives. The fix makes the analyzer more correct by eliminating false negatives.

Original prompt

This section details on the original issue you should resolve

<issue_title>Type.GetInterfaceMap(Type) is treated oddly by the trimming analyzers</issue_title>
<issue_description>### Description

The second usage of GetInterfaceMap causes

IL2072: 'interfaceType' argument does not satisfy 'DynamicallyAccessedMemberTypes.PublicMethods', 'DynamicallyAccessedMemberTypes.NonPublicMethods' in call to 'System.Type.GetInterfaceMap(Type)'. The return value of method 'System.Linq.Enumerable.First<TSource>(IEnumerable<TSource>)' 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.

while the first usage doesn't cause any warning.

I am not sure whether it's how it's supposed to be or it's an actual bug.

Reproduction Steps

object o = new();

var type = o.GetType();

var @interface = type.GetInterfaces().First();

_ = type.GetInterfaceMap(@interface).TargetMethods;

_ = type.GetInterfaceMap(@interface);

Expected behavior

I am not sure

Actual behavior

The warning is reported or not depending on whether some field is referenced or not

Regression?

No response

Known Workarounds

No response

Configuration

.NET 8

Other information

No response</issue_description>

Comments on the Issue (you are @copilot in this section)


💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.

…s for method calls

Co-authored-by: sbomer <787361+sbomer@users.noreply.github.com>
@dotnet-policy-service dotnet-policy-service bot added the linkable-framework Issues associated with delivering a linker friendly framework label Jan 9, 2026
Co-authored-by: sbomer <787361+sbomer@users.noreply.github.com>
Copilot AI changed the title [WIP] Fix trimming analyzer issue with Type.GetInterfaceMap Fix trimming analyzer missing diagnostics for method calls in field access expressions Jan 9, 2026
Copilot AI requested a review from sbomer January 9, 2026 19:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

linkable-framework Issues associated with delivering a linker friendly framework

Projects

Status: No status

Development

Successfully merging this pull request may close these issues.

Type.GetInterfaceMap(Type) is treated oddly by the trimming analyzers

2 participants