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 very inconsistent with generated Rust types for opaque C++ types #652

jsgf opened this issue Apr 21, 2017 · 2 comments


Copy link

jsgf commented Apr 21, 2017

Input C/C++ Header

struct simple {
 int a, b, c;

template<class ty>
struct templated {
 ty a, b, c;

typedef templated<int> int_tmpl;

Bindgen Invokation

$ bindgen input.h --no-layout-tests --opaque-type '.*' -- -x c++

Actual Results

#[derive(Debug, Copy)]
pub struct simple {
    pub _bindgen_opaque_blob: [u32; 3usize],
impl Clone for simple {
    fn clone(&self) -> Self { *self }
#[derive(Debug, Copy, Clone)]
pub struct templated {
pub type int_tmpl = u8;

Expected Results

The type for simple is fine.

The type for templated is also OK, but this would be better:

pub struct templated<T> {
  _bindgen_opaque_blob: [size], // ideally
  _marker: PhantomData<T>,

The type for int_tmpl is bad - it should be one of:

  • type int_tmpl = templated<i32>
  • struct int_tmpl { _bindgen_opaque_blob: templated<i32>, }
  • or simply struct int_tmpl;

As plain u8, it's not possible to implement methods or traits on it, and it's awkward to use for any function signatures.

It will also generate type std_string = [u64; 3]; for std::string, but I haven't got a simple repro for that yet. It should generate struct std_string { _blob: [u64; 3], }.

Copy link

emilio commented Apr 22, 2017

So this is in particular about typedefs of template specializations, given std::string is a typedef to std::basic_string<char>.

We handle typedefs of types we don't know the layout of replacing them with a blob if we happen to know the size of the specialization (I'm actually surprised that templated<int> generates a u8, that's definitely a bug).

Perhaps we should make those generate at least a struct wrapping the blob, that seems doable.

@emilio emilio added the bug label Apr 22, 2017
Copy link
Contributor Author

jsgf commented Apr 22, 2017

Yeah, there's something more complex about std::string though that these examples don't capture. Maybe related to inheritance? I need to set up creduce again.

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

No branches or pull requests

2 participants