]> git.lizzy.rs Git - rust.git/blob - src/test/ui/consts/control-flow/feature-gate-const-if-match.rs
e4b65257531fdacbd999bfb03b86755822c40d18
[rust.git] / src / test / ui / consts / control-flow / feature-gate-const-if-match.rs
1 // Ensure that `if`, `if let` and `match` are only allowed in the various const contexts when
2 // `#![feature(const_if_match)]` is enabled. When the feature gate is removed, the `#[rustc_error]`
3 // on `main` should be removed and this test converted to `check-pass`.
4
5 // revisions: stock if_match
6
7 #![feature(rustc_attrs)]
8 #![cfg_attr(if_match, feature(const_if_match))]
9 #![feature(const_fn)]
10
11 const _: i32 = if true { //[stock]~ ERROR `if` is not allowed in a `const`
12     5
13 } else {
14     6
15 };
16
17 const _: i32 = if let Some(true) = Some(false) { //[stock]~ ERROR `if` is not allowed in a `const`
18     0
19 } else {
20     1
21 };
22
23 const _: i32 = match 1 { //[stock]~ ERROR `match` is not allowed in a `const`
24     2 => 3,
25     4 => 5,
26     _ => 0,
27 };
28
29 static FOO: i32 = {
30     let x = if true { 0 } else { 1 };
31     //[stock]~^ ERROR `if` is not allowed in a `static`
32     let x = match x { 0 => 1, _ => 0 };
33     //[stock]~^ ERROR `match` is not allowed in a `static`
34     if let Some(x) = Some(x) { x } else { 1 }
35     //[stock]~^ ERROR `if` is not allowed in a `static`
36 };
37
38 static mut BAR: i32 = {
39     let x = if true { 0 } else { 1 };
40     //[stock]~^ ERROR `if` is not allowed in a `static mut`
41     let x = match x { 0 => 1, _ => 0 };
42     //[stock]~^ ERROR `match` is not allowed in a `static mut`
43     if let Some(x) = Some(x) { x } else { 1 }
44     //[stock]~^ ERROR `if` is not allowed in a `static mut`
45 };
46
47 const fn if_() -> i32 {
48     if true { 5 } else { 6 } //[stock]~ ERROR `if` is not allowed in a `const fn`
49 }
50
51 const fn if_let(a: Option<bool>) -> i32 {
52     if let Some(true) = a { //[stock]~ ERROR `if` is not allowed in a `const fn`
53         0
54     } else {
55         1
56     }
57 }
58
59 const fn match_(i: i32) -> i32 {
60     match i { //[stock]~ ERROR `match` is not allowed in a `const fn`
61         i if i > 10 => i,
62         1 => 2,
63         _ => 0
64     }
65 }
66
67 pub trait Foo {
68     const IF: i32 = if true { 5 } else { 6 };
69     //[stock]~^ ERROR `if` is not allowed in a `const`
70
71     const IF_LET: i32 = if let Some(true) = None { 5 } else { 6 };
72     //[stock]~^ ERROR `if` is not allowed in a `const`
73
74     const MATCH: i32 = match 0 { 1 => 2, _ => 0 };
75     //[stock]~^ ERROR `match` is not allowed in a `const`
76 }
77
78 impl Foo for () {
79     const IF: i32 = if true { 5 } else { 6 };
80     //[stock]~^ ERROR `if` is not allowed in a `const`
81
82     const IF_LET: i32 = if let Some(true) = None { 5 } else { 6 };
83     //[stock]~^ ERROR `if` is not allowed in a `const`
84
85     const MATCH: i32 = match 0 { 1 => 2, _ => 0 };
86     //[stock]~^ ERROR `match` is not allowed in a `const`
87 }
88
89 fn non_const_outside() {
90     const fn const_inside(y: bool) -> i32 {
91         let x = if y { 0 } else { 1 };
92         //[stock]~^ ERROR `if` is not allowed in a `const fn`
93         let x = match x { 0 => 1, _ => 0 };
94         //[stock]~^ ERROR `match` is not allowed in a `const fn`
95         if let Some(x) = Some(x) { x } else { 1 }
96         //[stock]~^ ERROR `if` is not allowed in a `const fn`
97     }
98 }
99
100 const fn const_outside() {
101     fn non_const_inside(y: bool) -> i32 {
102         let x = if y { 0 } else { 1 };
103         let x = match x { 0 => 1, _ => 0 };
104         if let Some(x) = Some(x) { x } else { 1 }
105     }
106 }
107
108 #[rustc_error]
109 fn main() { //[if_match]~ ERROR fatal error triggered by #[rustc_error]
110     let _ = [0; {
111         let x = if false { 0 } else { 1 };
112         //[stock]~^ ERROR `if` is not allowed in a `const`
113         let x = match x { 0 => 1, _ => 0 };
114         //[stock]~^ ERROR `match` is not allowed in a `const`
115         if let Some(x) = Some(x) { x } else { 1 }
116         //[stock]~^ ERROR `if` is not allowed in a `const`
117         //[stock]~| ERROR constant contains unimplemented expression type
118     }];
119 }