rust-lang / rust-lang/rust-bindgen
Bindgen sometimes drops the type parameter of a struct for fields with multiple type parameters
Nobody has claimed this yet.
- Dominant language
- Rust
- Stars
- 5.3k
- Forks
- 829
- Avg merge
- 1d 1h
- Merged PRs (30d)
- 15
Description
Header File
Here is an example header, though there are a lot of others which exhibit similar behavior (briefly mentioned in the discussion).
template <typename T, typename K = bool> struct Inner {
T foo;
K bar;
};
template <typename T> struct Outer {
Inner<T> inner;
};
Expected Result
Something along the lines of:
/* automatically generated by rust-bindgen 0.71.1 */
#[repr(C)]
#[derive(Debug, Copy, Clone)]
pub struct Inner<T, K = bool> {
pub _phantom_0: ::std::marker::PhantomData<::std::cell::UnsafeCell<T>>,
pub _phantom_1: ::std::marker::PhantomData<::std::cell::UnsafeCell<K>>,
pub foo: T,
pub bar: K,
}
#[repr(C)]
#[derive(Debug, Copy, Clone)]
pub struct Outer<T> {
pub _phantom_0: ::std::marker::PhantomData<::std::cell::UnsafeCell<T>>,
pub inner: Inner<T>,
}
Actual Translation
When running bindgen lib.hpp -o bindings.rs (with lib.hpp containing the cpp code above), bindgen writes the following to bindings.rs:
/* automatically generated by rust-bindgen 0.71.1 */
#[repr(C)]
#[derive(Debug, Copy, Clone)]
pub struct Inner<T, K> {
pub _phantom_0: ::std::marker::PhantomData<::std::cell::UnsafeCell<T>>,
pub _phantom_1: ::std::marker::PhantomData<::std::cell::UnsafeCell<K>>,
pub foo: T,
pub bar: K,
}
#[repr(C)]
#[derive(Debug, Copy, Clone)]
pub struct Outer {
pub inner: Inner<T, K>,
}
The log is attached here.
Discussion
The problem seems to be in the translating Outer's field. Removing the default type of K (the = bool) and instead instantiating it in the field as Inner<T, bool> inner exhibits the same problem.
Interestingly, changing the field to be Inner<T, T> inner lets the Rust generate fine, creating
// Definition of Inner omitted for brevity
#[repr(C)]
#[derive(Debug, Copy, Clone)]
pub struct Outer<T> {
pub _phantom_0: ::std::marker::PhantomData<::std::cell::UnsafeCell<T>>,
pub inner: Inner<T, T>,
}
This leads me to believe the problem isn't to do with the default type parameter, but instead some more general problem when dealing with templates with multiple type parameters.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Reproduce the issue with the C++ declarations in lib.hpp and run bindgen lib.hpp -o bindings.rs. Compare the generated Outer and Inner definitions with the expected translation, then verify that fields using multiple template parameters preserve the correct parameters and defaults.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- cpp, rust
- Domain
- tooling
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100