Skip to content

TypeLoadException on minimal, correct and compiling program #97667

@cyanite

Description

@cyanite

Description

A small program which compiles under .NET 6-8 and executes correctly on .NET 6-7, crashes on .NET 8 with a TypeLoadException about a failed generic constraint. Manual inspection shows that this constraint is not violated. For us (my company), this is a serious bug, as it completely prevents us from moving to .NET 8.

Reproduction Steps

Use dotnet run on a console program with the following code:

var a = new LifSharedVersion<object>();

public interface ILifVersionReadable<TA> {}

public class LifVersion<TVersion, TIVersionReadable>
    where TVersion : TIVersionReadable
{}

public class LifSharedVersion<TSharedVersionData> :
    LifVersion<LifSharedVersion<TSharedVersionData>, ILifSharedVersionReadable<TSharedVersionData>>,
    ILifSharedVersionReadable<TSharedVersionData>
{}

public interface ILifSharedVersionReadable<TSharedVersionData> :
    ILifVersionReadable<LifSharedVersion<TSharedVersionData>>
{}

This is a reduction from the bug we found in our code base. It is not immediately possible to reduce the program further, without the error going away.

This crash also occurs when compiling for .NET 6 and using DOTNET_ROLL_FORWARD=LatestMajor or similar.

Expected behavior

The program completes without output.

Actual behavior

The program crashes with the following exception:

Unhandled exception. System.TypeLoadException: GenericArguments[0], 'LifSharedVersion`1[TSharedVersionData]', on 'LifVersion`2[TVersion,TIVersionReadable]' violates the constraint of type parameter 'TVersion'.
   at Program.<Main>$(String[] args)

that is, System.TypeLoadException: GenericArguments[0], 'LifSharedVersion`1[TSharedVersionData]', on 'LifVersion`2[TVersion,TIVersionReadable]' violates the constraint of type parameter 'TVersion'.

Regression?

There is no crash on .NET 6 or .NET 7.

Known Workarounds

Removing anything from the reproduction example removes the problem. However, this reproduction example is a massively reduced version of a very large code base, so we have no easy way to work around this problem in what is supposed to soon be production code.

Configuration

.NET 8, latest version. Otherwise, nothing special.

Other information

No response

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions