rust-lang / rust-bindgen

Automatically generates Rust FFI bindings to C (and some C++) libraries.
https://rust-lang.github.io/rust-bindgen/
BSD 3-Clause "New" or "Revised" License
4.23k stars 679 forks source link

Filter out lookup_can_derive_copy for template type #2764

Open ileixe opened 4 months ago

ileixe commented 4 months ago

Template type in tagged union should not be copiable field as it requires ManuallyDrop. Filter out copy derive when the variable is resolved to template type.

Fix: #2157

Now the input.hpp in the issue will generate non rust union or correct ManuallyDrop for generic type.

rust-bindgen$ target/debug/bindgen input.hpp
/* automatically generated by rust-bindgen 0.69.4 */

#[repr(C)]
pub struct __BindgenUnionField<T>(::std::marker::PhantomData<T>);
impl<T> __BindgenUnionField<T> {
    #[inline]
    pub const 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
    }
}
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> {}
#[repr(C)]
#[derive(Debug, Copy, Clone)]
pub struct _Vector_base {
    pub _address: u8,
}
#[repr(C)]
pub struct _Vector_base__Storage<_Tp> {
    pub _M_byte: __BindgenUnionField<::std::os::raw::c_uchar>,
    pub _M_val: __BindgenUnionField<_Tp>,
    pub bindgen_union_field: u8,
    pub _phantom_0: ::std::marker::PhantomData<::std::cell::UnsafeCell<_Tp>>,
}
rust-bindgen$ target/debug/bindgen input.hpp --default-non-copy-union-style manually_drop
/* automatically generated by rust-bindgen 0.69.4 */

#[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::mem::ManuallyDrop<::std::os::raw::c_uchar>,
    pub _M_val: ::std::mem::ManuallyDrop<_Tp>,
    pub _phantom_0: ::std::marker::PhantomData<::std::cell::UnsafeCell<_Tp>>,
}
rustbot commented 4 months ago

Error: The feature assign is not enabled in this repository. To enable it add its section in the triagebot.toml in the root of the repository.

Please file an issue on GitHub at triagebot if there's a problem with this bot, or reach out on #t-infra on Zulip.