Skip to content

Equality comparison between nullable and null inside Any produces incorrect compiled expression #341

@cervengoc

Description

@cervengoc

Hi,

First of all, thank you for your work in this very promising library.

We have lots of expressions, and currently experimenting with whether we can utilize this library or not. We noticed, that several unit tests are failing, and nailed it down to the issue given in the title.

Please see the following failing test,.

[TestClass]
public class CompiledExpressionIssue
{
    [TestMethod]
    public void Works_WithCompile()
    {
        // Arrange

        Expression<Func<Test, bool>> expression = t => t.A.Any(e => e.Value != null);

        var compiled = expression.Compile();

        var instance = new Test()
        {
            A = new[]
            {
                new Test2() { Value = 0 },
            },
        };

        // Act

        var result = compiled(instance);

        // Assert

        result.Should().BeTrue();
    }

    [TestMethod]
    public void Fails_WithCompileFast()
    {
        // Arrange

        Expression<Func<Test, bool>> expression = t => t.A.Any(e => e.Value != null);

        var compiled = expression.CompileFast(true, CompilerFlags.EnableDelegateDebugInfo);
        var di = compiled.Target as IDelegateDebugInfo;

        var instance = new Test()
        {
            A = new[]
            {
                new Test2() { Value = 0 },
            },
        };

        // Act

        var result = compiled(instance);

        // Assert

        result.Should().BeTrue();
    }

    public class Test
    {
        public Test2[] A { get; set; }
    }

    public class Test2
    {
        public decimal? Value { get; set; }
    }
}

Metadata

Metadata

Assignees

Labels

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions