Description
After playing with covariant return types and reporting #45037 I think I found another problem with covariant return types. I've written a piece of code that compiles without problems, and to me logically makes sense. But as soon as the class with the method that has a covariant return type is used I get a TypeLoadException. I've managed to bring down the reproduction to running the following code.
public abstract class AComponent { }
public class Component : AComponent { }
public abstract class Abstract
{
public abstract IReadOnlyList<AComponent> New { get; }
}
public sealed class Concrete<T> : Abstract
where T : AComponent
{
public override IReadOnlyList<T> New => throw null;
}
internal class Program
{
private static void Main(string[] args)
{
new Concrete<Component>();
}
}
Simplifying this even further, like replacing AComponent and Component with object and string no longer triggers the bug, which really puzzles me. You also need some generic to put the type that is actually 'varying' into. I've chosen IReadonlyList<> here. But using another interface also works as long as it has an out modifier for the type parameter. (Otherwise the code won't compile, which makes sense).
Configuration
I'm running .NET 5.0 (v5.0.100) on Window 10 x64. I have not tried it on another machine yet.
Regression?
Not really since covariant return types are a new feature in .NET 5.0.
Other information
The complete error message is:
System.TypeLoadException
HResult=0x80131522
Message=Return type in method 'MakeGenericBug.Concrete`1[System.__Canon].get_New()' on type 'MakeGenericBug.Concrete`1[System.__Canon]' from assembly 'MakeGenericBug, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' is not compatible with base type method 'MakeGenericBug.Abstract.get_New()'.
Source=<Cannot evaluate the exception source>
StackTrace:
<Cannot evaluate the exception stack trace>
Unfortunately there's no stack trace, I think the exception happens before encountering my code since no matter where I set a break-point, its never hit.
Description
After playing with covariant return types and reporting #45037 I think I found another problem with covariant return types. I've written a piece of code that compiles without problems, and to me logically makes sense. But as soon as the class with the method that has a covariant return type is used I get a TypeLoadException. I've managed to bring down the reproduction to running the following code.
Simplifying this even further, like replacing
AComponentandComponentwithobjectandstringno longer triggers the bug, which really puzzles me. You also need some generic to put the type that is actually 'varying' into. I've chosenIReadonlyList<>here. But using another interface also works as long as it has anoutmodifier for the type parameter. (Otherwise the code won't compile, which makes sense).Configuration
I'm running .NET 5.0 (v5.0.100) on Window 10 x64. I have not tried it on another machine yet.
Regression?
Not really since covariant return types are a new feature in .NET 5.0.
Other information
The complete error message is:
Unfortunately there's no stack trace, I think the exception happens before encountering my code since no matter where I set a break-point, its never hit.