rust-lang / rust-bindgen

Automatically generates Rust FFI bindings to C (and some C++) libraries.
https://rust-lang.github.io/rust-bindgen/
BSD 3-Clause "New" or "Revised" License
4.45k stars 695 forks source link

Duplicate definitions with forward declared pointer types #1864

Open 7Hazard opened 4 years ago

7Hazard commented 4 years ago

Input C/C++ Header

CAPI void alt_RotationLayout_CAPI_Free_Forward(struct alt_RotationLayout* ptr);
typedef struct alt_RotationLayout {
    float roll;
    float pitch;
    float yaw;
} alt_RotationLayout;
CAPI void alt_RotationLayout_CAPI_Free(struct alt_RotationLayout* ptr);

Bindgen Invocation

let bindings = bindgen::Builder::default()
        .rustfmt_bindings(true)
        .layout_tests(false)
        .header(format!("./altv-capi-{}-static-{}/include/altv-capi-{}.h", kind, platform, kind))
        .parse_callbacks(Box::new(bindgen::CargoCallbacks))
        .generate()
        .expect("Unable to generate bindings");

    bindings
        .write_to_file(format!("src/altv_{}.rs", kind))
        .expect("Couldn't write bindings!");

Actual Results

/* automatically generated by rust-bindgen 0.54.1 */

pub const true_: u32 = 1;
pub const false_: u32 = 0;
pub const __bool_true_false_are_defined: u32 = 1;
pub type nullptr_t = *mut ::std::os::raw::c_void;
extern "C" {
  pub fn alt_RotationLayout_CAPI_Free_Forward(ptr: *mut alt_RotationLayout);
}
#[repr(C)]
#[derive(Debug, Copy, Clone)]
pub struct alt_RotationLayout {
  pub roll: f32,
  pub pitch: f32,
  pub yaw: f32,
}
extern "C" {
  pub fn alt_RotationLayout_CAPI_Free(ptr: *mut alt_RotationLayout);
}
#[repr(C)]
#[derive(Debug, Copy, Clone)]
pub struct alt_RotationLayout {
  pub _address: u8,
}

Expected Results

No duplicate definitions (when using forward declared pointers).

/* automatically generated by rust-bindgen 0.54.1 */

pub const true_: u32 = 1;
pub const false_: u32 = 0;
pub const __bool_true_false_are_defined: u32 = 1;
pub type nullptr_t = *mut ::std::os::raw::c_void;
#[repr(C)]
#[derive(Debug, Copy, Clone)]
extern "C" {
  pub fn alt_RotationLayout_CAPI_Free_Forward(ptr: *mut alt_RotationLayout);
}
pub struct alt_RotationLayout {
  pub roll: f32,
  pub pitch: f32,
  pub yaw: f32,
}
extern "C" {
  pub fn alt_RotationLayout_CAPI_Free(ptr: *mut alt_RotationLayout);
}
pvdrz commented 2 years ago

Isn't this somewhat related to https://github.com/rust-lang/rust-bindgen/issues/2227 ?