Skip to content

codegen: Make zero-sized arrays affect alignment. #1477

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 29, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 3 additions & 8 deletions src/codegen/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down Expand Up @@ -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());
Expand Down
14 changes: 8 additions & 6 deletions tests/expectations/tests/class.rs
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down
14 changes: 8 additions & 6 deletions tests/expectations/tests/class_1_0.rs
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down Expand Up @@ -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> {
Expand Down
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down
14 changes: 8 additions & 6 deletions tests/expectations/tests/issue-643-inner-struct.rs
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down
5 changes: 2 additions & 3 deletions tests/expectations/tests/layout_align.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down
5 changes: 2 additions & 3 deletions tests/expectations/tests/layout_large_align_field.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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;
Expand Down
95 changes: 95 additions & 0 deletions tests/expectations/tests/zero-size-array-align.rs
Original file line number Diff line number Diff line change
@@ -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)
)
);
}
5 changes: 2 additions & 3 deletions tests/expectations/tests/zero-sized-array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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)]
Expand Down
5 changes: 5 additions & 0 deletions tests/headers/zero-size-array-align.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
struct dm_deps {
unsigned count;
unsigned filler;
unsigned long long device[0];
};