SharpLab icon indicating copy to clipboard operation
SharpLab copied to clipboard

Specifying a `ref struct` to a type parameter throws VerificationException in `run mode`

Open adrianoc opened this issue 1 year ago • 0 comments

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)

adrianoc avatar Dec 02 '24 09:12 adrianoc