Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Oct 2, 2025

Fixes #237 by documenting the limitation that DelegateDecompiler cannot always decompile is ... or ... pattern matching due to compiler optimizations.

The Problem

When using pattern matching with the is ... or ... syntax, the C# compiler sometimes optimizes these patterns into comparison operations for performance. For example:

// Original code
x is TestEnum.Foo or TestEnum.Bar

// Gets compiled to something like
(uint)x <= 1

This optimization makes it impossible for DelegateDecompiler to distinguish between:

  • A genuine comparison operation that the developer intended
  • An optimized pattern match that was transformed by the compiler

As noted in the issue discussion, there's no way to determine how the comparison operator came about - whether it was from optimized is … or … pattern matching or from explicit user code.

The Solution

This PR adds clear documentation in the README.md Limitations section explaining this constraint. The documentation:

  • Explains that pattern matching decompilation is not always possible due to compiler optimizations
  • Provides a concrete example showing how pattern matching gets optimized to comparison operations
  • Clarifies that this affects any type, not just enums
  • Helps users understand why certain decompilation scenarios may not work as expected

This addresses the issue by setting proper expectations for users encountering this limitation.

Original prompt

This section details on the original issue you should resolve

<issue_title>'or' pattern matching issue</issue_title>
<issue_description>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

</issue_description>

<agent_instructions>Update README.md with Limitations that it is not always possible to decompile is ... or ... pattern matching due to compiler optimizations. Use issue discussion as context.</agent_instructions>

Comments on the Issue (you are @copilot in this section)

@hazzik I would not be able to untangle the results. How I suppose to distinguish between genuine `<` and `or`? @hazzik 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. </comment_new>

Fixes #237

✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.

Copilot AI changed the title [WIP] 'or' pattern matching issue Document pattern matching limitations with compiler optimizations Oct 2, 2025
Copilot AI requested a review from hazzik October 2, 2025 04:39
Copilot finished work on behalf of hazzik October 2, 2025 04:39
@hazzik
Copy link
Owner

hazzik commented Oct 2, 2025

@copilot simplify the language.

Copy link
Contributor Author

Copilot AI commented Oct 2, 2025

@copilot simplify the language.

Simplified the language to be more concise and direct. Commit cc1a92a

Copilot finished work on behalf of hazzik October 2, 2025 04:42
README.md Outdated

`is ... or ...` pattern matching cannot always be decompiled due to compiler optimizations. The compiler may optimize enum patterns to use comparison operators, making it impossible to distinguish between genuine comparisons and optimized patterns.

For example, `x is SomeEnum.Value1 or SomeEnum.Value2` might compile to `(int)x <= 1`.
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@copilot add SomeEnum definition for clarity.

Copilot finished work on behalf of hazzik October 2, 2025 04:46
README.md Outdated
x is TestEnum.Foo or TestEnum.Bar

// Might compile to:
(int)x <= 1
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@copilot usually it is (uint)x <= 1u

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated the example to use (uint)x cast as it's more accurate. Commit 75f3c10

@hazzik hazzik marked this pull request as ready for review October 2, 2025 04:53
@hazzik hazzik enabled auto-merge (squash) October 2, 2025 04:53
Copilot finished work on behalf of hazzik October 2, 2025 04:53
Copilot finished work on behalf of hazzik October 2, 2025 04:55
@hazzik hazzik merged commit 3236c77 into main Oct 2, 2025
3 checks passed
@hazzik hazzik deleted the copilot/fix-7fd7d965-6d28-4b60-b863-ec35ed6209c1 branch October 2, 2025 05:34
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

Successfully merging this pull request may close these issues.

'or' pattern matching issue

2 participants