rust-bindgen icon indicating copy to clipboard operation
rust-bindgen copied to clipboard

Bitfield accessor method name conflict when two bit fields `x` and `set_x` exist

Open zRedShift opened this issue 1 month ago • 0 comments

Using some proprietary headers I can't really change that are full of bitfields, one of the structs has two bitfields, basically x and set_x. This triggers a compile error when trying to use the generated bindings, due to duplication of method definitions of the bitfield accessors. Here's a minimal repro's build.rs:

fn main() {
    bindgen::Builder::default()
        .header_contents("wrapper.h", "struct test { char set_x: 1; char x: 1 };")
        .generate()
        .unwrap()
        .write_to_file(PathBuf::from(env::var("OUT_DIR").unwrap()).join("bindings.rs"))
        .unwrap()
}

Will generate an error of duplicate method definitions as bindgen generates getters and setters for each bitfield:

error[E0592]: duplicate definitions with name `set_x`
   --> src/lib.rs:192:5
    |
155 |     pub fn set_x(&self) -> ::std::os::raw::c_char {
    |     --------------------------------------------- other definition for `set_x`
...
192 |     pub fn set_x(&mut self, val: ::std::os::raw::c_char) {
    |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ duplicate definitions for `set_x`

error[E0592]: duplicate definitions with name `set_x_raw`
   --> src/lib.rs:209:5
    |
166 |     pub unsafe fn set_x_raw(this: *const Self) -> ::std::os::raw::c_char {
    |     -------------------------------------------------------------------- other definition for `set_x_raw`
...
209 |     pub unsafe fn set_x_raw(this: *mut Self, val: ::std::os::raw::c_char...
    |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ duplicate definitions for `set_x_raw`

Right now I have a workaround of running a string replace on the output of bindgen before saving it to file since it only occurs in one struct, but it's not really scalable.

Since it's a bit field, these methods are generated automatically and don't correspond to real Rust struct fields, so there should either be an automatic name conflct resolution built into bindgen, or a way via ParseCallbacks to be able to rename (bit)fields (which I prefer).

zRedShift avatar Nov 18 '25 17:11 zRedShift