rust-lang / rust-lang/rust-bindgen

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

Open
#3,322 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Rust
Stars
5.3k
Forks
829
Avg merge
1d 1h
Merged PRs (30d)
15

Description

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).

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the build.rs minimal reproduction and inspect the generated bindings for the conflicting bitfield accessor names. Review bindgen's bitfield accessor generation and the ParseCallbacks entry point; done means the x and set_x fields no longer produce duplicate Rust methods and field renaming can be handled as requested.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
tooling
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.