]> git.proxmox.com Git - rustc.git/blame - src/test/run-pass/nullable-pointer-iotareduction.rs
Imported Upstream version 1.0.0~beta.3
[rustc.git] / src / test / run-pass / nullable-pointer-iotareduction.rs
CommitLineData
1a4d82fc 1// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
970d7e83
LB
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
c34b1796 11
1a4d82fc
JJ
12#![allow(unknown_features)]
13#![feature(box_syntax)]
14
15use std::{option, mem};
970d7e83
LB
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
c34b1796 25enum E<T> { Thing(isize, T), Nothing((), ((), ()), [i8; 0]) }
970d7e83
LB
26impl<T> E<T> {
27 fn is_none(&self) -> bool {
28 match *self {
1a4d82fc
JJ
29 E::Thing(..) => false,
30 E::Nothing(..) => true
970d7e83
LB
31 }
32 }
c34b1796 33 fn get_ref(&self) -> (isize, &T) {
970d7e83 34 match *self {
1a4d82fc
JJ
35 E::Nothing(..) => panic!("E::get_ref(Nothing::<{}>)", stringify!(T)),
36 E::Thing(x, ref y) => (x, y)
970d7e83
LB
37 }
38 }
39}
40
41macro_rules! check_option {
1a4d82fc
JJ
42 ($e:expr, $T:ty) => {{
43 check_option!($e, $T, |ptr| assert!(*ptr == $e));
970d7e83 44 }};
1a4d82fc
JJ
45 ($e:expr, $T:ty, |$v:ident| $chk:expr) => {{
46 assert!(option::Option::None::<$T>.is_none());
47 let e = $e;
48 let s_ = option::Option::Some::<$T>(e);
49 let $v = s_.as_ref().unwrap();
970d7e83
LB
50 $chk
51 }}
52}
53
54macro_rules! check_fancy {
1a4d82fc
JJ
55 ($e:expr, $T:ty) => {{
56 check_fancy!($e, $T, |ptr| assert!(*ptr == $e));
970d7e83 57 }};
1a4d82fc 58 ($e:expr, $T:ty, |$v:ident| $chk:expr) => {{
c34b1796 59 assert!(E::Nothing::<$T>((), ((), ()), [23; 0]).is_none());
1a4d82fc
JJ
60 let e = $e;
61 let t_ = E::Thing::<$T>(23, e);
970d7e83
LB
62 match t_.get_ref() {
63 (23, $v) => { $chk }
1a4d82fc 64 _ => panic!("Thing::<{}>(23, {}).get_ref() != (23, _)",
970d7e83
LB
65 stringify!($T), stringify!($e))
66 }
67 }}
68}
69
70macro_rules! check_type {
71 ($($a:tt)*) => {{
72 check_option!($($a)*);
73 check_fancy!($($a)*);
74 }}
75}
76
77pub fn main() {
c34b1796
AL
78 check_type!(&17, &isize);
79 check_type!(box 18, Box<isize>);
1a4d82fc 80 check_type!("foo".to_string(), String);
c34b1796
AL
81 check_type!(vec!(20, 22), Vec<isize> );
82 let mint: usize = unsafe { mem::transmute(main) };
1a4d82fc
JJ
83 check_type!(main, fn(), |pthing| {
84 assert!(mint == unsafe { mem::transmute(*pthing) })
970d7e83
LB
85 });
86}