-
Notifications
You must be signed in to change notification settings - Fork 4.9k
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
NativeAOT does not honor new() constraint #80037
Comments
Tagging subscribers to this area: @agocke, @MichalStrehovsky, @jkotas Issue DetailsDescriptionNativeAOT does not honor Reproduction Stepsusing System.Diagnostics.CodeAnalysis;
var serviceCollection = new ServiceCollection();
var w = serviceCollection.GetRequiredService<IGeneric<Target>>();
Console.WriteLine("Working");
class ServiceCollection
{
public T GetRequiredService<T>()
{
return (T)Activator.CreateInstance(typeof(GenericInstantiaion<>).MakeGenericType(typeof(T).GenericTypeArguments[0]))!;
}
}
interface IGeneric<T> where T: new() {}
class GenericInstantiaion<T> : IGeneric<T> where T: new()
{
public GenericInstantiaion()
{
new T();
}
}
public class Target {
} Expected behaviorDo not trim constructor of Target class. Actual behaviorException at runtime. Regression?No Known WorkaroundsNo response ConfigurationNo response Other informationNo response
|
Thanks! Here's a repro that doesn't generate trimming warnings (and is also broken): IGeneric<Target>.Hey();
interface IGeneric<T> where T: new()
{
public static void Hey()
{
Activator.CreateInstance(typeof(T));
}
}
public class Target { } |
I think my sample closer to how open generic registered in MEDI. Your example probably different issue :) |
The issue would be a won't fix if it required MakeGeneric, especially if it's with something that has a new constraint. If there's a warning, trimming can and will generate broken things. The only aspects that are analysis holes are if there's no warnings. |
I do not want replicate whole DI setup. That’s bigger sample with only one warning from BuildServiceProvider which I believe is fine. using System.Diagnostics.CodeAnalysis;
using Microsoft.Extensions.DependencyInjection;
var serviceCollection = new ServiceCollection();
serviceCollection.AddSingleton(typeof(IGeneric<>),typeof(GenericInstantiaion<>));
var serviceProvider = serviceCollection.BuildServiceProvider();
var w = serviceProvider.GetRequiredService<IGeneric<Target>>();
Console.WriteLine("Working");
interface IGeneric<T> where T: new() {}
class GenericInstantiaion<T> : IGeneric<T> where T: new()
{
public GenericInstantiaion()
{
new T();
}
}
public class Target {
} |
We can have situations like in MEDI where library author would support similar pattern for construction as in medi for open generics and just suppress warning like here runtime/src/libraries/Microsoft.Extensions.DependencyInjection/src/ServiceLookup/CallSiteFactory.cs Lines 349 to 353 in 199436b
I think it's reasonable to perform analysys on parameters and return types of method when they specified. Develop specify that he need type, and that type guaranteed to return specific type by compiler, so that means that constraints on specific generic type would be satisfied. I honestly did not see how developer can break something with this extra requirement. Yes, internally it's |
Oh, I see, it's the pattern from DI. Yes, the suppression in DI relies on the fact that because the interface has a |
Vitek fixed this in #82818. |
Description
NativeAOT does not honor
new()
constraint applied to interface.Reproduction Steps
Expected behavior
Do not trim constructor of Target class.
Actual behavior
Exception at runtime.
Regression?
No
Known Workarounds
No response
Configuration
No response
Other information
No response
The text was updated successfully, but these errors were encountered: