1 // Test that slice subslice patterns are correctly handled in const evaluation.
5 #[derive(PartialEq, Debug, Clone)]
8 #[derive(PartialEq, Debug, Clone)]
12 ($($e:expr),* $(,)?) => {
17 // This macro has an unused variable so that it can be repeated base on the
18 // number of times a repeated variable (`$e` in `z`) occurs.
24 ($($e:expr),* $(,)?) => {
29 // Compare constant evaluation and runtime evaluation of a given expression.
30 macro_rules! compare_evaluation_inner {
31 ($e:expr, $t:ty $(,)?) => {{
32 const CONST_EVAL: $t = $e;
33 const fn const_eval() -> $t { $e }
34 static CONST_EVAL2: $t = const_eval();
35 let runtime_eval = $e;
36 assert_eq!(CONST_EVAL, runtime_eval);
37 assert_eq!(CONST_EVAL2, runtime_eval);
41 // Compare the result of matching `$e` against `$p` using both `if let` and
43 macro_rules! compare_evaluation {
44 ($p:pat, $e:expr, $matches:expr, $t:ty $(,)?) => {{
45 compare_evaluation_inner!(if let $p = $e as &[_] { $matches } else { None }, $t);
46 compare_evaluation_inner!(match $e as &[_] { $p => $matches, _ => None }, $t);
50 // Repeat `$test`, substituting the given macro variables with the given
62 // struct X; struct Y;
64 // This is used to repeat the tests using both the `N` and `Z`
67 (($($dollar:tt $placeholder:ident)*); $($($values:ident),+);*: $($test:tt)*) => {
69 ($($dollar $placeholder:ident),*) => { $($test)* }
71 $(single!($($values),+);)*
77 ($arr $Ty); n, N; z, Z:
78 compare_evaluation!([_, x @ .., _], &$arr!(1, 2, 3, 4), Some(x), Option<&'static [$Ty]>);
79 compare_evaluation!([x, .., _], &$arr!(1, 2, 3, 4), Some(x), Option<&'static $Ty>);
80 compare_evaluation!([_, .., x], &$arr!(1, 2, 3, 4), Some(x), Option<&'static $Ty>);
82 compare_evaluation!([_, x @ .., _], &$arr!(1, 2), Some(x), Option<&'static [$Ty]>);
83 compare_evaluation!([x, .., _], &$arr!(1, 2), Some(x), Option<&'static $Ty>);
84 compare_evaluation!([_, .., x], &$arr!(1, 2), Some(x), Option<&'static $Ty>);
86 compare_evaluation!([_, x @ .., _], &$arr!(1), Some(x), Option<&'static [$Ty]>);
87 compare_evaluation!([x, .., _], &$arr!(1), Some(x), Option<&'static $Ty>);
88 compare_evaluation!([_, .., x], &$arr!(1), Some(x), Option<&'static $Ty>);
91 compare_evaluation!([N(x), .., _], &n!(1, 2, 3, 4), Some(x), Option<&'static u8>);
92 compare_evaluation!([_, .., N(x)], &n!(1, 2, 3, 4), Some(x), Option<&'static u8>);
94 compare_evaluation!([N(x), .., _], &n!(1, 2), Some(x), Option<&'static u8>);
95 compare_evaluation!([_, .., N(x)], &n!(1, 2), Some(x), Option<&'static u8>);