2018-08-30 12:18:55 +00:00
|
|
|
//@ run-pass
|
2018-09-25 21:51:35 +00:00
|
|
|
#![allow(dead_code)]
|
|
|
|
#![allow(unused_unsafe)]
|
2017-10-23 03:01:00 +00:00
|
|
|
|
2012-04-27 00:49:38 +00:00
|
|
|
// Issue #2303
|
|
|
|
|
2023-09-25 19:40:40 +00:00
|
|
|
#![feature(intrinsics, rustc_attrs)]
|
2014-06-20 23:39:23 +00:00
|
|
|
|
2013-10-17 01:34:01 +00:00
|
|
|
use std::mem;
|
2013-05-25 02:35:29 +00:00
|
|
|
|
2013-03-05 22:42:58 +00:00
|
|
|
mod rusti {
|
2013-07-19 02:08:57 +00:00
|
|
|
extern "rust-intrinsic" {
|
2015-03-26 00:06:52 +00:00
|
|
|
pub fn pref_align_of<T>() -> usize;
|
2022-08-18 09:15:26 +00:00
|
|
|
#[rustc_safe_intrinsic]
|
2015-03-26 00:06:52 +00:00
|
|
|
pub fn min_align_of<T>() -> usize;
|
2013-03-05 22:42:58 +00:00
|
|
|
}
|
2012-04-27 06:39:44 +00:00
|
|
|
}
|
|
|
|
|
2012-04-27 00:49:38 +00:00
|
|
|
// This is the type with the questionable alignment
|
2015-01-20 23:45:07 +00:00
|
|
|
#[derive(Debug)]
|
2013-01-26 06:46:32 +00:00
|
|
|
struct Inner {
|
2012-05-03 01:25:22 +00:00
|
|
|
c64: u64
|
2013-01-26 06:46:32 +00:00
|
|
|
}
|
2012-04-27 00:49:38 +00:00
|
|
|
|
|
|
|
// This is the type that contains the type with the
|
|
|
|
// questionable alignment, for testing
|
2015-01-20 23:45:07 +00:00
|
|
|
#[derive(Debug)]
|
2013-01-26 06:46:32 +00:00
|
|
|
struct Outer {
|
2012-04-27 00:49:38 +00:00
|
|
|
c8: u8,
|
2013-01-26 06:46:32 +00:00
|
|
|
t: Inner
|
|
|
|
}
|
2012-04-27 00:49:38 +00:00
|
|
|
|
2024-05-27 13:17:18 +00:00
|
|
|
#[cfg(any(
|
|
|
|
target_os = "android",
|
|
|
|
target_os = "dragonfly",
|
|
|
|
target_os = "freebsd",
|
|
|
|
target_os = "fuchsia",
|
|
|
|
target_os = "hurd",
|
|
|
|
target_os = "illumos",
|
|
|
|
target_os = "linux",
|
|
|
|
target_os = "netbsd",
|
|
|
|
target_os = "openbsd",
|
|
|
|
target_os = "solaris",
|
|
|
|
target_os = "vxworks",
|
|
|
|
target_os = "nto",
|
|
|
|
target_vendor = "apple",
|
2023-01-10 09:44:05 +00:00
|
|
|
))]
|
2012-04-30 23:44:34 +00:00
|
|
|
mod m {
|
|
|
|
#[cfg(target_arch = "x86")]
|
2013-01-30 23:56:40 +00:00
|
|
|
pub mod m {
|
2015-03-26 00:06:52 +00:00
|
|
|
pub fn align() -> usize { 4 }
|
|
|
|
pub fn size() -> usize { 12 }
|
2012-04-30 23:44:34 +00:00
|
|
|
}
|
|
|
|
|
2016-01-13 00:55:51 +00:00
|
|
|
#[cfg(not(target_arch = "x86"))]
|
2013-10-05 21:44:37 +00:00
|
|
|
pub mod m {
|
2015-03-26 00:06:52 +00:00
|
|
|
pub fn align() -> usize { 8 }
|
2019-04-24 16:26:33 +00:00
|
|
|
pub fn size() -> usize { 16 }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(target_env = "sgx")]
|
|
|
|
mod m {
|
|
|
|
#[cfg(target_arch = "x86_64")]
|
|
|
|
pub mod m {
|
|
|
|
pub fn align() -> usize { 8 }
|
2015-03-26 00:06:52 +00:00
|
|
|
pub fn size() -> usize { 16 }
|
2015-01-17 07:51:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-08-11 04:26:45 +00:00
|
|
|
#[cfg(target_os = "windows")]
|
2012-04-30 23:44:34 +00:00
|
|
|
mod m {
|
2014-08-01 08:51:08 +00:00
|
|
|
pub mod m {
|
2015-03-26 00:06:52 +00:00
|
|
|
pub fn align() -> usize { 8 }
|
|
|
|
pub fn size() -> usize { 16 }
|
2014-08-01 08:51:08 +00:00
|
|
|
}
|
2012-04-30 23:44:34 +00:00
|
|
|
}
|
|
|
|
|
2024-03-06 20:44:54 +00:00
|
|
|
#[cfg(target_family = "wasm")]
|
|
|
|
mod m {
|
|
|
|
pub mod m {
|
|
|
|
pub fn align() -> usize { 8 }
|
|
|
|
pub fn size() -> usize { 16 }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-02-02 03:43:17 +00:00
|
|
|
pub fn main() {
|
2013-01-24 00:29:31 +00:00
|
|
|
unsafe {
|
2015-03-03 08:42:26 +00:00
|
|
|
let x = Outer {c8: 22, t: Inner {c64: 44}};
|
2012-04-27 00:49:38 +00:00
|
|
|
|
2014-12-20 08:09:35 +00:00
|
|
|
let y = format!("{:?}", x);
|
2012-04-27 00:49:38 +00:00
|
|
|
|
2014-12-20 08:09:35 +00:00
|
|
|
println!("align inner = {:?}", rusti::min_align_of::<Inner>());
|
|
|
|
println!("size outer = {:?}", mem::size_of::<Outer>());
|
|
|
|
println!("y = {:?}", y);
|
2012-04-27 00:49:38 +00:00
|
|
|
|
2013-01-26 06:46:32 +00:00
|
|
|
// per clang/gcc the alignment of `Inner` is 4 on x86.
|
2013-05-19 02:02:45 +00:00
|
|
|
assert_eq!(rusti::min_align_of::<Inner>(), m::m::align());
|
2012-04-27 00:49:38 +00:00
|
|
|
|
2013-01-26 06:46:32 +00:00
|
|
|
// per clang/gcc the size of `Outer` should be 12
|
|
|
|
// because `Inner`s alignment was 4.
|
2013-10-17 01:34:01 +00:00
|
|
|
assert_eq!(mem::size_of::<Outer>(), m::m::size());
|
2012-04-27 00:49:38 +00:00
|
|
|
|
2015-01-20 23:45:07 +00:00
|
|
|
assert_eq!(y, "Outer { c8: 22, t: Inner { c64: 44 } }".to_string());
|
2013-01-24 00:29:31 +00:00
|
|
|
}
|
2012-04-27 00:49:38 +00:00
|
|
|
}
|