Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
adrianoc opened this issue Dec 2, 2024 · 0 comments

Comments

@adrianoc
Copy link

adrianoc commented Dec 2, 2024

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)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant