]> git.proxmox.com Git - rustc.git/blame - src/test/ui/nullable-pointer-iotareduction.rs
New upstream version 1.49.0+dfsg1
[rustc.git] / src / test / ui / nullable-pointer-iotareduction.rs
CommitLineData
416331ca
XL
1// run-pass
2
1a4d82fc
JJ
3#![feature(box_syntax)]
4
970d7e83
LB
5// Iota-reduction is a rule in the Calculus of (Co-)Inductive Constructions,
6// which "says that a destructor applied to an object built from a constructor
7// behaves as expected". -- http://coq.inria.fr/doc/Reference-Manual006.html
8//
9// It's a little more complicated here, because of pointers and regions and
10// trying to get assert failure messages that at least identify which case
11// failed.
12
c34b1796 13enum E<T> { Thing(isize, T), Nothing((), ((), ()), [i8; 0]) }
970d7e83
LB
14impl<T> E<T> {
15 fn is_none(&self) -> bool {
16 match *self {
1a4d82fc
JJ
17 E::Thing(..) => false,
18 E::Nothing(..) => true
970d7e83
LB
19 }
20 }
c34b1796 21 fn get_ref(&self) -> (isize, &T) {
970d7e83 22 match *self {
1a4d82fc
JJ
23 E::Nothing(..) => panic!("E::get_ref(Nothing::<{}>)", stringify!(T)),
24 E::Thing(x, ref y) => (x, y)
970d7e83
LB
25 }
26 }
27}
28
29macro_rules! check_option {
1a4d82fc 30 ($e:expr, $T:ty) => {{
62682a34 31 check_option!($e, $T, |ptr| assert_eq!(*ptr, $e));
970d7e83 32 }};
1a4d82fc 33 ($e:expr, $T:ty, |$v:ident| $chk:expr) => {{
54a0048b 34 assert!(None::<$T>.is_none());
1a4d82fc 35 let e = $e;
54a0048b 36 let s_ = Some::<$T>(e);
1a4d82fc 37 let $v = s_.as_ref().unwrap();
970d7e83
LB
38 $chk
39 }}
40}
41
42macro_rules! check_fancy {
1a4d82fc 43 ($e:expr, $T:ty) => {{
62682a34 44 check_fancy!($e, $T, |ptr| assert_eq!(*ptr, $e));
970d7e83 45 }};
1a4d82fc 46 ($e:expr, $T:ty, |$v:ident| $chk:expr) => {{
c34b1796 47 assert!(E::Nothing::<$T>((), ((), ()), [23; 0]).is_none());
1a4d82fc
JJ
48 let e = $e;
49 let t_ = E::Thing::<$T>(23, e);
970d7e83
LB
50 match t_.get_ref() {
51 (23, $v) => { $chk }
1a4d82fc 52 _ => panic!("Thing::<{}>(23, {}).get_ref() != (23, _)",
970d7e83
LB
53 stringify!($T), stringify!($e))
54 }
55 }}
56}
57
58macro_rules! check_type {
59 ($($a:tt)*) => {{
60 check_option!($($a)*);
61 check_fancy!($($a)*);
62 }}
63}
64
65pub fn main() {
c34b1796
AL
66 check_type!(&17, &isize);
67 check_type!(box 18, Box<isize>);
1a4d82fc 68 check_type!("foo".to_string(), String);
c30ab7b3 69 check_type!(vec![20, 22], Vec<isize>);
1a4d82fc 70 check_type!(main, fn(), |pthing| {
54a0048b 71 assert_eq!(main as fn(), *pthing as fn())
970d7e83
LB
72 });
73}