rust-lang / rust-lang/rust-bindgen

Tagged unions inside template fail to build.

Open
#2,157 8 comments 4 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

This is sort of a rustc limitation, but we could try to detect it better.

Input C/C++ Header
template <typename _Tp, typename _Alloc>
struct _Vector_base {
  union _Storage {
    constexpr _Storage() : _M_byte() {}
    ~_Storage() {}
    _Storage& operator=(const _Storage&) = delete;
    unsigned char _M_byte;
    _Tp _M_val;
  };
};
Bindgen Invocation
$ bindgen input.hpp | rustc -
Actual Results
#[repr(C)]
#[derive(Debug, Copy, Clone)]
pub struct _Vector_base {
    pub _address: u8,
}
#[repr(C)]
pub union _Vector_base__Storage<_Tp> {
    pub _M_byte: ::std::os::raw::c_uchar,
    pub _M_val: _Tp,
    pub _phantom_0: ::std::marker::PhantomData<::std::cell::UnsafeCell<_Tp>>,
}

and/or

error[E0740]: unions may not contain fields that need dropping
  --> <anon>:11:5
   |
11 |     pub _M_val: _Tp,
   |     ^^^^^^^^^^^^^^^
   |
help: wrap the type with `std::mem::ManuallyDrop` and ensure it is manually dropped
   |
11 |     pub _M_val: std::mem::ManuallyDrop<_Tp>,
   |                 +++++++++++++++++++++++   +

error: aborting due to 2 previous errors
Expected Results

We should probably generate non-rust unions in this case, or add ManuallyDrop around all the types in a union or something like that.

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.hpp template and the generated _Vector_base__Storage union, then reproduce the failure with bindgen input.hpp | rustc -. Determine how bindgen handles template unions containing potentially droppable fields; done means the reported input is detected or generated bindings compile without the union error.

Written by the indexing model from the issue text.

Assessment

Tech stack
cpp, rust
Domain
compilers, devtools
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
30/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.