Skip to content

Implement constant expression handling for enum members and private const fields - #97

Merged
koenbeuk merged 2 commits into
mainfrom
fix/enum-constants-as-constant-expression
Sep 20, 2026
Merged

koenbeuk merged 2 commits into
mainfrom
fix/enum-constants-as-constant-expression

Conversation

@koenbeuk

Copy link
Copy Markdown
Collaborator

fixes #95

Copilot AI lite review requested due to automatic review settings September 20, 2026 01:53

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.

Copilot review overview

🟡 Changes recommended

The new enum-member constant emission uses field.Name without keyword escaping, which can produce uncompilable generated code for valid enums containing escaped identifiers (e.g. @class).

Get a fresh assessment by requesting another Copilot review.

Review effort: Lite
Findings: 1 Medium severity

Open (1)
What changed in this PR

This PR updates the source generator to inline enum members and const fields as Expression.Constant(...) nodes (instead of emitting reflective Expression.Field(... GetField(...)) access), aligning generated trees with C# compiler semantics and avoiding query-provider funcletization into parameters (per issue #95).

Changes:

  • Inline enum member access and const fields as Expression.Constant(...) during expression tree emission.
  • Add/adjust generator snapshot tests to validate constant emission for enums and private const fields.
  • Add integration tests to ensure expanded expressions (and EF Core SQL) no longer contain literal-field MemberExpression access patterns.
File Description
src/​ExpressiveSharp.Generator/​Emitter/​ExpressionTreeEmitter.cs Inline constant fields/enum members into Expression.Constant to avoid reflective field access.
tests/​ExpressiveSharp.IntegrationTests/​Tests/​EnumComparisonTests.cs Adds runtime validation + tree-shape assertion ensuring no literal field-member access remains for enum-to-enum switch mapping.
tests/​ExpressiveSharp.EntityFrameworkCore.IntegrationTests/​Tests/​Sqlite/​CommonScenarioTests.cs Adds SQL assertion that enum-switch translation emits CASE and avoids parameterization.
tests/​ExpressiveSharp.Generator.Tests/​ExpressiveGenerator/​EnumTests.cs Adds generator tests covering enum-to-enum switch mapping and private const fields.
tests/​ExpressiveSharp.Generator.Tests/​ExpressiveGenerator/​EnumTests.EnumToEnumSwitchMapping.verified.txt New snapshot verifying enum switch emits enum constants (not reflected fields).
tests/​ExpressiveSharp.Generator.Tests/​ExpressiveGenerator/​EnumTests.PrivateConstFields.verified.txt New snapshot verifying private const fields are emitted as constants (including enum-typed const).
tests/​ExpressiveSharp.Generator.Tests/​ExpressiveGenerator/​SwitchPatternTests.SwitchStatementWithReturnArms.verified.txt Updates snapshot to expect enum constants rather than reflective field access.
tests/​ExpressiveSharp.Generator.Tests/​ExpressiveGenerator/​MethodTests.ConstMember.verified.txt Updates snapshot to expect const field emission as Expression.Constant.
tests/​ExpressiveSharp.Generator.Tests/​ExpressiveGenerator/​LoopTests.ForEach_Min.verified.txt Updates snapshot to expect int.MaxValue emitted as a constant literal.
tests/​ExpressiveSharp.Generator.Tests/​ExpressiveGenerator/​LoopTests.ForEach_ConditionalMinWithMemberAccess.verified.txt Updates snapshot similarly for int.MaxValue.
tests/​ExpressiveSharp.Generator.Tests/​ExpressiveGenerator/​EnumTests.EnumRelationalPattern.verified.txt Updates snapshot to emit enum constants rather than reflective field access.
tests/​ExpressiveSharp.Generator.Tests/​ExpressiveGenerator/​EnumTests.EnumRelationalComparisonWithNullableEnum.verified.txt Updates snapshot to emit enum constants rather than reflective field access.
tests/​ExpressiveSharp.Generator.Tests/​ExpressiveGenerator/​EnumTests.EnumRelationalComparison.verified.txt Updates snapshot to emit enum constants rather than reflective field access.
tests/​ExpressiveSharp.Generator.Tests/​ExpressiveGenerator/​ConstructorTests.ProjectableConstructor_ReferencingStaticConstMember.verified.txt Updates snapshot to emit string const field as Expression.Constant.

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

@codecov

codecov Bot commented Sep 20, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

@koenbeuk
koenbeuk merged commit 36513c6 into main Sep 20, 2026
11 checks passed
@koenbeuk
koenbeuk deleted the fix/enum-constants-as-constant-expression branch September 20, 2026 02:19
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.

Enum constants accessed via reflection

2 participants