Skip to content

Commit b4895d9

Browse files
author
Zhiting Zhu
committed
Small dedicated tests for derive Hash
1 parent 3edb811 commit b4895d9

13 files changed

+411
-0
lines changed

tests/expectations/struct_with_anon_struct_array_float.rs

Whitespace-only changes.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/* automatically generated by rust-bindgen */
2+
3+
4+
#![allow(dead_code, non_snake_case, non_camel_case_types, non_upper_case_globals)]
5+
6+
#[repr(C)] #[derive(Debug, Hash, Copy, Clone)] pub struct Blacklisted<T> {t: T, pub _phantom_0: ::std::marker::PhantomData<::std::cell::UnsafeCell<T>> }
7+
8+
/// This would derive(Hash) if it didn't contain a blacklisted type,
9+
/// causing us to conservatively avoid deriving hash for it.
10+
#[repr(C)]
11+
#[derive(Debug, Copy)]
12+
pub struct WhitelistedOne {
13+
pub a: Blacklisted<::std::os::raw::c_int>,
14+
}
15+
#[test]
16+
fn bindgen_test_layout_WhitelistedOne() {
17+
assert_eq!(::std::mem::size_of::<WhitelistedOne>() , 4usize , concat ! (
18+
"Size of: " , stringify ! ( WhitelistedOne ) ));
19+
assert_eq! (::std::mem::align_of::<WhitelistedOne>() , 4usize , concat ! (
20+
"Alignment of " , stringify ! ( WhitelistedOne ) ));
21+
assert_eq! (unsafe {
22+
& ( * ( 0 as * const WhitelistedOne ) ) . a as * const _ as
23+
usize } , 0usize , concat ! (
24+
"Alignment of field: " , stringify ! ( WhitelistedOne ) , "::"
25+
, stringify ! ( a ) ));
26+
}
27+
impl Clone for WhitelistedOne {
28+
fn clone(&self) -> Self { *self }
29+
}
30+
impl Default for WhitelistedOne {
31+
fn default() -> Self { unsafe { ::std::mem::zeroed() } }
32+
}
33+
/// This can't derive(Hash) even if it didn't contain a blacklisted type.
34+
#[repr(C)]
35+
#[derive(Debug, Copy)]
36+
pub struct WhitelistedTwo {
37+
pub b: Blacklisted<f32>,
38+
}
39+
#[test]
40+
fn bindgen_test_layout_WhitelistedTwo() {
41+
assert_eq!(::std::mem::size_of::<WhitelistedTwo>() , 4usize , concat ! (
42+
"Size of: " , stringify ! ( WhitelistedTwo ) ));
43+
assert_eq! (::std::mem::align_of::<WhitelistedTwo>() , 4usize , concat ! (
44+
"Alignment of " , stringify ! ( WhitelistedTwo ) ));
45+
assert_eq! (unsafe {
46+
& ( * ( 0 as * const WhitelistedTwo ) ) . b as * const _ as
47+
usize } , 0usize , concat ! (
48+
"Alignment of field: " , stringify ! ( WhitelistedTwo ) , "::"
49+
, stringify ! ( b ) ));
50+
}
51+
impl Clone for WhitelistedTwo {
52+
fn clone(&self) -> Self { *self }
53+
}
54+
impl Default for WhitelistedTwo {
55+
fn default() -> Self { unsafe { ::std::mem::zeroed() } }
56+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/* automatically generated by rust-bindgen */
2+
3+
4+
#![allow(dead_code, non_snake_case, non_camel_case_types, non_upper_case_globals)]
5+
6+
7+
/// A struct containing a struct containing a float that cannot derive hash.
8+
#[repr(C)]
9+
#[derive(Debug, Default, Copy)]
10+
pub struct foo {
11+
pub bar: foo__bindgen_ty_1,
12+
}
13+
#[repr(C)]
14+
#[derive(Debug, Default, Copy)]
15+
pub struct foo__bindgen_ty_1 {
16+
pub a: f32,
17+
pub b: f32,
18+
}
19+
#[test]
20+
fn bindgen_test_layout_foo__bindgen_ty_1() {
21+
assert_eq!(::std::mem::size_of::<foo__bindgen_ty_1>() , 8usize , concat !
22+
( "Size of: " , stringify ! ( foo__bindgen_ty_1 ) ));
23+
assert_eq! (::std::mem::align_of::<foo__bindgen_ty_1>() , 4usize , concat
24+
! ( "Alignment of " , stringify ! ( foo__bindgen_ty_1 ) ));
25+
assert_eq! (unsafe {
26+
& ( * ( 0 as * const foo__bindgen_ty_1 ) ) . a as * const _ as
27+
usize } , 0usize , concat ! (
28+
"Alignment of field: " , stringify ! ( foo__bindgen_ty_1 ) ,
29+
"::" , stringify ! ( a ) ));
30+
assert_eq! (unsafe {
31+
& ( * ( 0 as * const foo__bindgen_ty_1 ) ) . b as * const _ as
32+
usize } , 4usize , concat ! (
33+
"Alignment of field: " , stringify ! ( foo__bindgen_ty_1 ) ,
34+
"::" , stringify ! ( b ) ));
35+
}
36+
impl Clone for foo__bindgen_ty_1 {
37+
fn clone(&self) -> Self { *self }
38+
}
39+
#[test]
40+
fn bindgen_test_layout_foo() {
41+
assert_eq!(::std::mem::size_of::<foo>() , 8usize , concat ! (
42+
"Size of: " , stringify ! ( foo ) ));
43+
assert_eq! (::std::mem::align_of::<foo>() , 4usize , concat ! (
44+
"Alignment of " , stringify ! ( foo ) ));
45+
assert_eq! (unsafe {
46+
& ( * ( 0 as * const foo ) ) . bar as * const _ as usize } ,
47+
0usize , concat ! (
48+
"Alignment of field: " , stringify ! ( foo ) , "::" ,
49+
stringify ! ( bar ) ));
50+
}
51+
impl Clone for foo {
52+
fn clone(&self) -> Self { *self }
53+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/* automatically generated by rust-bindgen */
2+
3+
4+
#![allow(dead_code, non_snake_case, non_camel_case_types, non_upper_case_globals)]
5+
6+
7+
/// A struct containing an array of floats that cannot derive hash.
8+
#[repr(C)]
9+
#[derive(Debug, Default, Copy)]
10+
pub struct foo {
11+
pub bar: [f32; 3usize],
12+
}
13+
#[test]
14+
fn bindgen_test_layout_foo() {
15+
assert_eq!(::std::mem::size_of::<foo>() , 12usize , concat ! (
16+
"Size of: " , stringify ! ( foo ) ));
17+
assert_eq! (::std::mem::align_of::<foo>() , 4usize , concat ! (
18+
"Alignment of " , stringify ! ( foo ) ));
19+
assert_eq! (unsafe {
20+
& ( * ( 0 as * const foo ) ) . bar as * const _ as usize } ,
21+
0usize , concat ! (
22+
"Alignment of field: " , stringify ! ( foo ) , "::" ,
23+
stringify ! ( bar ) ));
24+
}
25+
impl Clone for foo {
26+
fn clone(&self) -> Self { *self }
27+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
/* automatically generated by rust-bindgen */
2+
3+
4+
#![allow(dead_code, non_snake_case, non_camel_case_types, non_upper_case_globals)]
5+
6+
7+
/// Pointers can derive hash
8+
#[repr(C)]
9+
#[derive(Debug, Copy, Hash)]
10+
pub struct ConstPtrMutObj {
11+
pub bar: *const ::std::os::raw::c_int,
12+
}
13+
#[test]
14+
fn bindgen_test_layout_ConstPtrMutObj() {
15+
assert_eq!(::std::mem::size_of::<ConstPtrMutObj>() , 8usize , concat ! (
16+
"Size of: " , stringify ! ( ConstPtrMutObj ) ));
17+
assert_eq! (::std::mem::align_of::<ConstPtrMutObj>() , 8usize , concat ! (
18+
"Alignment of " , stringify ! ( ConstPtrMutObj ) ));
19+
assert_eq! (unsafe {
20+
& ( * ( 0 as * const ConstPtrMutObj ) ) . bar as * const _ as
21+
usize } , 0usize , concat ! (
22+
"Alignment of field: " , stringify ! ( ConstPtrMutObj ) , "::"
23+
, stringify ! ( bar ) ));
24+
}
25+
impl Clone for ConstPtrMutObj {
26+
fn clone(&self) -> Self { *self }
27+
}
28+
impl Default for ConstPtrMutObj {
29+
fn default() -> Self { unsafe { ::std::mem::zeroed() } }
30+
}
31+
#[repr(C)]
32+
#[derive(Debug, Copy, Hash)]
33+
pub struct MutPtrMutObj {
34+
pub bar: *mut ::std::os::raw::c_int,
35+
}
36+
#[test]
37+
fn bindgen_test_layout_MutPtrMutObj() {
38+
assert_eq!(::std::mem::size_of::<MutPtrMutObj>() , 8usize , concat ! (
39+
"Size of: " , stringify ! ( MutPtrMutObj ) ));
40+
assert_eq! (::std::mem::align_of::<MutPtrMutObj>() , 8usize , concat ! (
41+
"Alignment of " , stringify ! ( MutPtrMutObj ) ));
42+
assert_eq! (unsafe {
43+
& ( * ( 0 as * const MutPtrMutObj ) ) . bar as * const _ as
44+
usize } , 0usize , concat ! (
45+
"Alignment of field: " , stringify ! ( MutPtrMutObj ) , "::" ,
46+
stringify ! ( bar ) ));
47+
}
48+
impl Clone for MutPtrMutObj {
49+
fn clone(&self) -> Self { *self }
50+
}
51+
impl Default for MutPtrMutObj {
52+
fn default() -> Self { unsafe { ::std::mem::zeroed() } }
53+
}
54+
#[repr(C)]
55+
#[derive(Debug, Copy, Hash)]
56+
pub struct MutPtrConstObj {
57+
pub bar: *const ::std::os::raw::c_int,
58+
}
59+
#[test]
60+
fn bindgen_test_layout_MutPtrConstObj() {
61+
assert_eq!(::std::mem::size_of::<MutPtrConstObj>() , 8usize , concat ! (
62+
"Size of: " , stringify ! ( MutPtrConstObj ) ));
63+
assert_eq! (::std::mem::align_of::<MutPtrConstObj>() , 8usize , concat ! (
64+
"Alignment of " , stringify ! ( MutPtrConstObj ) ));
65+
assert_eq! (unsafe {
66+
& ( * ( 0 as * const MutPtrConstObj ) ) . bar as * const _ as
67+
usize } , 0usize , concat ! (
68+
"Alignment of field: " , stringify ! ( MutPtrConstObj ) , "::"
69+
, stringify ! ( bar ) ));
70+
}
71+
impl Clone for MutPtrConstObj {
72+
fn clone(&self) -> Self { *self }
73+
}
74+
impl Default for MutPtrConstObj {
75+
fn default() -> Self { unsafe { ::std::mem::zeroed() } }
76+
}
77+
#[repr(C)]
78+
#[derive(Debug, Copy, Hash)]
79+
pub struct ConstPtrConstObj {
80+
pub bar: *const ::std::os::raw::c_int,
81+
}
82+
#[test]
83+
fn bindgen_test_layout_ConstPtrConstObj() {
84+
assert_eq!(::std::mem::size_of::<ConstPtrConstObj>() , 8usize , concat ! (
85+
"Size of: " , stringify ! ( ConstPtrConstObj ) ));
86+
assert_eq! (::std::mem::align_of::<ConstPtrConstObj>() , 8usize , concat !
87+
( "Alignment of " , stringify ! ( ConstPtrConstObj ) ));
88+
assert_eq! (unsafe {
89+
& ( * ( 0 as * const ConstPtrConstObj ) ) . bar as * const _
90+
as usize } , 0usize , concat ! (
91+
"Alignment of field: " , stringify ! ( ConstPtrConstObj ) ,
92+
"::" , stringify ! ( bar ) ));
93+
}
94+
impl Clone for ConstPtrConstObj {
95+
fn clone(&self) -> Self { *self }
96+
}
97+
impl Default for ConstPtrConstObj {
98+
fn default() -> Self { unsafe { ::std::mem::zeroed() } }
99+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
/* automatically generated by rust-bindgen */
2+
3+
4+
#![allow(dead_code, non_snake_case, non_camel_case_types, non_upper_case_globals)]
5+
6+
7+
/// Template definition containing a float, which cannot derive hash.
8+
#[repr(C)]
9+
#[derive(Debug, Copy, Clone)]
10+
pub struct foo<T> {
11+
pub data: T,
12+
pub b: f32,
13+
pub _phantom_0: ::std::marker::PhantomData<::std::cell::UnsafeCell<T>>,
14+
}
15+
impl <T> Default for foo<T> {
16+
fn default() -> Self { unsafe { ::std::mem::zeroed() } }
17+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
/* automatically generated by rust-bindgen */
2+
3+
4+
#![allow(dead_code, non_snake_case, non_camel_case_types, non_upper_case_globals)]
5+
6+
7+
/// Template definition that doesn't contain float can derive hash
8+
#[repr(C)]
9+
#[derive(Debug, Copy, Clone, Hash)]
10+
pub struct foo<T> {
11+
pub data: T,
12+
pub _phantom_0: ::std::marker::PhantomData<::std::cell::UnsafeCell<T>>,
13+
}
14+
impl <T> Default for foo<T> {
15+
fn default() -> Self { unsafe { ::std::mem::zeroed() } }
16+
}
17+
/// Can derive hash when instantiated with int
18+
#[repr(C)]
19+
#[derive(Debug, Copy, Hash)]
20+
pub struct IntStr {
21+
pub a: foo<::std::os::raw::c_int>,
22+
}
23+
#[test]
24+
fn bindgen_test_layout_IntStr() {
25+
assert_eq!(::std::mem::size_of::<IntStr>() , 4usize , concat ! (
26+
"Size of: " , stringify ! ( IntStr ) ));
27+
assert_eq! (::std::mem::align_of::<IntStr>() , 4usize , concat ! (
28+
"Alignment of " , stringify ! ( IntStr ) ));
29+
assert_eq! (unsafe {
30+
& ( * ( 0 as * const IntStr ) ) . a as * const _ as usize } ,
31+
0usize , concat ! (
32+
"Alignment of field: " , stringify ! ( IntStr ) , "::" ,
33+
stringify ! ( a ) ));
34+
}
35+
impl Clone for IntStr {
36+
fn clone(&self) -> Self { *self }
37+
}
38+
impl Default for IntStr {
39+
fn default() -> Self { unsafe { ::std::mem::zeroed() } }
40+
}
41+
/// Cannot derive hash when instantiated with float
42+
#[repr(C)]
43+
#[derive(Debug, Copy)]
44+
pub struct FloatStr {
45+
pub a: foo<f32>,
46+
}
47+
#[test]
48+
fn bindgen_test_layout_FloatStr() {
49+
assert_eq!(::std::mem::size_of::<FloatStr>() , 4usize , concat ! (
50+
"Size of: " , stringify ! ( FloatStr ) ));
51+
assert_eq! (::std::mem::align_of::<FloatStr>() , 4usize , concat ! (
52+
"Alignment of " , stringify ! ( FloatStr ) ));
53+
assert_eq! (unsafe {
54+
& ( * ( 0 as * const FloatStr ) ) . a as * const _ as usize }
55+
, 0usize , concat ! (
56+
"Alignment of field: " , stringify ! ( FloatStr ) , "::" ,
57+
stringify ! ( a ) ));
58+
}
59+
impl Clone for FloatStr {
60+
fn clone(&self) -> Self { *self }
61+
}
62+
impl Default for FloatStr {
63+
fn default() -> Self { unsafe { ::std::mem::zeroed() } }
64+
}
65+
#[test]
66+
fn __bindgen_test_layout_foo_open0_int_close0_instantiation() {
67+
assert_eq!(::std::mem::size_of::<foo<::std::os::raw::c_int>>() , 4usize ,
68+
concat ! (
69+
"Size of template specialization: " , stringify ! (
70+
foo<::std::os::raw::c_int> ) ));
71+
assert_eq!(::std::mem::align_of::<foo<::std::os::raw::c_int>>() , 4usize ,
72+
concat ! (
73+
"Alignment of template specialization: " , stringify ! (
74+
foo<::std::os::raw::c_int> ) ));
75+
}
76+
#[test]
77+
fn __bindgen_test_layout_foo_open0_float_close0_instantiation() {
78+
assert_eq!(::std::mem::size_of::<foo<f32>>() , 4usize , concat ! (
79+
"Size of template specialization: " , stringify ! ( foo<f32> )
80+
));
81+
assert_eq!(::std::mem::align_of::<foo<f32>>() , 4usize , concat ! (
82+
"Alignment of template specialization: " , stringify ! (
83+
foo<f32> ) ));
84+
}
+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// bindgen-flags: --with-derive-hash --whitelist-type 'Whitelisted.*' --blacklist-type Blacklisted --raw-line "#[repr(C)] #[derive(Debug, Hash, Copy, Clone)] pub struct Blacklisted<T> {t: T, pub _phantom_0: ::std::marker::PhantomData<::std::cell::UnsafeCell<T>> }"
2+
//
3+
template<class T>
4+
struct Blacklisted {
5+
T t;
6+
};
7+
8+
/// This would derive(Hash) if it didn't contain a blacklisted type,
9+
/// causing us to conservatively avoid deriving hash for it.
10+
struct WhitelistedOne {
11+
Blacklisted<int> a;
12+
};
13+
14+
/// This can't derive(Hash) even if it didn't contain a blacklisted type.
15+
struct WhitelistedTwo {
16+
Blacklisted<float> b;
17+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// bindgen-flags: --with-derive-hash
2+
//
3+
/// A struct containing a struct containing a float that cannot derive hash.
4+
struct foo {
5+
struct {
6+
float a;
7+
float b;
8+
} bar;
9+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
// bindgen-flags: --with-derive-hash
2+
//
3+
/// A struct containing an array of floats that cannot derive hash.
4+
struct foo {
5+
float bar[3];
6+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// bindgen-flags: --with-derive-hash
2+
//
3+
/// Pointers can derive hash
4+
struct ConstPtrMutObj {
5+
int* const bar;
6+
};
7+
8+
struct MutPtrMutObj {
9+
int* bar;
10+
};
11+
12+
struct MutPtrConstObj {
13+
const int* bar;
14+
};
15+
16+
struct ConstPtrConstObj {
17+
const int* const bar;
18+
};

0 commit comments

Comments
 (0)