]> git.lizzy.rs Git - rust.git/blob - tests/ui/array-slice-vec/subslice-patterns-const-eval-match.rs
Optimize `TyKind::eq`.
[rust.git] / tests / ui / array-slice-vec / subslice-patterns-const-eval-match.rs
1 // Test that slice subslice patterns are correctly handled in const evaluation.
2
3 // run-pass
4
5 #[derive(PartialEq, Debug, Clone)]
6 struct N(u8);
7
8 #[derive(PartialEq, Debug, Clone)]
9 struct Z;
10
11 macro_rules! n {
12     ($($e:expr),* $(,)?) => {
13         [$(N($e)),*]
14     }
15 }
16
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.
19 macro_rules! zed {
20     ($e:expr) => { Z }
21 }
22
23 macro_rules! z {
24     ($($e:expr),* $(,)?) => {
25         [$(zed!($e)),*]
26     }
27 }
28
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);
38     }}
39 }
40
41 // Compare the result of matching `$e` against `$p` using both `if let` and
42 // `match`.
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);
47     }}
48 }
49
50 // Repeat `$test`, substituting the given macro variables with the given
51 // identifiers.
52 //
53 // For example:
54 //
55 // repeat! {
56 //     ($name); X; Y:
57 //     struct $name;
58 // }
59 //
60 // Expands to:
61 //
62 // struct X; struct Y;
63 //
64 // This is used to repeat the tests using both the `N` and `Z`
65 // types.
66 macro_rules! repeat {
67     (($($dollar:tt $placeholder:ident)*); $($($values:ident),+);*: $($test:tt)*) => {
68         macro_rules! single {
69             ($($dollar $placeholder:ident),*) => { $($test)* }
70         }
71         $(single!($($values),+);)*
72     }
73 }
74
75 fn main() {
76     repeat! {
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>);
81
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>);
85
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>);
89     }
90
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>);
93
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>);
96 }