diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-17 12:02:58 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-17 12:02:58 +0000 |
commit | 698f8c2f01ea549d77d7dc3338a12e04c11057b9 (patch) | |
tree | 173a775858bd501c378080a10dca74132f05bc50 /src/test/ui/structs-enums/enum-null-pointer-opt.rs | |
parent | Initial commit. (diff) | |
download | rustc-698f8c2f01ea549d77d7dc3338a12e04c11057b9.tar.xz rustc-698f8c2f01ea549d77d7dc3338a12e04c11057b9.zip |
Adding upstream version 1.64.0+dfsg1.upstream/1.64.0+dfsg1
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'src/test/ui/structs-enums/enum-null-pointer-opt.rs')
-rw-r--r-- | src/test/ui/structs-enums/enum-null-pointer-opt.rs | 74 |
1 files changed, 74 insertions, 0 deletions
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 000000000..85fa1eac2 --- /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<T> Mirror for T { type Image = T; } +struct ParamTypeStruct<T>(#[allow(unused_tuple_struct_fields)] T); +struct AssocTypeStruct<T>(#[allow(unused_tuple_struct_fields)] <T as Mirror>::Image); +#[repr(transparent)] +union MaybeUninitUnion<T: Copy> { + _value: T, + _uninit: (), +} + +fn main() { + // Functions + assert_eq!(size_of::<fn(isize)>(), size_of::<Option<fn(isize)>>()); + assert_eq!(size_of::<extern "C" fn(isize)>(), size_of::<Option<extern "C" fn(isize)>>()); + + // Slices - &str / &[T] / &mut [T] + assert_eq!(size_of::<&str>(), size_of::<Option<&str>>()); + assert_eq!(size_of::<&[isize]>(), size_of::<Option<&[isize]>>()); + assert_eq!(size_of::<&mut [isize]>(), size_of::<Option<&mut [isize]>>()); + + // Traits - Box<Trait> / &Trait / &mut Trait + assert_eq!(size_of::<Box<dyn Trait>>(), size_of::<Option<Box<dyn Trait>>>()); + assert_eq!(size_of::<&dyn Trait>(), size_of::<Option<&dyn Trait>>()); + assert_eq!(size_of::<&mut dyn Trait>(), size_of::<Option<&mut dyn Trait>>()); + + // Pointers - Box<T> + assert_eq!(size_of::<Box<isize>>(), size_of::<Option<Box<isize>>>()); + + // The optimization can't apply to raw pointers or unions with a ZST field. + assert!(size_of::<Option<*const isize>>() != size_of::<*const isize>()); + assert!(Some(std::ptr::null::<isize>()).is_some()); // Can't collapse None to null + assert_ne!(size_of::<fn(isize)>(), size_of::<Option<MaybeUninitUnion<fn(isize)>>>()); + assert_ne!(size_of::<&str>(), size_of::<Option<MaybeUninitUnion<&str>>>()); + assert_ne!(size_of::<NonNull<isize>>(), size_of::<Option<MaybeUninitUnion<NonNull<isize>>>>()); + + struct Foo { + _a: Box<isize> + } + struct Bar(#[allow(unused_tuple_struct_fields)] Box<isize>); + + // Should apply through structs + assert_eq!(size_of::<Foo>(), size_of::<Option<Foo>>()); + assert_eq!(size_of::<Bar>(), size_of::<Option<Bar>>()); + // and tuples + assert_eq!(size_of::<(u8, Box<isize>)>(), size_of::<Option<(u8, Box<isize>)>>()); + // and fixed-size arrays + assert_eq!(size_of::<[Box<isize>; 1]>(), size_of::<Option<[Box<isize>; 1]>>()); + + // Should apply to NonZero + assert_eq!(size_of::<NonZeroUsize>(), size_of::<Option<NonZeroUsize>>()); + assert_eq!(size_of::<NonNull<i8>>(), size_of::<Option<NonNull<i8>>>()); + + // Should apply to types that use NonZero internally + assert_eq!(size_of::<Vec<isize>>(), size_of::<Option<Vec<isize>>>()); + assert_eq!(size_of::<Arc<isize>>(), size_of::<Option<Arc<isize>>>()); + assert_eq!(size_of::<Rc<isize>>(), size_of::<Option<Rc<isize>>>()); + + // Should apply to types that have NonZero transitively + assert_eq!(size_of::<String>(), size_of::<Option<String>>()); + + // Should apply to types where the pointer is substituted + assert_eq!(size_of::<&u8>(), size_of::<Option<ParamTypeStruct<&u8>>>()); + assert_eq!(size_of::<&u8>(), size_of::<Option<AssocTypeStruct<&u8>>>()); +} |