]> git.proxmox.com Git - rustc.git/blob - src/test/run-pass/rec-align-u64.rs
New upstream version 1.12.0+dfsg1
[rustc.git] / src / test / run-pass / rec-align-u64.rs
1 // Copyright 2012-2015 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
4 //
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
10
11 // Issue #2303
12
13 #![feature(intrinsics)]
14
15 use std::mem;
16
17 mod rusti {
18 extern "rust-intrinsic" {
19 pub fn pref_align_of<T>() -> usize;
20 pub fn min_align_of<T>() -> usize;
21 }
22 }
23
24 // This is the type with the questionable alignment
25 #[derive(Debug)]
26 struct Inner {
27 c64: u64
28 }
29
30 // This is the type that contains the type with the
31 // questionable alignment, for testing
32 #[derive(Debug)]
33 struct Outer {
34 c8: u8,
35 t: Inner
36 }
37
38
39 #[cfg(any(target_os = "linux",
40 target_os = "macos",
41 target_os = "freebsd",
42 target_os = "dragonfly",
43 target_os = "netbsd",
44 target_os = "openbsd",
45 target_os = "solaris",
46 target_os = "emscripten"))]
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_os = "bitrig")]
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 #[cfg(target_arch = "x86")]
73 pub mod m {
74 pub fn align() -> usize { 8 }
75 pub fn size() -> usize { 16 }
76 }
77
78 #[cfg(target_arch = "x86_64")]
79 pub mod m {
80 pub fn align() -> usize { 8 }
81 pub fn size() -> usize { 16 }
82 }
83 }
84
85 #[cfg(target_os = "android")]
86 mod m {
87 #[cfg(any(target_arch = "arm", target_arch = "aarch64"))]
88 pub mod m {
89 pub fn align() -> usize { 8 }
90 pub fn size() -> usize { 16 }
91 }
92 }
93
94 pub fn main() {
95 unsafe {
96 let x = Outer {c8: 22, t: Inner {c64: 44}};
97
98 let y = format!("{:?}", x);
99
100 println!("align inner = {:?}", rusti::min_align_of::<Inner>());
101 println!("size outer = {:?}", mem::size_of::<Outer>());
102 println!("y = {:?}", y);
103
104 // per clang/gcc the alignment of `Inner` is 4 on x86.
105 assert_eq!(rusti::min_align_of::<Inner>(), m::m::align());
106
107 // per clang/gcc the size of `Outer` should be 12
108 // because `Inner`s alignment was 4.
109 assert_eq!(mem::size_of::<Outer>(), m::m::size());
110
111 assert_eq!(y, "Outer { c8: 22, t: Inner { c64: 44 } }".to_string());
112 }
113 }