Strip the r# prefix from raw identifiers in derive(Debug) - #402
Merged
Conversation
`#[builder(derive(Debug))]` printed the `r#` escape as if it were part of the
name. A builder whose type and members use raw identifiers formatted as
`r#type { r#struct: 1, r#enum: 0, while: 2 }`, while the std
`#[derive(Debug)]` prints those same names as `type`, `struct` and `enum`.
Named members were already correct: `derive_debug` in
`bon-macros/src/builder/builder_gen/builder_derives/debug.rs` formats them
with `MemberName::snake_raw_str`, which is the name with the prefix removed.
The three other names in the generated `fmt` came from `Ident::to_string()`,
which keeps it:
* the builder struct name passed to `Formatter::debug_struct`, reachable
through `#[builder(builder_type = r#type)]`
* `#[builder(start_fn)]` member names
* `#[builder(field)]` member names
All three now use `IdentExt::raw_name()`, the helper `snake_raw_str` is itself
built from. `#[builder(name = ...)]` cannot be combined with `start_fn` or
with `field`, so for those members the original identifier is the only name
they can have and stripping the prefix is the whole fix.
Veetaha
approved these changes
Sep 7, 2026
Veetaha
left a comment
Collaborator
There was a problem hiding this comment.
Thanks, I'll make a patch for this
Merged
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.
#[builder(derive(Debug))]treats ther#escape as part of the name, so a builder whose type or members use raw identifiers prints the prefix:The std
#[derive(Debug)]printstype,structandenumfor those same names, andbonalready does it for ther#whilemember.The cause is in
bon-macros/src/builder/builder_gen/builder_derives/debug.rs. Named members are formatted withMemberName::snake_raw_stron line 40, which is the name with the prefix removed. The other three names come fromIdent::to_string(), which keeps it: the builder struct name passed toFormatter::debug_structon line 80,#[builder(start_fn)]members on line 14, and#[builder(field)]members on line 27.All three now use
IdentExt::raw_name(), the helper thatsnake_raw_stris itself built from.#[builder(name = ...)]cannot be combined withstart_fnor withfield, so for those members the original identifier is the only name they have and stripping the prefix is the whole fix.The regression test is
test_derive_debuginbon/tests/integration/builder/raw_idents.rs. It fails on master withr#type { r#struct: 1, r#enum: 0, while: 2 }and passes here withtype { struct: 1, enum: 0, while: 2 }.Verified on the pinned 1.98.0 toolchain:
cargo fmt --check,cargo clippy -p bon -p bon-macros --all-features --all-targetsunderRUSTFLAGS=--deny warnings,cargo test -p bon-macros --all-features,cargo test -p bon --all-features --doc(7 passed) andcargo test -p bon --all-features --test integration(204 passed). No.stderrsnapshot changed.