Skip to content
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

C# 9 covariant return types: Still can get System.TypeLoadException when using generics #45344

Closed
ASolomatin opened this issue Nov 25, 2020 · 16 comments
Labels
area-TypeSystem-coreclr untriaged New issue has not been triaged by the area owner

Comments

@ASolomatin
Copy link

Description

This looks like dotnet/core#5319 but with generics.

The C # 9.0 overridden virtual function works fine when the return type is a covariant derived type, continues to work if the return type is a covariant derived type that contains the overridden function. But System.TypeLoadException is thrown at runtime if you add some generics. As before, everything builds without errors and warnings.

Code to reproduce

using System;

var c = new AChild();

Console.WriteLine("Hello World!");

public abstract class A<T>
{
    public abstract B<T> GetB();
}

public class AChild : A<string>
{
    public override BChild GetB() => null;
}

public class B<T>
{
}

public class BChild : B<string>
{
}

Error:

$ dotnet run
Unhandled exception. System.TypeLoadException: Return type in method 'AChild.GetB()' on type 'AChild' from assembly 'N5, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' is not compatible with base type method 'A`1[System.__Canon].GetB()'.
$ dotnet --version
5.0.100

Versions

5.0.100-rc.2.20479.15
5.0.100

Originally posted by @ASolomatin in dotnet/core#5319 (comment)

@MrKWatkins
Copy link

This also happens in 5.0.100 if you just return T directly, rather than a type that uses T, e.g.

using System;

_ = new CovariantReturn();

public abstract class Base<T>
{
   public virtual T Get() => throw new NotImplementedException();
}

public sealed class CovariantReturn : Base<object>
{
   public override string Get() => throw new NotImplementedException();
}

@danmoseley danmoseley transferred this issue from dotnet/core Nov 30, 2020
@Dotnet-GitSync-Bot Dotnet-GitSync-Bot added area-TypeSystem-coreclr untriaged New issue has not been triaged by the area owner labels Nov 30, 2020
@MichalStrehovsky
Copy link
Member

MichalStrehovsky commented Nov 30, 2020

Sounds like #45037/#45082

Cc @janvorli

@janvorli
Copy link
Member

I've verified this issue is fixed by #45275, it is a dup of #45037

@adamjones1
Copy link

Hi, in the meantime before this fix is released, I am trying to continue developing my app on machines with 5.0.101 installed (I didn't experience the error on 5.0.100 for some reason, it only appeared when 5.0.101 was installed). Is there any way to force usage of 5.0.100 for running it with a global.json? When I attempt this, dotnet --version gives me 5.0.100 but when the app actually launches unit tests with dotnet test it still gives me the error as if it were ignoring the global.json.

@janvorli
Copy link
Member

Would setting the runtime version property in the csproj file to 5.0.100 work as a workaround?

<RuntimeFrameworkVersion>5.0.100</RuntimeFrameworkVersion>

@adamjones1
Copy link

Trying this I get "Unable to find package Microsoft.NETCore.App.Host.win-x64 with version (= 5.0.100)" (seems only 5.0.1 exists at https://www.nuget.org/packages/Microsoft.NETCore.App.Host.win-x64/?). Using 5.0.1 instead the issue is still present I assume as this still instructs it to use the latest patch version.

@janvorli
Copy link
Member

I am sorry for the confusion, I meant 5.0.0.

@adamjones1
Copy link

Ah ok, using 5.0.0 unfortunately the TypeLoadException is still present. My full csproj is:

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <TargetFramework>net5.0</TargetFramework>
    <Nullable>enable</Nullable>
    <IsPackable>false</IsPackable>
    <RuntimeFrameworkVersion>5.0.0</RuntimeFrameworkVersion>
  </PropertyGroup>
  <ItemGroup>
    <FrameworkReference Include="Microsoft.AspNetCore.App" />
    <ProjectReference Include="..\..\src\MyProject\MyProject.csproj" />
  </ItemGroup>
  <Import Project="..\..\.paket\Paket.Restore.targets" />
</Project>

@ASolomatin
Copy link
Author

Still can reproduce with small code modifications:

using System;

var c = new AChild<string>();

Console.WriteLine("Hello World!");

public abstract class A<T>
{
    public abstract B<T> GetB();
}

public class AChild<T> : A<T>
{
    public override BChild<T> GetB() => null;
}

public class B<T>
{
}

public class BChild<T> : B<T>
{
}

@janvorli
Copy link
Member

@ASolomatin what version of runtime have you reproed it with? The latest fix is not in any released .NET 5 versions, it will be part of 5.0.2.

@ASolomatin
Copy link
Author

Sorry. ) I thought, it's already released because the original issue stops to reproduce on 5.0.1

@janvorli
Copy link
Member

@ASolomatin I'll verify it with the latest version to make sure this is not yet another problem.

@janvorli
Copy link
Member

I can confirm that your test above passes with runtime from the master branch, which means that it should pass with the 5.0.2 once it is out.

@Unknown6656
Copy link

Unknown6656 commented Jan 15, 2021

@janvorli I have the same bug with 5.0.2
Although I have to admit that my classes are more complex than @ASolomatin's example. I'll try to post a MWE as soon as possible.

@ASolomatin
Copy link
Author

Just one next code modification to reproduce on 5.0.2 somesing look likes the same:

using System;

var c = new AChild<string>();

Console.WriteLine("Hello World!");

public abstract class A<T1, T2>
{
    public abstract B<T1, T2> GetB();
}

public class AChild<T> : A<T, string>
{
    public override BChild<T> GetB() => null;
}

public class B<T1, T2>
{
}

public class BChild<T> : B<T, string>
{
}
alex@nout-1:~/Projects/DummyTests/N5$ dotnet --version
5.0.102
alex@nout-1:~/Projects/DummyTests/N5$ dotnet run
Unhandled exception. System.TypeLoadException: Return type in method 'AChild`1[T].GetB()' on type 'AChild`1[T]' from assembly 'N5, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' is not compatible with base type method 'A`2[T,System.__Canon].GetB()'.

@janvorli
Copy link
Member

@ASolomatin thank you for the repro. I have tested it with my pending fix for the #47007 and the issue is gone with that fix.

@ghost ghost locked as resolved and limited conversation to collaborators Feb 17, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-TypeSystem-coreclr untriaged New issue has not been triaged by the area owner
Projects
None yet
Development

No branches or pull requests

7 participants