]> git.lizzy.rs Git - rust.git/blob - src/test/run-pass/enum-null-pointer-opt.rs
Auto merge of #22517 - brson:relnotes, r=Gankro
[rust.git] / src / test / run-pass / enum-null-pointer-opt.rs
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
11
12 extern crate core;
13
14 use core::nonzero::NonZero;
15 use std::mem::size_of;
16 use std::rc::Rc;
17 use std::sync::Arc;
18
19 trait Trait {}
20
21 fn main() {
22     // Functions
23     assert_eq!(size_of::<fn(int)>(), size_of::<Option<fn(int)>>());
24     assert_eq!(size_of::<extern "C" fn(int)>(), size_of::<Option<extern "C" fn(int)>>());
25
26     // Slices - &str / &[T] / &mut [T]
27     assert_eq!(size_of::<&str>(), size_of::<Option<&str>>());
28     assert_eq!(size_of::<&[int]>(), size_of::<Option<&[int]>>());
29     assert_eq!(size_of::<&mut [int]>(), size_of::<Option<&mut [int]>>());
30
31     // Traits - Box<Trait> / &Trait / &mut Trait
32     assert_eq!(size_of::<Box<Trait>>(), size_of::<Option<Box<Trait>>>());
33     assert_eq!(size_of::<&Trait>(), size_of::<Option<&Trait>>());
34     assert_eq!(size_of::<&mut Trait>(), size_of::<Option<&mut Trait>>());
35
36     // Pointers - Box<T>
37     assert_eq!(size_of::<Box<int>>(), size_of::<Option<Box<int>>>());
38
39     // The optimization can't apply to raw pointers
40     assert!(size_of::<Option<*const int>>() != size_of::<*const int>());
41     assert!(Some(0 as *const int).is_some()); // Can't collapse None to null
42
43     struct Foo {
44         _a: Box<int>
45     }
46     struct Bar(Box<int>);
47
48     // Should apply through structs
49     assert_eq!(size_of::<Foo>(), size_of::<Option<Foo>>());
50     assert_eq!(size_of::<Bar>(), size_of::<Option<Bar>>());
51     // and tuples
52     assert_eq!(size_of::<(u8, Box<int>)>(), size_of::<Option<(u8, Box<int>)>>());
53     // and fixed-size arrays
54     assert_eq!(size_of::<[Box<int>; 1]>(), size_of::<Option<[Box<int>; 1]>>());
55
56     // Should apply to NonZero
57     assert_eq!(size_of::<NonZero<uint>>(), size_of::<Option<NonZero<uint>>>());
58     assert_eq!(size_of::<NonZero<*mut i8>>(), size_of::<Option<NonZero<*mut i8>>>());
59
60     // Should apply to types that use NonZero internally
61     assert_eq!(size_of::<Vec<int>>(), size_of::<Option<Vec<int>>>());
62     assert_eq!(size_of::<Arc<int>>(), size_of::<Option<Arc<int>>>());
63     assert_eq!(size_of::<Rc<int>>(), size_of::<Option<Rc<int>>>());
64
65     // Should apply to types that have NonZero transitively
66     assert_eq!(size_of::<String>(), size_of::<Option<String>>());
67
68 }