2014-12-12 17:39:27 -06:00
|
|
|
// Copyright 2012-2015 The Rust Project Developers. See the COPYRIGHT
|
2012-12-10 19:32:48 -06:00
|
|
|
// file at the top-level directory of this distribution and at
|
|
|
|
// http://rust-lang.org/COPYRIGHT.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
|
|
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
|
|
|
// option. This file may not be copied, modified, or distributed
|
|
|
|
// except according to those terms.
|
|
|
|
|
2012-04-26 19:49:38 -05:00
|
|
|
// Issue #2303
|
|
|
|
|
2014-06-20 18:39:23 -05:00
|
|
|
#![feature(intrinsics)]
|
|
|
|
|
2013-10-16 20:34:01 -05:00
|
|
|
use std::mem;
|
2013-05-24 21:35:29 -05:00
|
|
|
|
2013-03-05 16:42:58 -06:00
|
|
|
mod rusti {
|
2013-07-18 21:08:57 -05:00
|
|
|
extern "rust-intrinsic" {
|
2015-03-25 19:06:52 -05:00
|
|
|
pub fn pref_align_of<T>() -> usize;
|
|
|
|
pub fn min_align_of<T>() -> usize;
|
2013-03-05 16:42:58 -06:00
|
|
|
}
|
2012-04-27 01:39:44 -05:00
|
|
|
}
|
|
|
|
|
2012-04-26 19:49:38 -05:00
|
|
|
// This is the type with the questionable alignment
|
2015-01-20 17:45:07 -06:00
|
|
|
#[derive(Debug)]
|
2013-01-26 00:46:32 -06:00
|
|
|
struct Inner {
|
2012-05-02 20:25:22 -05:00
|
|
|
c64: u64
|
2013-01-26 00:46:32 -06:00
|
|
|
}
|
2012-04-26 19:49:38 -05:00
|
|
|
|
|
|
|
// This is the type that contains the type with the
|
|
|
|
// questionable alignment, for testing
|
2015-01-20 17:45:07 -06:00
|
|
|
#[derive(Debug)]
|
2013-01-26 00:46:32 -06:00
|
|
|
struct Outer {
|
2012-04-26 19:49:38 -05:00
|
|
|
c8: u8,
|
2013-01-26 00:46:32 -06:00
|
|
|
t: Inner
|
|
|
|
}
|
2012-04-26 19:49:38 -05:00
|
|
|
|
2012-04-30 18:44:34 -05:00
|
|
|
|
2014-10-11 20:05:54 -05:00
|
|
|
#[cfg(any(target_os = "linux",
|
|
|
|
target_os = "macos",
|
|
|
|
target_os = "freebsd",
|
2015-01-29 01:19:28 -06:00
|
|
|
target_os = "dragonfly",
|
2015-06-30 22:37:11 -05:00
|
|
|
target_os = "netbsd",
|
2015-01-29 01:19:28 -06:00
|
|
|
target_os = "openbsd"))]
|
2012-04-30 18:44:34 -05:00
|
|
|
mod m {
|
|
|
|
#[cfg(target_arch = "x86")]
|
2013-01-30 17:56:40 -06:00
|
|
|
pub mod m {
|
2015-03-25 19:06:52 -05:00
|
|
|
pub fn align() -> usize { 4 }
|
|
|
|
pub fn size() -> usize { 12 }
|
2012-04-30 18:44:34 -05:00
|
|
|
}
|
|
|
|
|
2015-12-28 15:09:06 -06:00
|
|
|
#[cfg(any(target_arch = "x86_64", target_arch = "arm",
|
|
|
|
target_arch = "aarch64", target_arch = "powerpc64",
|
|
|
|
target_arch = "powerpc64le"))]
|
2013-10-05 16:44:37 -05:00
|
|
|
pub mod m {
|
2015-03-25 19:06:52 -05:00
|
|
|
pub fn align() -> usize { 8 }
|
|
|
|
pub fn size() -> usize { 16 }
|
2012-04-30 18:44:34 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-17 01:51:04 -06:00
|
|
|
#[cfg(target_os = "bitrig")]
|
|
|
|
mod m {
|
|
|
|
#[cfg(target_arch = "x86_64")]
|
|
|
|
pub mod m {
|
2015-03-25 19:06:52 -05:00
|
|
|
pub fn align() -> usize { 8 }
|
|
|
|
pub fn size() -> usize { 16 }
|
2015-01-17 01:51:04 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-08-10 23:26:45 -05:00
|
|
|
#[cfg(target_os = "windows")]
|
2012-04-30 18:44:34 -05:00
|
|
|
mod m {
|
|
|
|
#[cfg(target_arch = "x86")]
|
2013-01-30 17:56:40 -06:00
|
|
|
pub mod m {
|
2015-03-25 19:06:52 -05:00
|
|
|
pub fn align() -> usize { 8 }
|
|
|
|
pub fn size() -> usize { 16 }
|
2012-04-30 18:44:34 -05:00
|
|
|
}
|
2014-08-01 03:51:08 -05:00
|
|
|
|
|
|
|
#[cfg(target_arch = "x86_64")]
|
|
|
|
pub mod m {
|
2015-03-25 19:06:52 -05:00
|
|
|
pub fn align() -> usize { 8 }
|
|
|
|
pub fn size() -> usize { 16 }
|
2014-08-01 03:51:08 -05:00
|
|
|
}
|
2012-04-30 18:44:34 -05:00
|
|
|
}
|
|
|
|
|
2013-04-22 21:31:54 -05:00
|
|
|
#[cfg(target_os = "android")]
|
|
|
|
mod m {
|
2015-01-25 23:46:21 -06:00
|
|
|
#[cfg(any(target_arch = "arm", target_arch = "aarch64"))]
|
2013-04-22 21:31:54 -05:00
|
|
|
pub mod m {
|
2015-03-25 19:06:52 -05:00
|
|
|
pub fn align() -> usize { 8 }
|
|
|
|
pub fn size() -> usize { 16 }
|
2013-04-22 21:31:54 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-02-01 21:43:17 -06:00
|
|
|
pub fn main() {
|
2013-01-23 18:29:31 -06:00
|
|
|
unsafe {
|
2015-03-03 02:42:26 -06:00
|
|
|
let x = Outer {c8: 22, t: Inner {c64: 44}};
|
2012-04-26 19:49:38 -05:00
|
|
|
|
2014-12-20 02:09:35 -06:00
|
|
|
let y = format!("{:?}", x);
|
2012-04-26 19:49:38 -05:00
|
|
|
|
2014-12-20 02:09:35 -06:00
|
|
|
println!("align inner = {:?}", rusti::min_align_of::<Inner>());
|
|
|
|
println!("size outer = {:?}", mem::size_of::<Outer>());
|
|
|
|
println!("y = {:?}", y);
|
2012-04-26 19:49:38 -05:00
|
|
|
|
2013-01-26 00:46:32 -06:00
|
|
|
// per clang/gcc the alignment of `Inner` is 4 on x86.
|
2013-05-18 21:02:45 -05:00
|
|
|
assert_eq!(rusti::min_align_of::<Inner>(), m::m::align());
|
2012-04-26 19:49:38 -05:00
|
|
|
|
2013-01-26 00:46:32 -06:00
|
|
|
// per clang/gcc the size of `Outer` should be 12
|
|
|
|
// because `Inner`s alignment was 4.
|
2013-10-16 20:34:01 -05:00
|
|
|
assert_eq!(mem::size_of::<Outer>(), m::m::size());
|
2012-04-26 19:49:38 -05:00
|
|
|
|
2015-01-20 17:45:07 -06:00
|
|
|
assert_eq!(y, "Outer { c8: 22, t: Inner { c64: 44 } }".to_string());
|
2013-01-23 18:29:31 -06:00
|
|
|
}
|
2012-04-26 19:49:38 -05:00
|
|
|
}
|