about summary refs log tree commit diff
path: root/src
diff options
context:
space:
mode:
authorMatthias Krüger <matthias.krueger@famsik.de>2022-02-25 14:14:40 +0100
committerGitHub <noreply@github.com>2022-02-25 14:14:40 +0100
commit731cd3fbeb0e08a44d52c14039fe942c74491f0d (patch)
tree603794c02d501f40e703078f1c3335bd54e5706d /src
parentcf3bb098881da40eed6ce7ad913a7e5d904663e2 (diff)
parentfd35770e8d092b79eef16a7061e202307f730c90 (diff)
downloadrust-731cd3fbeb0e08a44d52c14039fe942c74491f0d.tar.gz
rust-731cd3fbeb0e08a44d52c14039fe942c74491f0d.zip
Rollup merge of #94344 - notriddle:notriddle/suggest-parens-more, r=oli-obk
diagnostic: suggest parens when users want logical ops, but get closures

Fixes #93536
Diffstat (limited to 'src')
-rw-r--r--src/test/ui/parser/expr-as-stmt.fixed27
-rw-r--r--src/test/ui/parser/expr-as-stmt.rs27
-rw-r--r--src/test/ui/parser/expr-as-stmt.stderr82
3 files changed, 135 insertions, 1 deletions
diff --git a/src/test/ui/parser/expr-as-stmt.fixed b/src/test/ui/parser/expr-as-stmt.fixed
index 101959d6da0..36709eea17c 100644
--- a/src/test/ui/parser/expr-as-stmt.fixed
+++ b/src/test/ui/parser/expr-as-stmt.fixed
@@ -37,4 +37,31 @@ fn qux() -> u32 {
     //~^ ERROR mismatched types
 }
 
+fn space_cadet() -> bool {
+    ({ true }) | { true } //~ ERROR E0308
+    //~^ ERROR expected parameter name
+}
+
+fn revenge_from_mars() -> bool {
+    ({ true }) && { true } //~ ERROR E0308
+    //~^ ERROR mismatched types
+}
+
+fn attack_from_mars() -> bool {
+    ({ true }) || { true } //~ ERROR E0308
+    //~^ ERROR mismatched types
+}
+
+// This gets corrected by adding a semicolon, instead of parens.
+// It's placed here to help keep track of the way this diagnostic
+// needs to interact with type checking to avoid MachineApplicable
+// suggestions that actually break stuff.
+//
+// If you're wondering what happens if that `foo()` is a `true` like
+// all the ones above use? Nothing. It makes neither suggestion in
+// that case.
+fn asteroids() -> impl FnOnce() -> bool {
+    { foo(); } || { true } //~ ERROR E0308
+}
+
 fn main() {}
diff --git a/src/test/ui/parser/expr-as-stmt.rs b/src/test/ui/parser/expr-as-stmt.rs
index 45c4f977502..92bb972b240 100644
--- a/src/test/ui/parser/expr-as-stmt.rs
+++ b/src/test/ui/parser/expr-as-stmt.rs
@@ -37,4 +37,31 @@ fn qux() -> u32 {
     //~^ ERROR mismatched types
 }
 
+fn space_cadet() -> bool {
+    { true } | { true } //~ ERROR E0308
+    //~^ ERROR expected parameter name
+}
+
+fn revenge_from_mars() -> bool {
+    { true } && { true } //~ ERROR E0308
+    //~^ ERROR mismatched types
+}
+
+fn attack_from_mars() -> bool {
+    { true } || { true } //~ ERROR E0308
+    //~^ ERROR mismatched types
+}
+
+// This gets corrected by adding a semicolon, instead of parens.
+// It's placed here to help keep track of the way this diagnostic
+// needs to interact with type checking to avoid MachineApplicable
+// suggestions that actually break stuff.
+//
+// If you're wondering what happens if that `foo()` is a `true` like
+// all the ones above use? Nothing. It makes neither suggestion in
+// that case.
+fn asteroids() -> impl FnOnce() -> bool {
+    { foo() } || { true } //~ ERROR E0308
+}
+
 fn main() {}
diff --git a/src/test/ui/parser/expr-as-stmt.stderr b/src/test/ui/parser/expr-as-stmt.stderr
index cae775099e0..df0e4dcb16e 100644
--- a/src/test/ui/parser/expr-as-stmt.stderr
+++ b/src/test/ui/parser/expr-as-stmt.stderr
@@ -44,6 +44,25 @@ LL |         _ => 1,
 LL ~     }) > 0
    |
 
+error: expected parameter name, found `{`
+  --> $DIR/expr-as-stmt.rs:41:16
+   |
+LL |     { true } | { true }
+   |                ^ expected parameter name
+   |
+help: parentheses are required to parse this as an expression
+   |
+LL |     ({ true }) | { true }
+   |     +        +
+
+error[E0308]: mismatched types
+  --> $DIR/expr-as-stmt.rs:64:7
+   |
+LL |     { foo() } || { true }
+   |       ^^^^^- help: consider using a semicolon here: `;`
+   |       |
+   |       expected `()`, found `i32`
+
 error[E0308]: mismatched types
   --> $DIR/expr-as-stmt.rs:8:6
    |
@@ -121,7 +140,68 @@ help: parentheses are required to parse this as an expression
 LL |     ({2}) - 2
    |     +   +
 
-error: aborting due to 11 previous errors
+error[E0308]: mismatched types
+  --> $DIR/expr-as-stmt.rs:41:7
+   |
+LL |     { true } | { true }
+   |       ^^^^ expected `()`, found `bool`
+   |
+help: you might have meant to return this value
+   |
+LL |     { return true; } | { true }
+   |       ++++++     +
+
+error[E0308]: mismatched types
+  --> $DIR/expr-as-stmt.rs:46:7
+   |
+LL |     { true } && { true }
+   |       ^^^^ expected `()`, found `bool`
+   |
+help: you might have meant to return this value
+   |
+LL |     { return true; } && { true }
+   |       ++++++     +
+
+error[E0308]: mismatched types
+  --> $DIR/expr-as-stmt.rs:46:14
+   |
+LL | fn revenge_from_mars() -> bool {
+   |                           ---- expected `bool` because of return type
+LL |     { true } && { true }
+   |              ^^^^^^^^^^^ expected `bool`, found `&&bool`
+   |
+help: parentheses are required to parse this as an expression
+   |
+LL |     ({ true }) && { true }
+   |     +        +
+
+error[E0308]: mismatched types
+  --> $DIR/expr-as-stmt.rs:51:7
+   |
+LL |     { true } || { true }
+   |       ^^^^ expected `()`, found `bool`
+   |
+help: you might have meant to return this value
+   |
+LL |     { return true; } || { true }
+   |       ++++++     +
+
+error[E0308]: mismatched types
+  --> $DIR/expr-as-stmt.rs:51:14
+   |
+LL | fn attack_from_mars() -> bool {
+   |                          ---- expected `bool` because of return type
+LL |     { true } || { true }
+   |              ^^^^^^^^^^^ expected `bool`, found closure
+   |
+   = note: expected type `bool`
+           found closure `[closure@$DIR/expr-as-stmt.rs:51:14: 51:25]`
+help: parentheses are required to parse this as an expression
+   |
+LL |     ({ true }) || { true }
+   |     +        +
+
+error: aborting due to 18 previous errors
 
 Some errors have detailed explanations: E0308, E0600, E0614.
 For more information about an error, try `rustc --explain E0308`.