Skip to content

.NET 8 Genenerics runtime issue #97807

Closed
Closed

Description

@webmaster442

Description

In .NET 8 there is a bug regarding the handling of generic types, which produces a TypeLoadException

Reproduction Steps

ON .NET 8 Compile & run the following C# console application code code:

public interface IGeneric<T>
{
}

public class DualGeneric<T1, T2> where T1 : T2
{
}

public class Record : DualGeneric<Record, IRecord>, IRecord
{
}

public interface IRecord : IGeneric<Record>
{
}

public interface IRecord<T> : IGeneric<Record<T>>
{
}

public class Record<T> : DualGeneric<Record<T>, IRecord<T>>, IRecord<T>
{
}


internal class Program
{
    private static void Main(string[] args)
    {
        var record = new Record<int>();
        Console.WriteLine("Works");
    }
}

Expected behavior

Program prints Works message.

Actual behavior

Actual result: System.TypeLoadException: 'GenericArguments[0], 'Record1[T]', on 'DualGeneric2[T1,T2]' violates the constraint of type parameter 'T1'.'

Regression?

Code runs perfectly fine, when it's targeting .NET 7

Known Workarounds

Run the code on .NET 7

Configuration

  • .NET 8
  • Windows 10 22H2
  • X64
  • Shouldn't be specific to configuration

Other information

No response

Activity

  1. ghost added
    untriaged
    on Feb 1, 2024
  2. webmaster442 commented on Feb 1, 2024

    @webmaster442
    Author

    Additional note: When removing the where T1 : T2 constraint from the DualGeneric type, the code works.

  3. MichalStrehovsky commented on Feb 1, 2024

    @MichalStrehovsky
    Member

    Maybe same as #97667? Cc @davidwrighton

  4. davidwrighton commented on Feb 2, 2024

    @davidwrighton
    Member

    @MichalStrehovsky @webmaster442 yes, this looks like a duplicate. The fix is currently in code review.

  5. lambdageek commented on Feb 6, 2024

    @lambdageek
    Member

    Duplicate of #97667

  6. ghost removed
    untriaged
    on Feb 6, 2024
  7. locked and limited conversation to collaborators on Mar 8, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions