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))]
11 const _: i32 = if true { //[stock]~ ERROR `if` is not allowed in a `const`
17 const _: i32 = if let Some(true) = Some(false) { //[stock]~ ERROR `if` is not allowed in a `const`
23 const _: i32 = match 1 { //[stock]~ ERROR `match` is not allowed in a `const`
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`
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`
47 const fn if_() -> i32 {
48 if true { 5 } else { 6 } //[stock]~ ERROR `if` is not allowed in a `const fn`
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`
59 const fn match_(i: i32) -> i32 {
60 match i { //[stock]~ ERROR `match` is not allowed in a `const fn`
68 const IF: i32 = if true { 5 } else { 6 };
69 //[stock]~^ ERROR `if` is not allowed in a `const`
71 const IF_LET: i32 = if let Some(true) = None { 5 } else { 6 };
72 //[stock]~^ ERROR `if` is not allowed in a `const`
74 const MATCH: i32 = match 0 { 1 => 2, _ => 0 };
75 //[stock]~^ ERROR `match` is not allowed in a `const`
79 const IF: i32 = if true { 5 } else { 6 };
80 //[stock]~^ ERROR `if` is not allowed in a `const`
82 const IF_LET: i32 = if let Some(true) = None { 5 } else { 6 };
83 //[stock]~^ ERROR `if` is not allowed in a `const`
85 const MATCH: i32 = match 0 { 1 => 2, _ => 0 };
86 //[stock]~^ ERROR `match` is not allowed in a `const`
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`
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 }
109 fn main() { //[if_match]~ ERROR fatal error triggered by #[rustc_error]
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