Skip to content

Render unnamed negative enum values correctly in LinqToCSharpSyntaxTranslator - #39497

Merged
AndriySvyryd merged 1 commit into
dotnet:mainfrom
ilkertskn:fix/linq-to-csharp-negative-unnamed-enum
Nov 3, 2026
Merged

AndriySvyryd merged 1 commit into
dotnet:mainfrom
ilkertskn:fix/linq-to-csharp-negative-unnamed-enum

Conversation

@ilkertskn

Copy link
Copy Markdown
Contributor

An unknown enum value is formatted by Enum.Format as a number, which was only detected when it started with a digit. A negative value such as (MyEnum)(-1000) was therefore emitted as the member access MyEnum.-1000, which doesn't compile. Detect any non-identifier formatting, read the value via Convert instead of re-parsing the string, and parenthesize negative literals, since (MyEnum)-1000L is parsed as a subtraction.

  • [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)
  • [x ] Code follows the same patterns and style as existing code in this repo

…anslator

An unknown enum value is formatted by Enum.Format as a number, which was only
detected when it started with a digit. A negative value such as (MyEnum)(-1000)
was therefore emitted as the member access MyEnum.-1000, which doesn't compile.
Detect any non-identifier formatting, read the value via Convert instead of
re-parsing the string, and parenthesize negative literals, since (MyEnum)-1000L
is parsed as a subtraction.

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.

🟢 Approval recommended

No active review issues are identified, and the described implementation and regression coverage address negative signed and large unsigned unnamed enum values.

No open issues 🎉

What changed in this PR

This pull request fixes C# generation for unnamed negative and unsigned enum values in LinqToCSharpSyntaxTranslator, ensuring the emitted casts are valid and preserve the underlying values.

Changes:

  • Detects non-identifier enum formatting, including negative values.
  • Converts enum values directly using their signed or unsigned underlying representation.
  • Parenthesizes negative literals to avoid invalid cast syntax.
  • Adds regression tests for negative signed and large unsigned enum values.
File Description
test/​EFCore.Design.Tests/​Query/​LinqToCSharpSyntaxTranslatorTest.cs Adds coverage for negative unnamed values and large unsigned enum values.
src/​EFCore.Design/​Query/​Internal/​LinqToCSharpSyntaxTranslator.cs Generates valid casts for unnamed signed and unsigned enum values.

Review effort: Lite

@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 merged commit 56209cb into dotnet:main Nov 3, 2026
19 of 20 checks passed
@dotnet-milestone-bot dotnet-milestone-bot Bot added this to the 12.0-preview1 milestone Nov 3, 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