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

Sigil incorrectly fails when calling a virtual method on a struct #53

Open
benjamin-hodgson opened this issue Mar 22, 2017 · 0 comments · Fixed by arlm/Sigil-vNext#8 · May be fixed by #54
Open

Sigil incorrectly fails when calling a virtual method on a struct #53

benjamin-hodgson opened this issue Mar 22, 2017 · 0 comments · Fixed by arlm/Sigil-vNext#8 · May be fixed by #54

Comments

@benjamin-hodgson
Copy link
Contributor

benjamin-hodgson commented Mar 22, 2017

Found this while debugging kevin-montrose/Jil#257. (Jil emits virtual calls to IEnumerable<T>.GetEnumerator.)

public struct MyStruct { }
string Example(MyStruct s) => s.ToString();

Looking at the above method in dotPeek, I see a constrained callvirt on the address of the MyStruct argument:

IL_0000: ldarga.s     s
IL_0002: constrained. MyStruct
IL_0008: callvirt     instance string [mscorlib]System.Object::ToString()
IL_000d: ret

Here's (what I think is) the equivalent Sigil:

var obj = typeof(object);
var myStruct = typeof(MyStruct);
var myDelegate = Emit<Func<MyStruct, string>>.NewDynamicMethod()
    .LoadArgumentAddress(0)
    .CallVirtual(obj.GetMethod("ToString"), myStruct)
    .Return()
    .CreateDelegate();

This fails with a SigilVerificationException.

Sigil.SigilVerificationException: CallVirtual expected a System.Object; found MyStruct*
   at Sigil.Emit`1.UpdateStackAndInstrStream(Nullable`1 instr, TransitionWrapper transitions, Boolean firstParamIsThis)
   at Sigil.Emit`1.UpdateState(OpCode instr, MethodInfo method, IEnumerable`1 parameterTypes, TransitionWrapper transitions, Boolean firstParamIsThis, Type[] arglist)
   at Sigil.Emit`1.CallVirtual(MethodInfo method, Type constrained, Type[] arglist)
   at Program.Main(String[] args) in Program.cs:line 18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
1 participant