The following code:
using System;
M(new RS(), new RS());
//M(new C(), new C());
void M<T>(T l, T r) where T : IComparable<T>, allows ref struct
{
Console.WriteLine(l.CompareTo(r));
}
class C : IComparable<C>
{
public int CompareTo(C? other) => 0;
}
//public struct RS : IComparable<RS>
public ref struct RS : IComparable<RS>
{
public int CompareTo(RS other) => i - other.i;
private int i = Random.Shared.Next();
public RS()
{
}
}
throws a VerificationException in run mode:
System.Security.VerificationException: Method SharpLab.Runtime.Internal.Flow.ReportValue: type argument 'RS' violates the constraint of type parameter 'T'.
at Program.<<Main>$>g__M|0_0[T](T l, T r)
at Program.<Main>$(String[] args)
at System.RuntimeMethodHandle.InvokeMethod(Object target, Void** arguments, Signature sig, Boolean isConstructor)
at System.Reflection.MethodBaseInvoker.InvokeDirectByRefWithFewArgs(Object obj, Span`1 copyOfArgs, BindingFlags invokeAttr)