diff --git a/bindgen-integration/cpp/Test.cc b/bindgen-integration/cpp/Test.cc index d9c13a7671..fa0ff827e3 100644 --- a/bindgen-integration/cpp/Test.cc +++ b/bindgen-integration/cpp/Test.cc @@ -1,5 +1,12 @@ #include "Test.h" +const int Test::COUNTDOWN[] = { 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0 }; +const int* Test::COUNTDOWN_PTR = Test::COUNTDOWN; + +const int* Test::countdown() { + return COUNTDOWN; +} + const char* Test::name() { return "Test"; } diff --git a/bindgen-integration/cpp/Test.h b/bindgen-integration/cpp/Test.h index f1e38c40f6..db90f4d7b9 100644 --- a/bindgen-integration/cpp/Test.h +++ b/bindgen-integration/cpp/Test.h @@ -7,6 +7,10 @@ class Test { static const char* name(); Test(int foo); Test(double foo); + + static const int COUNTDOWN[]; + static const int* COUNTDOWN_PTR; + static const int* countdown(); }; namespace testing { diff --git a/bindgen-integration/src/lib.rs b/bindgen-integration/src/lib.rs index 5c4b43083f..c64589a8bc 100644 --- a/bindgen-integration/src/lib.rs +++ b/bindgen-integration/src/lib.rs @@ -3,6 +3,29 @@ mod bindings { } use std::ffi::CStr; +use std::os::raw::c_int; + +#[test] +fn test_static_array() { + let mut test = unsafe { bindings::Test_COUNTDOWN.as_ptr() }; + let expected = unsafe { bindings::Test_countdown()}; + let also_expected = unsafe { bindings::Test_COUNTDOWN_PTR }; + assert!(!test.is_null()); + assert_eq!(also_expected, expected); + assert_eq!(test, also_expected); + + let mut expected = 10; + unsafe { + loop { + assert_eq!(*test, expected); + if *test == 0 { + break; + } + test = test.offset(1); + expected -= 1; + } + } +} #[test] fn test_static_method() { diff --git a/src/codegen/mod.rs b/src/codegen/mod.rs index 0a6737bb66..313ca8b0d3 100644 --- a/src/codegen/mod.rs +++ b/src/codegen/mod.rs @@ -2385,31 +2385,48 @@ mod utils { #[inline] pub unsafe fn as_slice(&self, len: usize) -> &[T] { - ::std::slice::from_raw_parts(self.as_ptr(), len) + ::$prefix::slice::from_raw_parts(self.as_ptr(), len) } #[inline] pub unsafe fn as_mut_slice(&mut self, len: usize) -> &mut [T] { - ::std::slice::from_raw_parts_mut(self.as_mut_ptr(), len) + ::$prefix::slice::from_raw_parts_mut(self.as_mut_ptr(), len) } } ) .unwrap(); let incomplete_array_debug_impl = quote_item!(ctx.ext_cx(), - impl ::std::fmt::Debug for __IncompleteArrayField { - fn fmt(&self, fmt: &mut ::std::fmt::Formatter) - -> ::std::fmt::Result { + impl ::$prefix::fmt::Debug for __IncompleteArrayField { + fn fmt(&self, fmt: &mut ::$prefix::fmt::Formatter) + -> ::$prefix::fmt::Result { fmt.write_str("__IncompleteArrayField") } } ) .unwrap(); + let incomplete_array_clone_impl = quote_item!(&ctx.ext_cx(), + impl ::$prefix::clone::Clone for __IncompleteArrayField { + #[inline] + fn clone(&self) -> Self { + Self::new() + } + } + ) + .unwrap(); + + let incomplete_array_copy_impl = quote_item!(&ctx.ext_cx(), + impl ::$prefix::marker::Copy for __IncompleteArrayField {} + ) + .unwrap(); + let items = vec![ incomplete_array_decl, incomplete_array_impl, incomplete_array_debug_impl, + incomplete_array_clone_impl, + incomplete_array_copy_impl, ]; let old_items = mem::replace(result, items); diff --git a/tests/expectations/tests/class.rs b/tests/expectations/tests/class.rs index 29d1e90480..e55a9fe357 100644 --- a/tests/expectations/tests/class.rs +++ b/tests/expectations/tests/class.rs @@ -31,6 +31,11 @@ impl ::std::fmt::Debug for __IncompleteArrayField { fmt.write_str("__IncompleteArrayField") } } +impl ::std::clone::Clone for __IncompleteArrayField { + #[inline] + fn clone(&self) -> Self { Self::new() } +} +impl ::std::marker::Copy for __IncompleteArrayField { } #[repr(C)] pub struct __BindgenUnionField(::std::marker::PhantomData); impl __BindgenUnionField { @@ -112,6 +117,16 @@ fn bindgen_test_layout_WithDtor() { assert_eq!(::std::mem::align_of::() , 4usize); } #[repr(C)] +pub struct IncompleteArrayNonCopiable { + pub whatever: *mut ::std::os::raw::c_void, + pub incomplete_array: __IncompleteArrayField, +} +#[test] +fn bindgen_test_layout_IncompleteArrayNonCopiable() { + assert_eq!(::std::mem::size_of::() , 8usize); + assert_eq!(::std::mem::align_of::() , 8usize); +} +#[repr(C)] #[derive(Debug, Copy)] pub struct Union { pub d: __BindgenUnionField, diff --git a/tests/headers/class.hpp b/tests/headers/class.hpp index 67ecb37b24..1de164710d 100644 --- a/tests/headers/class.hpp +++ b/tests/headers/class.hpp @@ -32,6 +32,11 @@ class WithDtor { ~WithDtor() {} }; +class IncompleteArrayNonCopiable { + void* whatever; + C incomplete_array[]; +}; + union Union { float d; int i;