Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

'or' pattern matching issue #237

Open
andreperezdp opened this issue May 10, 2024 · 4 comments
Open

'or' pattern matching issue #237

andreperezdp opened this issue May 10, 2024 · 4 comments

Comments

@andreperezdp
Copy link

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
Copy link
Owner

hazzik commented May 10, 2024

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

@andreperezdp
Copy link
Author

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
Copy link
Owner

hazzik commented May 11, 2024

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
Copy link
Author

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants