fix: resolve bitfield accessor name conflicts#3354
Open
zRedShift wants to merge 1 commit intorust-lang:mainfrom
Open
fix: resolve bitfield accessor name conflicts#3354zRedShift wants to merge 1 commit intorust-lang:mainfrom
zRedShift wants to merge 1 commit intorust-lang:mainfrom
Conversation
Fixes rust-lang#3322 When a struct has bitfields like `x` and `set_x`, the generated getter for `set_x` collides with the generated setter for `x` (both named `set_x`), producing duplicate method definitions that fail to compile. The fix has two parts: 1. In the IR phase (comp.rs), resolve inter-bitfield accessor name collisions by tracking all assigned names (getter, setter, and their _raw variants) and suffixing with `_bindgen_bitfield` until unique. This handles chains like `x` / `set_x` / `set_x_bindgen_bitfield`. 2. In codegen (mod.rs), seed the method_names set with bitfield accessor names before emitting C++ methods, constructors, and destructors. This ensures the existing method dedup mechanism renames colliding C++ wrappers (e.g. `destruct` → `destruct1`) instead of producing duplicates. Method names are Rust-mangled before dedup so keyword collisions (e.g. C++ `type` → Rust `type_`) are caught. The behavior change: bitfield accessors now keep their natural names, and colliding C++ method/ctor/dtor wrappers are the ones that get numeric suffixes.
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 #3322
When a struct has bitfields like
xandset_x, the generated getter forset_xcollides with the generated setter forx(both namedset_x), producing duplicate method definitions that fail to compile.Root cause
Bitfield accessor name dedup only checked against C++ methods (
has_method), not against other bitfield accessors, and codegen's method/constructor/destructor emission had no knowledge of bitfield accessor names at all. This left several collision classes unhandled:x+set_x)foo1) collide with a bitfielddestruct) and constructors (new,new1) colliding with bitfields_rawaccessor variant collisions (set_x_rawvs bitfieldset_x_raw)type→type_) not considered during method dedupFix
IR phase (
comp.rs): resolve inter-bitfield accessor collisions by tracking all assigned names (getter, setter, and_rawvariants) and suffixing with_bindgen_bitfielduntil unique. Handles chains likex/set_x/set_x_bindgen_bitfield.Codegen phase (
mod.rs): seed themethod_namesset with bitfield accessor names before emitting C++ methods, constructors, and destructors. The existing method dedup mechanism then renames colliding wrappers instead of producing duplicates. Method names are Rust-mangled before dedup so keyword collisions (e.g. C++type→ Rusttype_) are caught.The behavior change: bitfield accessors keep their natural names, and colliding C++ method/ctor/dtor wrappers get numeric suffixes (
type_1,destruct1, etc.).