Skip to content

Propagate Type.GetInterfaces through dataflow analysis - #114149

Merged
MichalStrehovsky merged 5 commits into
dotnet:mainfrom
MichalStrehovsky:fixlinker1731
Apr 15, 2025
Merged

Propagate Type.GetInterfaces through dataflow analysis#114149
MichalStrehovsky merged 5 commits into
dotnet:mainfrom
MichalStrehovsky:fixlinker1731

Conversation

@MichalStrehovsky

@MichalStrehovskyMichalStrehovsky commented Apr 2, 2025

Copy link
Copy Markdown
Member

Fixesdotnet/linker#1731

We currently maintain two invariants:

  1. The types returned by the API will have .Interfaces annotations at minimum
  2. If the parent type was annotated .All, the types returned by the API will also be .All

We do this in the logic that keeps things, but we don't do this in terms of dataflow analysis warnings (the types retrieved from the array are not annotated as such, as far as the analysis is concerned). Because of the lacking annotation, we have warning suppressions in multiple places within the framework.

This fixes it.

Cc @dotnet/illink

Fixesdotnet/linker#1731
We currently maintain two invariants:
1. The types returned by the API will have `.Interfaces` annotations at minimum
2. If the parent type was annotated `.All`, the types returned by the API will also be `.All`
We do this in the logic that keeps things, but we don't do this in terms of dataflow analysis warnings (the types retrieved from the array are not annotated as such, as far as the analysis is concerned). Because of the lacking annotation, we have warning suppressions in multiple places within the framework.
This fixes it.
Opening as a draft because I'm having trouble making `foreach` of arrays work with the Roslyn analyzer. Roslyn models it as `IEnumerable` instead of array indexing (which is how foreach is actually expanded for arrays). I hope we can somehow force Roslyn to surface this as array indexing. In the worst case we'll need to live with the wart that `foreach` doesn't work, only `for`.
@ghostghost added the needs-area-label An area label is needed to ensure this gets routed to the appropriate area owners label Apr 2, 2025
Return result
End Function

<UnconditionalSuppressMessage("ReflectionAnalysis", "IL2075:UnrecognizedReflectionPattern",

Copy link
Copy Markdown
MemberAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This suppression was wrong, all that was needed was to annotate GetClassConstraint above.

/// Retrieves custom attributes.
/// </summary>
[UnconditionalSuppressMessage("ReflectionAnalysis", "IL2062:UnrecognizedReflectionPattern",
Justification = "_type is annotated as preserve All members, so any Types returned from GetInterfaces should be preserved as well once https://github.com/mono/linker/issues/1731 is fixed.")]

Copy link
Copy Markdown
MemberAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The justification was incorrect, the warning is on lines working with elements of Type[] returned by TrimSafeReflectionHelper.GetInterfaces, not Type.GetInterfaces. That will not work without warnings.

@MichalStrehovsky
MichalStrehovsky marked this pull request as ready for review April 9, 2025 13:05
@MichalStrehovskyMichalStrehovsky added area-Tools-ILLink .NET linker development as well as trimming analyzers and removed needs-area-label An area label is needed to ensure this gets routed to the appropriate area owners labels Apr 14, 2025

@sbomersbomer left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, thank you!

Sign up for freeto subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

area-Tools-ILLink.NET linker development as well as trimming analyzers

Projects

None yet

Development

Successfully merging this pull request may close these issues.

DynamicallyAccessedMembers should transitively apply to GetInterfaces()

3 participants

@MichalStrehovsky@sbomer@eerhardt