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