codegen: escape a oneof enum name that PascalCases to Self - #465
Merged
iainmcgin merged 2 commits intoSep 25, 2026
Merged
Conversation
|
All contributors have signed the CLA ✍️ ✅ |
Contributor
Author
|
I have read the CLA Document and I hereby sign the CLA |
The unit test checks that the generated file parses. Add a oneof named self_ to the keywords fixture with a round-trip test, so the Self_ enum and the self_ field are also type-checked together. The doc comment on oneof_enum_ident no longer claims sibling oneofs cannot share an ident; names that PascalCase alike do, undiagnosed. The changelog fragment is one paragraph and cites the PR.
Collaborator
|
[claude code] Thanks for this, and for the careful write-up. The diagnosis is right: I pushed one maintainer commit (123f9d6) on top:
|
iainmcgin
approved these changes
Sep 25, 2026
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 subscribe to this conversation on GitHub.
Already have an account?
Sign in.
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.
A oneof named
selfmakes code generation fail outright. PascalCasing the name producesSelf, which is a reserved Rust identifier, so the generator emitspub enum Self, the generated file does not parse, andgeneratereturns an error that names no oneof at all. The same escape is already applied one function below to the oneof's variant names, and to the struct field, so this applies it to the enum's own name as well and the three finally agree.oneof_enum_ident(buffa-codegen/src/oneof.rs:932) builds the identifier with a bareformat_ident!.self,self_and_selfall reach it, becauseto_pascal_case(oneof.rs:972) splits on_and capitalises each part, andSelfis the only reserved Rust identifier with a leading capital, so it is the only reachable case. The ident is produced once inresolve_oneof_idents(oneof.rs:954) and consumed for both the owned enum (oneof.rs:679) and the view enum (view.rs:1105).format_tokens(buffa-codegen/src/lib.rs:4332) runssyn::parse2::<syn::File>before prettyplease sees the tokens, so the result isCodeGenError::InvalidSyntax("generated code failed to parse as Rust: ...")for a perfectly legal.proto.oneof_variant_ident(oneof.rs:967) already routes throughmake_field_ident, and its doc comment spells out this exact failure: it "would otherwise producepub enum Foo { Self(...) }and fail to parse".CodeGenContext::oneof_ident(context.rs:534) escapes the struct field too. Only the enum's own name was left raw, so for a oneof namedself_the generated field isself_and the type it points at isSelf.No fixture puts a keyword on the oneof itself, which is why this survived:
buffa-test/protos/keywords.protocovers keyword package, message, field and enum-value names, includingstring self = 4;at line 35, but its one oneof is namedvalue, and the #47 regression test (buffa-codegen/src/tests/naming.rs:1147) puts the keyword on a variant while naming the oneofidentity.The fix routes the enum name through
make_field_ident(idents.rs:67), exactly as the variant name already is, soSelfbecomesSelf_and matches the #47 convention. It is a no-op for every other name:make_field_identonly rewrites whenis_rust_keywordmatches, PascalCasing always yields a leading capital, andSelfis the only capitalised entry in that table (idents.rs:181). The checked-in generated trees are unaffected, since the only oneof in either isgoogle.protobuf.Value.kindyieldingKind, so nothing needs regenerating.format_ident!had no other use inoneof.rs, so its import goes with it; that is the only other line in the diff.test_oneof_named_self_escapes_its_enum_to_self_underscoresits beside the #47 test it mirrors: it generates a message with a oneof namedself_and asserts the enum isSelf_while the non-keyword variant is untouched. On unpatched source it is thegenerate(...).expect(...)that fails, because generation errors out before any assertion runs.cargo fmt --check -p buffa-codegenis clean on the pinned 1.95.0 toolchain. I have not run the test suite on this machine, so CI is the check on the new test.The changelog fragment is
.changes/unreleased/fixed-20260919-210200.yaml. It does not cite a PR number yet; happy to amend that in once this has one.