-
Notifications
You must be signed in to change notification settings - Fork 4.7k
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] Method overrides with covariant return type don't work in all cases #96175
Comments
Tagging subscribers to this area: @agocke, @MichalStrehovsky, @jkotas Issue DetailsDescriptionwhen NativeAOT finds an covariant override it will pick that type as new return type and ignore further overrides that don't return the same type Reproduction Stepsvar impl = new Impl();
var subImpl = new SubImpl();
Console.WriteLine(impl.FromBase());
Console.WriteLine(impl.FromType());
Console.WriteLine(subImpl.FromBase());
Console.WriteLine(subImpl.FromType());
class Base
{
public Base FromBase()
{
return FromType();
}
public virtual Base FromType()
{
return new Base();
}
}
class Impl : Base
{
public override Impl FromType()
{
return new Impl();
}
}
class SubImpl : Impl
{
// not called from FromBase()
public override SubImpl FromType()
{
return new SubImpl();
}
} Expected behavior
Actual behavior
Regression?No response Known Workaroundsdon't use covariant return type Configuration.NET 8, Windows 10, x64, NativeAOT. Other informationNo response
|
This is an issue in the virtual method resolution algorithm: when asked "what method on This has been a bug ever since covariant returns got added to the managed type system in #35308 for crossgen2. It would also show up as a bug in crossgen2, but the compiler (neither crossgen2, nor ilc) actually doesn't devirtualize any covariant returns due to this code: runtime/src/coreclr/tools/Common/Compiler/DevirtualizationManager.cs Lines 183 to 197 in 6f4f268
That one was also added for crossgen2's sake and I don't actually understand why we do it the way we do it. Fixing it would make this bug also show up in crossgen2. |
We just accidentally hit this in our app when implementing handlers in MAUI code. We have We had the following code: namespace MailClient.Mobile.iOS.Handlers
{
class CarouselViewHandler : Microsoft.Maui.Controls.Handlers.Items.CarouselViewHandler
{
protected override CarouselViewController CreateController(CarouselView newElement, ItemsViewLayout layout)
{
return new CarouselViewController(newElement, layout);
}
}
} The @@ -8,7 +8,7 @@ namespace MailClient.Mobile.iOS.Handlers
{
class CarouselViewHandler : Microsoft.Maui.Controls.Handlers.Items.CarouselViewHandler
{
- protected override CarouselViewController CreateController(CarouselView newElement, ItemsViewLayout layout)
+ protected override Microsoft.Maui.Controls.Handlers.Items.CarouselViewController CreateController(CarouselView newElement, ItemsViewLayout layout)
{
return new CarouselViewController(newElement, layout);
} |
For context on that bit of slot-testing code, see the discussion in #10809 |
If I understand it correctly, the discussion happened when this was an IL-level corner case. These can now be hit with regular C#. |
…in ilc.exe for native aot - This was causing real C# applications to fail to behave correctly on NativeAOT builds - Enable testing for covariant byref returns on nativeaot (split testing up so that the tests do not expect TypeLoadException, which NativeAOT doesn't reliably generate) - Fix implementation of SynthesizedPgoIncompatible project file flag for test script generation - Put copy of attributetesting.il test into the managed type system unit test suite - Add regression test of issue noted in dotnet#96175 into managed type system unit test suite - Update workflow documentation to include a better path to finding details on how to run CoreCLR and Libraries tests for Native AOT Fixes dotnet#96175
…in ilc.exe for native aot - This was causing real C# applications to fail to behave correctly on NativeAOT builds - Enable testing for covariant byref returns on nativeaot (split testing up so that the tests do not expect TypeLoadException, which NativeAOT doesn't reliably generate) - Fix implementation of SynthesizedPgoIncompatible project file flag for test script generation - Put copy of attributetesting.il test into the managed type system unit test suite - Add regression test of issue noted in #96175 into managed type system unit test suite - Update workflow documentation to include a better path to finding details on how to run CoreCLR and Libraries tests for Native AOT Fixes #96175
…d as expected in ilc.exe for native aot (#107409) * Fix a case in MethodImpl overriding which wasn't handled as expected in ilc.exe for native aot - This was causing real C# applications to fail to behave correctly on NativeAOT builds - Enable testing for covariant byref returns on nativeaot (split testing up so that the tests do not expect TypeLoadException, which NativeAOT doesn't reliably generate) - Fix implementation of SynthesizedPgoIncompatible project file flag for test script generation - Put copy of attributetesting.il test into the managed type system unit test suite - Add regression test of issue noted in #96175 into managed type system unit test suite - Update workflow documentation to include a better path to finding details on how to run CoreCLR and Libraries tests for Native AOT Fixes #96175 * Fix test with incorrect IL * Make the remaining TODO comments follow existing practice in this file for todo comments * Fix test exclusion for mono llvmaot * Address nits from code review --------- Co-authored-by: David Wrighton <davidwr@microsoft.com> Co-authored-by: Jeff Schwartz <jeffschw@microsoft.com>
…in ilc.exe for native aot (dotnet#106716) * Fix a case in MethodImpl overriding which wasn't handled as expected in ilc.exe for native aot - This was causing real C# applications to fail to behave correctly on NativeAOT builds - Enable testing for covariant byref returns on nativeaot (split testing up so that the tests do not expect TypeLoadException, which NativeAOT doesn't reliably generate) - Put copy of attributetesting.il test into the managed type system unit test suite - Add regression test of issue noted in dotnet#96175 into managed type system unit test suite - Update workflow documentation to include a better path to finding details on how to run CoreCLR and Libraries tests for Native AOT Fixes dotnet#96175
…in ilc.exe for native aot (dotnet#106716) * Fix a case in MethodImpl overriding which wasn't handled as expected in ilc.exe for native aot - This was causing real C# applications to fail to behave correctly on NativeAOT builds - Enable testing for covariant byref returns on nativeaot (split testing up so that the tests do not expect TypeLoadException, which NativeAOT doesn't reliably generate) - Put copy of attributetesting.il test into the managed type system unit test suite - Add regression test of issue noted in dotnet#96175 into managed type system unit test suite - Update workflow documentation to include a better path to finding details on how to run CoreCLR and Libraries tests for Native AOT Fixes dotnet#96175
Description
when NativeAOT finds an covariant override it will pick that type as new return type and ignore further overrides that don't return the same type
tested with NativeAOT net7/8
Reproduction Steps
Expected behavior
Actual behavior
Regression?
No response
Known Workarounds
don't use covariant return type
Configuration
.NET 8, Windows 10, x64, NativeAOT.
Other information
No response
The text was updated successfully, but these errors were encountered: