]> git.proxmox.com Git - rustc.git/blob - compiler/rustc_codegen_cranelift/example/subslice-patterns-const-eval.rs
New upstream version 1.49.0~beta.4+dfsg1
[rustc.git] / compiler / rustc_codegen_cranelift / example / subslice-patterns-const-eval.rs
1 // Based on https://github.com/rust-lang/rust/blob/c5840f9d252c2f5cc16698dbf385a29c5de3ca07/src/test/ui/array-slice-vec/subslice-patterns-const-eval-match.rs
2
3 // Test that array subslice patterns are correctly handled in const evaluation.
4
5 // run-pass
6
7 #[derive(PartialEq, Debug, Clone)]
8 struct N(u8);
9
10 #[derive(PartialEq, Debug, Clone)]
11 struct Z;
12
13 macro_rules! n {
14 ($($e:expr),* $(,)?) => {
15 [$(N($e)),*]
16 }
17 }
18
19 // This macro has an unused variable so that it can be repeated base on the
20 // number of times a repeated variable (`$e` in `z`) occurs.
21 macro_rules! zed {
22 ($e:expr) => { Z }
23 }
24
25 macro_rules! z {
26 ($($e:expr),* $(,)?) => {
27 [$(zed!($e)),*]
28 }
29 }
30
31 // Compare constant evaluation and runtime evaluation of a given expression.
32 macro_rules! compare_evaluation {
33 ($e:expr, $t:ty $(,)?) => {{
34 const CONST_EVAL: $t = $e;
35 const fn const_eval() -> $t { $e }
36 static CONST_EVAL2: $t = const_eval();
37 let runtime_eval = $e;
38 assert_eq!(CONST_EVAL, runtime_eval);
39 assert_eq!(CONST_EVAL2, runtime_eval);
40 }}
41 }
42
43 // Repeat `$test`, substituting the given macro variables with the given
44 // identifiers.
45 //
46 // For example:
47 //
48 // repeat! {
49 // ($name); X; Y:
50 // struct $name;
51 // }
52 //
53 // Expands to:
54 //
55 // struct X; struct Y;
56 //
57 // This is used to repeat the tests using both the `N` and `Z`
58 // types.
59 macro_rules! repeat {
60 (($($dollar:tt $placeholder:ident)*); $($($values:ident),+);*: $($test:tt)*) => {
61 macro_rules! single {
62 ($($dollar $placeholder:ident),*) => { $($test)* }
63 }
64 $(single!($($values),+);)*
65 }
66 }
67
68 fn main() {
69 repeat! {
70 ($arr $Ty); n, N; z, Z:
71 compare_evaluation!({ let [_, x @ .., _] = $arr!(1, 2, 3, 4); x }, [$Ty; 2]);
72 compare_evaluation!({ let [_, ref x @ .., _] = $arr!(1, 2, 3, 4); x }, &'static [$Ty; 2]);
73 compare_evaluation!({ let [_, x @ .., _] = &$arr!(1, 2, 3, 4); x }, &'static [$Ty; 2]);
74
75 compare_evaluation!({ let [_, _, x @ .., _, _] = $arr!(1, 2, 3, 4); x }, [$Ty; 0]);
76 compare_evaluation!(
77 { let [_, _, ref x @ .., _, _] = $arr!(1, 2, 3, 4); x },
78 &'static [$Ty; 0],
79 );
80 compare_evaluation!(
81 { let [_, _, x @ .., _, _] = &$arr!(1, 2, 3, 4); x },
82 &'static [$Ty; 0],
83 );
84
85 compare_evaluation!({ let [_, .., x] = $arr!(1, 2, 3, 4); x }, $Ty);
86 compare_evaluation!({ let [_, .., ref x] = $arr!(1, 2, 3, 4); x }, &'static $Ty);
87 compare_evaluation!({ let [_, _y @ .., x] = &$arr!(1, 2, 3, 4); x }, &'static $Ty);
88 }
89
90 compare_evaluation!({ let [_, .., N(x)] = n!(1, 2, 3, 4); x }, u8);
91 compare_evaluation!({ let [_, .., N(ref x)] = n!(1, 2, 3, 4); x }, &'static u8);
92 compare_evaluation!({ let [_, .., N(x)] = &n!(1, 2, 3, 4); x }, &'static u8);
93
94 compare_evaluation!({ let [N(x), .., _] = n!(1, 2, 3, 4); x }, u8);
95 compare_evaluation!({ let [N(ref x), .., _] = n!(1, 2, 3, 4); x }, &'static u8);
96 compare_evaluation!({ let [N(x), .., _] = &n!(1, 2, 3, 4); x }, &'static u8);
97 }