dadhi/FastExpressionCompiler

Calling a user defined == operator runs into a CLR invalid program exception

Marluwe opened this issue · 2 comments

First of all: Thank you for this amazing package!

I found out that there is an issue with user defined == operators. The tested version is the current release 4.1.0 running on .net6.

Here is minimal example. The .net internal compile is fine.

using System.Linq.Expressions;
using FastExpressionCompiler;
// a lambda using a user defined equals operator
LambdaExpression expressionA = (TestInstance instance) => instance == 5;
var funcA = expressionA.CompileFast();
Console.WriteLine("Compilation OK");
var instanceA = new TestInstance { Value = 5 };
try
{
    Console.WriteLine(funcA.DynamicInvoke(instanceA));
    Console.WriteLine("Calling funcA - OK");
}
catch (Exception e)
{
    Console.WriteLine("Calling funcA => " + e.Message + Environment.NewLine + e.InnerException.Message);
}

public class TestInstance
{
    public int Value;
    public static bool operator ==(TestInstance instance, int value) => instance.Value == value;
    public static bool operator !=(TestInstance instance, int value) => instance.Value != value;
}

The output is

Compilation OK
Calling funcA => Exception has been thrown by the target of an invocation.
Common Language Runtime detected an invalid program.

I assume the issue lies here, as the equality operator needs to check the second parameter type to match the right operand.

image

I tested it locally, changing the

if (ps.Length == 2 && ps[0].ParameterType == leftOpType && ps[1].ParameterType ==leftOpType)

to

if (ps.Length == 2 && ps[0].ParameterType == leftOpType && ps[1].ParameterType == rightOpType)
```.

@Marluwe Thanks for the fix. Coming with the v4.1.1 release.