DelegateDecompiler icon indicating copy to clipboard operation
DelegateDecompiler copied to clipboard

.net 8 or vs ||

Open PoKNicolas opened this issue 2 years ago • 1 comments

With .net 8 RC2 the "or" causes an exception when decompile, while "||" works. With .net 7 "or" and "||" both works fine.

Unhandled exception. System.InvalidOperationException: The binary operator LessThanOrEqual is not defined for the types 'ConsoleApp1.TestEnum' and 'System.Int32'.

Here is some sample code:

public class TestClass
{
    public Guid Id { get; set; }
    public TestEnum Enum { get; set; }

    // will throw
    [Computed] public bool TestWithOr => Enum is TestEnum.A or TestEnum.B;
    // works
    [Computed] public bool TestWithPipes => Enum is TestEnum.A || Enum is TestEnum.B;
}

public enum TestEnum
{
    A,B,C
}

and try to decompile

using var db = new TestContext();

        var testPipes = db.TestClasses.Where(x => x.TestWithPipes).Decompile().FirstOrDefault();
        Console.WriteLine("Test with pipes woks.");
        
        // this will fail
        var testOr = db.TestClasses.Where(x => x.TestWithOr).Decompile().FirstOrDefault();
        Console.WriteLine("Test with or woks.");

PoKNicolas avatar Nov 03 '23 10:11 PoKNicolas

That is interesting. .NET 8 optimized the condition to:

    public bool TestWithOr
    {
        get
        {
            TestEnum @enum = Enum;
            if ((uint)@enum <= 1u)
            {
                return true;
            }
            return false;
        }
    }

hazzik avatar Nov 15 '23 11:11 hazzik