Skip to content

Commit f64606f

Browse files
author
bors-servo
authored
Auto merge of #33 - emilio:union-dtor, r=Manishearth
Unions with destructors. r? @Manishearth
2 parents eab08be + ed92708 commit f64606f

File tree

3 files changed

+60
-4
lines changed

3 files changed

+60
-4
lines changed

src/types.rs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -681,13 +681,17 @@ impl CompInfo {
681681
if self.no_copy {
682682
return false;
683683
}
684+
685+
// NOTE: Take into account that while unions in C and C++ are copied by
686+
// default, the may have an explicit destructor in C++, so we can't
687+
// defer this check just for the union case.
688+
if self.has_destructor() {
689+
return false;
690+
}
691+
684692
match self.kind {
685693
CompKind::Union => true,
686694
CompKind::Struct => {
687-
if self.has_destructor() {
688-
return false;
689-
}
690-
691695
// With template args, use a safe subset of the types,
692696
// since copyability depends on the types itself.
693697
self.ref_template.as_ref().map_or(true, |t| t.can_derive_copy()) &&

tests/expectations/union_dtor.rs

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/* automatically generated by rust-bindgen */
2+
3+
4+
#![allow(non_snake_case)]
5+
6+
7+
#[derive(Copy, Debug)]
8+
#[repr(C)]
9+
pub struct __BindgenUnionField<T>(::std::marker::PhantomData<T>);
10+
impl <T> __BindgenUnionField<T> {
11+
#[inline]
12+
pub fn new() -> Self { __BindgenUnionField(::std::marker::PhantomData) }
13+
#[inline]
14+
pub unsafe fn as_ref(&self) -> &T { ::std::mem::transmute(self) }
15+
#[inline]
16+
pub unsafe fn as_mut(&mut self) -> &mut T { ::std::mem::transmute(self) }
17+
}
18+
impl <T> ::std::default::Default for __BindgenUnionField<T> {
19+
#[inline]
20+
fn default() -> Self { Self::new() }
21+
}
22+
impl <T> ::std::clone::Clone for __BindgenUnionField<T> {
23+
#[inline]
24+
fn clone(&self) -> Self { Self::new() }
25+
}
26+
#[repr(C)]
27+
#[derive(Debug)]
28+
pub struct Union_UnionWithDtor {
29+
pub mFoo: __BindgenUnionField<::std::os::raw::c_int>,
30+
pub mBar: __BindgenUnionField<*mut ::std::os::raw::c_void>,
31+
pub _bindgen_data_: u64,
32+
}
33+
impl Union_UnionWithDtor {
34+
pub unsafe fn mFoo(&mut self) -> *mut ::std::os::raw::c_int {
35+
let raw: *mut u8 = ::std::mem::transmute(&self._bindgen_data_);
36+
::std::mem::transmute(raw.offset(0))
37+
}
38+
pub unsafe fn mBar(&mut self) -> *mut *mut ::std::os::raw::c_void {
39+
let raw: *mut u8 = ::std::mem::transmute(&self._bindgen_data_);
40+
::std::mem::transmute(raw.offset(0))
41+
}
42+
}
43+
#[test]
44+
fn bindgen_test_layout_Union_UnionWithDtor() {
45+
assert_eq!(::std::mem::size_of::<Union_UnionWithDtor>() , 8usize);
46+
assert_eq!(::std::mem::align_of::<Union_UnionWithDtor>() , 8usize);
47+
}

tests/headers/union_dtor.hpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
union UnionWithDtor {
2+
~UnionWithDtor();
3+
int mFoo;
4+
void* mBar;
5+
};

0 commit comments

Comments
 (0)