]> git.proxmox.com Git - rustc.git/blame - src/test/run-pass/enum-null-pointer-opt.rs
New upstream version 1.25.0+dfsg1
[rustc.git] / src / test / run-pass / enum-null-pointer-opt.rs
CommitLineData
1a4d82fc
JJ
1// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
2// file at the top-level directory of this distribution and at
3// http://rust-lang.org/COPYRIGHT.
4//
5// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8// option. This file may not be copied, modified, or distributed
9// except according to those terms.
10
62682a34 11#![feature(nonzero, core)]
1a4d82fc
JJ
12
13extern crate core;
14
15use core::nonzero::NonZero;
16use std::mem::size_of;
17use std::rc::Rc;
18use std::sync::Arc;
19
85aaf69f 20trait Trait { fn dummy(&self) { } }
e9174d1e
SL
21trait Mirror { type Image; }
22impl<T> Mirror for T { type Image = T; }
23struct ParamTypeStruct<T>(T);
24struct AssocTypeStruct<T>(<T as Mirror>::Image);
1a4d82fc
JJ
25
26fn main() {
27 // Functions
c34b1796
AL
28 assert_eq!(size_of::<fn(isize)>(), size_of::<Option<fn(isize)>>());
29 assert_eq!(size_of::<extern "C" fn(isize)>(), size_of::<Option<extern "C" fn(isize)>>());
1a4d82fc
JJ
30
31 // Slices - &str / &[T] / &mut [T]
32 assert_eq!(size_of::<&str>(), size_of::<Option<&str>>());
c34b1796
AL
33 assert_eq!(size_of::<&[isize]>(), size_of::<Option<&[isize]>>());
34 assert_eq!(size_of::<&mut [isize]>(), size_of::<Option<&mut [isize]>>());
1a4d82fc
JJ
35
36 // Traits - Box<Trait> / &Trait / &mut Trait
37 assert_eq!(size_of::<Box<Trait>>(), size_of::<Option<Box<Trait>>>());
38 assert_eq!(size_of::<&Trait>(), size_of::<Option<&Trait>>());
39 assert_eq!(size_of::<&mut Trait>(), size_of::<Option<&mut Trait>>());
40
41 // Pointers - Box<T>
c34b1796 42 assert_eq!(size_of::<Box<isize>>(), size_of::<Option<Box<isize>>>());
1a4d82fc
JJ
43
44 // The optimization can't apply to raw pointers
c34b1796
AL
45 assert!(size_of::<Option<*const isize>>() != size_of::<*const isize>());
46 assert!(Some(0 as *const isize).is_some()); // Can't collapse None to null
1a4d82fc
JJ
47
48 struct Foo {
c34b1796 49 _a: Box<isize>
1a4d82fc 50 }
c34b1796 51 struct Bar(Box<isize>);
1a4d82fc
JJ
52
53 // Should apply through structs
54 assert_eq!(size_of::<Foo>(), size_of::<Option<Foo>>());
55 assert_eq!(size_of::<Bar>(), size_of::<Option<Bar>>());
56 // and tuples
c34b1796 57 assert_eq!(size_of::<(u8, Box<isize>)>(), size_of::<Option<(u8, Box<isize>)>>());
1a4d82fc 58 // and fixed-size arrays
c34b1796 59 assert_eq!(size_of::<[Box<isize>; 1]>(), size_of::<Option<[Box<isize>; 1]>>());
1a4d82fc
JJ
60
61 // Should apply to NonZero
c34b1796 62 assert_eq!(size_of::<NonZero<usize>>(), size_of::<Option<NonZero<usize>>>());
1a4d82fc
JJ
63 assert_eq!(size_of::<NonZero<*mut i8>>(), size_of::<Option<NonZero<*mut i8>>>());
64
65 // Should apply to types that use NonZero internally
c34b1796
AL
66 assert_eq!(size_of::<Vec<isize>>(), size_of::<Option<Vec<isize>>>());
67 assert_eq!(size_of::<Arc<isize>>(), size_of::<Option<Arc<isize>>>());
68 assert_eq!(size_of::<Rc<isize>>(), size_of::<Option<Rc<isize>>>());
1a4d82fc
JJ
69
70 // Should apply to types that have NonZero transitively
71 assert_eq!(size_of::<String>(), size_of::<Option<String>>());
72
e9174d1e
SL
73 // Should apply to types where the pointer is substituted
74 assert_eq!(size_of::<&u8>(), size_of::<Option<ParamTypeStruct<&u8>>>());
75 assert_eq!(size_of::<&u8>(), size_of::<Option<AssocTypeStruct<&u8>>>());
1a4d82fc 76}