From 9be35f82c1abf2ecbab489bca9eca138ea648312 Mon Sep 17 00:00:00 2001 From: Vadim Petrochenkov Date: Sat, 27 Jul 2019 01:33:01 +0300 Subject: tests: Move run-pass tests without naming conflicts to ui --- src/test/ui/structs-enums/enum-null-pointer-opt.rs | 74 ++++++++++++++++++++++ 1 file changed, 74 insertions(+) create mode 100644 src/test/ui/structs-enums/enum-null-pointer-opt.rs (limited to 'src/test/ui/structs-enums/enum-null-pointer-opt.rs') diff --git a/src/test/ui/structs-enums/enum-null-pointer-opt.rs b/src/test/ui/structs-enums/enum-null-pointer-opt.rs new file mode 100644 index 00000000000..32fdbf620a9 --- /dev/null +++ b/src/test/ui/structs-enums/enum-null-pointer-opt.rs @@ -0,0 +1,74 @@ +// run-pass +#![feature(transparent_unions)] + +use std::mem::size_of; +use std::num::NonZeroUsize; +use std::ptr::NonNull; +use std::rc::Rc; +use std::sync::Arc; + +trait Trait { fn dummy(&self) { } } +trait Mirror { type Image; } +impl Mirror for T { type Image = T; } +struct ParamTypeStruct(T); +struct AssocTypeStruct(::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(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::>>()); +} -- cgit 1.4.1-3-g733a5