Skip to content

Commit dc1e546

Browse files
author
bors-servo
authored
Auto merge of #1094 - photoszzt:fix_hash_incomplete_arr, r=emilio
Fix mistakenly derive hash for struct that contains IncompleteArrayField Fix #1093 r? @fitzgen or @emilio
2 parents 17adb13 + fa9b5e3 commit dc1e546

File tree

7 files changed

+351
-4
lines changed

7 files changed

+351
-4
lines changed

src/ir/analysis/derive_hash.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,10 @@ impl<'ctx> MonotoneFramework for CannotDeriveHash<'ctx> {
187187
return self.insert(id);
188188
}
189189

190-
if len <= RUST_DERIVE_IN_ARRAY_LIMIT {
190+
if len == 0 {
191+
trace!(" cannot derive `Hash` for incomplete arrays");
192+
self.insert(id)
193+
} else if len <= RUST_DERIVE_IN_ARRAY_LIMIT {
191194
trace!(" array is small enough to derive Hash");
192195
ConstrainResult::Same
193196
} else {

tests/expectations/tests/class.rs

+88-2
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,47 @@ impl Default for C_with_zero_length_array {
142142
}
143143
}
144144
#[repr(C)]
145+
#[derive(Debug, Default)]
146+
pub struct C_with_zero_length_array_2 {
147+
pub a: ::std::os::raw::c_int,
148+
pub zero_length_array: __IncompleteArrayField<::std::os::raw::c_char>,
149+
}
150+
#[test]
151+
fn bindgen_test_layout_C_with_zero_length_array_2() {
152+
assert_eq!(
153+
::std::mem::size_of::<C_with_zero_length_array_2>(),
154+
4usize,
155+
concat!("Size of: ", stringify!(C_with_zero_length_array_2))
156+
);
157+
assert_eq!(
158+
::std::mem::align_of::<C_with_zero_length_array_2>(),
159+
4usize,
160+
concat!("Alignment of ", stringify!(C_with_zero_length_array_2))
161+
);
162+
assert_eq!(
163+
unsafe { &(*(0 as *const C_with_zero_length_array_2)).a as *const _ as usize },
164+
0usize,
165+
concat!(
166+
"Alignment of field: ",
167+
stringify!(C_with_zero_length_array_2),
168+
"::",
169+
stringify!(a)
170+
)
171+
);
172+
assert_eq!(
173+
unsafe {
174+
&(*(0 as *const C_with_zero_length_array_2)).zero_length_array as *const _ as usize
175+
},
176+
4usize,
177+
concat!(
178+
"Alignment of field: ",
179+
stringify!(C_with_zero_length_array_2),
180+
"::",
181+
stringify!(zero_length_array)
182+
)
183+
);
184+
}
185+
#[repr(C)]
145186
pub struct C_with_incomplete_array {
146187
pub a: ::std::os::raw::c_int,
147188
pub big_array: [::std::os::raw::c_char; 33usize],
@@ -166,6 +207,25 @@ impl Default for C_with_incomplete_array {
166207
}
167208
}
168209
#[repr(C)]
210+
#[derive(Debug, Default)]
211+
pub struct C_with_incomplete_array_2 {
212+
pub a: ::std::os::raw::c_int,
213+
pub incomplete_array: __IncompleteArrayField<::std::os::raw::c_char>,
214+
}
215+
#[test]
216+
fn bindgen_test_layout_C_with_incomplete_array_2() {
217+
assert_eq!(
218+
::std::mem::size_of::<C_with_incomplete_array_2>(),
219+
4usize,
220+
concat!("Size of: ", stringify!(C_with_incomplete_array_2))
221+
);
222+
assert_eq!(
223+
::std::mem::align_of::<C_with_incomplete_array_2>(),
224+
4usize,
225+
concat!("Alignment of ", stringify!(C_with_incomplete_array_2))
226+
);
227+
}
228+
#[repr(C)]
169229
pub struct C_with_zero_length_array_and_incomplete_array {
170230
pub a: ::std::os::raw::c_int,
171231
pub big_array: [::std::os::raw::c_char; 33usize],
@@ -197,7 +257,33 @@ impl Default for C_with_zero_length_array_and_incomplete_array {
197257
}
198258
}
199259
#[repr(C)]
200-
#[derive(Debug, Default, Hash, PartialEq, Eq)]
260+
#[derive(Debug, Default)]
261+
pub struct C_with_zero_length_array_and_incomplete_array_2 {
262+
pub a: ::std::os::raw::c_int,
263+
pub zero_length_array: __IncompleteArrayField<::std::os::raw::c_char>,
264+
pub incomplete_array: __IncompleteArrayField<::std::os::raw::c_char>,
265+
}
266+
#[test]
267+
fn bindgen_test_layout_C_with_zero_length_array_and_incomplete_array_2() {
268+
assert_eq!(
269+
::std::mem::size_of::<C_with_zero_length_array_and_incomplete_array_2>(),
270+
4usize,
271+
concat!(
272+
"Size of: ",
273+
stringify!(C_with_zero_length_array_and_incomplete_array_2)
274+
)
275+
);
276+
assert_eq!(
277+
::std::mem::align_of::<C_with_zero_length_array_and_incomplete_array_2>(),
278+
4usize,
279+
concat!(
280+
"Alignment of ",
281+
stringify!(C_with_zero_length_array_and_incomplete_array_2)
282+
)
283+
);
284+
}
285+
#[repr(C)]
286+
#[derive(Debug, Default, Hash, PartialOrd, Ord, PartialEq, Eq)]
201287
pub struct WithDtor {
202288
pub b: ::std::os::raw::c_int,
203289
}
@@ -336,7 +422,7 @@ impl Default for WithUnion {
336422
}
337423
}
338424
#[repr(C)]
339-
#[derive(Debug, Default, Copy, Hash, PartialEq, Eq)]
425+
#[derive(Debug, Default, Copy, Hash, PartialOrd, Ord, PartialEq, Eq)]
340426
pub struct RealAbstractionWithTonsOfMethods {
341427
pub _address: u8,
342428
}

tests/expectations/tests/class_1_0.rs

+86
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,47 @@ impl Default for C_with_zero_length_array {
190190
}
191191
}
192192
#[repr(C)]
193+
#[derive(Debug, Default)]
194+
pub struct C_with_zero_length_array_2 {
195+
pub a: ::std::os::raw::c_int,
196+
pub zero_length_array: __IncompleteArrayField<::std::os::raw::c_char>,
197+
}
198+
#[test]
199+
fn bindgen_test_layout_C_with_zero_length_array_2() {
200+
assert_eq!(
201+
::std::mem::size_of::<C_with_zero_length_array_2>(),
202+
4usize,
203+
concat!("Size of: ", stringify!(C_with_zero_length_array_2))
204+
);
205+
assert_eq!(
206+
::std::mem::align_of::<C_with_zero_length_array_2>(),
207+
4usize,
208+
concat!("Alignment of ", stringify!(C_with_zero_length_array_2))
209+
);
210+
assert_eq!(
211+
unsafe { &(*(0 as *const C_with_zero_length_array_2)).a as *const _ as usize },
212+
0usize,
213+
concat!(
214+
"Alignment of field: ",
215+
stringify!(C_with_zero_length_array_2),
216+
"::",
217+
stringify!(a)
218+
)
219+
);
220+
assert_eq!(
221+
unsafe {
222+
&(*(0 as *const C_with_zero_length_array_2)).zero_length_array as *const _ as usize
223+
},
224+
4usize,
225+
concat!(
226+
"Alignment of field: ",
227+
stringify!(C_with_zero_length_array_2),
228+
"::",
229+
stringify!(zero_length_array)
230+
)
231+
);
232+
}
233+
#[repr(C)]
193234
pub struct C_with_incomplete_array {
194235
pub a: ::std::os::raw::c_int,
195236
pub big_array: [::std::os::raw::c_char; 33usize],
@@ -214,6 +255,25 @@ impl Default for C_with_incomplete_array {
214255
}
215256
}
216257
#[repr(C)]
258+
#[derive(Debug, Default)]
259+
pub struct C_with_incomplete_array_2 {
260+
pub a: ::std::os::raw::c_int,
261+
pub incomplete_array: __IncompleteArrayField<::std::os::raw::c_char>,
262+
}
263+
#[test]
264+
fn bindgen_test_layout_C_with_incomplete_array_2() {
265+
assert_eq!(
266+
::std::mem::size_of::<C_with_incomplete_array_2>(),
267+
4usize,
268+
concat!("Size of: ", stringify!(C_with_incomplete_array_2))
269+
);
270+
assert_eq!(
271+
::std::mem::align_of::<C_with_incomplete_array_2>(),
272+
4usize,
273+
concat!("Alignment of ", stringify!(C_with_incomplete_array_2))
274+
);
275+
}
276+
#[repr(C)]
217277
pub struct C_with_zero_length_array_and_incomplete_array {
218278
pub a: ::std::os::raw::c_int,
219279
pub big_array: [::std::os::raw::c_char; 33usize],
@@ -245,6 +305,32 @@ impl Default for C_with_zero_length_array_and_incomplete_array {
245305
}
246306
}
247307
#[repr(C)]
308+
#[derive(Debug, Default)]
309+
pub struct C_with_zero_length_array_and_incomplete_array_2 {
310+
pub a: ::std::os::raw::c_int,
311+
pub zero_length_array: __IncompleteArrayField<::std::os::raw::c_char>,
312+
pub incomplete_array: __IncompleteArrayField<::std::os::raw::c_char>,
313+
}
314+
#[test]
315+
fn bindgen_test_layout_C_with_zero_length_array_and_incomplete_array_2() {
316+
assert_eq!(
317+
::std::mem::size_of::<C_with_zero_length_array_and_incomplete_array_2>(),
318+
4usize,
319+
concat!(
320+
"Size of: ",
321+
stringify!(C_with_zero_length_array_and_incomplete_array_2)
322+
)
323+
);
324+
assert_eq!(
325+
::std::mem::align_of::<C_with_zero_length_array_and_incomplete_array_2>(),
326+
4usize,
327+
concat!(
328+
"Alignment of ",
329+
stringify!(C_with_zero_length_array_and_incomplete_array_2)
330+
)
331+
);
332+
}
333+
#[repr(C)]
248334
#[derive(Debug, Default, Hash, PartialEq, Eq)]
249335
pub struct WithDtor {
250336
pub b: ::std::os::raw::c_int,
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
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+
#[repr(C)]
8+
#[derive(Default)]
9+
pub struct __IncompleteArrayField<T>(::std::marker::PhantomData<T>);
10+
impl<T> __IncompleteArrayField<T> {
11+
#[inline]
12+
pub fn new() -> Self {
13+
__IncompleteArrayField(::std::marker::PhantomData)
14+
}
15+
#[inline]
16+
pub unsafe fn as_ptr(&self) -> *const T {
17+
::std::mem::transmute(self)
18+
}
19+
#[inline]
20+
pub unsafe fn as_mut_ptr(&mut self) -> *mut T {
21+
::std::mem::transmute(self)
22+
}
23+
#[inline]
24+
pub unsafe fn as_slice(&self, len: usize) -> &[T] {
25+
::std::slice::from_raw_parts(self.as_ptr(), len)
26+
}
27+
#[inline]
28+
pub unsafe fn as_mut_slice(&mut self, len: usize) -> &mut [T] {
29+
::std::slice::from_raw_parts_mut(self.as_mut_ptr(), len)
30+
}
31+
}
32+
impl<T> ::std::fmt::Debug for __IncompleteArrayField<T> {
33+
fn fmt(&self, fmt: &mut ::std::fmt::Formatter) -> ::std::fmt::Result {
34+
fmt.write_str("__IncompleteArrayField")
35+
}
36+
}
37+
impl<T> ::std::clone::Clone for __IncompleteArrayField<T> {
38+
#[inline]
39+
fn clone(&self) -> Self {
40+
Self::new()
41+
}
42+
}
43+
impl<T> ::std::marker::Copy for __IncompleteArrayField<T> {}
44+
#[repr(C)]
45+
#[derive(Debug, Default)]
46+
pub struct test {
47+
pub a: ::std::os::raw::c_int,
48+
pub zero_length_array: __IncompleteArrayField<::std::os::raw::c_char>,
49+
}
50+
#[test]
51+
fn bindgen_test_layout_test() {
52+
assert_eq!(
53+
::std::mem::size_of::<test>(),
54+
4usize,
55+
concat!("Size of: ", stringify!(test))
56+
);
57+
assert_eq!(
58+
::std::mem::align_of::<test>(),
59+
4usize,
60+
concat!("Alignment of ", stringify!(test))
61+
);
62+
assert_eq!(
63+
unsafe { &(*(0 as *const test)).a as *const _ as usize },
64+
0usize,
65+
concat!(
66+
"Alignment of field: ",
67+
stringify!(test),
68+
"::",
69+
stringify!(a)
70+
)
71+
);
72+
assert_eq!(
73+
unsafe { &(*(0 as *const test)).zero_length_array as *const _ as usize },
74+
4usize,
75+
concat!(
76+
"Alignment of field: ",
77+
stringify!(test),
78+
"::",
79+
stringify!(zero_length_array)
80+
)
81+
);
82+
}
83+
#[repr(C)]
84+
#[derive(Debug, Default)]
85+
pub struct test2 {
86+
pub a: ::std::os::raw::c_int,
87+
pub incomplete_array: __IncompleteArrayField<::std::os::raw::c_char>,
88+
}
89+
#[test]
90+
fn bindgen_test_layout_test2() {
91+
assert_eq!(
92+
::std::mem::size_of::<test2>(),
93+
4usize,
94+
concat!("Size of: ", stringify!(test2))
95+
);
96+
assert_eq!(
97+
::std::mem::align_of::<test2>(),
98+
4usize,
99+
concat!("Alignment of ", stringify!(test2))
100+
);
101+
}
102+
#[repr(C)]
103+
#[derive(Debug, Default)]
104+
pub struct test3 {
105+
pub a: ::std::os::raw::c_int,
106+
pub zero_length_array: __IncompleteArrayField<::std::os::raw::c_char>,
107+
pub incomplete_array: __IncompleteArrayField<::std::os::raw::c_char>,
108+
}
109+
#[test]
110+
fn bindgen_test_layout_test3() {
111+
assert_eq!(
112+
::std::mem::size_of::<test3>(),
113+
4usize,
114+
concat!("Size of: ", stringify!(test3))
115+
);
116+
assert_eq!(
117+
::std::mem::align_of::<test3>(),
118+
4usize,
119+
concat!("Alignment of ", stringify!(test3))
120+
);
121+
}

0 commit comments

Comments
 (0)