2025-07-01 00:43:31 +05:00
|
|
|
//! null pointer optimization preserves type sizes.
|
|
|
|
|
//!
|
|
|
|
|
//! Verifies that Option<T> has the same size as T for non-null pointer types,
|
|
|
|
|
//! and for custom enums that have a niche.
|
|
|
|
|
|
2024-02-16 20:02:50 +00:00
|
|
|
//@ run-pass
|
2019-07-27 00:54:25 +03:00
|
|
|
|
2025-07-01 00:43:31 +05:00
|
|
|
// Needs for Nothing variat in Enum
|
2018-09-14 12:20:28 +02:00
|
|
|
#![allow(dead_code)]
|
2015-03-22 13:13:15 -07:00
|
|
|
|
2013-10-16 18:34:01 -07:00
|
|
|
use std::mem;
|
2013-05-24 19:35:29 -07:00
|
|
|
|
2025-07-01 00:43:31 +05:00
|
|
|
enum E<T> {
|
|
|
|
|
Thing(isize, T),
|
|
|
|
|
Nothing((), ((), ()), [i8; 0]),
|
|
|
|
|
}
|
|
|
|
|
|
2015-03-25 17:06:52 -07:00
|
|
|
struct S<T>(isize, T);
|
2013-04-22 08:49:21 -07:00
|
|
|
|
|
|
|
|
// These are macros so we get useful assert messages.
|
|
|
|
|
|
|
|
|
|
macro_rules! check_option {
|
|
|
|
|
($T:ty) => {
|
2013-10-16 18:34:01 -07:00
|
|
|
assert_eq!(mem::size_of::<Option<$T>>(), mem::size_of::<$T>());
|
2025-07-01 00:43:31 +05:00
|
|
|
};
|
2013-04-22 08:49:21 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
macro_rules! check_fancy {
|
|
|
|
|
($T:ty) => {
|
2013-10-16 18:34:01 -07:00
|
|
|
assert_eq!(mem::size_of::<E<$T>>(), mem::size_of::<S<$T>>());
|
2025-07-01 00:43:31 +05:00
|
|
|
};
|
2013-04-22 08:49:21 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
macro_rules! check_type {
|
|
|
|
|
($T:ty) => {{
|
|
|
|
|
check_option!($T);
|
|
|
|
|
check_fancy!($T);
|
2025-07-01 00:43:31 +05:00
|
|
|
}};
|
2013-04-22 08:49:21 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn main() {
|
2015-03-25 17:06:52 -07:00
|
|
|
check_type!(&'static isize);
|
|
|
|
|
check_type!(Box<isize>);
|
2020-09-01 17:28:11 -04:00
|
|
|
check_type!(extern "C" fn());
|
2013-04-22 08:49:21 -07:00
|
|
|
}
|