]> git.lizzy.rs Git - rust.git/blob - src/test/ui/array-slice-vec/subslice-patterns-const-eval.rs
slice_patterns: remove gates in tests
[rust.git] / src / test / ui / array-slice-vec / subslice-patterns-const-eval.rs
1 // Test that array 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 {
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 // Repeat `$test`, substituting the given macro variables with the given
42 // identifiers.
43 //
44 // For example:
45 //
46 // repeat! {
47 //     ($name); X; Y:
48 //     struct $name;
49 // }
50 //
51 // Expands to:
52 //
53 // struct X; struct Y;
54 //
55 // This is used to repeat the tests using both the `N` and `Z`
56 // types.
57 macro_rules! repeat {
58     (($($dollar:tt $placeholder:ident)*); $($($values:ident),+);*: $($test:tt)*) => {
59         macro_rules! single {
60             ($($dollar $placeholder:ident),*) => { $($test)* }
61         }
62         $(single!($($values),+);)*
63     }
64 }
65
66 fn main() {
67     repeat! {
68         ($arr $Ty); n, N; z, Z:
69         compare_evaluation!({ let [_, x @ .., _] = $arr!(1, 2, 3, 4); x }, [$Ty; 2]);
70         compare_evaluation!({ let [_, ref x @ .., _] = $arr!(1, 2, 3, 4); x }, &'static [$Ty; 2]);
71         compare_evaluation!({ let [_, x @ .., _] = &$arr!(1, 2, 3, 4); x }, &'static [$Ty; 2]);
72
73         compare_evaluation!({ let [_, _, x @ .., _, _] = $arr!(1, 2, 3, 4); x }, [$Ty; 0]);
74         compare_evaluation!(
75             { let [_, _, ref x @ .., _, _] = $arr!(1, 2, 3, 4); x },
76             &'static [$Ty; 0],
77         );
78         compare_evaluation!(
79             { let [_, _, x @ .., _, _] = &$arr!(1, 2, 3, 4); x },
80             &'static [$Ty; 0],
81         );
82
83         compare_evaluation!({ let [_, .., x] = $arr!(1, 2, 3, 4); x }, $Ty);
84         compare_evaluation!({ let [_, .., ref x] = $arr!(1, 2, 3, 4); x }, &'static $Ty);
85         compare_evaluation!({ let [_, _y @ .., x] = &$arr!(1, 2, 3, 4); x }, &'static $Ty);
86     }
87
88     compare_evaluation!({ let [_, .., N(x)] = n!(1, 2, 3, 4); x }, u8);
89     compare_evaluation!({ let [_, .., N(ref x)] = n!(1, 2, 3, 4); x }, &'static u8);
90     compare_evaluation!({ let [_, .., N(x)] = &n!(1, 2, 3, 4); x }, &'static u8);
91
92     compare_evaluation!({ let [N(x), .., _] = n!(1, 2, 3, 4); x }, u8);
93     compare_evaluation!({ let [N(ref x), .., _] = n!(1, 2, 3, 4); x }, &'static u8);
94     compare_evaluation!({ let [N(x), .., _] = &n!(1, 2, 3, 4); x }, &'static u8);
95 }