Skip to content

CS0508 not triggering in net 5 preview-8, which allows runtime TypeLoadException errors #41571

Closed
@ChadJessup

Description

@ChadJessup

Sorry if this is the incorrect repo, hard to know where this bug report should go..

Description

In net 5 preview 7 CS0508 - 'Type 1': return type must be 'Type 2' to match overridden member 'Member Name' would prevent compilation. But in preview 8 this builds just fine, and during runtime a TypeLoadException is thrown that is very difficult to troubleshoot.

This top-level program demonstrates the problem:

new Base().CrashyCrashInCallingScope();

class Base
{
    public GenericDerived<string> CrashyCrashInCallingScope() => new GenericDerived<string>();
    public virtual Base NeededToCrash() => this;
}

class GenericDerived<TInputKey> : Base
{
    public override GenericDerived<TInputKey> NeededToCrash() => this;
}

There seems to be some special interaction happening between CrashyCrashInCallingScope() and NeededToCrash().

Compilation and running is fine if NeededToCrash() is called, even though that's where I would expect the compilation error, but if the valid CrashyCrashInCallingScope() is referenced, the whole thing blows up on startup.

Oddly enough, both methods have to exist, and one of them overridden in a derived class to trigger the issue.

Configuration

  1. dotnet run

Regression?

Yes - 5.0.100-preview.7.20366.6 would prevent compilation:

Program.cs(11,47): error CS0508: 'GenericDerived<TInputKey>.NeededToCrash()': return type must be 'Base' to match overridden member 'Base.NeededToCrash()' [C:\Projects\TypeLoadTrigger\TypeLoadTrigger.csproj]

The build failed. Fix the build errors and run again.

In 5.0.100-preview.8.20417.9

Unhandled exception. System.TypeLoadException: Could not load type 'GenericDerived`1' from assembly 'TypeLoadTrigger, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null'.
   at <Program>$.<Main>$(String[] args)

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions