Skip to content

Commit 3ef1a27

Browse files
author
bors-servo
authored
Auto merge of rust-lang#457 - emilio:test-array, r=fitzgen
tests: Add an integration test for static arrays. Turns out they were broken before rust-lang#456. Let's test it so it doesn't regress. r? @fitzgen
2 parents 3660d4d + fedca48 commit 3ef1a27

File tree

6 files changed

+76
-5
lines changed

6 files changed

+76
-5
lines changed

bindgen-integration/cpp/Test.cc

+7
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
11
#include "Test.h"
22

3+
const int Test::COUNTDOWN[] = { 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0 };
4+
const int* Test::COUNTDOWN_PTR = Test::COUNTDOWN;
5+
6+
const int* Test::countdown() {
7+
return COUNTDOWN;
8+
}
9+
310
const char* Test::name() {
411
return "Test";
512
}

bindgen-integration/cpp/Test.h

+4
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ class Test {
77
static const char* name();
88
Test(int foo);
99
Test(double foo);
10+
11+
static const int COUNTDOWN[];
12+
static const int* COUNTDOWN_PTR;
13+
static const int* countdown();
1014
};
1115

1216
namespace testing {

bindgen-integration/src/lib.rs

+23
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,29 @@ mod bindings {
33
}
44

55
use std::ffi::CStr;
6+
use std::os::raw::c_int;
7+
8+
#[test]
9+
fn test_static_array() {
10+
let mut test = unsafe { bindings::Test_COUNTDOWN.as_ptr() };
11+
let expected = unsafe { bindings::Test_countdown()};
12+
let also_expected = unsafe { bindings::Test_COUNTDOWN_PTR };
13+
assert!(!test.is_null());
14+
assert_eq!(also_expected, expected);
15+
assert_eq!(test, also_expected);
16+
17+
let mut expected = 10;
18+
unsafe {
19+
loop {
20+
assert_eq!(*test, expected);
21+
if *test == 0 {
22+
break;
23+
}
24+
test = test.offset(1);
25+
expected -= 1;
26+
}
27+
}
28+
}
629

730
#[test]
831
fn test_static_method() {

src/codegen/mod.rs

+22-5
Original file line numberDiff line numberDiff line change
@@ -2385,31 +2385,48 @@ mod utils {
23852385

23862386
#[inline]
23872387
pub unsafe fn as_slice(&self, len: usize) -> &[T] {
2388-
::std::slice::from_raw_parts(self.as_ptr(), len)
2388+
::$prefix::slice::from_raw_parts(self.as_ptr(), len)
23892389
}
23902390

23912391
#[inline]
23922392
pub unsafe fn as_mut_slice(&mut self, len: usize) -> &mut [T] {
2393-
::std::slice::from_raw_parts_mut(self.as_mut_ptr(), len)
2393+
::$prefix::slice::from_raw_parts_mut(self.as_mut_ptr(), len)
23942394
}
23952395
}
23962396
)
23972397
.unwrap();
23982398

23992399
let incomplete_array_debug_impl = quote_item!(ctx.ext_cx(),
2400-
impl<T> ::std::fmt::Debug for __IncompleteArrayField<T> {
2401-
fn fmt(&self, fmt: &mut ::std::fmt::Formatter)
2402-
-> ::std::fmt::Result {
2400+
impl<T> ::$prefix::fmt::Debug for __IncompleteArrayField<T> {
2401+
fn fmt(&self, fmt: &mut ::$prefix::fmt::Formatter)
2402+
-> ::$prefix::fmt::Result {
24032403
fmt.write_str("__IncompleteArrayField")
24042404
}
24052405
}
24062406
)
24072407
.unwrap();
24082408

2409+
let incomplete_array_clone_impl = quote_item!(&ctx.ext_cx(),
2410+
impl<T> ::$prefix::clone::Clone for __IncompleteArrayField<T> {
2411+
#[inline]
2412+
fn clone(&self) -> Self {
2413+
Self::new()
2414+
}
2415+
}
2416+
)
2417+
.unwrap();
2418+
2419+
let incomplete_array_copy_impl = quote_item!(&ctx.ext_cx(),
2420+
impl<T> ::$prefix::marker::Copy for __IncompleteArrayField<T> {}
2421+
)
2422+
.unwrap();
2423+
24092424
let items = vec![
24102425
incomplete_array_decl,
24112426
incomplete_array_impl,
24122427
incomplete_array_debug_impl,
2428+
incomplete_array_clone_impl,
2429+
incomplete_array_copy_impl,
24132430
];
24142431

24152432
let old_items = mem::replace(result, items);

tests/expectations/tests/class.rs

+15
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,11 @@ impl <T> ::std::fmt::Debug for __IncompleteArrayField<T> {
3131
fmt.write_str("__IncompleteArrayField")
3232
}
3333
}
34+
impl <T> ::std::clone::Clone for __IncompleteArrayField<T> {
35+
#[inline]
36+
fn clone(&self) -> Self { Self::new() }
37+
}
38+
impl <T> ::std::marker::Copy for __IncompleteArrayField<T> { }
3439
#[repr(C)]
3540
pub struct __BindgenUnionField<T>(::std::marker::PhantomData<T>);
3641
impl <T> __BindgenUnionField<T> {
@@ -112,6 +117,16 @@ fn bindgen_test_layout_WithDtor() {
112117
assert_eq!(::std::mem::align_of::<WithDtor>() , 4usize);
113118
}
114119
#[repr(C)]
120+
pub struct IncompleteArrayNonCopiable {
121+
pub whatever: *mut ::std::os::raw::c_void,
122+
pub incomplete_array: __IncompleteArrayField<C>,
123+
}
124+
#[test]
125+
fn bindgen_test_layout_IncompleteArrayNonCopiable() {
126+
assert_eq!(::std::mem::size_of::<IncompleteArrayNonCopiable>() , 8usize);
127+
assert_eq!(::std::mem::align_of::<IncompleteArrayNonCopiable>() , 8usize);
128+
}
129+
#[repr(C)]
115130
#[derive(Debug, Copy)]
116131
pub struct Union {
117132
pub d: __BindgenUnionField<f32>,

tests/headers/class.hpp

+5
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,11 @@ class WithDtor {
3232
~WithDtor() {}
3333
};
3434

35+
class IncompleteArrayNonCopiable {
36+
void* whatever;
37+
C incomplete_array[];
38+
};
39+
3540
union Union {
3641
float d;
3742
int i;

0 commit comments

Comments
 (0)