Skip to content

Covariant Return crashes on runtime when there is additional abstract class in between not overriding the abstract method. #45053

Closed

Description

@pmrogala

Description

In net 5.0, windows 10x64, Visual Studio 2019 and C#9 the below code will not run and will produce the following error:

System.TypeLoadException: Return type in method 'Application.TB.GetA()' on type 'Application.TB' from assembly 'Application, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' is not compatible with base type method 'Application.T`1[System.__Canon].GetA()'.

    public abstract class T<TR>
    {
        public abstract TR GetA();
    }

    // This abstract causes the error - not overriding the base method cause the runtime to crash
    public abstract class TA : T<A> { }

    // This works
    // public abstract class TA : T<A>
    // {
    //  // Overriding in between fixes the problem
    //    public override A GetA() => new ();
    // }
    
     // Overriden here, in the grandson
    public class TB : TA
    {
        public override B GetA() => new ();
    }
    public class A { }

    public class B : A { }

public static class Program
{
        public static void Main(string[] args)
        {
            System.Console.WriteLine((new TB() as T<A>).GetA().GetType().FullName);
        }
}

Regression?

Don't know. It is a new feature for C#.

Other information

Works with Mono (thanks @lambdageek )

Activity

  1. added
    untriaged
    on Nov 21, 2020
  2. changed the title [-]Covariant Return fails when there is additional abstract class in between not overriding the base method.[/-] [+]Covariant Return fails when there is additional abstract class in between not overriding the abstract method.[/+] on Nov 21, 2020
  3. changed the title [-]Covariant Return fails when there is additional abstract class in between not overriding the abstract method.[/-] [+]Covariant Return cruches on runtime when there is additional abstract class in between not overriding the abstract method.[/+] on Nov 22, 2020
  4. changed the title [-]Covariant Return cruches on runtime when there is additional abstract class in between not overriding the abstract method.[/-] [+]Covariant Return crashes on runtime when there is additional abstract class in between not overriding the abstract method.[/+] on Nov 22, 2020
  5. lambdageek commented on Nov 23, 2020

    @lambdageek
    Member

    (works on mono - (new TB() as T<A>).GetA().GetType().FullName returns B))

  6. pmrogala commented on Nov 23, 2020

    @pmrogala
    Author

    (works on mono - (new TB() as T<A>).GetA().GetType().FullName returns B))

    Thanks, I updated the description with your discovery :)

  7. pentp commented on Dec 22, 2020

    @pentp
    Contributor

    @janvorli - This is probably the same issue as #45037, but in reverse (concrete type overriding generic type)?

  8. janvorli commented on Dec 22, 2020

    @janvorli
    Member

    This is unfortunately a new issue. With the latest fixes for covariant returns issues that will be released in 5.0.2, it fails with

    Unhandled exception. System.BadImageFormatException: An attempt was made to load a program with an incorrect format. (0x8007000B (COR_E_BADIMAGEFORMAT))
       at Program.Main(String[] args)
    
  9. self-assigned this
    on Dec 22, 2020
  10. removed
    untriaged
    on Dec 22, 2020
  11. janvorli commented on Dec 22, 2020

    @janvorli
    Member

    Fortunately, it was just a local issue, I have probably built debug runtime and tested the test with an old checked one. After rebuilding, the test above is passing. Closing this issue now as a dup of #45037.

  12. ghost locked as resolved and limited conversation to collaborators on Jan 21, 2021
  13. janvorli commented on Jan 26, 2021

    @janvorli
    Member

    It turns out the issue wasn't fixed. There must have been some issue in my local process.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions