Deduplicate aliased enum values in generated metadata#376
Merged
Conversation
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>
Contributor
There was a problem hiding this comment.
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
An enum with aliased members — multiple names sharing the same constant value, e.g.
made
EnumMetadataGeneratoremit a switch expression with duplicate constant patterns, breaking the consuming project's build withCS8510: The pattern is unreachablein 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
EnumMetadataRegistryapplies the same semantics so both paths behave identically.Note: this was also broken in v3.0.x, where
EnumHelperthrewArgumentExceptionat runtime on the duplicate dictionary key — so this has never worked, it just fails earlier (at compile time) in v3.1.Test plan
*.EnumMetadata.g.cs); after the fix it builds and resolvesItems = [None, Value]withDefaultlabeledNonedotnet formatclean🤖 Generated with Claude Code