diff --git a/src/codegen/mod.rs b/src/codegen/mod.rs index 8c113f5a82..4da3fce36e 100644 --- a/src/codegen/mod.rs +++ b/src/codegen/mod.rs @@ -3658,17 +3658,23 @@ mod utils { let incomplete_array_decl = quote! { #[repr(C)] #[derive(Default)] - pub struct __IncompleteArrayField( - ::#prefix::marker::PhantomData); + pub struct __IncompleteArrayField { + _alignment: [T; 0], + _marker: ::#prefix::marker::PhantomData, + } }; let incomplete_array_impl = quote! { impl __IncompleteArrayField { #[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) @@ -3709,15 +3715,10 @@ mod utils { } }; - let incomplete_array_copy_impl = quote! { - impl ::#prefix::marker::Copy for __IncompleteArrayField {} - }; - 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 b508395798..5873566298 100644 --- a/tests/expectations/tests/class.rs +++ b/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(::std::marker::PhantomData); +pub struct __IncompleteArrayField { + _alignment: [T; 0], + _marker: ::std::marker::PhantomData, +} impl __IncompleteArrayField { #[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 { @@ -40,7 +49,6 @@ impl ::std::clone::Clone for __IncompleteArrayField { Self::new() } } -impl ::std::marker::Copy for __IncompleteArrayField {} #[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 333a6eb4be..b850ff22c4 100644 --- a/tests/expectations/tests/class_1_0.rs +++ b/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(::std::marker::PhantomData); +pub struct __IncompleteArrayField { + _alignment: [T; 0], + _marker: ::std::marker::PhantomData, +} impl __IncompleteArrayField { #[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 { @@ -40,7 +49,6 @@ impl ::std::clone::Clone for __IncompleteArrayField { Self::new() } } -impl ::std::marker::Copy for __IncompleteArrayField {} #[repr(C)] pub struct __BindgenUnionField(::std::marker::PhantomData); impl __BindgenUnionField { 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 7f56de7bfe..7016845aef 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,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(::std::marker::PhantomData); +pub struct __IncompleteArrayField { + _alignment: [T; 0], + _marker: ::std::marker::PhantomData, +} impl __IncompleteArrayField { #[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 { @@ -40,7 +49,6 @@ impl ::std::clone::Clone for __IncompleteArrayField { Self::new() } } -impl ::std::marker::Copy for __IncompleteArrayField {} #[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 7f2d4e3551..77d999bde1 100644 --- a/tests/expectations/tests/issue-643-inner-struct.rs +++ b/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(::std::marker::PhantomData); +pub struct __IncompleteArrayField { + _alignment: [T; 0], + _marker: ::std::marker::PhantomData, +} impl __IncompleteArrayField { #[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 { @@ -40,7 +49,6 @@ impl ::std::clone::Clone for __IncompleteArrayField { Self::new() } } -impl ::std::marker::Copy for __IncompleteArrayField {} #[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 71ba76fbd2..d080787871 100644 --- a/tests/expectations/tests/layout_align.rs +++ b/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)] @@ -102,11 +107,17 @@ where } #[repr(C)] #[derive(Default)] -pub struct __IncompleteArrayField(::std::marker::PhantomData); +pub struct __IncompleteArrayField { + _alignment: [T; 0], + _marker: ::std::marker::PhantomData, +} impl __IncompleteArrayField { #[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 { @@ -136,7 +147,6 @@ impl ::std::clone::Clone for __IncompleteArrayField { Self::new() } } -impl ::std::marker::Copy for __IncompleteArrayField {} #[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 af738d04d8..eaa7c9e866 100644 --- a/tests/expectations/tests/layout_large_align_field.rs +++ b/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(::std::marker::PhantomData); +pub struct __IncompleteArrayField { + _alignment: [T; 0], + _marker: ::std::marker::PhantomData, +} impl __IncompleteArrayField { #[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 { @@ -38,7 +49,6 @@ impl ::std::clone::Clone for __IncompleteArrayField { Self::new() } } -impl ::std::marker::Copy for __IncompleteArrayField {} 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/layout_mbuf.rs b/tests/expectations/tests/layout_mbuf.rs index 0dbc362fcf..af58ca1c99 100644 --- a/tests/expectations/tests/layout_mbuf.rs +++ b/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)] @@ -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!( @@ -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!( diff --git a/tests/expectations/tests/layout_mbuf_1_0.rs b/tests/expectations/tests/layout_mbuf_1_0.rs index 0bfcc13e56..da9d84b821 100644 --- a/tests/expectations/tests/layout_mbuf_1_0.rs +++ b/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)] @@ -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!( @@ -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!( diff --git a/tests/expectations/tests/sentry-defined-multiple-times.rs b/tests/expectations/tests/sentry-defined-multiple-times.rs index 887d84bb10..7f96b45023 100644 --- a/tests/expectations/tests/sentry-defined-multiple-times.rs +++ b/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 { @@ -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!( diff --git a/tests/expectations/tests/struct_with_incomplete_array.rs b/tests/expectations/tests/struct_with_incomplete_array.rs new file mode 100644 index 0000000000..77b1d18af4 --- /dev/null +++ b/tests/expectations/tests/struct_with_incomplete_array.rs @@ -0,0 +1,135 @@ +/* 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 { + _alignment: [T; 0], + _marker: ::std::marker::PhantomData, +} +impl __IncompleteArrayField { + #[inline] + pub fn new() -> Self { + __IncompleteArrayField { + _marker: ::std::marker::PhantomData, + _alignment: Default::default(), + } + } + #[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 ::std::fmt::Debug for __IncompleteArrayField { + fn fmt(&self, fmt: &mut ::std::fmt::Formatter) -> ::std::fmt::Result { + fmt.write_str("__IncompleteArrayField") + } +} +impl ::std::clone::Clone for __IncompleteArrayField { + #[inline] + fn clone(&self) -> Self { + Self::new() + } +} +#[repr(C)] +#[derive(Debug, Default)] +pub struct bpf_raw_tracepoint_args { + pub args: __IncompleteArrayField<::std::os::raw::c_ulonglong>, +} +#[test] +fn bindgen_test_layout_bpf_raw_tracepoint_args() { + assert_eq!( + ::std::mem::size_of::(), + 0usize, + concat!("Size of: ", stringify!(bpf_raw_tracepoint_args)) + ); + assert_eq!( + ::std::mem::align_of::(), + 8usize, + concat!("Alignment of ", stringify!(bpf_raw_tracepoint_args)) + ); + assert_eq!( + unsafe { &(*(::std::ptr::null::())).args as *const _ as usize }, + 0usize, + concat!( + "Offset of field: ", + stringify!(bpf_raw_tracepoint_args), + "::", + stringify!(args) + ) + ); +} +#[repr(C)] +#[derive(Debug, Default)] +pub struct test_short { + pub args: __IncompleteArrayField<::std::os::raw::c_short>, +} +#[test] +fn bindgen_test_layout_test_short() { + assert_eq!( + ::std::mem::size_of::(), + 0usize, + concat!("Size of: ", stringify!(test_short)) + ); + assert_eq!( + ::std::mem::align_of::(), + 2usize, + concat!("Alignment of ", stringify!(test_short)) + ); + assert_eq!( + unsafe { &(*(::std::ptr::null::())).args as *const _ as usize }, + 0usize, + concat!( + "Offset of field: ", + stringify!(test_short), + "::", + stringify!(args) + ) + ); +} +#[repr(C)] +#[derive(Debug, Default)] +pub struct test_int { + pub args: __IncompleteArrayField<::std::os::raw::c_int>, +} +#[test] +fn bindgen_test_layout_test_int() { + assert_eq!( + ::std::mem::size_of::(), + 0usize, + concat!("Size of: ", stringify!(test_int)) + ); + assert_eq!( + ::std::mem::align_of::(), + 4usize, + concat!("Alignment of ", stringify!(test_int)) + ); + assert_eq!( + unsafe { &(*(::std::ptr::null::())).args as *const _ as usize }, + 0usize, + concat!( + "Offset of field: ", + stringify!(test_int), + "::", + stringify!(args) + ) + ); +} diff --git a/tests/expectations/tests/zero-sized-array.rs b/tests/expectations/tests/zero-sized-array.rs index fe78ae86ce..12056dea67 100644 --- a/tests/expectations/tests/zero-sized-array.rs +++ b/tests/expectations/tests/zero-sized-array.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(::std::marker::PhantomData); +pub struct __IncompleteArrayField { + _alignment: [T; 0], + _marker: ::std::marker::PhantomData, +} impl __IncompleteArrayField { #[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 { @@ -40,7 +49,6 @@ impl ::std::clone::Clone for __IncompleteArrayField { Self::new() } } -impl ::std::marker::Copy for __IncompleteArrayField {} /// Bizarrely enough, this should *not* get an `_address` field. #[repr(C)] #[derive(Debug, Default)] diff --git a/tests/headers/struct_with_incomplete_array.hpp b/tests/headers/struct_with_incomplete_array.hpp new file mode 100644 index 0000000000..bab743cac0 --- /dev/null +++ b/tests/headers/struct_with_incomplete_array.hpp @@ -0,0 +1,11 @@ +struct bpf_raw_tracepoint_args { + unsigned long long args[0]; +}; + +struct test_short { + short args[0]; +}; + +struct test_int { + int args[0]; +};