Suppress IL3050 warning for MakeGenericType/MakeGenericMethod with known reference types #121323
+135
−7
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
The AOT analyzer warns with IL3050 when calling
MakeGenericTypeeven when the type arguments are provably reference types through dataflow analysis (e.g., after checking!t.IsValueType) or haveclassconstraints.Changes
Added reference type analysis to HandleCallAction.cs:
AreAllArgumentsKnownReferenceTypes()method checks if type arguments are provably reference types by examining:SystemTypeValuefor!Type.IsValueTypeGenericParameterValueforHasReferenceTypeConstraint(class constraint)Type_MakeGenericTypeandMethodInfo_MakeGenericMethodintrinsicsAdded test coverage:
Fixes #110797
Original prompt
This section details on the original issue you should resolve
<issue_title>AOT/analyzer should not warn for MakeGenericType of reference type</issue_title>
<issue_description>- [ ]
MakeGenericTypeover reference types is supported in native AOT, so the following should not warn:classconstraint should be supported as well:(The latter idea is something that has come up in discussion with @vitek-karas and @MichalStrehovsky, not sure if there's another tracking issue for it already.)
Context: https://github.com/dotnet/maui/pull/20058/files/7f4d26ca0aee7dc9d995e9776573125b35ad44b6#r1463169163</issue_description>
Comments on the Issue (you are @copilot in this section)
@ Tagging subscribers to this area: @agocke, @MichalStrehovsky, @jkotas See info in [area-owners.md](https://github.com/dotnet/runtime/blob/main/docs/area-owners.md) if you want to be subscribed.Issue Details
MakeGenericTypeover reference types is supported in native AOT, so the following should not warn:Probably the
classconstraint should be supported as well:We may also want to introduce an annotation to propagate the fact that a type is a reference type across methods, for example:
(The latter idea is something that has come up in discussion with @vitek-karas and @MichalStrehovsky, not sure if there's another tracking issue for it already.)
Context: https://github.com/dotnet/maui/pull/20058/files/7f4d26ca0aee7dc9d995e9776573125b35ad44b6#r1463169163
untriaged,area-NativeAOT-coreclr,needs-area-labelbut succeeds if you uncomment the
GetMethod("Foo").@MichalStrehovsky could probably give a better answer.</comment_new>
<comment_new>@MichalStrehovsky
Looks like we don't treat
MakeGenericTypespecially in dataflow analysis - the only special handling is to trigger the AOT warning here:runtime/src/coreclr/tools/aot/ILCompiler.Compiler/Compiler/Dataflow/ReflectionMethodBodyScanner.cs
Lines 376 to 382 in 135fec0
It works if a method/constructor/field is subsequently accessed because dataflow will report "
G<>.Foois reflection accessed" and then the compiler will try to makeG<>.Foowork on at least something.It shouldn't be hard to make this work for an unused type.</comment_new>
💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.