]>
Commit | Line | Data |
---|---|---|
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 | // ignore-wasm32-bare seems unimportant to test | |
12 | ||
13 | // Issue #2303 | |
14 | ||
15 | #![feature(intrinsics)] | |
16 | ||
17 | use std::mem; | |
18 | ||
19 | mod rusti { | |
20 | extern "rust-intrinsic" { | |
21 | pub fn pref_align_of<T>() -> usize; | |
22 | pub fn min_align_of<T>() -> usize; | |
23 | } | |
24 | } | |
25 | ||
26 | // This is the type with the questionable alignment | |
27 | #[derive(Debug)] | |
28 | struct Inner { | |
29 | c64: u64 | |
30 | } | |
31 | ||
32 | // This is the type that contains the type with the | |
33 | // questionable alignment, for testing | |
34 | #[derive(Debug)] | |
35 | struct Outer { | |
36 | c8: u8, | |
37 | t: Inner | |
38 | } | |
39 | ||
40 | ||
41 | #[cfg(any(target_os = "linux", | |
42 | target_os = "macos", | |
43 | target_os = "freebsd", | |
44 | target_os = "dragonfly", | |
45 | target_os = "netbsd", | |
46 | target_os = "openbsd", | |
47 | target_os = "solaris", | |
48 | target_os = "emscripten"))] | |
49 | mod m { | |
50 | #[cfg(target_arch = "x86")] | |
51 | pub mod m { | |
52 | pub fn align() -> usize { 4 } | |
53 | pub fn size() -> usize { 12 } | |
54 | } | |
55 | ||
56 | #[cfg(not(target_arch = "x86"))] | |
57 | pub mod m { | |
58 | pub fn align() -> usize { 8 } | |
59 | pub fn size() -> usize { 16 } | |
60 | } | |
61 | } | |
62 | ||
63 | #[cfg(target_os = "bitrig")] | |
64 | mod m { | |
65 | #[cfg(target_arch = "x86_64")] | |
66 | pub mod m { | |
67 | pub fn align() -> usize { 8 } | |
68 | pub fn size() -> usize { 16 } | |
69 | } | |
70 | } | |
71 | ||
72 | #[cfg(target_os = "windows")] | |
73 | mod m { | |
74 | #[cfg(target_arch = "x86")] | |
75 | pub mod m { | |
76 | pub fn align() -> usize { 8 } | |
77 | pub fn size() -> usize { 16 } | |
78 | } | |
79 | ||
80 | #[cfg(target_arch = "x86_64")] | |
81 | pub mod m { | |
82 | pub fn align() -> usize { 8 } | |
83 | pub fn size() -> usize { 16 } | |
84 | } | |
85 | } | |
86 | ||
87 | #[cfg(target_os = "android")] | |
88 | mod m { | |
89 | #[cfg(any(target_arch = "arm", target_arch = "aarch64"))] | |
90 | pub mod m { | |
91 | pub fn align() -> usize { 8 } | |
92 | pub fn size() -> usize { 16 } | |
93 | } | |
94 | } | |
95 | ||
96 | pub fn main() { | |
97 | unsafe { | |
98 | let x = Outer {c8: 22, t: Inner {c64: 44}}; | |
99 | ||
100 | let y = format!("{:?}", x); | |
101 | ||
102 | println!("align inner = {:?}", rusti::min_align_of::<Inner>()); | |
103 | println!("size outer = {:?}", mem::size_of::<Outer>()); | |
104 | println!("y = {:?}", y); | |
105 | ||
106 | // per clang/gcc the alignment of `Inner` is 4 on x86. | |
107 | assert_eq!(rusti::min_align_of::<Inner>(), m::m::align()); | |
108 | ||
109 | // per clang/gcc the size of `Outer` should be 12 | |
110 | // because `Inner`s alignment was 4. | |
111 | assert_eq!(mem::size_of::<Outer>(), m::m::size()); | |
112 | ||
113 | assert_eq!(y, "Outer { c8: 22, t: Inner { c64: 44 } }".to_string()); | |
114 | } | |
115 | } |