]> git.lizzy.rs Git - rust.git/blob - src/test/ui/deriving/deriving-with-repr-packed.rs
:arrow_up: rust-analyzer
[rust.git] / src / test / ui / deriving / deriving-with-repr-packed.rs
1 // run-pass
2 // check that derive on a packed struct does not call field
3 // methods with a misaligned field.
4
5 use std::mem;
6
7 #[derive(Copy, Clone)]
8 struct Aligned(usize);
9
10 #[inline(never)]
11 fn check_align(ptr: *const Aligned) {
12     assert_eq!(ptr as usize % mem::align_of::<Aligned>(),
13                0);
14 }
15
16 impl PartialEq for Aligned {
17     fn eq(&self, other: &Self) -> bool {
18         check_align(self);
19         check_align(other);
20         self.0 == other.0
21     }
22 }
23
24 #[repr(packed)]
25 #[derive(Copy, Clone, PartialEq)]
26 struct Packed(Aligned, Aligned);
27
28 #[derive(PartialEq)]
29 #[repr(C)]
30 struct Dealigned<T>(u8, T);
31
32 fn main() {
33     let d1 = Dealigned(0, Packed(Aligned(1), Aligned(2)));
34     let ck = d1 == d1;
35     assert!(ck);
36 }