Open sbomer opened 5 months ago
Tagging subscribers to this area: @agocke, @MichalStrehovsky, @jkotas See info in area-owners.md if you want to be subscribed.
Author: | sbomer |
---|---|
Assignees: | - |
Labels: | `untriaged`, `area-NativeAOT-coreclr`, `needs-area-label` |
Milestone: | - |
Does NativeAOT always generate a shared generic impl for this or would it need to scan for MakeGenericType
and generate it then?
Based on some experimentation, it looks like it doesn't always generate the shared impl. I'm not sure exactly what is enough to generate the shared impl. Just calling G<SomeOtherReferenceType>.Method()
won't do it - but accessing some of the generic type's methods via reflection will. For example MakeGenericType
fails here:
typeof(G<>).MakeGenericType(typeof(C));
// typeof(G<>).GetMethod("Foo");
class G<T> {
public static void Foo() {
Console.WriteLine(typeof(T));
}
}
class C {}
but succeeds if you uncomment the GetMethod("Foo")
.
@MichalStrehovsky could probably give a better answer.
Based on some experimentation, it looks like it doesn't always generate the shared impl. I'm not sure exactly what is enough to generate the shared impl. Just calling
G<SomeOtherReferenceType>.Method()
won't do it - but accessing some of the generic type's methods via reflection will. For exampleMakeGenericType
fails here:typeof(G<>).MakeGenericType(typeof(C)); // typeof(G<>).GetMethod("Foo"); class G<T> { public static void Foo() { Console.WriteLine(typeof(T)); } } class C {}
but succeeds if you uncomment the
GetMethod("Foo")
.@MichalStrehovsky could probably give a better answer.
Looks like we don't treat MakeGenericType
specially in dataflow analysis - the only special handling is to trigger the AOT warning here:
It works if a method/constructor/field is subsequently accessed because dataflow will report "G<>.Foo
is reflection accessed" and then the compiler will try to make G<>.Foo
work on at least something.
It shouldn't be hard to make this work for an unused type.
MakeGenericType
over reference types is supported in native AOT, so the following should not warn:[x] Probably the
class
constraint 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:
void Bar([ReferenceType] Type t) { typeof(G<>).MakeGenericType(t); }