Skip to content

fix: deduplicate enum constant names that collapse to the same identifier#4519

Open
alliasgher wants to merge 1 commit into
sqlc-dev:mainfrom
alliasgher:fix/4515-enum-duplicate-constant-names
Open

fix: deduplicate enum constant names that collapse to the same identifier#4519
alliasgher wants to merge 1 commit into
sqlc-dev:mainfrom
alliasgher:fix/4515-enum-duplicate-constant-names

Conversation

@alliasgher

Copy link
Copy Markdown

What

buildEnums deduplicated enum constant names on EnumReplace(v), but the name actually emitted is StructName(enumName + "_" + value), which collapses characters further (non-idents to _, then _-split + title-case with no separator). So values that produce distinct EnumReplace outputs can still land on the same final identifier.

For a PostgreSQL enum like blood groups:

CREATE TYPE blood_group_type AS ENUM ('A+','A-','B+','B-','AB+','AB-','O+','O-');

"A+"EnumReplace "A" and "A-""A_" are different (so the old dedup didn't fire), but StructName maps both to BloodGroupTypeA. The result was duplicate Go constants that fail to compile.

Fix

Dedup on the final constant name (the StructName output) rather than the intermediate EnumReplace value, keeping the existing value_<i> fallback on collision. Non-colliding enums are unaffected (identical output, no golden changes).

Fixes #4515

Testing

Added TestBuildEnums_DeduplicatesConstantNames. It fails on main with duplicate constant name "BloodGroupTypeA" for values "A+" and "A-" (and B/AB/O), and passes with the fix.

…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
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.

Go code generation fails for PostgreSQL ENUM values with '+' and '-' due to duplicate constant names

1 participant