rust-lang / rust-lang/rust-bindgen

Type confusion using templates

Open
#1,943 0 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

This issue is a minimized version of a bug I ran into trying to use std::vector on my system.

Input C/C++ Header
struct Foo {
    typedef void* pointer;
};

template<typename T>
struct Bar {
    typedef Foo foo;
};

template<typename T>
struct Abstract {
    typename Bar<T>::foo::pointer pointer;
};

struct Concrete {
    Abstract<int> v;
};
Bindgen Invocation
$ bindgen input.hpp -- -x c++
Actual Results

Generated structs:

#[repr(C)]
#[derive(Debug, Copy, Clone)]
pub struct Foo {
    pub _address: u8,
}
pub type Foo_pointer = *mut ::std::os::raw::c_void;
#[repr(C)]
#[derive(Debug, Copy, Clone)]
pub struct Bar {
    pub _address: u8,
}
pub type Bar_foo = Foo;
#[repr(C)]
#[derive(Debug, Copy, Clone)]
pub struct Abstract {
    pub pointer: Bar,
}
#[repr(C)]
#[derive(Debug, Copy, Clone)]
pub struct Concrete {
    pub v: Abstract,
}

Layout tests fail:

running 3 tests
test __bindgen_test_layout_Abstract_open0_int_close0_instantiation ... FAILED
test bindgen_test_layout_Foo ... ok
test bindgen_test_layout_Concrete ... FAILED

failures:

---- __bindgen_test_layout_Abstract_open0_int_close0_instantiation stdout ----
thread '__bindgen_test_layout_Abstract_open0_int_close0_instantiation' panicked at 'assertion failed: `(left == right)`
  left: `1`,
 right: `8`: Size of template specialization: Abstract', src/bindings.rs:63:5
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace

---- bindgen_test_layout_Concrete stdout ----
thread 'bindgen_test_layout_Concrete' panicked at 'assertion failed: `(left == right)`
  left: `1`,
 right: `8`: Size of: Concrete', src/bindings.rs:40:5


failures:
    __bindgen_test_layout_Abstract_open0_int_close0_instantiation
    bindgen_test_layout_Concrete

Expected Results

Abstract::pointer should have type Foo_pointer instead of type Bar. That should make the layout correct, and cause the tests to pass.

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 the shown input.hpp and bindgen input.hpp -- -x c++, then inspect the generated Abstract binding and the layout tests reported in src/bindings.rs. The work is done when Abstract::pointer uses Foo_pointer rather than Bar and the Abstract and Concrete layout tests pass.

Written by the indexing model from the issue text.

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.