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