Closed akerfoot closed 5 years ago
Here is a sample project that demonstrates the error InlineILTest.tar.gz
I noticed a similar-looking error with a different weaver reported here: https://github.com/Fody/Fody/issues/312
The above error occurs when Debugging Information is set to Full.
When generating a Portable PDB, or none at all, I get a different error:
MSBUILD : error : Fody/InlineIL: Unexpected instruction, expected a method call but was: IL_005f: ldloc V_0 (in System.Int32 InlineILTest.Binding::AddOne(System.Int32) at instruction IL_005f: ldloc V_0)
Again, it works fine in Release.
The "Unexpected instruction" error is correct here: InlineIL expects your code to pass "constant" (for lack of a better word) compile-time arguments to the IL-emitting methods, but you pass a local variable to the Calli
method instead.
You need to write that code like this:
Calli(new StandAloneMethodSig(...));
Your code works in Release mode because the compiler sees the local variable is not needed, and eliminates it. Debug builds produce code which is closer to the source C# in this regard.
I probably should improve the error message and document this better.
What is not expected is the "Fody: An unhandled exception occurred:" part though. The PDB should have been written correctly (or maybe Fody shouldn't output the assembly at all if there were errors... hmm). Portable PDB works as expected. I'll take a look at why the legacy PDB writer fails. Thanks for the repro!
Oh, also you should replace this:
Ret();
return 0;
With this:
return IL.Return<int>();
As the former code will leave an unreachable IL block (the return 0;
part).
The "An unhandled exception occurred" part should be fixed in v1.3.1. I also added a clarification to the error message and to the readme.
Perfect, thank you!
Writing InlineIL to call a function pointer with
Calli
will trigger a build error. This only happens in the Debug configuration. Release builds and runs as expected.For example, this code:
causes this: