Skip to content

Tagged unions inside template fail to build. #2157

Open
@emilio

Description

@emilio

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions