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`.
5 // revisions: stock if_match
7 #![feature(rustc_attrs)]
8 #![cfg_attr(if_match, feature(const_if_match))]
10 const _: i32 = if true { //[stock]~ ERROR `if` is not allowed in a `const`
16 const _: i32 = if let Some(true) = Some(false) { //[stock]~ ERROR `if` is not allowed in a `const`
22 const _: i32 = match 1 { //[stock]~ ERROR `match` is not allowed in a `const`
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`
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`
46 const fn if_() -> i32 {
47 if true { 5 } else { 6 } //[stock]~ ERROR `if` is not allowed in a `const fn`
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`
58 const fn match_(i: i32) -> i32 {
59 match i { //[stock]~ ERROR `match` is not allowed in a `const fn`
67 const IF: i32 = if true { 5 } else { 6 };
68 //[stock]~^ ERROR `if` is not allowed in a `const`
70 const IF_LET: i32 = if let Some(true) = None { 5 } else { 6 };
71 //[stock]~^ ERROR `if` is not allowed in a `const`
73 const MATCH: i32 = match 0 { 1 => 2, _ => 0 };
74 //[stock]~^ ERROR `match` is not allowed in a `const`
78 const IF: i32 = if true { 5 } else { 6 };
79 //[stock]~^ ERROR `if` is not allowed in a `const`
81 const IF_LET: i32 = if let Some(true) = None { 5 } else { 6 };
82 //[stock]~^ ERROR `if` is not allowed in a `const`
84 const MATCH: i32 = match 0 { 1 => 2, _ => 0 };
85 //[stock]~^ ERROR `match` is not allowed in a `const`
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`
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 }
108 fn main() { //[if_match]~ ERROR fatal error triggered by #[rustc_error]
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