]> git.lizzy.rs Git - rust.git/blob - src/test/run-pass/nullable-pointer-iotareduction.rs
test: Remove `@str` from the test suite
[rust.git] / src / test / run-pass / nullable-pointer-iotareduction.rs
1 // xfail-test
2
3 // xfail'd due to a bug in move detection for macros.
4
5 // Copyright 2013 The Rust Project Developers. See the COPYRIGHT
6 // file at the top-level directory of this distribution and at
7 // http://rust-lang.org/COPYRIGHT.
8 //
9 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
10 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
11 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
12 // option. This file may not be copied, modified, or distributed
13 // except according to those terms.
14
15 use std::{option, cast};
16
17 // Iota-reduction is a rule in the Calculus of (Co-)Inductive Constructions,
18 // which "says that a destructor applied to an object built from a constructor
19 // behaves as expected".  -- http://coq.inria.fr/doc/Reference-Manual006.html
20 //
21 // It's a little more complicated here, because of pointers and regions and
22 // trying to get assert failure messages that at least identify which case
23 // failed.
24
25 enum E<T> { Thing(int, T), Nothing((), ((), ()), [i8, ..0]) }
26 impl<T> E<T> {
27     fn is_none(&self) -> bool {
28         match *self {
29             Thing(..) => false,
30             Nothing(..) => true
31         }
32     }
33     fn get_ref<'r>(&'r self) -> (int, &'r T) {
34         match *self {
35             Nothing(..) => fail!("E::get_ref(Nothing::<%s>)",  stringify!($T)),
36             Thing(x, ref y) => (x, y)
37         }
38     }
39 }
40
41 macro_rules! check_option {
42     ($e:expr: $T:ty) => {{
43         check_option!($e: $T, |ptr| assert!(*ptr == $e));
44     }};
45     ($e:expr: $T:ty, |$v:ident| $chk:expr) => {{
46         assert!(option::None::<$T>.is_none());
47         let e = $e;
48         let s_ = option::Some::<$T>(e);
49         let $v = s_.get_ref();
50         $chk
51     }}
52 }
53
54 macro_rules! check_fancy {
55     ($e:expr: $T:ty) => {{
56         check_fancy!($e: $T, |ptr| assert!(*ptr == $e));
57     }};
58     ($e:expr: $T:ty, |$v:ident| $chk:expr) => {{
59         assert!(Nothing::<$T>((), ((), ()), [23i8, ..0]).is_none());
60         let e = $e;
61         let t_ = Thing::<$T>(23, e);
62         match t_.get_ref() {
63             (23, $v) => { $chk }
64             _ => fail!("Thing::<%s>(23, %s).get_ref() != (23, _)",
65                        stringify!($T), stringify!($e))
66         }
67     }}
68 }
69
70 macro_rules! check_type {
71     ($($a:tt)*) => {{
72         check_option!($($a)*);
73         check_fancy!($($a)*);
74     }}
75 }
76
77 pub fn main() {
78     check_type!(&17: &int);
79     check_type!(~18: ~int);
80     check_type!(@19: @int);
81     check_type!(~"foo": ~str);
82     check_type!(~[20, 22]: ~[int]);
83     check_type!(@[]: @[int]);
84     check_type!(@[24, 26]: @[int]);
85     let mint: uint = unsafe { cast::transmute(main) };
86     check_type!(main: extern fn(), |pthing| {
87         assert!(mint == unsafe { cast::transmute(*pthing) })
88     });
89 }