about summary refs log tree commit diff
path: root/tests/ui/consts/control-flow/feature-gate-const-if-match.rs
diff options
context:
space:
mode:
authorAlbert Larsan <74931857+albertlarsan68@users.noreply.github.com>2023-01-05 09:13:28 +0100
committerAlbert Larsan <74931857+albertlarsan68@users.noreply.github.com>2023-01-11 09:32:08 +0000
commitcf2dff2b1e3fa55fa5415d524200070d0d7aacfe (patch)
tree40a88d9a46aaf3e8870676eb2538378b75a263eb /tests/ui/consts/control-flow/feature-gate-const-if-match.rs
parentca855e6e42787ecd062d81d53336fe6788ef51a9 (diff)
downloadrust-cf2dff2b1e3fa55fa5415d524200070d0d7aacfe.tar.gz
rust-cf2dff2b1e3fa55fa5415d524200070d0d7aacfe.zip
Move /src/test to /tests
Diffstat (limited to 'tests/ui/consts/control-flow/feature-gate-const-if-match.rs')
-rw-r--r--tests/ui/consts/control-flow/feature-gate-const-if-match.rs96
1 files changed, 96 insertions, 0 deletions
diff --git a/tests/ui/consts/control-flow/feature-gate-const-if-match.rs b/tests/ui/consts/control-flow/feature-gate-const-if-match.rs
new file mode 100644
index 00000000000..cb66bc75309
--- /dev/null
+++ b/tests/ui/consts/control-flow/feature-gate-const-if-match.rs
@@ -0,0 +1,96 @@
+// check-pass
+
+const _: i32 = if true { 5 } else { 6 };
+
+const _: i32 = if let Some(true) = Some(false) { 0 } else { 1 };
+
+const _: i32 = match 1 {
+    2 => 3,
+    4 => 5,
+    _ => 0,
+};
+
+static FOO: i32 = {
+    let x = if true { 0 } else { 1 };
+    let x = match x {
+        0 => 1,
+        _ => 0,
+    };
+    if let Some(x) = Some(x) { x } else { 1 }
+};
+
+static mut BAR: i32 = {
+    let x = if true { 0 } else { 1 };
+    let x = match x {
+        0 => 1,
+        _ => 0,
+    };
+    if let Some(x) = Some(x) { x } else { 1 }
+};
+
+const fn if_() -> i32 {
+    if true { 5 } else { 6 }
+}
+
+const fn if_let(a: Option<bool>) -> i32 {
+    if let Some(true) = a { 0 } else { 1 }
+}
+
+const fn match_(i: i32) -> i32 {
+    match i {
+        i if i > 10 => i,
+        1 => 2,
+        _ => 0,
+    }
+}
+
+pub trait Foo {
+    const IF: i32 = if true { 5 } else { 6 };
+    const IF_LET: i32 = if let Some(true) = None { 5 } else { 6 };
+    const MATCH: i32 = match 0 {
+        1 => 2,
+        _ => 0,
+    };
+}
+
+impl Foo for () {
+    const IF: i32 = if true { 5 } else { 6 };
+    const IF_LET: i32 = if let Some(true) = None { 5 } else { 6 };
+    const MATCH: i32 = match 0 {
+        1 => 2,
+        _ => 0,
+    };
+}
+
+fn non_const_outside() {
+    const fn const_inside(y: bool) -> i32 {
+        let x = if y { 0 } else { 1 };
+        let x = match x {
+            0 => 1,
+            _ => 0,
+        };
+        if let Some(x) = Some(x) { x } else { 1 }
+    }
+}
+
+const fn const_outside() {
+    fn non_const_inside(y: bool) -> i32 {
+        let x = if y { 0 } else { 1 };
+        let x = match x {
+            0 => 1,
+            _ => 0,
+        };
+        if let Some(x) = Some(x) { x } else { 1 }
+    }
+}
+
+fn main() {
+    let _ = [0; {
+        let x = if false { 0 } else { 1 };
+        let x = match x {
+            0 => 1,
+            _ => 0,
+        };
+        if let Some(x) = Some(x) { x } else { 1 }
+    }];
+}