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

Open
#1,569 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug help wanted
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

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 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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.