]> git.proxmox.com Git - rustc.git/blame - src/test/run-pass/nullable-pointer-iotareduction.rs
Imported Upstream version 1.9.0+dfsg1
[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
970d7e83
LB
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
c34b1796 23enum E<T> { Thing(isize, T), Nothing((), ((), ()), [i8; 0]) }
970d7e83
LB
24impl<T> E<T> {
25 fn is_none(&self) -> bool {
26 match *self {
1a4d82fc
JJ
27 E::Thing(..) => false,
28 E::Nothing(..) => true
970d7e83
LB
29 }
30 }
c34b1796 31 fn get_ref(&self) -> (isize, &T) {
970d7e83 32 match *self {
1a4d82fc
JJ
33 E::Nothing(..) => panic!("E::get_ref(Nothing::<{}>)", stringify!(T)),
34 E::Thing(x, ref y) => (x, y)
970d7e83
LB
35 }
36 }
37}
38
39macro_rules! check_option {
1a4d82fc 40 ($e:expr, $T:ty) => {{
62682a34 41 check_option!($e, $T, |ptr| assert_eq!(*ptr, $e));
970d7e83 42 }};
1a4d82fc 43 ($e:expr, $T:ty, |$v:ident| $chk:expr) => {{
54a0048b 44 assert!(None::<$T>.is_none());
1a4d82fc 45 let e = $e;
54a0048b 46 let s_ = Some::<$T>(e);
1a4d82fc 47 let $v = s_.as_ref().unwrap();
970d7e83
LB
48 $chk
49 }}
50}
51
52macro_rules! check_fancy {
1a4d82fc 53 ($e:expr, $T:ty) => {{
62682a34 54 check_fancy!($e, $T, |ptr| assert_eq!(*ptr, $e));
970d7e83 55 }};
1a4d82fc 56 ($e:expr, $T:ty, |$v:ident| $chk:expr) => {{
c34b1796 57 assert!(E::Nothing::<$T>((), ((), ()), [23; 0]).is_none());
1a4d82fc
JJ
58 let e = $e;
59 let t_ = E::Thing::<$T>(23, e);
970d7e83
LB
60 match t_.get_ref() {
61 (23, $v) => { $chk }
1a4d82fc 62 _ => panic!("Thing::<{}>(23, {}).get_ref() != (23, _)",
970d7e83
LB
63 stringify!($T), stringify!($e))
64 }
65 }}
66}
67
68macro_rules! check_type {
69 ($($a:tt)*) => {{
70 check_option!($($a)*);
71 check_fancy!($($a)*);
72 }}
73}
74
75pub fn main() {
c34b1796
AL
76 check_type!(&17, &isize);
77 check_type!(box 18, Box<isize>);
1a4d82fc 78 check_type!("foo".to_string(), String);
54a0048b 79 check_type!(vec!(20, 22), Vec<isize>);
1a4d82fc 80 check_type!(main, fn(), |pthing| {
54a0048b 81 assert_eq!(main as fn(), *pthing as fn())
970d7e83
LB
82 });
83}