Skip to content

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

@adrianoc

Description

@adrianoc

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)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions