summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorbors-servo <lbergstrom+bors@mozilla.com>2017-01-29 15:18:13 -0800
committerGitHub <noreply@github.com>2017-01-29 15:18:13 -0800
commit3ef1a277c01e5c146cfdeb112c01c6261555c8b9 (patch)
treee88b1005701d46c0271435d79e5e00d0c753733d
parent3660d4d443b2c4a6620376899786b4d75aebf3a2 (diff)
parentfedca4883881b3d589df06a8808fef22ce205808 (diff)
Auto merge of #457 - emilio:test-array, r=fitzgen
tests: Add an integration test for static arrays. Turns out they were broken before https://github.com/servo/rust-bindgen/issues/456. Let's test it so it doesn't regress. r? @fitzgen
-rw-r--r--bindgen-integration/cpp/Test.cc7
-rw-r--r--bindgen-integration/cpp/Test.h4
-rw-r--r--bindgen-integration/src/lib.rs23
-rw-r--r--src/codegen/mod.rs27
-rw-r--r--tests/expectations/tests/class.rs15
-rw-r--r--tests/headers/class.hpp5
6 files changed, 76 insertions, 5 deletions
diff --git a/bindgen-integration/cpp/Test.cc b/bindgen-integration/cpp/Test.cc
index d9c13a76..fa0ff827 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 f1e38c40..db90f4d7 100644
--- a/bindgen-integration/cpp/Test.h
+++ b/bindgen-integration/cpp/Test.h
@@ -7,6 +7,10 @@ public:
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 5c4b4308..c64589a8 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 0a6737bb..313ca8b0 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<T> ::std::fmt::Debug for __IncompleteArrayField<T> {
- fn fmt(&self, fmt: &mut ::std::fmt::Formatter)
- -> ::std::fmt::Result {
+ impl<T> ::$prefix::fmt::Debug for __IncompleteArrayField<T> {
+ 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<T> ::$prefix::clone::Clone for __IncompleteArrayField<T> {
+ #[inline]
+ fn clone(&self) -> Self {
+ Self::new()
+ }
+ }
+ )
+ .unwrap();
+
+ let incomplete_array_copy_impl = quote_item!(&ctx.ext_cx(),
+ impl<T> ::$prefix::marker::Copy for __IncompleteArrayField<T> {}
+ )
+ .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 29d1e904..e55a9fe3 100644
--- a/tests/expectations/tests/class.rs
+++ b/tests/expectations/tests/class.rs
@@ -31,6 +31,11 @@ impl <T> ::std::fmt::Debug for __IncompleteArrayField<T> {
fmt.write_str("__IncompleteArrayField")
}
}
+impl <T> ::std::clone::Clone for __IncompleteArrayField<T> {
+ #[inline]
+ fn clone(&self) -> Self { Self::new() }
+}
+impl <T> ::std::marker::Copy for __IncompleteArrayField<T> { }
#[repr(C)]
pub struct __BindgenUnionField<T>(::std::marker::PhantomData<T>);
impl <T> __BindgenUnionField<T> {
@@ -112,6 +117,16 @@ fn bindgen_test_layout_WithDtor() {
assert_eq!(::std::mem::align_of::<WithDtor>() , 4usize);
}
#[repr(C)]
+pub struct IncompleteArrayNonCopiable {
+ pub whatever: *mut ::std::os::raw::c_void,
+ pub incomplete_array: __IncompleteArrayField<C>,
+}
+#[test]
+fn bindgen_test_layout_IncompleteArrayNonCopiable() {
+ assert_eq!(::std::mem::size_of::<IncompleteArrayNonCopiable>() , 8usize);
+ assert_eq!(::std::mem::align_of::<IncompleteArrayNonCopiable>() , 8usize);
+}
+#[repr(C)]
#[derive(Debug, Copy)]
pub struct Union {
pub d: __BindgenUnionField<f32>,
diff --git a/tests/headers/class.hpp b/tests/headers/class.hpp
index 67ecb37b..1de16471 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;