Skip to content
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

Fix incomplete array alignment in struct #1360

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
19 changes: 10 additions & 9 deletions src/codegen/mod.rs
Expand Up @@ -3658,17 +3658,23 @@ mod utils {
let incomplete_array_decl = quote! {
#[repr(C)]
#[derive(Default)]
pub struct __IncompleteArrayField<T>(
::#prefix::marker::PhantomData<T>);
pub struct __IncompleteArrayField<T> {
_alignment: [T; 0],
_marker: ::#prefix::marker::PhantomData<T>,
}
};

let incomplete_array_impl = quote! {
impl<T> __IncompleteArrayField<T> {
#[inline]
pub fn new() -> Self {
__IncompleteArrayField(::#prefix::marker::PhantomData)
__IncompleteArrayField {
_marker: ::#prefix::marker::PhantomData,
_alignment: Default::default(),
}
}


#[inline]
pub unsafe fn as_ptr(&self) -> *const T {
::#prefix::mem::transmute(self)
Expand Down Expand Up @@ -3709,15 +3715,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
20 changes: 14 additions & 6 deletions tests/expectations/tests/class.rs
@@ -1,16 +1,25 @@
/* 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> {
_alignment: [T; 0],
_marker: ::std::marker::PhantomData<T>,
}
impl<T> __IncompleteArrayField<T> {
#[inline]
pub fn new() -> Self {
__IncompleteArrayField(::std::marker::PhantomData)
__IncompleteArrayField {
_marker: ::std::marker::PhantomData,
_alignment: Default::default(),
}
}
#[inline]
pub unsafe fn as_ptr(&self) -> *const T {
Expand Down Expand Up @@ -40,7 +49,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
20 changes: 14 additions & 6 deletions tests/expectations/tests/class_1_0.rs
@@ -1,16 +1,25 @@
/* 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> {
_alignment: [T; 0],
_marker: ::std::marker::PhantomData<T>,
}
impl<T> __IncompleteArrayField<T> {
#[inline]
pub fn new() -> Self {
__IncompleteArrayField(::std::marker::PhantomData)
__IncompleteArrayField {
_marker: ::std::marker::PhantomData,
_alignment: Default::default(),
}
}
#[inline]
pub unsafe fn as_ptr(&self) -> *const T {
Expand Down Expand Up @@ -40,7 +49,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
@@ -1,16 +1,25 @@
/* 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> {
_alignment: [T; 0],
_marker: ::std::marker::PhantomData<T>,
}
impl<T> __IncompleteArrayField<T> {
#[inline]
pub fn new() -> Self {
__IncompleteArrayField(::std::marker::PhantomData)
__IncompleteArrayField {
_marker: ::std::marker::PhantomData,
_alignment: Default::default(),
}
}
#[inline]
pub unsafe fn as_ptr(&self) -> *const T {
Expand Down Expand Up @@ -40,7 +49,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
20 changes: 14 additions & 6 deletions tests/expectations/tests/issue-643-inner-struct.rs
@@ -1,16 +1,25 @@
/* 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> {
_alignment: [T; 0],
_marker: ::std::marker::PhantomData<T>,
}
impl<T> __IncompleteArrayField<T> {
#[inline]
pub fn new() -> Self {
__IncompleteArrayField(::std::marker::PhantomData)
__IncompleteArrayField {
_marker: ::std::marker::PhantomData,
_alignment: Default::default(),
}
}
#[inline]
pub unsafe fn as_ptr(&self) -> *const T {
Expand Down Expand Up @@ -40,7 +49,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
18 changes: 14 additions & 4 deletions tests/expectations/tests/layout_align.rs
@@ -1,6 +1,11 @@
/* 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(Copy, Clone, Debug, Default, Eq, Hash, Ord, PartialEq, PartialOrd)]
Expand Down Expand Up @@ -102,11 +107,17 @@ where
}
#[repr(C)]
#[derive(Default)]
pub struct __IncompleteArrayField<T>(::std::marker::PhantomData<T>);
pub struct __IncompleteArrayField<T> {
_alignment: [T; 0],
_marker: ::std::marker::PhantomData<T>,
}
impl<T> __IncompleteArrayField<T> {
#[inline]
pub fn new() -> Self {
__IncompleteArrayField(::std::marker::PhantomData)
__IncompleteArrayField {
_marker: ::std::marker::PhantomData,
_alignment: Default::default(),
}
}
#[inline]
pub unsafe fn as_ptr(&self) -> *const T {
Expand Down Expand Up @@ -136,7 +147,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
18 changes: 14 additions & 4 deletions tests/expectations/tests/layout_large_align_field.rs
@@ -1,14 +1,25 @@
/* 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> {
_alignment: [T; 0],
_marker: ::std::marker::PhantomData<T>,
}
impl<T> __IncompleteArrayField<T> {
#[inline]
pub fn new() -> Self {
__IncompleteArrayField(::std::marker::PhantomData)
__IncompleteArrayField {
_marker: ::std::marker::PhantomData,
_alignment: Default::default(),
}
}
#[inline]
pub unsafe fn as_ptr(&self) -> *const T {
Expand Down Expand Up @@ -38,7 +49,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
17 changes: 8 additions & 9 deletions tests/expectations/tests/layout_mbuf.rs
@@ -1,6 +1,11 @@
/* 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(Copy, Clone, Debug, Default, Eq, Hash, Ord, PartialEq, PartialOrd)]
Expand Down Expand Up @@ -474,10 +479,7 @@ fn bindgen_test_layout_rte_mbuf__bindgen_ty_3__bindgen_ty_1__bindgen_ty_1__bindg
);
assert_eq!(
unsafe {
&(*(::std::ptr::null::<
rte_mbuf__bindgen_ty_3__bindgen_ty_1__bindgen_ty_1__bindgen_ty_1,
>()))
.hash as *const _ as usize
& ( * ( :: std :: ptr :: null :: < rte_mbuf__bindgen_ty_3__bindgen_ty_1__bindgen_ty_1__bindgen_ty_1 > ( ) ) ) . hash as * const _ as usize
},
0usize,
concat!(
Expand All @@ -489,10 +491,7 @@ fn bindgen_test_layout_rte_mbuf__bindgen_ty_3__bindgen_ty_1__bindgen_ty_1__bindg
);
assert_eq!(
unsafe {
&(*(::std::ptr::null::<
rte_mbuf__bindgen_ty_3__bindgen_ty_1__bindgen_ty_1__bindgen_ty_1,
>()))
.id as *const _ as usize
& ( * ( :: std :: ptr :: null :: < rte_mbuf__bindgen_ty_3__bindgen_ty_1__bindgen_ty_1__bindgen_ty_1 > ( ) ) ) . id as * const _ as usize
},
2usize,
concat!(
Expand Down
17 changes: 8 additions & 9 deletions tests/expectations/tests/layout_mbuf_1_0.rs
@@ -1,6 +1,11 @@
/* 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(Copy, Clone, Debug, Default, Eq, Hash, Ord, PartialEq, PartialOrd)]
Expand Down Expand Up @@ -528,10 +533,7 @@ fn bindgen_test_layout_rte_mbuf__bindgen_ty_3__bindgen_ty_1__bindgen_ty_1__bindg
);
assert_eq!(
unsafe {
&(*(::std::ptr::null::<
rte_mbuf__bindgen_ty_3__bindgen_ty_1__bindgen_ty_1__bindgen_ty_1,
>()))
.hash as *const _ as usize
& ( * ( :: std :: ptr :: null :: < rte_mbuf__bindgen_ty_3__bindgen_ty_1__bindgen_ty_1__bindgen_ty_1 > ( ) ) ) . hash as * const _ as usize
},
0usize,
concat!(
Expand All @@ -543,10 +545,7 @@ fn bindgen_test_layout_rte_mbuf__bindgen_ty_3__bindgen_ty_1__bindgen_ty_1__bindg
);
assert_eq!(
unsafe {
&(*(::std::ptr::null::<
rte_mbuf__bindgen_ty_3__bindgen_ty_1__bindgen_ty_1__bindgen_ty_1,
>()))
.id as *const _ as usize
& ( * ( :: std :: ptr :: null :: < rte_mbuf__bindgen_ty_3__bindgen_ty_1__bindgen_ty_1__bindgen_ty_1 > ( ) ) ) . id as * const _ as usize
},
2usize,
concat!(
Expand Down
14 changes: 7 additions & 7 deletions tests/expectations/tests/sentry-defined-multiple-times.rs
@@ -1,8 +1,11 @@
/* 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
)]

#[allow(non_snake_case, non_camel_case_types, non_upper_case_globals)]
pub mod root {
Expand Down Expand Up @@ -274,10 +277,7 @@ pub mod root {
);
assert_eq!(
unsafe {
&(*(::std::ptr::null::<
OuterDoubleInlineWrapper_InnerDoubleInlineWrapper_sentry,
>()))
.i_am_double_wrapper_inline_sentry as *const _ as usize
& ( * ( :: std :: ptr :: null :: < OuterDoubleInlineWrapper_InnerDoubleInlineWrapper_sentry > ( ) ) ) . i_am_double_wrapper_inline_sentry as * const _ as usize
},
0usize,
concat!(
Expand Down