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