]> git.lizzy.rs Git - rust.git/blob - src/test/run-pass/nullable-pointer-iotareduction.rs
rollup merge of #20482: kmcallister/macro-reform
[rust.git] / src / test / run-pass / nullable-pointer-iotareduction.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 use std::{option, mem};
12
13 // Iota-reduction is a rule in the Calculus of (Co-)Inductive Constructions,
14 // which "says that a destructor applied to an object built from a constructor
15 // behaves as expected".  -- http://coq.inria.fr/doc/Reference-Manual006.html
16 //
17 // It's a little more complicated here, because of pointers and regions and
18 // trying to get assert failure messages that at least identify which case
19 // failed.
20
21 enum E<T> { Thing(int, T), Nothing((), ((), ()), [i8; 0]) }
22 impl<T> E<T> {
23     fn is_none(&self) -> bool {
24         match *self {
25             E::Thing(..) => false,
26             E::Nothing(..) => true
27         }
28     }
29     fn get_ref(&self) -> (int, &T) {
30         match *self {
31             E::Nothing(..) => panic!("E::get_ref(Nothing::<{}>)",  stringify!(T)),
32             E::Thing(x, ref y) => (x, y)
33         }
34     }
35 }
36
37 macro_rules! check_option {
38     ($e:expr: $T:ty) => {{
39         check_option!($e: $T, |ptr| assert!(*ptr == $e));
40     }};
41     ($e:expr: $T:ty, |$v:ident| $chk:expr) => {{
42         assert!(option::Option::None::<$T>.is_none());
43         let e = $e;
44         let s_ = option::Option::Some::<$T>(e);
45         let $v = s_.as_ref().unwrap();
46         $chk
47     }}
48 }
49
50 macro_rules! check_fancy {
51     ($e:expr: $T:ty) => {{
52         check_fancy!($e: $T, |ptr| assert!(*ptr == $e));
53     }};
54     ($e:expr: $T:ty, |$v:ident| $chk:expr) => {{
55         assert!(E::Nothing::<$T>((), ((), ()), [23i8; 0]).is_none());
56         let e = $e;
57         let t_ = E::Thing::<$T>(23, e);
58         match t_.get_ref() {
59             (23, $v) => { $chk }
60             _ => panic!("Thing::<{}>(23, {}).get_ref() != (23, _)",
61                        stringify!($T), stringify!($e))
62         }
63     }}
64 }
65
66 macro_rules! check_type {
67     ($($a:tt)*) => {{
68         check_option!($($a)*);
69         check_fancy!($($a)*);
70     }}
71 }
72
73 pub fn main() {
74     check_type!(&17: &int);
75     check_type!(box 18: Box<int>);
76     check_type!("foo".to_string(): String);
77     check_type!(vec!(20, 22): Vec<int> );
78     let mint: uint = unsafe { mem::transmute(main) };
79     check_type!(main: fn(), |pthing| {
80         assert!(mint == unsafe { mem::transmute(*pthing) })
81     });
82 }