rust-lang / rust-lang/rust-bindgen
`--no-derive-copy` causes all union bindings to use `__BindgenUnionField` even when a native Rust union would be safe
Nobody has claimed this yet.
- Dominant language
- Rust
- Stars
- 5.3k
- Forks
- 829
- Avg merge
- 1d 1h
- Merged PRs (30d)
- 15
Description
Input C/C++ Header
// bindgen-flags: --no-derive-copy
/// <div rustbindgen derive="Copy" derive="Clone">
typedef union {
int mInt;
float mFloat;
} nsStyleUnion;
Bindgen Invocation
$ bindgen tests/headers/union_fields_primitives.hpp --no-derive-copy
Actual Results
/* automatically generated by rust-bindgen */
#[repr(C)]
pub struct __BindgenUnionField<T>(::std::marker::PhantomData<T>);
impl<T> __BindgenUnionField<T> {
#[inline]
pub fn new() -> Self {
__BindgenUnionField(::std::marker::PhantomData)
}
#[inline]
pub unsafe fn as_ref(&self) -> &T {
::std::mem::transmute(self)
}
#[inline]
pub unsafe fn as_mut(&mut self) -> &mut T {
::std::mem::transmute(self)
}
}
impl<T> ::std::default::Default for __BindgenUnionField<T> {
#[inline]
fn default() -> Self {
Self::new()
}
}
impl<T> ::std::clone::Clone for __BindgenUnionField<T> {
#[inline]
fn clone(&self) -> Self {
Self::new()
}
}
impl<T> ::std::marker::Copy for __BindgenUnionField<T> {}
impl<T> ::std::fmt::Debug for __BindgenUnionField<T> {
fn fmt(&self, fmt: &mut ::std::fmt::Formatter<'_>) -> ::std::fmt::Result {
fmt.write_str("__BindgenUnionField")
}
}
impl<T> ::std::hash::Hash for __BindgenUnionField<T> {
fn hash<H: ::std::hash::Hasher>(&self, _state: &mut H) {}
}
impl<T> ::std::cmp::PartialEq for __BindgenUnionField<T> {
fn eq(&self, _other: &__BindgenUnionField<T>) -> bool {
true
}
}
impl<T> ::std::cmp::Eq for __BindgenUnionField<T> {}
#[doc = " <div rustbindgen derive=\"Copy\" derive=\"Clone\">"]
#[repr(C)]
#[derive(Copy, Clone)]
pub struct nsStyleUnion {
pub mInt: __BindgenUnionField<::std::os::raw::c_int>,
pub mFloat: __BindgenUnionField<f32>,
pub bindgen_union_field: u32,
}
Expected Results
Bindgen should produce a union type since all the types involved are Copy. The bindings should look the same as what $ bindgen tests/headers/union_fields_primitives.hpp outputs (without --no-derive-copy), i.e.
/* automatically generated by rust-bindgen */
#[doc = " <div rustbindgen derive=\"Copy\" derive=\"Clone\">"]
#[repr(C)]
#[derive(Copy, Clone)]
pub union nsStyleUnion {
pub mInt: ::std::os::raw::c_int,
pub mFloat: f32,
_bindgen_union_align: u32,
}
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
Start by running the bindgen invocation against tests/headers/union_fields_primitives.hpp with and without --no-derive-copy, then compare the generated union bindings. Trace the union-generation entry point that decides between a native union and __BindgenUnionField. Done means primitive Copy fields still produce the native union shown in Expected Results when --no-derive-copy is used.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- cpp, rust
- Domain
- tooling
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 42/100