about summary refs log tree commit diff
diff options
context:
space:
mode:
authorDylan DPC <99973273+Dylan-DPC@users.noreply.github.com>2022-03-16 03:34:35 +0100
committerGitHub <noreply@github.com>2022-03-16 03:34:35 +0100
commitaaf2255379c22f93e53c5fad14453ac7a791ae9e (patch)
tree54a6a9c71193e87ad3ccaf056d231d88a8084fc9
parent0732ea2f3ed277283972da124d070ba6d4d19d10 (diff)
parent261d5fc95bf1ad5ec45e464492f7d6583d0af80f (diff)
downloadrust-aaf2255379c22f93e53c5fad14453ac7a791ae9e.tar.gz
rust-aaf2255379c22f93e53c5fad14453ac7a791ae9e.zip
Rollup merge of #94974 - c410-f3r:let-chain-dashufwrqwemkf-let-else, r=joshtriplett
Ensure that `let_else` does not interact with `let_chains`

As requested on https://github.com/rust-lang/rust/pull/94927.

cc `@joshtriplett` `@estebank`
-rw-r--r--src/test/ui/rfc-2497-if-let-chains/ensure-that-let-else-does-not-interact-with-let-chains.rs54
-rw-r--r--src/test/ui/rfc-2497-if-let-chains/ensure-that-let-else-does-not-interact-with-let-chains.stderr119
2 files changed, 173 insertions, 0 deletions
diff --git a/src/test/ui/rfc-2497-if-let-chains/ensure-that-let-else-does-not-interact-with-let-chains.rs b/src/test/ui/rfc-2497-if-let-chains/ensure-that-let-else-does-not-interact-with-let-chains.rs
new file mode 100644
index 00000000000..e24649ea044
--- /dev/null
+++ b/src/test/ui/rfc-2497-if-let-chains/ensure-that-let-else-does-not-interact-with-let-chains.rs
@@ -0,0 +1,54 @@
+#![feature(let_chains, let_else)]
+
+fn main() {
+    let opt = Some(1i32);
+
+    let Some(n) = opt else {
+        return;
+    };
+    let Some(n) = opt && n == 1 else {
+    //~^ ERROR a `&&` expression cannot be directly assigned in `let...else`
+    //~| ERROR mismatched types
+    //~| ERROR mismatched types
+        return;
+    };
+    let Some(n) = opt && let another = n else {
+    //~^ ERROR a `&&` expression cannot be directly assigned in `let...else`
+    //~| ERROR `let` expressions are not supported here
+    //~| ERROR mismatched types
+    //~| ERROR mismatched types
+        return;
+    };
+
+    if let Some(n) = opt else {
+    //~^ ERROR missing condition for `if` expression
+        return;
+    };
+    if let Some(n) = opt && n == 1 else {
+    //~^ ERROR missing condition for `if` expression
+        return;
+    };
+    if let Some(n) = opt && let another = n else {
+    //~^ ERROR missing condition for `if` expression
+        return;
+    };
+
+    {
+        while let Some(n) = opt else {
+        //~^ ERROR expected `{`, found keyword `else`
+            return;
+        };
+    }
+    {
+        while let Some(n) = opt && n == 1 else {
+        //~^ ERROR expected `{`, found keyword `else`
+            return;
+        };
+    }
+    {
+        while let Some(n) = opt && let another = n else {
+        //~^ ERROR expected `{`, found keyword `else`
+            return;
+        };
+    }
+}
diff --git a/src/test/ui/rfc-2497-if-let-chains/ensure-that-let-else-does-not-interact-with-let-chains.stderr b/src/test/ui/rfc-2497-if-let-chains/ensure-that-let-else-does-not-interact-with-let-chains.stderr
new file mode 100644
index 00000000000..992c34eb402
--- /dev/null
+++ b/src/test/ui/rfc-2497-if-let-chains/ensure-that-let-else-does-not-interact-with-let-chains.stderr
@@ -0,0 +1,119 @@
+error: a `&&` expression cannot be directly assigned in `let...else`
+  --> $DIR/ensure-that-let-else-does-not-interact-with-let-chains.rs:9:19
+   |
+LL |     let Some(n) = opt && n == 1 else {
+   |                   ^^^^^^^^^^^^^
+   |
+help: wrap the expression in parentheses
+   |
+LL |     let Some(n) = (opt && n == 1) else {
+   |                   +             +
+
+error: a `&&` expression cannot be directly assigned in `let...else`
+  --> $DIR/ensure-that-let-else-does-not-interact-with-let-chains.rs:15:19
+   |
+LL |     let Some(n) = opt && let another = n else {
+   |                   ^^^^^^^^^^^^^^^^^^^^^^
+   |
+help: wrap the expression in parentheses
+   |
+LL |     let Some(n) = (opt && let another = n) else {
+   |                   +                      +
+
+error: missing condition for `if` expression
+  --> $DIR/ensure-that-let-else-does-not-interact-with-let-chains.rs:23:7
+   |
+LL |     if let Some(n) = opt else {
+   |       ^ expected if condition here
+
+error: missing condition for `if` expression
+  --> $DIR/ensure-that-let-else-does-not-interact-with-let-chains.rs:27:7
+   |
+LL |     if let Some(n) = opt && n == 1 else {
+   |       ^ expected if condition here
+
+error: missing condition for `if` expression
+  --> $DIR/ensure-that-let-else-does-not-interact-with-let-chains.rs:31:7
+   |
+LL |     if let Some(n) = opt && let another = n else {
+   |       ^ expected if condition here
+
+error: expected `{`, found keyword `else`
+  --> $DIR/ensure-that-let-else-does-not-interact-with-let-chains.rs:37:33
+   |
+LL |         while let Some(n) = opt else {
+   |         ----- ----------------- ^^^^ expected `{`
+   |         |     |
+   |         |     this `while` condition successfully parsed
+   |         while parsing the body of this `while` expression
+
+error: expected `{`, found keyword `else`
+  --> $DIR/ensure-that-let-else-does-not-interact-with-let-chains.rs:43:43
+   |
+LL |         while let Some(n) = opt && n == 1 else {
+   |         ----- --------------------------- ^^^^ expected `{`
+   |         |     |
+   |         |     this `while` condition successfully parsed
+   |         while parsing the body of this `while` expression
+
+error: expected `{`, found keyword `else`
+  --> $DIR/ensure-that-let-else-does-not-interact-with-let-chains.rs:49:52
+   |
+LL |         while let Some(n) = opt && let another = n else {
+   |         ----- ------------------------------------ ^^^^ expected `{`
+   |         |     |
+   |         |     this `while` condition successfully parsed
+   |         while parsing the body of this `while` expression
+
+error: `let` expressions are not supported here
+  --> $DIR/ensure-that-let-else-does-not-interact-with-let-chains.rs:15:26
+   |
+LL |     let Some(n) = opt && let another = n else {
+   |                          ^^^^^^^^^^^^^^^
+   |
+   = note: only supported directly in conditions of `if` and `while` expressions
+   = note: as well as when nested within `&&` and parentheses in those conditions
+
+error[E0308]: mismatched types
+  --> $DIR/ensure-that-let-else-does-not-interact-with-let-chains.rs:9:19
+   |
+LL |     let Some(n) = opt && n == 1 else {
+   |                   ^^^ expected `bool`, found enum `Option`
+   |
+   = note: expected type `bool`
+              found enum `Option<i32>`
+
+error[E0308]: mismatched types
+  --> $DIR/ensure-that-let-else-does-not-interact-with-let-chains.rs:9:9
+   |
+LL |     let Some(n) = opt && n == 1 else {
+   |         ^^^^^^^   ------------- this expression has type `bool`
+   |         |
+   |         expected `bool`, found enum `Option`
+   |
+   = note: expected type `bool`
+              found enum `Option<_>`
+
+error[E0308]: mismatched types
+  --> $DIR/ensure-that-let-else-does-not-interact-with-let-chains.rs:15:19
+   |
+LL |     let Some(n) = opt && let another = n else {
+   |                   ^^^ expected `bool`, found enum `Option`
+   |
+   = note: expected type `bool`
+              found enum `Option<i32>`
+
+error[E0308]: mismatched types
+  --> $DIR/ensure-that-let-else-does-not-interact-with-let-chains.rs:15:9
+   |
+LL |     let Some(n) = opt && let another = n else {
+   |         ^^^^^^^   ---------------------- this expression has type `bool`
+   |         |
+   |         expected `bool`, found enum `Option`
+   |
+   = note: expected type `bool`
+              found enum `Option<_>`
+
+error: aborting due to 13 previous errors
+
+For more information about this error, try `rustc --explain E0308`.