hazzik / DelegateDecompiler

A library which is able to decompile a delegate or a method body to its lambda representation
MIT License
522 stars 62 forks source link

'or' pattern matching issue #237

Open andreperezdp opened 1 month ago

andreperezdp commented 1 month ago

With .NET 8, enum computed properties that use 'or' pattern matching returns different results that using the '||' operator.

It seems to be related with the way .NET 8 optimize the code using the 'less than' operator and the int value of the enum. I have attached a small sample that reproduces the problem. Using the 'or' pattern matching, the query returns more entities than using the '||' operator.

EnumPatternMatching.zip

hazzik commented 1 month ago

I would not be able to untangle the results. How I suppose to distinguish between genuine < and or?

andreperezdp commented 1 month ago

The computed property doesn't use <, it is using or:

public bool Is2Or3PatternMatching => Status is StatusEnum.Status2 or StatusEnum.Status3;

If I use that property to filter an in-memory list, the Linq Where() method works fine and returns the correct result (only entities with the Status values StatusEnum.Status2 and StatusEnum.Status3):

var entity1 = new Entity { Status = StatusEnum.Status1 };
var entity2 = new Entity { Status = StatusEnum.Status2 };
var entity3 = new Entity { Status = StatusEnum.Status3 };
var entity4 = new Entity { Status = StatusEnum.Status4 };

var entities = new List<Entity> { entity1, entity2, entity3, entity4 };

// 'result' only contains 'entity2' and 'entity3'
var result = entities.Where(e => e.Is2Or3PatternMatching).ToList();

But when I use the Decompile() method to query the DB, the result also includes entities with the Status value StatusEnum.Status1:

// 'result' also contains 'entity1'
var result = dbContext.Entities.Where(e => e.Is2Or3PatternMatching).Decompile().ToList();
hazzik commented 1 month ago

When compiler compiles is … or … it sometimes optimizes the IL code to produce the <.

What I’m saying is that for me it would not be possible to know how the < came about: was it optimized is … or … or it was used in the user code.

andreperezdp commented 1 month ago

Ok, thanks for the clarification. Then we can consider that this is a limitation of the library, right?