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