]>
Commit | Line | Data |
---|---|---|
1a4d82fc | 1 | // Copyright 2012-2015 The Rust Project Developers. See the COPYRIGHT |
223e47cc LB |
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 | ||
1a4d82fc JJ |
13 | #![feature(intrinsics)] |
14 | ||
15 | use std::mem; | |
970d7e83 | 16 | |
223e47cc | 17 | mod rusti { |
1a4d82fc | 18 | extern "rust-intrinsic" { |
c34b1796 AL |
19 | pub fn pref_align_of<T>() -> usize; |
20 | pub fn min_align_of<T>() -> usize; | |
223e47cc LB |
21 | } |
22 | } | |
23 | ||
24 | // This is the type with the questionable alignment | |
85aaf69f | 25 | #[derive(Debug)] |
223e47cc LB |
26 | struct Inner { |
27 | c64: u64 | |
28 | } | |
29 | ||
30 | // This is the type that contains the type with the | |
31 | // questionable alignment, for testing | |
85aaf69f | 32 | #[derive(Debug)] |
223e47cc LB |
33 | struct Outer { |
34 | c8: u8, | |
35 | t: Inner | |
36 | } | |
37 | ||
38 | ||
1a4d82fc JJ |
39 | #[cfg(any(target_os = "linux", |
40 | target_os = "macos", | |
41 | target_os = "freebsd", | |
85aaf69f | 42 | target_os = "dragonfly", |
c1a9b12d | 43 | target_os = "netbsd", |
7453a54e | 44 | target_os = "openbsd", |
5bcae85e SL |
45 | target_os = "solaris", |
46 | target_os = "emscripten"))] | |
223e47cc LB |
47 | mod m { |
48 | #[cfg(target_arch = "x86")] | |
49 | pub mod m { | |
c34b1796 AL |
50 | pub fn align() -> usize { 4 } |
51 | pub fn size() -> usize { 12 } | |
223e47cc LB |
52 | } |
53 | ||
9cc50fc6 | 54 | #[cfg(not(target_arch = "x86"))] |
1a4d82fc | 55 | pub mod m { |
c34b1796 AL |
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 } | |
223e47cc LB |
67 | } |
68 | } | |
69 | ||
1a4d82fc | 70 | #[cfg(target_os = "windows")] |
223e47cc LB |
71 | mod m { |
72 | #[cfg(target_arch = "x86")] | |
73 | pub mod m { | |
c34b1796 AL |
74 | pub fn align() -> usize { 8 } |
75 | pub fn size() -> usize { 16 } | |
223e47cc | 76 | } |
1a4d82fc JJ |
77 | |
78 | #[cfg(target_arch = "x86_64")] | |
79 | pub mod m { | |
c34b1796 AL |
80 | pub fn align() -> usize { 8 } |
81 | pub fn size() -> usize { 16 } | |
1a4d82fc | 82 | } |
223e47cc LB |
83 | } |
84 | ||
970d7e83 LB |
85 | #[cfg(target_os = "android")] |
86 | mod m { | |
85aaf69f | 87 | #[cfg(any(target_arch = "arm", target_arch = "aarch64"))] |
970d7e83 | 88 | pub mod m { |
c34b1796 AL |
89 | pub fn align() -> usize { 8 } |
90 | pub fn size() -> usize { 16 } | |
970d7e83 LB |
91 | } |
92 | } | |
93 | ||
223e47cc LB |
94 | pub fn main() { |
95 | unsafe { | |
c34b1796 | 96 | let x = Outer {c8: 22, t: Inner {c64: 44}}; |
223e47cc | 97 | |
1a4d82fc | 98 | let y = format!("{:?}", x); |
223e47cc | 99 | |
1a4d82fc JJ |
100 | println!("align inner = {:?}", rusti::min_align_of::<Inner>()); |
101 | println!("size outer = {:?}", mem::size_of::<Outer>()); | |
102 | println!("y = {:?}", y); | |
223e47cc LB |
103 | |
104 | // per clang/gcc the alignment of `Inner` is 4 on x86. | |
970d7e83 | 105 | assert_eq!(rusti::min_align_of::<Inner>(), m::m::align()); |
223e47cc LB |
106 | |
107 | // per clang/gcc the size of `Outer` should be 12 | |
108 | // because `Inner`s alignment was 4. | |
1a4d82fc | 109 | assert_eq!(mem::size_of::<Outer>(), m::m::size()); |
223e47cc | 110 | |
85aaf69f | 111 | assert_eq!(y, "Outer { c8: 22, t: Inner { c64: 44 } }".to_string()); |
223e47cc LB |
112 | } |
113 | } |