rust-lang / rust-lang/rust-bindgen

bindgen very inconsistent with generated Rust types for opaque C++ types

Open
#652 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug
Dominant language
Rust
Stars
5.3k
Forks
829
Avg merge
1d 1h
Merged PRs (30d)
15

Description

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
#[repr(C)]
#[derive(Debug, Copy)]
pub struct simple {
    pub _bindgen_opaque_blob: [u32; 3usize],
}
impl Clone for simple {
    fn clone(&self) -> Self { *self }
}
#[repr(C)]
#[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:

#[repr(C)]
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], }.

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 input.h reproduction and run the reported bindgen command using --no-layout-tests and --opaque-type '.*' with C++ parsing enabled. Compare the generated Rust for simple, templated, int_tmpl, and std::string with the expected opaque representations. Done means opaque templates and typedefs retain usable types rather than being reduced to primitive aliases.

Written by the indexing model from the issue text.

Assessment

Tech stack
cpp, rust
Domain
tooling
Issue type
Bug
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.