dadhi / FastExpressionCompiler

Fast Compiler for C# Expression Trees and the lightweight LightExpression alternative. Diagnostic and code generation tools for the expressions.
MIT License
1.15k stars 81 forks source link

Fix or support return ref value #415

Open quifi opened 3 months ago

quifi commented 3 months ago

With C#, we can return a ref value:

ref Int32 ReturnByRef(ref Int32 x) => ref x;

System expression simply says it is not supported

System.ArgumentException : Expression of type 'System.Int32' cannot be used for return type 'System.Int32&'

LightExpression supports return ref value, but sometimes fails to unref returned ref value. e.g.

Int32 f(ref Int32 x) => ReturnByRef(ref x);

emits

ldarg.1
call Int32& ReturnRef(Int32 ByRef)
(missing ldind.i4)
ret

See the "ReadReturnedRef" test case of attached source file. ReturnRef.zip

I tried to add a EmitLoadIndirectlyByRef after each Demit(OpCodes.Call), which seemed worked.

dadhi commented 3 months ago

@quifi Thank you for the reporting and for the 2 other issues

dadhi commented 2 months ago

@quifi I have checked your sample, but I don't see ldind.i4 in the decompiled IL code

So the current FEC output is fine, I think.

dadhi commented 2 months ago

@quifi The new FEC v4.2.1 with the fixes is released.

quifi commented 2 months ago

FEC

In order to reproduce the bug, I think 'Bar' should return int, instead of int&. See modified sample code

With v4.2.1, this one seems not fixed yet.