]> git.lizzy.rs Git - rust.git/blob - src/test/ui/structs-enums/rec-align-u64.rs
Rollup merge of #105843 - compiler-errors:sugg-const, r=lcnr
[rust.git] / src / test / ui / structs-enums / rec-align-u64.rs
1 // run-pass
2 #![allow(dead_code)]
3 #![allow(unused_unsafe)]
4 // ignore-wasm32-bare seems unimportant to test
5
6 // Issue #2303
7
8 #![feature(intrinsics)]
9
10 use std::mem;
11
12 mod rusti {
13     extern "rust-intrinsic" {
14         pub fn pref_align_of<T>() -> usize;
15         #[rustc_safe_intrinsic]
16         pub fn min_align_of<T>() -> usize;
17     }
18 }
19
20 // This is the type with the questionable alignment
21 #[derive(Debug)]
22 struct Inner {
23     c64: u64
24 }
25
26 // This is the type that contains the type with the
27 // questionable alignment, for testing
28 #[derive(Debug)]
29 struct Outer {
30     c8: u8,
31     t: Inner
32 }
33
34
35 #[cfg(any(target_os = "android",
36           target_os = "dragonfly",
37           target_os = "emscripten",
38           target_os = "freebsd",
39           target_os = "fuchsia",
40           target_os = "illumos",
41           target_os = "linux",
42           target_os = "macos",
43           target_os = "netbsd",
44           target_os = "openbsd",
45           target_os = "solaris",
46           target_os = "vxworks"))]
47 mod m {
48     #[cfg(target_arch = "x86")]
49     pub mod m {
50         pub fn align() -> usize { 4 }
51         pub fn size() -> usize { 12 }
52     }
53
54     #[cfg(not(target_arch = "x86"))]
55     pub mod m {
56         pub fn align() -> usize { 8 }
57         pub fn size() -> usize { 16 }
58     }
59 }
60
61 #[cfg(target_env = "sgx")]
62 mod m {
63     #[cfg(target_arch = "x86_64")]
64     pub mod m {
65         pub fn align() -> usize { 8 }
66         pub fn size() -> usize { 16 }
67     }
68 }
69
70 #[cfg(target_os = "windows")]
71 mod m {
72     pub mod m {
73         pub fn align() -> usize { 8 }
74         pub fn size() -> usize { 16 }
75     }
76 }
77
78 pub fn main() {
79     unsafe {
80         let x = Outer {c8: 22, t: Inner {c64: 44}};
81
82         let y = format!("{:?}", x);
83
84         println!("align inner = {:?}", rusti::min_align_of::<Inner>());
85         println!("size outer = {:?}", mem::size_of::<Outer>());
86         println!("y = {:?}", y);
87
88         // per clang/gcc the alignment of `Inner` is 4 on x86.
89         assert_eq!(rusti::min_align_of::<Inner>(), m::m::align());
90
91         // per clang/gcc the size of `Outer` should be 12
92         // because `Inner`s alignment was 4.
93         assert_eq!(mem::size_of::<Outer>(), m::m::size());
94
95         assert_eq!(y, "Outer { c8: 22, t: Inner { c64: 44 } }".to_string());
96     }
97 }