Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
lulitao1997 opened this issue Dec 22, 2024 · 1 comment

Comments

@lulitao1997
Copy link

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 lulitao1997 changed the title bindgen generates *mut instead of *const from C++ header, when the parameter type is from another namespace bindgen incorrectly generates *mut instead of *const from C++ header, when the parameter type is from another namespace Dec 22, 2024
@lulitao1997
Copy link
Author

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant