]> git.proxmox.com Git - rustc.git/blame - src/test/run-pass/structs-enums/enum-null-pointer-opt.rs
New upstream version 1.36.0+dfsg1
[rustc.git] / src / test / run-pass / structs-enums / enum-null-pointer-opt.rs
CommitLineData
b7449926 1// run-pass
1a4d82fc 2use std::mem::size_of;
0531ce1d
XL
3use std::num::NonZeroUsize;
4use std::ptr::NonNull;
1a4d82fc
JJ
5use std::rc::Rc;
6use std::sync::Arc;
7
85aaf69f 8trait Trait { fn dummy(&self) { } }
e9174d1e
SL
9trait Mirror { type Image; }
10impl<T> Mirror for T { type Image = T; }
11struct ParamTypeStruct<T>(T);
12struct AssocTypeStruct<T>(<T as Mirror>::Image);
1a4d82fc
JJ
13
14fn main() {
15 // Functions
c34b1796
AL
16 assert_eq!(size_of::<fn(isize)>(), size_of::<Option<fn(isize)>>());
17 assert_eq!(size_of::<extern "C" fn(isize)>(), size_of::<Option<extern "C" fn(isize)>>());
1a4d82fc
JJ
18
19 // Slices - &str / &[T] / &mut [T]
20 assert_eq!(size_of::<&str>(), size_of::<Option<&str>>());
c34b1796
AL
21 assert_eq!(size_of::<&[isize]>(), size_of::<Option<&[isize]>>());
22 assert_eq!(size_of::<&mut [isize]>(), size_of::<Option<&mut [isize]>>());
1a4d82fc
JJ
23
24 // Traits - Box<Trait> / &Trait / &mut Trait
25 assert_eq!(size_of::<Box<Trait>>(), size_of::<Option<Box<Trait>>>());
26 assert_eq!(size_of::<&Trait>(), size_of::<Option<&Trait>>());
27 assert_eq!(size_of::<&mut Trait>(), size_of::<Option<&mut Trait>>());
28
29 // Pointers - Box<T>
c34b1796 30 assert_eq!(size_of::<Box<isize>>(), size_of::<Option<Box<isize>>>());
1a4d82fc
JJ
31
32 // The optimization can't apply to raw pointers
c34b1796
AL
33 assert!(size_of::<Option<*const isize>>() != size_of::<*const isize>());
34 assert!(Some(0 as *const isize).is_some()); // Can't collapse None to null
1a4d82fc
JJ
35
36 struct Foo {
c34b1796 37 _a: Box<isize>
1a4d82fc 38 }
c34b1796 39 struct Bar(Box<isize>);
1a4d82fc
JJ
40
41 // Should apply through structs
42 assert_eq!(size_of::<Foo>(), size_of::<Option<Foo>>());
43 assert_eq!(size_of::<Bar>(), size_of::<Option<Bar>>());
44 // and tuples
c34b1796 45 assert_eq!(size_of::<(u8, Box<isize>)>(), size_of::<Option<(u8, Box<isize>)>>());
1a4d82fc 46 // and fixed-size arrays
c34b1796 47 assert_eq!(size_of::<[Box<isize>; 1]>(), size_of::<Option<[Box<isize>; 1]>>());
1a4d82fc
JJ
48
49 // Should apply to NonZero
0531ce1d
XL
50 assert_eq!(size_of::<NonZeroUsize>(), size_of::<Option<NonZeroUsize>>());
51 assert_eq!(size_of::<NonNull<i8>>(), size_of::<Option<NonNull<i8>>>());
1a4d82fc
JJ
52
53 // Should apply to types that use NonZero internally
c34b1796
AL
54 assert_eq!(size_of::<Vec<isize>>(), size_of::<Option<Vec<isize>>>());
55 assert_eq!(size_of::<Arc<isize>>(), size_of::<Option<Arc<isize>>>());
56 assert_eq!(size_of::<Rc<isize>>(), size_of::<Option<Rc<isize>>>());
1a4d82fc
JJ
57
58 // Should apply to types that have NonZero transitively
59 assert_eq!(size_of::<String>(), size_of::<Option<String>>());
60
e9174d1e
SL
61 // Should apply to types where the pointer is substituted
62 assert_eq!(size_of::<&u8>(), size_of::<Option<ParamTypeStruct<&u8>>>());
63 assert_eq!(size_of::<&u8>(), size_of::<Option<AssocTypeStruct<&u8>>>());
1a4d82fc 64}