diff options
author | Emilio Cobos Álvarez <emilio@crisal.io> | 2018-12-28 16:59:55 +0100 |
---|---|---|
committer | Emilio Cobos Álvarez <emilio@crisal.io> | 2018-12-28 16:59:55 +0100 |
commit | c7bf20061f35efc5bbe3534d8287c3a2c762c247 (patch) | |
tree | 994b3190d504254afdc6b041a2a9581cbc093f3d | |
parent | fbb84204233dc6e17d7dc2260d19eeda0ef48b03 (diff) |
codegen: Make zero-sized arrays affect alignment.
Fixes #684
-rw-r--r-- | src/codegen/mod.rs | 11 | ||||
-rw-r--r-- | tests/expectations/tests/class.rs | 14 | ||||
-rw-r--r-- | tests/expectations/tests/class_1_0.rs | 14 | ||||
-rw-r--r-- | tests/expectations/tests/derive-hash-struct-with-incomplete-array.rs | 14 | ||||
-rw-r--r-- | tests/expectations/tests/issue-643-inner-struct.rs | 14 | ||||
-rw-r--r-- | tests/expectations/tests/layout_align.rs | 5 | ||||
-rw-r--r-- | tests/expectations/tests/layout_large_align_field.rs | 5 | ||||
-rw-r--r-- | tests/expectations/tests/zero-size-array-align.rs | 95 | ||||
-rw-r--r-- | tests/expectations/tests/zero-sized-array.rs | 5 | ||||
-rw-r--r-- | tests/headers/zero-size-array-align.h | 5 |
10 files changed, 141 insertions, 41 deletions
diff --git a/src/codegen/mod.rs b/src/codegen/mod.rs index 9a7bf897..f189fc69 100644 --- a/src/codegen/mod.rs +++ b/src/codegen/mod.rs @@ -3740,14 +3740,14 @@ mod utils { #[repr(C)] #[derive(Default)] pub struct __IncompleteArrayField<T>( - ::#prefix::marker::PhantomData<T>); + ::#prefix::marker::PhantomData<T>, [T; 0]); }; let incomplete_array_impl = quote! { impl<T> __IncompleteArrayField<T> { #[inline] pub fn new() -> Self { - __IncompleteArrayField(::#prefix::marker::PhantomData) + __IncompleteArrayField(::#prefix::marker::PhantomData, []) } #[inline] @@ -3790,15 +3790,10 @@ mod utils { } }; - let incomplete_array_copy_impl = quote! { - impl<T> ::#prefix::marker::Copy for __IncompleteArrayField<T> {} - }; - let items = vec![incomplete_array_decl, incomplete_array_impl, incomplete_array_debug_impl, - incomplete_array_clone_impl, - incomplete_array_copy_impl]; + incomplete_array_clone_impl]; let old_items = mem::replace(result, items); result.extend(old_items.into_iter()); diff --git a/tests/expectations/tests/class.rs b/tests/expectations/tests/class.rs index f7639aac..f52f0f85 100644 --- a/tests/expectations/tests/class.rs +++ b/tests/expectations/tests/class.rs @@ -1,16 +1,19 @@ /* automatically generated by rust-bindgen */ - -#![allow(dead_code, non_snake_case, non_camel_case_types, non_upper_case_globals)] - +#![allow( + dead_code, + non_snake_case, + non_camel_case_types, + non_upper_case_globals +)] #[repr(C)] #[derive(Default)] -pub struct __IncompleteArrayField<T>(::std::marker::PhantomData<T>); +pub struct __IncompleteArrayField<T>(::std::marker::PhantomData<T>, [T; 0]); impl<T> __IncompleteArrayField<T> { #[inline] pub fn new() -> Self { - __IncompleteArrayField(::std::marker::PhantomData) + __IncompleteArrayField(::std::marker::PhantomData, []) } #[inline] pub unsafe fn as_ptr(&self) -> *const T { @@ -40,7 +43,6 @@ impl<T> ::std::clone::Clone for __IncompleteArrayField<T> { Self::new() } } -impl<T> ::std::marker::Copy for __IncompleteArrayField<T> {} #[repr(C)] #[derive(Copy, Clone)] pub struct C { diff --git a/tests/expectations/tests/class_1_0.rs b/tests/expectations/tests/class_1_0.rs index a5639b25..b35e8814 100644 --- a/tests/expectations/tests/class_1_0.rs +++ b/tests/expectations/tests/class_1_0.rs @@ -1,16 +1,19 @@ /* automatically generated by rust-bindgen */ - -#![allow(dead_code, non_snake_case, non_camel_case_types, non_upper_case_globals)] - +#![allow( + dead_code, + non_snake_case, + non_camel_case_types, + non_upper_case_globals +)] #[repr(C)] #[derive(Default)] -pub struct __IncompleteArrayField<T>(::std::marker::PhantomData<T>); +pub struct __IncompleteArrayField<T>(::std::marker::PhantomData<T>, [T; 0]); impl<T> __IncompleteArrayField<T> { #[inline] pub fn new() -> Self { - __IncompleteArrayField(::std::marker::PhantomData) + __IncompleteArrayField(::std::marker::PhantomData, []) } #[inline] pub unsafe fn as_ptr(&self) -> *const T { @@ -40,7 +43,6 @@ impl<T> ::std::clone::Clone for __IncompleteArrayField<T> { Self::new() } } -impl<T> ::std::marker::Copy for __IncompleteArrayField<T> {} #[repr(C)] pub struct __BindgenUnionField<T>(::std::marker::PhantomData<T>); impl<T> __BindgenUnionField<T> { diff --git a/tests/expectations/tests/derive-hash-struct-with-incomplete-array.rs b/tests/expectations/tests/derive-hash-struct-with-incomplete-array.rs index e95c3391..8e87432f 100644 --- a/tests/expectations/tests/derive-hash-struct-with-incomplete-array.rs +++ b/tests/expectations/tests/derive-hash-struct-with-incomplete-array.rs @@ -1,16 +1,19 @@ /* automatically generated by rust-bindgen */ - -#![allow(dead_code, non_snake_case, non_camel_case_types, non_upper_case_globals)] - +#![allow( + dead_code, + non_snake_case, + non_camel_case_types, + non_upper_case_globals +)] #[repr(C)] #[derive(Default)] -pub struct __IncompleteArrayField<T>(::std::marker::PhantomData<T>); +pub struct __IncompleteArrayField<T>(::std::marker::PhantomData<T>, [T; 0]); impl<T> __IncompleteArrayField<T> { #[inline] pub fn new() -> Self { - __IncompleteArrayField(::std::marker::PhantomData) + __IncompleteArrayField(::std::marker::PhantomData, []) } #[inline] pub unsafe fn as_ptr(&self) -> *const T { @@ -40,7 +43,6 @@ impl<T> ::std::clone::Clone for __IncompleteArrayField<T> { Self::new() } } -impl<T> ::std::marker::Copy for __IncompleteArrayField<T> {} #[repr(C)] #[derive(Debug, Default)] pub struct test { diff --git a/tests/expectations/tests/issue-643-inner-struct.rs b/tests/expectations/tests/issue-643-inner-struct.rs index 83b52c22..a9696a6d 100644 --- a/tests/expectations/tests/issue-643-inner-struct.rs +++ b/tests/expectations/tests/issue-643-inner-struct.rs @@ -1,16 +1,19 @@ /* automatically generated by rust-bindgen */ - -#![allow(dead_code, non_snake_case, non_camel_case_types, non_upper_case_globals)] - +#![allow( + dead_code, + non_snake_case, + non_camel_case_types, + non_upper_case_globals +)] #[repr(C)] #[derive(Default)] -pub struct __IncompleteArrayField<T>(::std::marker::PhantomData<T>); +pub struct __IncompleteArrayField<T>(::std::marker::PhantomData<T>, [T; 0]); impl<T> __IncompleteArrayField<T> { #[inline] pub fn new() -> Self { - __IncompleteArrayField(::std::marker::PhantomData) + __IncompleteArrayField(::std::marker::PhantomData, []) } #[inline] pub unsafe fn as_ptr(&self) -> *const T { @@ -40,7 +43,6 @@ impl<T> ::std::clone::Clone for __IncompleteArrayField<T> { Self::new() } } -impl<T> ::std::marker::Copy for __IncompleteArrayField<T> {} #[repr(C)] #[derive(Debug)] pub struct rte_ring { diff --git a/tests/expectations/tests/layout_align.rs b/tests/expectations/tests/layout_align.rs index a1350b14..39fb2948 100644 --- a/tests/expectations/tests/layout_align.rs +++ b/tests/expectations/tests/layout_align.rs @@ -91,11 +91,11 @@ where } #[repr(C)] #[derive(Default)] -pub struct __IncompleteArrayField<T>(::std::marker::PhantomData<T>); +pub struct __IncompleteArrayField<T>(::std::marker::PhantomData<T>, [T; 0]); impl<T> __IncompleteArrayField<T> { #[inline] pub fn new() -> Self { - __IncompleteArrayField(::std::marker::PhantomData) + __IncompleteArrayField(::std::marker::PhantomData, []) } #[inline] pub unsafe fn as_ptr(&self) -> *const T { @@ -125,7 +125,6 @@ impl<T> ::std::clone::Clone for __IncompleteArrayField<T> { Self::new() } } -impl<T> ::std::marker::Copy for __IncompleteArrayField<T> {} #[repr(C)] #[derive(Debug)] pub struct rte_kni_fifo { diff --git a/tests/expectations/tests/layout_large_align_field.rs b/tests/expectations/tests/layout_large_align_field.rs index 5a865189..8b7be4ca 100644 --- a/tests/expectations/tests/layout_large_align_field.rs +++ b/tests/expectations/tests/layout_large_align_field.rs @@ -9,11 +9,11 @@ #[repr(C)] #[derive(Default)] -pub struct __IncompleteArrayField<T>(::std::marker::PhantomData<T>); +pub struct __IncompleteArrayField<T>(::std::marker::PhantomData<T>, [T; 0]); impl<T> __IncompleteArrayField<T> { #[inline] pub fn new() -> Self { - __IncompleteArrayField(::std::marker::PhantomData) + __IncompleteArrayField(::std::marker::PhantomData, []) } #[inline] pub unsafe fn as_ptr(&self) -> *const T { @@ -43,7 +43,6 @@ impl<T> ::std::clone::Clone for __IncompleteArrayField<T> { Self::new() } } -impl<T> ::std::marker::Copy for __IncompleteArrayField<T> {} pub const RTE_CACHE_LINE_SIZE: u32 = 64; pub const RTE_LIBRTE_IP_FRAG_MAX_FRAG: u32 = 4; pub const IP_LAST_FRAG_IDX: _bindgen_ty_1 = _bindgen_ty_1::IP_LAST_FRAG_IDX; diff --git a/tests/expectations/tests/zero-size-array-align.rs b/tests/expectations/tests/zero-size-array-align.rs new file mode 100644 index 00000000..9c60d14e --- /dev/null +++ b/tests/expectations/tests/zero-size-array-align.rs @@ -0,0 +1,95 @@ +/* automatically generated by rust-bindgen */ + +#![allow( + dead_code, + non_snake_case, + non_camel_case_types, + non_upper_case_globals +)] + +#[repr(C)] +#[derive(Default)] +pub struct __IncompleteArrayField<T>(::std::marker::PhantomData<T>, [T; 0]); +impl<T> __IncompleteArrayField<T> { + #[inline] + pub fn new() -> Self { + __IncompleteArrayField(::std::marker::PhantomData, []) + } + #[inline] + pub unsafe fn as_ptr(&self) -> *const T { + ::std::mem::transmute(self) + } + #[inline] + pub unsafe fn as_mut_ptr(&mut self) -> *mut T { + ::std::mem::transmute(self) + } + #[inline] + pub unsafe fn as_slice(&self, len: usize) -> &[T] { + ::std::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) + } +} +impl<T> ::std::fmt::Debug for __IncompleteArrayField<T> { + fn fmt(&self, fmt: &mut ::std::fmt::Formatter<'_>) -> ::std::fmt::Result { + fmt.write_str("__IncompleteArrayField") + } +} +impl<T> ::std::clone::Clone for __IncompleteArrayField<T> { + #[inline] + fn clone(&self) -> Self { + Self::new() + } +} +#[repr(C)] +#[derive(Debug, Default)] +pub struct dm_deps { + pub count: ::std::os::raw::c_uint, + pub filler: ::std::os::raw::c_uint, + pub device: __IncompleteArrayField<::std::os::raw::c_ulonglong>, +} +#[test] +fn bindgen_test_layout_dm_deps() { + assert_eq!( + ::std::mem::size_of::<dm_deps>(), + 8usize, + concat!("Size of: ", stringify!(dm_deps)) + ); + assert_eq!( + ::std::mem::align_of::<dm_deps>(), + 8usize, + concat!("Alignment of ", stringify!(dm_deps)) + ); + assert_eq!( + unsafe { &(*(::std::ptr::null::<dm_deps>())).count as *const _ as usize }, + 0usize, + concat!( + "Offset of field: ", + stringify!(dm_deps), + "::", + stringify!(count) + ) + ); + assert_eq!( + unsafe { &(*(::std::ptr::null::<dm_deps>())).filler as *const _ as usize }, + 4usize, + concat!( + "Offset of field: ", + stringify!(dm_deps), + "::", + stringify!(filler) + ) + ); + assert_eq!( + unsafe { &(*(::std::ptr::null::<dm_deps>())).device as *const _ as usize }, + 8usize, + concat!( + "Offset of field: ", + stringify!(dm_deps), + "::", + stringify!(device) + ) + ); +} diff --git a/tests/expectations/tests/zero-sized-array.rs b/tests/expectations/tests/zero-sized-array.rs index 4464bb4b..e002251d 100644 --- a/tests/expectations/tests/zero-sized-array.rs +++ b/tests/expectations/tests/zero-sized-array.rs @@ -9,11 +9,11 @@ #[repr(C)] #[derive(Default)] -pub struct __IncompleteArrayField<T>(::std::marker::PhantomData<T>); +pub struct __IncompleteArrayField<T>(::std::marker::PhantomData<T>, [T; 0]); impl<T> __IncompleteArrayField<T> { #[inline] pub fn new() -> Self { - __IncompleteArrayField(::std::marker::PhantomData) + __IncompleteArrayField(::std::marker::PhantomData, []) } #[inline] pub unsafe fn as_ptr(&self) -> *const T { @@ -43,7 +43,6 @@ impl<T> ::std::clone::Clone for __IncompleteArrayField<T> { Self::new() } } -impl<T> ::std::marker::Copy for __IncompleteArrayField<T> {} /// Bizarrely enough, this should *not* get an `_address` field. #[repr(C)] #[derive(Debug, Default)] diff --git a/tests/headers/zero-size-array-align.h b/tests/headers/zero-size-array-align.h new file mode 100644 index 00000000..05b11bce --- /dev/null +++ b/tests/headers/zero-size-array-align.h @@ -0,0 +1,5 @@ +struct dm_deps { + unsigned count; + unsigned filler; + unsigned long long device[0]; +}; |