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

bindgen incorrectly generates `*mut` instead of `*const` from C++ header, when the parameter type is from another namespace

Open lulitao1997 opened this issue 1 year ago • 2 comments

Input C/C++ Header

namespace root {

namespace ns1 {
struct A{};
}

namespace ns2 {
using ns1::A;
// using A = ns1::A; swtich to this line generates the correct result
struct B{
        void f(const A& a);
};
}


}

Bindgen Invocation

bindgen gen.h -- -xc++

Actual Results

/* automatically generated by rust-bindgen 0.71.1 */

#[repr(C)]
#[derive(Debug, Copy, Clone)]
pub struct root_ns1_A {
    pub _address: u8,
}
#[allow(clippy::unnecessary_operation, clippy::identity_op)]
const _: () = {
    ["Size of root_ns1_A"][::std::mem::size_of::<root_ns1_A>() - 1usize];
    ["Alignment of root_ns1_A"][::std::mem::align_of::<root_ns1_A>() - 1usize];
};
#[repr(C)]
#[derive(Debug, Copy, Clone)]
pub struct root_ns2_B {
    pub _address: u8,
}
#[allow(clippy::unnecessary_operation, clippy::identity_op)]
const _: () = {
    ["Size of root_ns2_B"][::std::mem::size_of::<root_ns2_B>() - 1usize];
    ["Alignment of root_ns2_B"][::std::mem::align_of::<root_ns2_B>() - 1usize];
};
unsafe extern "C" {
    #[link_name = "\u{1}_ZN4root3ns21B1fERKNS_3ns11AE"]
    pub fn root_ns2_B_f(this: *mut root_ns2_B, a: *mut root_ns1_A);
}
impl root_ns2_B {
    #[inline]
    pub unsafe fn f(&mut self, a: *mut root_ns1_A) {
        root_ns2_B_f(self, a)
    }
}

Expected Results

should be

unsafe extern "C" {
    #[link_name = "\u{1}_ZN4root3ns21B1fERKNS_3ns11AE"]
    pub fn root_ns2_B_f(this: *mut root_ns2_B, a: *const root_ns2_A);
}

instead of

unsafe extern "C" {
    #[link_name = "\u{1}_ZN4root3ns21B1fERKNS_3ns11AE"]
    pub fn root_ns2_B_f(this: *mut root_ns2_B, a: *mut root_ns1_A);
}

note that if i replace using ns1::A with using A = ns1::A, it will produce correct result

lulitao1997 avatar Dec 22 '24 05:12 lulitao1997

$ clang --version
clang version 18.1.8
Target: x86_64-pc-linux-gnu
Thread model: posix
InstalledDir: /usr/bin

lulitao1997 avatar Dec 22 '24 05:12 lulitao1997

Fairly sure this is https://github.com/rust-lang/rust-bindgen/pull/2372 basically, which unfortunately also needs LLVM changes and I haven't found the time to push forward.

emilio avatar Feb 01 '25 20:02 emilio