rust-lang / rust-lang/rust-bindgen

C++ template with nested declaration and out-of-line definition

Open
#798 10 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

A-spe bug I-bogus-codegen
Dominant language
Rust
Stars
5.3k
Forks
829
Avg merge
1d 1h
Merged PRs (30d)
15

Description

Using the magic of creduce I found an issue on nested templates which apparently lurk somewhere in a large library that I try to wrap. I think this is a bug, or am I doing something wrong?

Input C/C++ Header
template <typename, typename = int> class A {
  class B;
};

template <typename _CharT, typename _Traits> class A<_CharT, _Traits>::B {
  _Traits traits_type;
};

class C {
protected:
  void m_fn1(A<char>);
};

class Test {
  C fChain;
};
Bindgen Invocation
    let bindings = bindgen::Builder::default()
        .clang_arg("-x")
        .clang_arg("c++")
        .clang_arg("-std=c++11")
        .whitelisted_type("Test")
        .generate_comments(false)
        .header("src/test.hpp")
        .generate()
        .expect("Unable to generate bindings");
Actual Results

The generated rust code uses undefined types:

/* automatically generated by rust-bindgen */

#[repr(C)]
#[derive(Debug, Copy, Clone)]
pub struct A {
    pub _address: u8,
}
#[repr(C)]
#[derive(Debug, Copy, Clone)]
pub struct A_B {
    pub traits_type: _Traits,
}
#[repr(C)]
#[derive(Debug, Copy)]
pub struct C {
    pub _address: u8,
}
#[test]
fn bindgen_test_layout_C() {
    assert_eq!(::std::mem::size_of::<C>() , 1usize , concat ! (
               "Size of: " , stringify ! ( C ) ));
    assert_eq! (::std::mem::align_of::<C>() , 1usize , concat ! (
                "Alignment of " , stringify ! ( C ) ));
}
extern "C" {
    #[link_name = "_ZN1C5m_fn1E1AIciE"]
    pub fn C_m_fn1(this: *mut C, arg1: A);
}
impl Clone for C {
    fn clone(&self) -> Self { *self }
}
impl C {
    #[inline]
    pub unsafe fn m_fn1(&mut self, arg1: A) { C_m_fn1(self, arg1) }
}
#[repr(C)]
#[derive(Debug, Copy)]
pub struct Test {
    pub fChain: C,
}
#[test]
fn bindgen_test_layout_Test() {
    assert_eq!(::std::mem::size_of::<Test>() , 1usize , concat ! (
               "Size of: " , stringify ! ( Test ) ));
    assert_eq! (::std::mem::align_of::<Test>() , 1usize , concat ! (
                "Alignment of " , stringify ! ( Test ) ));
    assert_eq! (unsafe {
                & ( * ( 0 as * const Test ) ) . fChain as * const _ as usize }
                , 0usize , concat ! (
                "Alignment of field: " , stringify ! ( Test ) , "::" ,
                stringify ! ( fChain ) ));
}
impl Clone for Test {
    fn clone(&self) -> Self { *self }
}

which yields the following error when compiled

cargo build
   Compiling alice-sys v0.1.0 (file:///home/christian/repos/rust/bindgen_mvnp)
error[E0412]: cannot find type `_Traits` in this scope
  --> /home/christian/repos/rust/bindgen_mvnp/target/debug/build/alice-sys-350880093f241c3d/out/bindings.rs:11:22
   |
11 |     pub traits_type: _Traits,
   |                      ^^^^^^^ not found in this scope

error[E0412]: cannot find type `_Traits` in this scope
  --> /home/christian/repos/rust/bindgen_mvnp/target/debug/build/alice-sys-350880093f241c3d/out/bindings.rs:11:22
   |
11 |     pub traits_type: _Traits,
   |                      ^^^^^^^ not found in this scope

error[E0204]: the trait `Copy` may not be implemented for this type
  --> /home/christian/repos/rust/bindgen_mvnp/target/debug/build/alice-sys-350880093f241c3d/out/bindings.rs:9:17
   |
9  | #[derive(Debug, Copy, Clone)]
   |                 ^^^^
10 | pub struct A_B {
11 |     pub traits_type: _Traits,
   |     ------------------------ this field does not implement `Copy`

error: aborting due to 3 previous errors

Expected Results

I would expect the _Traits type to be defined in the rust code.

RUST_LOG=bindgen Output

RUST_LOG=bindgen did not yield additional output

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 reduced C++ header in src/test.hpp and the Rust bindgen invocation, then inspect how the nested template A<...>::B is represented in the generated bindings. Reproduce the cargo build failure and verify that generated code handles the _Traits field without undefined types.

Written by the indexing model from the issue text.

Assessment

Tech stack
cpp, rust
Domain
devtools
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.