Skip to content

Fix Not on bool? and Increment/Decrement on non-int types in LinqToCSharpSyntaxTranslator - #39230

Merged
AndriySvyryd merged 2 commits into
dotnet:mainfrom
ilkertskn:fix/linq-to-csharp-unary-nullable-bool-and-increment
Oct 16, 2026
Merged

AndriySvyryd merged 2 commits into
dotnet:mainfrom
ilkertskn:fix/linq-to-csharp-unary-nullable-bool-and-increment

Conversation

@ilkertskn

Copy link
Copy Markdown
Contributor

…harpSyntaxTranslator

  • ExpressionType.Not on a bool? operand was translated as a bitwise complement (~b), which doesn't compile for bool?. Use logical negation for bool? as for bool.
  • ExpressionType.Increment/Decrement were rewritten as Add/Subtract with an int constant, which throws InvalidOperationException for any operand type other than int (long, double, decimal, int?, ...). Create the constant with the operand's type.
  • [x ] I've read the guidelines for contributing and seen the walkthrough
  • [x ] I've posted a comment on an issue with a detailed description of how I am planning to contribute and got approval from a member of the team
  • [x ] The code builds and tests pass locally (also verified by our automated build checks)
  • [x ] Commit messages follow this format:
        Summary of the changes
        - Detail 1
        - Detail 2

        Fixes #bugnumber
  • [ x] Tests for the changes have been added (for bug fixes / features)
  • Code follows the same patterns and style as existing code in this repo

…harpSyntaxTranslator

- ExpressionType.Not on a bool? operand was translated as a bitwise
  complement (~b), which doesn't compile for bool?. Use logical negation for
  bool? as for bool.
- ExpressionType.Increment/Decrement were rewritten as Add/Subtract with an
  int constant, which throws InvalidOperationException for any operand type
  other than int (long, double, decimal, int?, ...). Create the constant with
  the operand's type.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🟡 Changes recommended

Increment and decrement still fail for small integral operands such as byte.

1 open finding
What changed in this PR

This PR updates EF Core’s LINQ-to-C# translator to handle nullable Boolean negation and increment/decrement on non-int numeric types.

Changes:

  • Emit logical negation for bool?.
  • Create a type-matched constant for increment/decrement and add regression tests.
File Description
test/​EFCore.Design.Tests/​Query/​LinqToCSharpSyntaxTranslatorTest.cs Tests nullable Boolean and non-int numeric translation.
src/​EFCore.Design/​Query/​Internal/​LinqToCSharpSyntaxTranslator.cs Updates unary-expression translation.

🧠 Review effort: Lite

…oCSharpSyntaxTranslator

C# promotes short/ushort arithmetic to int, so translating Increment(short i)
as `i + 1` produced an int-typed expression instead of a short one. Compute in
int (int? for nullable operands) and convert back to the operand type.
byte/sbyte don't need handling: Expression.Increment/Decrement aren't defined
for them.
@ilkertskn

Copy link
Copy Markdown
Contributor Author

byte/sbyte can’t reach the translator: Expression.Increment/Decrement aren’t defined for them and throw when the tree is built. short/ushort were affected, though. They didn’t throw, but i + 1 is int-typed in C#. They’re now computed in int (int? for nullable operands) and converted back, e.g. ((short)(((int)i) + 1)), with a regression test for short, ushort and short?.

@AndriySvyryd AndriySvyryd left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thanks for your contribution!

@AndriySvyryd AndriySvyryd changed the title Fix Not on bool? and Increment/Decrement on non-int types in LinqToCS… Fix Not on bool? and Increment/Decrement on non-int types in LinqToCSharpSyntaxTranslator Oct 16, 2026
@AndriySvyryd
AndriySvyryd merged commit 98af607 into dotnet:main Oct 16, 2026
19 checks passed
@dotnet-milestone-bot dotnet-milestone-bot Bot added this to the 12.0-preview1 milestone Oct 20, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants