diff --git a/src/codegen/helpers.rs b/src/codegen/helpers.rs index 06dadab0c9..9e19637c50 100644 --- a/src/codegen/helpers.rs +++ b/src/codegen/helpers.rs @@ -89,7 +89,9 @@ pub mod ast_ty { let prefix = ctx.rust_ident_raw(prefix); quote_ty!(ctx.ext_cx(), $prefix::$ident) } - None => quote_ty!(ctx.ext_cx(), ::std::os::raw::$ident), + None => { + quote_ty!(ctx.ext_cx(), ::std::os::raw::$ident) + } } } diff --git a/src/codegen/mod.rs b/src/codegen/mod.rs index 5ee43173c8..d3e204ce91 100644 --- a/src/codegen/mod.rs +++ b/src/codegen/mod.rs @@ -1644,9 +1644,10 @@ impl MethodCodegen for Method { // If it's a constructor, we need to insert an extra parameter with a // variable called `__bindgen_tmp` we're going to create. if self.is_constructor() { + let prefix = ctx.trait_prefix(); let tmp_variable_decl = quote_stmt!(ctx.ext_cx(), - let mut __bindgen_tmp = ::std::mem::uninitialized()) + let mut __bindgen_tmp = ::$prefix::mem::uninitialized()) .unwrap(); stmts.push(tmp_variable_decl); exprs[0] = quote_expr!(ctx.ext_cx(), &mut __bindgen_tmp); @@ -2601,9 +2602,9 @@ mod utils { .unwrap(); let union_field_debug_impl = quote_item!(ctx.ext_cx(), - impl ::std::fmt::Debug for __BindgenUnionField { - fn fmt(&self, fmt: &mut ::std::fmt::Formatter) - -> ::std::fmt::Result { + impl ::$prefix::fmt::Debug for __BindgenUnionField { + fn fmt(&self, fmt: &mut ::$prefix::fmt::Formatter) + -> ::$prefix::fmt::Result { fmt.write_str("__BindgenUnionField") } } diff --git a/tests/expectations/tests/use-core.rs b/tests/expectations/tests/use-core.rs index dd13156f68..c2996f74d5 100644 --- a/tests/expectations/tests/use-core.rs +++ b/tests/expectations/tests/use-core.rs @@ -5,6 +5,30 @@ extern crate core; +#[repr(C)] +pub struct __BindgenUnionField(::core::marker::PhantomData); +impl __BindgenUnionField { + #[inline] + pub fn new() -> Self { __BindgenUnionField(::core::marker::PhantomData) } + #[inline] + pub unsafe fn as_ref(&self) -> &T { ::core::mem::transmute(self) } + #[inline] + pub unsafe fn as_mut(&mut self) -> &mut T { ::core::mem::transmute(self) } +} +impl ::core::default::Default for __BindgenUnionField { + #[inline] + fn default() -> Self { Self::new() } +} +impl ::core::clone::Clone for __BindgenUnionField { + #[inline] + fn clone(&self) -> Self { Self::new() } +} +impl ::core::marker::Copy for __BindgenUnionField { } +impl ::core::fmt::Debug for __BindgenUnionField { + fn fmt(&self, fmt: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { + fmt.write_str("__BindgenUnionField") + } +} #[repr(C)] #[derive(Debug, Copy)] pub struct foo { @@ -40,5 +64,36 @@ impl Clone for foo { impl Default for foo { fn default() -> Self { unsafe { ::core::mem::zeroed() } } } +#[repr(C)] +#[derive(Debug, Default, Copy)] +pub struct _bindgen_ty_1 { + pub bar: __BindgenUnionField<::std::os::raw::c_int>, + pub baz: __BindgenUnionField<::std::os::raw::c_long>, + pub bindgen_union_field: u64, +} +#[test] +fn bindgen_test_layout__bindgen_ty_1() { + assert_eq!(::core::mem::size_of::<_bindgen_ty_1>() , 8usize , concat ! ( + "Size of: " , stringify ! ( _bindgen_ty_1 ) )); + assert_eq! (::core::mem::align_of::<_bindgen_ty_1>() , 8usize , concat ! ( + "Alignment of " , stringify ! ( _bindgen_ty_1 ) )); + assert_eq! (unsafe { + & ( * ( 0 as * const _bindgen_ty_1 ) ) . bar as * const _ as + usize } , 0usize , concat ! ( + "Alignment of field: " , stringify ! ( _bindgen_ty_1 ) , "::" + , stringify ! ( bar ) )); + assert_eq! (unsafe { + & ( * ( 0 as * const _bindgen_ty_1 ) ) . baz as * const _ as + usize } , 0usize , concat ! ( + "Alignment of field: " , stringify ! ( _bindgen_ty_1 ) , "::" + , stringify ! ( baz ) )); +} +impl Clone for _bindgen_ty_1 { + fn clone(&self) -> Self { *self } +} +extern "C" { + #[link_name = "bazz"] + pub static mut bazz: _bindgen_ty_1; +} pub type fooFunction = ::core::option::Option; diff --git a/tests/headers/use-core.h b/tests/headers/use-core.h index 42bb10d9d1..0e8a8d6e5d 100644 --- a/tests/headers/use-core.h +++ b/tests/headers/use-core.h @@ -5,4 +5,9 @@ struct foo { void* bar; }; +union { + int bar; + long baz; +} bazz; + typedef void (*fooFunction)(int bar);