fix: deduplicate enum constant names that collapse to the same identifier#4519
Open
alliasgher wants to merge 1 commit into
Open
fix: deduplicate enum constant names that collapse to the same identifier#4519alliasgher wants to merge 1 commit into
alliasgher wants to merge 1 commit into
Conversation
…fier
buildEnums deduplicated on EnumReplace(v), but the final constant name is
StructName(enumName+"_"+value), which collapses characters further (e.g. a
trailing underscore). So enum values like "A+" and "A-" reduced to distinct
EnumReplace outputs ("A" and "A_") that both mapped to the same constant
name, producing duplicate Go constants that fail to compile. Dedup on the final
constant name instead, falling back to value_<i> on collision.
Fixes sqlc-dev#4515
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.
What
buildEnumsdeduplicated enum constant names onEnumReplace(v), but the name actually emitted isStructName(enumName + "_" + value), which collapses characters further (non-idents to_, then_-split + title-case with no separator). So values that produce distinctEnumReplaceoutputs can still land on the same final identifier.For a PostgreSQL enum like blood groups:
"A+"→EnumReplace"A"and"A-"→"A_"are different (so the old dedup didn't fire), butStructNamemaps both toBloodGroupTypeA. The result was duplicate Go constants that fail to compile.Fix
Dedup on the final constant name (the
StructNameoutput) rather than the intermediateEnumReplacevalue, keeping the existingvalue_<i>fallback on collision. Non-colliding enums are unaffected (identical output, no golden changes).Fixes #4515
Testing
Added
TestBuildEnums_DeduplicatesConstantNames. It fails onmainwithduplicate constant name "BloodGroupTypeA" for values "A+" and "A-"(and B/AB/O), and passes with the fix.