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.
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.
11 use std::{option, mem};
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
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
21 enum E<T> { Thing(int, T), Nothing((), ((), ()), [i8; 0]) }
23 fn is_none(&self) -> bool {
25 E::Thing(..) => false,
26 E::Nothing(..) => true
29 fn get_ref(&self) -> (int, &T) {
31 E::Nothing(..) => panic!("E::get_ref(Nothing::<{}>)", stringify!(T)),
32 E::Thing(x, ref y) => (x, y)
37 macro_rules! check_option {
38 ($e:expr: $T:ty) => {{
39 check_option!($e: $T, |ptr| assert!(*ptr == $e));
41 ($e:expr: $T:ty, |$v:ident| $chk:expr) => {{
42 assert!(option::Option::None::<$T>.is_none());
44 let s_ = option::Option::Some::<$T>(e);
45 let $v = s_.as_ref().unwrap();
50 macro_rules! check_fancy {
51 ($e:expr: $T:ty) => {{
52 check_fancy!($e: $T, |ptr| assert!(*ptr == $e));
54 ($e:expr: $T:ty, |$v:ident| $chk:expr) => {{
55 assert!(E::Nothing::<$T>((), ((), ()), [23i8; 0]).is_none());
57 let t_ = E::Thing::<$T>(23, e);
60 _ => panic!("Thing::<{}>(23, {}).get_ref() != (23, _)",
61 stringify!($T), stringify!($e))
66 macro_rules! check_type {
68 check_option!($($a)*);
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) })