Skip to content

Propagate Type.GetInterfaces through dataflow analysis #114149

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

Merged
merged 5 commits into from
Apr 15, 2025

Conversation

MichalStrehovsky
Copy link
Member

@MichalStrehovsky MichalStrehovsky commented Apr 2, 2025

Fixes dotnet/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

Fixes dotnet/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`.
@ghost ghost added the needs-area-label An area label is needed to ensure this gets routed to the appropriate area owners label Apr 2, 2025
@@ -950,9 +950,6 @@ Namespace Microsoft.VisualBasic.CompilerServices
Return result
End Function

<UnconditionalSuppressMessage("ReflectionAnalysis", "IL2075:UnrecognizedReflectionPattern",
Copy link
Member Author

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.

@@ -45,7 +45,7 @@ internal ReflectedTypeData(Type type, bool isRegisteredType)
/// 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
Member Author

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
@MichalStrehovsky MichalStrehovsky 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
Copy link
Member

@sbomer sbomer left a comment

Choose a reason for hiding this comment

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

LGTM, thank you!

@MichalStrehovsky MichalStrehovsky merged commit bb418ad into dotnet:main Apr 15, 2025
136 of 139 checks passed
@MichalStrehovsky MichalStrehovsky deleted the fixlinker1731 branch April 15, 2025 13:36
@github-actions github-actions bot locked and limited conversation to collaborators May 16, 2025
Sign up for free to 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