Skip to content

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

@cbourjau

Description

@cbourjau

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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions