Skip to content

Commit fedca48

Browse files
committed
Force copy for incomplete arrays.
These aren't extremely great, since this usually requires extra bookkeeping. But C allows it, so let's keep the same semantics.
1 parent be02472 commit fedca48

File tree

3 files changed

+42
-5
lines changed

3 files changed

+42
-5
lines changed

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)