Skip to content

Commit 8ac9ad5

Browse files
committed
nullable-pointer-iotareduction
1 parent bbdaf3e commit 8ac9ad5

File tree

1 file changed

+24
-20
lines changed

1 file changed

+24
-20
lines changed
Lines changed: 24 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,34 @@
1-
//@ run-pass
1+
//! Nullable pointer optimization with iota-reduction for enums.
2+
//!
3+
//! Iota-reduction is a rule from the Calculus of (Co-)Inductive Constructions:
4+
//! "a destructor applied to an object built from a constructor behaves as expected".
5+
//! See <https://coq.inria.fr/doc/language/core/conversion.html#iota-reduction>.
6+
//!
7+
//! This test verifies that nullable pointer optimization works correctly for both
8+
//! Option<T> and custom enums, accounting for pointers and regions.
29
3-
// Iota-reduction is a rule in the Calculus of (Co-)Inductive Constructions,
4-
// which "says that a destructor applied to an object built from a constructor
5-
// behaves as expected". -- https://coq.inria.fr/doc/language/core/conversion.html#iota-reduction
6-
//
7-
// It's a little more complicated here, because of pointers and regions and
8-
// trying to get assert failure messages that at least identify which case
9-
// failed.
10+
//@ run-pass
1011

1112
#![allow(unpredictable_function_pointer_comparisons)]
1213

13-
enum E<T> { Thing(isize, T), #[allow(dead_code)] Nothing((), ((), ()), [i8; 0]) }
14+
enum E<T> {
15+
Thing(isize, T),
16+
#[allow(dead_code)]
17+
Nothing((), ((), ()), [i8; 0]),
18+
}
19+
1420
impl<T> E<T> {
1521
fn is_none(&self) -> bool {
1622
match *self {
1723
E::Thing(..) => false,
18-
E::Nothing(..) => true
24+
E::Nothing(..) => true,
1925
}
2026
}
27+
2128
fn get_ref(&self) -> (isize, &T) {
2229
match *self {
23-
E::Nothing(..) => panic!("E::get_ref(Nothing::<{}>)", stringify!(T)),
24-
E::Thing(x, ref y) => (x, y)
30+
E::Nothing(..) => panic!("E::get_ref(Nothing::<{}>)", stringify!(T)),
31+
E::Thing(x, ref y) => (x, y),
2532
}
2633
}
2734
}
@@ -36,7 +43,7 @@ macro_rules! check_option {
3643
let s_ = Some::<$T>(e);
3744
let $v = s_.as_ref().unwrap();
3845
$chk
39-
}}
46+
}};
4047
}
4148

4249
macro_rules! check_fancy {
@@ -48,11 +55,10 @@ macro_rules! check_fancy {
4855
let e = $e;
4956
let t_ = E::Thing::<$T>(23, e);
5057
match t_.get_ref() {
51-
(23, $v) => { $chk }
52-
_ => panic!("Thing::<{}>(23, {}).get_ref() != (23, _)",
53-
stringify!($T), stringify!($e))
58+
(23, $v) => $chk,
59+
_ => panic!("Thing::<{}>(23, {}).get_ref() != (23, _)", stringify!($T), stringify!($e)),
5460
}
55-
}}
61+
}};
5662
}
5763

5864
macro_rules! check_type {
@@ -67,7 +73,5 @@ pub fn main() {
6773
check_type!(Box::new(18), Box<isize>);
6874
check_type!("foo".to_string(), String);
6975
check_type!(vec![20, 22], Vec<isize>);
70-
check_type!(main, fn(), |pthing| {
71-
assert_eq!(main as fn(), *pthing as fn())
72-
});
76+
check_type!(main, fn(), |pthing| assert_eq!(main as fn(), *pthing as fn()));
7377
}

0 commit comments

Comments
 (0)