Skip to content

Deduplicate aliased enum values in generated metadata#376

Merged
shibayan merged 1 commit into
masterfrom
fix/enum-alias-values
Jul 5, 2026
Merged

Deduplicate aliased enum values in generated metadata#376
shibayan merged 1 commit into
masterfrom
fix/enum-alias-values

Conversation

@shibayan

@shibayan shibayan commented Jul 5, 2026

Copy link
Copy Markdown
Owner

Summary

An enum with aliased members — multiple names sharing the same constant value, e.g.

public enum AliasEnum
{
    None = 0,
    Default = 0,
    Value = 1
}

made EnumMetadataGenerator emit a switch expression with duplicate constant patterns, breaking the consuming project's build with CS8510: The pattern is unreachable in the generated file.

Members are now deduplicated by constant value keeping the first declaration (aliases are indistinguishable at runtime anyway), for both the generated values array and the display-name switch. The reflection fallback in EnumMetadataRegistry applies the same semantics so both paths behave identically.

Note: this was also broken in v3.0.x, where EnumHelper threw ArgumentException at runtime on the duplicate dictionary key — so this has never worked, it just fails earlier (at compile time) in v3.1.

Test plan

  • Verified with a standalone consumer project: the alias enum previously failed to compile (CS8510 in *.EnumMetadata.g.cs); after the fix it builds and resolves Items = [None, Value] with Default labeled None
  • New unit test for the reflection fallback dedup semantics
  • Full suite: 325 passed, dotnet format clean

🤖 Generated with Claude Code

An enum with aliased members (multiple names sharing the same constant
value, e.g. None = 0, Default = 0) made EnumMetadataGenerator emit a switch
expression with duplicate constant patterns, breaking the consuming
project's build with CS8510. Deduplicate members by constant value keeping
the first declaration, and apply the same semantics to the reflection
fallback in EnumMetadataRegistry.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
@shibayan shibayan self-assigned this Jul 5, 2026
@shibayan shibayan added the bug Something isn't working label Jul 5, 2026
@shibayan shibayan requested a review from Copilot July 5, 2026 11:35

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.

Pull request overview

Fixes enum metadata generation for enums with aliased members (multiple names sharing the same constant value) by deduplicating members so generated switch expressions don’t contain duplicate constant patterns (avoiding CS8510) and aligning runtime fallback behavior with the generator’s semantics.

Changes:

  • Deduplicate aliased enum members by constant value in the source generator (keep first declaration).
  • Deduplicate aliased enum members in reflection fallback metadata creation.
  • Add a unit test covering alias-dedup semantics for the reflection fallback.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 1 comment.

File Description
tests/Sharprompt.Tests/Binding/EnumMetadataRegistryTests.cs Adds coverage to ensure aliased enum values keep the first declaration in fallback metadata.
src/Sharprompt/EnumMetadataRegistry.cs Deduplicates reflected enum members by value to match generator behavior and avoid duplicate entries.
src/Sharprompt.SourceGenerator/EnumMetadataGenerator.cs Deduplicates aliased enum members to prevent unreachable-pattern compiler errors in generated code.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread src/Sharprompt/EnumMetadataRegistry.cs
@shibayan shibayan merged commit 7606fc9 into master Jul 5, 2026
6 checks passed
@shibayan shibayan deleted the fix/enum-alias-values branch July 5, 2026 12:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants