Strip r# prefix from raw identifiers in generated C++ names - #1749
Open
HarnageaGabriel wants to merge 1 commit into
Open
Strip r# prefix from raw identifiers in generated C++ names#1749HarnageaGabriel wants to merge 1 commit into
HarnageaGabriel wants to merge 1 commit into
Conversation
extern "C++" parameters/fields named with a raw identifier (e.g. r#type, used only to dodge a Rust keyword) were emitted verbatim into generated C++, producing invalid code like `void foo(::std::int32_t r#type) noexcept`. ForeignName::parse is the single place that turns a Rust identifier into its C++-facing name, both for the implicit default name and for explicit #[cxx_name = ...] attributes, so strip the r# prefix there. This mirrors the existing precedent in QualifiedName::parse_unquoted (syntax/qualified.rs) for namespace segments. Fixes dtolnay#1324
HarnageaGabriel
force-pushed
the
fix-raw-ident-cpp-codegen
branch
from
August 19, 2026 19:34
4723722 to
7d9b121
Compare
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.
Summary
Fixes #1324.
A raw identifier used in an
extern "C++"block purely to dodge a Rustkeyword (e.g.
pub fn highlight(r#type: i32);) was being emitted verbatiminto the generated C++, including the
r#prefix:which is not valid C++.
Root cause
ForeignName::parse(syntax/names.rs) is the single place that turns aRust identifier into its C++-facing name — used both for the implicit
default name and for explicit
#[cxx_name = ...]attributes — but itnever stripped the
r#marker before storing the name.Fix
Strip a leading
r#inForeignName::parse, mirroring the existingprecedent in
QualifiedName::parse_unquoted(syntax/qualified.rs), whichalready does this for raw identifiers in namespace segments.
This intentionally stays narrowly scoped to the issue: it doesn't attempt
to detect or rename identifiers that, after stripping
r#, would collidewith a C++ reserved keyword (e.g.
class,template) — the crate has noC++ keyword list today, and adding one is out of scope for this fix.
Test plan
test_raw_identifier_paramintests/cxx_gen.rs, assertingthe generated header/implementation contain no
r#and the expectedC++ signature uses the bare identifier.
cargo test --test cxx_genpasses (4/4).cargo buildsucceeds for the whole workspace.🤖 Generated with Claude Code