]> git.proxmox.com Git - rustc.git/blame - compiler/rustc_codegen_cranelift/example/subslice-patterns-const-eval.rs
New upstream version 1.71.1+dfsg1
[rustc.git] / compiler / rustc_codegen_cranelift / example / subslice-patterns-const-eval.rs
CommitLineData
29967ef6
XL
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)]
8struct N(u8);
9
10#[derive(PartialEq, Debug, Clone)]
11struct Z;
12
13macro_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.
21macro_rules! zed {
49aad941
FG
22 ($e:expr) => {
23 Z
24 };
29967ef6
XL
25}
26
27macro_rules! z {
28 ($($e:expr),* $(,)?) => {
29 [$(zed!($e)),*]
30 }
31}
32
33// Compare constant evaluation and runtime evaluation of a given expression.
34macro_rules! compare_evaluation {
35 ($e:expr, $t:ty $(,)?) => {{
36 const CONST_EVAL: $t = $e;
49aad941
FG
37 const fn const_eval() -> $t {
38 $e
39 }
29967ef6
XL
40 static CONST_EVAL2: $t = const_eval();
41 let runtime_eval = $e;
42 assert_eq!(CONST_EVAL, runtime_eval);
43 assert_eq!(CONST_EVAL2, runtime_eval);
49aad941 44 }};
29967ef6
XL
45}
46
47// Repeat `$test`, substituting the given macro variables with the given
48// identifiers.
49//
50// For example:
51//
52// repeat! {
53// ($name); X; Y:
54// struct $name;
55// }
56//
57// Expands to:
58//
59// struct X; struct Y;
60//
61// This is used to repeat the tests using both the `N` and `Z`
62// types.
63macro_rules! repeat {
64 (($($dollar:tt $placeholder:ident)*); $($($values:ident),+);*: $($test:tt)*) => {
65 macro_rules! single {
66 ($($dollar $placeholder:ident),*) => { $($test)* }
67 }
68 $(single!($($values),+);)*
69 }
70}
71
49aad941 72#[rustfmt::skip]
29967ef6
XL
73fn main() {
74 repeat! {
75 ($arr $Ty); n, N; z, Z:
76 compare_evaluation!({ let [_, x @ .., _] = $arr!(1, 2, 3, 4); x }, [$Ty; 2]);
77 compare_evaluation!({ let [_, ref x @ .., _] = $arr!(1, 2, 3, 4); x }, &'static [$Ty; 2]);
78 compare_evaluation!({ let [_, x @ .., _] = &$arr!(1, 2, 3, 4); x }, &'static [$Ty; 2]);
79
80 compare_evaluation!({ let [_, _, x @ .., _, _] = $arr!(1, 2, 3, 4); x }, [$Ty; 0]);
81 compare_evaluation!(
82 { let [_, _, ref x @ .., _, _] = $arr!(1, 2, 3, 4); x },
83 &'static [$Ty; 0],
84 );
85 compare_evaluation!(
86 { let [_, _, x @ .., _, _] = &$arr!(1, 2, 3, 4); x },
87 &'static [$Ty; 0],
88 );
89
90 compare_evaluation!({ let [_, .., x] = $arr!(1, 2, 3, 4); x }, $Ty);
91 compare_evaluation!({ let [_, .., ref x] = $arr!(1, 2, 3, 4); x }, &'static $Ty);
92 compare_evaluation!({ let [_, _y @ .., x] = &$arr!(1, 2, 3, 4); x }, &'static $Ty);
93 }
94
95 compare_evaluation!({ let [_, .., N(x)] = n!(1, 2, 3, 4); x }, u8);
96 compare_evaluation!({ let [_, .., N(ref x)] = n!(1, 2, 3, 4); x }, &'static u8);
97 compare_evaluation!({ let [_, .., N(x)] = &n!(1, 2, 3, 4); x }, &'static u8);
98
99 compare_evaluation!({ let [N(x), .., _] = n!(1, 2, 3, 4); x }, u8);
100 compare_evaluation!({ let [N(ref x), .., _] = n!(1, 2, 3, 4); x }, &'static u8);
101 compare_evaluation!({ let [N(x), .., _] = &n!(1, 2, 3, 4); x }, &'static u8);
102}