//@ run-pass #![feature(generic_nonzero)] #![feature(transparent_unions)] use std::mem::size_of; use std::num::NonZero; use std::ptr::NonNull; use std::rc::Rc; use std::sync::Arc; trait Trait { fn dummy(&self) { } } //~ WARN method `dummy` is never used trait Mirror { type Image; } impl Mirror for T { type Image = T; } struct ParamTypeStruct(#[allow(dead_code)] T); struct AssocTypeStruct(#[allow(dead_code)] ::Image); #[repr(transparent)] union MaybeUninitUnion { _value: T, _uninit: (), } fn main() { // Functions assert_eq!(size_of::(), size_of::>()); assert_eq!(size_of::(), size_of::>()); // Slices - &str / &[T] / &mut [T] assert_eq!(size_of::<&str>(), size_of::>()); assert_eq!(size_of::<&[isize]>(), size_of::>()); assert_eq!(size_of::<&mut [isize]>(), size_of::>()); // Traits - Box / &Trait / &mut Trait assert_eq!(size_of::>(), size_of::>>()); assert_eq!(size_of::<&dyn Trait>(), size_of::>()); assert_eq!(size_of::<&mut dyn Trait>(), size_of::>()); // Pointers - Box assert_eq!(size_of::>(), size_of::>>()); // The optimization can't apply to raw pointers or unions with a ZST field. assert!(size_of::>() != size_of::<*const isize>()); assert!(Some(std::ptr::null::()).is_some()); // Can't collapse None to null assert_ne!(size_of::(), size_of::>>()); assert_ne!(size_of::<&str>(), size_of::>>()); assert_ne!(size_of::>(), size_of::>>>()); struct Foo { _a: Box } struct Bar(#[allow(dead_code)] Box); // Should apply through structs assert_eq!(size_of::(), size_of::>()); assert_eq!(size_of::(), size_of::>()); // and tuples assert_eq!(size_of::<(u8, Box)>(), size_of::)>>()); // and fixed-size arrays assert_eq!(size_of::<[Box; 1]>(), size_of::; 1]>>()); // Should apply to NonZero assert_eq!(size_of::>(), size_of::>>()); assert_eq!(size_of::>(), size_of::>>()); // Should apply to types that use NonZero internally assert_eq!(size_of::>(), size_of::>>()); assert_eq!(size_of::>(), size_of::>>()); assert_eq!(size_of::>(), size_of::>>()); // Should apply to types that have NonZero transitively assert_eq!(size_of::(), size_of::>()); // Should apply to types where the pointer is substituted assert_eq!(size_of::<&u8>(), size_of::>>()); assert_eq!(size_of::<&u8>(), size_of::>>()); }