]> git.proxmox.com Git - rustc.git/blame - src/test/run-pass/rec-align-u64.rs
Imported Upstream version 1.2.0+dfsg1
[rustc.git] / src / test / run-pass / rec-align-u64.rs
CommitLineData
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
15use std::mem;
970d7e83 16
223e47cc 17mod 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
26struct 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
33struct 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
SL
42 target_os = "dragonfly",
43 target_os = "openbsd"))]
223e47cc
LB
44mod m {
45 #[cfg(target_arch = "x86")]
46 pub mod m {
c34b1796
AL
47 pub fn align() -> usize { 4 }
48 pub fn size() -> usize { 12 }
223e47cc
LB
49 }
50
1a4d82fc
JJ
51 #[cfg(any(target_arch = "x86_64", target_arch = "arm", target_arch = "aarch64"))]
52 pub mod m {
c34b1796
AL
53 pub fn align() -> usize { 8 }
54 pub fn size() -> usize { 16 }
55 }
56}
57
58#[cfg(target_os = "bitrig")]
59mod m {
60 #[cfg(target_arch = "x86_64")]
61 pub mod m {
62 pub fn align() -> usize { 8 }
63 pub fn size() -> usize { 16 }
223e47cc
LB
64 }
65}
66
1a4d82fc 67#[cfg(target_os = "windows")]
223e47cc
LB
68mod m {
69 #[cfg(target_arch = "x86")]
70 pub mod m {
c34b1796
AL
71 pub fn align() -> usize { 8 }
72 pub fn size() -> usize { 16 }
223e47cc 73 }
1a4d82fc
JJ
74
75 #[cfg(target_arch = "x86_64")]
76 pub mod m {
c34b1796
AL
77 pub fn align() -> usize { 8 }
78 pub fn size() -> usize { 16 }
1a4d82fc 79 }
223e47cc
LB
80}
81
970d7e83
LB
82#[cfg(target_os = "android")]
83mod m {
85aaf69f 84 #[cfg(any(target_arch = "arm", target_arch = "aarch64"))]
970d7e83 85 pub mod m {
c34b1796
AL
86 pub fn align() -> usize { 8 }
87 pub fn size() -> usize { 16 }
970d7e83
LB
88 }
89}
90
223e47cc
LB
91pub fn main() {
92 unsafe {
c34b1796 93 let x = Outer {c8: 22, t: Inner {c64: 44}};
223e47cc 94
1a4d82fc 95 let y = format!("{:?}", x);
223e47cc 96
1a4d82fc
JJ
97 println!("align inner = {:?}", rusti::min_align_of::<Inner>());
98 println!("size outer = {:?}", mem::size_of::<Outer>());
99 println!("y = {:?}", y);
223e47cc
LB
100
101 // per clang/gcc the alignment of `Inner` is 4 on x86.
970d7e83 102 assert_eq!(rusti::min_align_of::<Inner>(), m::m::align());
223e47cc
LB
103
104 // per clang/gcc the size of `Outer` should be 12
105 // because `Inner`s alignment was 4.
1a4d82fc 106 assert_eq!(mem::size_of::<Outer>(), m::m::size());
223e47cc 107
85aaf69f 108 assert_eq!(y, "Outer { c8: 22, t: Inner { c64: 44 } }".to_string());
223e47cc
LB
109 }
110}