rust-lang / rust-lang/rust-bindgen

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

Open
#3,059 2 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

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

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

Reproduce the issue with gen.h and the bindgen gen.h -- -xc++ invocation, comparing the generated function signature for using ns1::A with the expected const pointer. Done means the using-declaration case emits the correct const pointer while the using-alias case remains correct.

Written by the indexing model from the issue text.

Assessment

Tech stack
cpp, rust
Domain
compilers, tooling
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.