Skip to content

Disambiguate duplicate enum variant names instead of panicking#1036

Open
danieleades wants to merge 1 commit into
oxidecomputer:mainfrom
danieleades:fix/variant-name-collision
Open

Disambiguate duplicate enum variant names instead of panicking#1036
danieleades wants to merge 1 commit into
oxidecomputer:mainfrom
danieleades:fix/variant-name-collision

Conversation

@danieleades

Copy link
Copy Markdown
Contributor

Problem

Schemas whose enum values sanitize to identical Rust identifiers cause a panic during generation:

panic: Failed to make unique variant names for [=,>,<,≥,>=,≤,<=,≠,!=]

Variant naming makes two attempts (plain sanitization, then replacing non-identifier characters with X), and if names still collide — as with the comparison-operator enum above, where =, >, <, , , and all reduce to X — it panics.

Fixes #948

Fix

Replace the panic with a final, deterministic disambiguation pass: proceeding in variant order, the first variant with a given name keeps it unchanged, and each subsequent duplicate gets the smallest numeric suffix (starting at 2) that doesn't collide with any other variant name (guarding against, say, a pre-existing literal X2 variant). Three variants that all sanitize to X become X, X2, X3.

Serialization is unaffected: serde renames and the Display/FromStr impls are all generated from the variant's raw name, so suffixed variants round-trip through the original strings. The confusing duplicate-counting bookkeeping that fed the panic message is removed along the way.

Trade-offs

Schemas that previously generated successfully produce identical output; the new pass only runs where generation used to panic, so the suffixed names introduce no compatibility concern.

Tests

When enum values sanitize to identical identifiers (e.g. "=", ">",
"≥"), generation panicked. Instead, disambiguate deterministically:
in variant order, the first variant with a given name keeps it and each
subsequent duplicate gets the smallest numeric suffix (starting at 2)
that doesn't collide with any other variant name. Serialized names are
unaffected since serde renames and the Display/FromStr impls are
generated from the raw variant names.

Fixes oxidecomputer#948
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.

Failed to make unique variant names for [=,>,<,≥,>=,≤,<=,≠,!=]

1 participant