Skip to content
8 changes: 8 additions & 0 deletions src/tests/Regressions/coreclr/GitHub_122933/Test122933.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<CLRTestPriority>1</CLRTestPriority>
</PropertyGroup>
<ItemGroup>
<Compile Include="test122933.cs" />
</ItemGroup>
</Project>
117 changes: 117 additions & 0 deletions src/tests/Regressions/coreclr/GitHub_122933/test122933.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System;
using System.Linq;
Comment thread
jkotas marked this conversation as resolved.
Outdated
using System.Reflection;
using Xunit;

// Small repro for ilasm/ildasm roundtrip issue with duplicated constraints
// This demonstrates the issue where constraints get duplicated during ilasm roundtrip
Comment thread
jkotas marked this conversation as resolved.
Outdated
public interface IComp<TSelf>
: IEq<TSelf>
where TSelf : IComp<TSelf>?
{
}

public interface IEq<TSelf>
where TSelf : IEq<TSelf>?
{
}

public class Test122933
{
[Fact]
public static int TestEntryPoint()
{
// Get the types
Comment thread
jkotas marked this conversation as resolved.
Outdated
Type ieqType = typeof(IEq<>);
Type icompType = typeof(IComp<>);

// Get the generic type parameter for IEq<TSelf>
Type[] ieqTypeParams = ieqType.GetGenericArguments();
if (ieqTypeParams.Length != 1)
{
Console.WriteLine($"FAIL: IEq should have 1 generic parameter, but has {ieqTypeParams.Length}");
return -1;
}
Type ieqTSelf = ieqTypeParams[0];

// Get the constraints on IEq's TSelf parameter
Type[] ieqConstraints = ieqTSelf.GetGenericParameterConstraints();

// There should be exactly one constraint: IEq<TSelf>
if (ieqConstraints.Length != 1)
{
Console.WriteLine($"FAIL: IEq<TSelf> should have 1 constraint, but has {ieqConstraints.Length}");
foreach (var constraint in ieqConstraints)
{
Console.WriteLine($" - {constraint}");
}
Comment thread
jkotas marked this conversation as resolved.
Outdated
return -1;
}

if (!ieqConstraints[0].IsGenericType)
{
Console.WriteLine($"FAIL: IEq<TSelf> constraint should be generic");
return -1;
}

Comment thread
jkotas marked this conversation as resolved.
Outdated
if (ieqConstraints[0].GetGenericTypeDefinition() != ieqType)
{
Console.WriteLine($"FAIL: IEq<TSelf> constraint should be IEq<>, but is {ieqConstraints[0].GetGenericTypeDefinition()}");
return -1;
}

// Get the generic type parameter for IComp<TSelf>
Type[] icompTypeParams = icompType.GetGenericArguments();
if (icompTypeParams.Length != 1)
{
Console.WriteLine($"FAIL: IComp should have 1 generic parameter, but has {icompTypeParams.Length}");
return -1;
}
Type icompTSelf = icompTypeParams[0];

// Get the constraints on IComp's TSelf parameter
Type[] icompConstraints = icompTSelf.GetGenericParameterConstraints();

// There should be exactly one constraint: IComp<TSelf>
// After ilasm roundtrip, this becomes duplicated - that's the bug
if (icompConstraints.Length != 1)
{
Console.WriteLine($"FAIL: IComp<TSelf> should have 1 constraint, but has {icompConstraints.Length}");
foreach (var constraint in icompConstraints)
{
Console.WriteLine($" - {constraint}");
}
Comment thread
jkotas marked this conversation as resolved.
Outdated
return -1;
}

if (!icompConstraints[0].IsGenericType)
{
Console.WriteLine($"FAIL: IComp<TSelf> constraint should be generic");
return -1;
}

Comment thread
jkotas marked this conversation as resolved.
Outdated
if (icompConstraints[0].GetGenericTypeDefinition() != icompType)
{
Console.WriteLine($"FAIL: IComp<TSelf> constraint should be IComp<>, but is {icompConstraints[0].GetGenericTypeDefinition()}");
return -1;
}

// Log the constraints for debugging
Console.WriteLine($"PASS: IEq<TSelf> constraints count: {ieqConstraints.Length}");
foreach (var constraint in ieqConstraints)
{
Console.WriteLine($" - {constraint}");
}

Console.WriteLine($"PASS: IComp<TSelf> constraints count: {icompConstraints.Length}");
foreach (var constraint in icompConstraints)
{
Console.WriteLine($" - {constraint}");
}

Comment thread
jkotas marked this conversation as resolved.
Outdated
return 100;
}
}
Loading