about summary refs log tree commit diff
diff options
context:
space:
mode:
authorBastian Kauschke <bastian_kauschke@hotmail.de>2020-04-14 17:45:00 +0200
committerBastian Kauschke <bastian_kauschke@hotmail.de>2020-04-14 18:39:20 +0200
commit81a3cd7278f6117401b6b441485154e6ecf2f8c9 (patch)
tree7a4e2351f60abf75a1777fb68ad733c2131ab1a1
parent2765f426da76fd7b7b9bd377de7f92dd181387c0 (diff)
downloadrust-81a3cd7278f6117401b6b441485154e6ecf2f8c9.tar.gz
rust-81a3cd7278f6117401b6b441485154e6ecf2f8c9.zip
allow try as scrutinee, e.g. `match try ...`
-rw-r--r--src/librustc_parse/parser/expr.rs8
-rw-r--r--src/test/ui/try-block/try-block-in-match.rs6
-rw-r--r--src/test/ui/try-block/try-block-in-match.stderr10
-rw-r--r--src/test/ui/try-block/try-block-in-while.rs3
-rw-r--r--src/test/ui/try-block/try-block-in-while.stderr9
-rw-r--r--src/test/ui/try-block/try-block-unused-delims.rs28
-rw-r--r--src/test/ui/try-block/try-block-unused-delims.stderr44
7 files changed, 88 insertions, 20 deletions
diff --git a/src/librustc_parse/parser/expr.rs b/src/librustc_parse/parser/expr.rs
index cbff99f8da6..4e3c5fa63de 100644
--- a/src/librustc_parse/parser/expr.rs
+++ b/src/librustc_parse/parser/expr.rs
@@ -1846,11 +1846,9 @@ impl<'a> Parser<'a> {
     }
 
     fn is_try_block(&self) -> bool {
-        self.token.is_keyword(kw::Try) &&
-        self.look_ahead(1, |t| *t == token::OpenDelim(token::Brace)) &&
-        self.token.uninterpolated_span().rust_2018() &&
-        // Prevent `while try {} {}`, `if try {} {} else {}`, etc.
-        !self.restrictions.contains(Restrictions::NO_STRUCT_LITERAL)
+        self.token.is_keyword(kw::Try)
+            && self.look_ahead(1, |t| *t == token::OpenDelim(token::Brace))
+            && self.token.uninterpolated_span().rust_2018()
     }
 
     /// Parses an `async move? {...}` expression.
diff --git a/src/test/ui/try-block/try-block-in-match.rs b/src/test/ui/try-block/try-block-in-match.rs
index bce0d0340b6..cd0b967e79d 100644
--- a/src/test/ui/try-block/try-block-in-match.rs
+++ b/src/test/ui/try-block/try-block-in-match.rs
@@ -1,7 +1,11 @@
+// run-pass
 // compile-flags: --edition 2018
 
 #![feature(try_blocks)]
 
 fn main() {
-    match try { false } { _ => {} } //~ ERROR expected expression, found reserved keyword `try`
+    match try { } {
+        Err(()) => (),
+        Ok(()) => (),
+    }
 }
diff --git a/src/test/ui/try-block/try-block-in-match.stderr b/src/test/ui/try-block/try-block-in-match.stderr
deleted file mode 100644
index 936e0fe19ba..00000000000
--- a/src/test/ui/try-block/try-block-in-match.stderr
+++ /dev/null
@@ -1,10 +0,0 @@
-error: expected expression, found reserved keyword `try`
-  --> $DIR/try-block-in-match.rs:6:11
-   |
-LL |     match try { false } { _ => {} }
-   |     ----- ^^^ expected expression
-   |     |
-   |     while parsing this match expression
-
-error: aborting due to previous error
-
diff --git a/src/test/ui/try-block/try-block-in-while.rs b/src/test/ui/try-block/try-block-in-while.rs
index 98af796dd37..33d27236519 100644
--- a/src/test/ui/try-block/try-block-in-while.rs
+++ b/src/test/ui/try-block/try-block-in-while.rs
@@ -3,5 +3,6 @@
 #![feature(try_blocks)]
 
 fn main() {
-    while try { false } {} //~ ERROR expected expression, found reserved keyword `try`
+    while try { false } {}
+    //~^ ERROR the trait bound `bool: std::ops::Try` is not satisfied
 }
diff --git a/src/test/ui/try-block/try-block-in-while.stderr b/src/test/ui/try-block/try-block-in-while.stderr
index 026df15eb87..ac41ddfd8c0 100644
--- a/src/test/ui/try-block/try-block-in-while.stderr
+++ b/src/test/ui/try-block/try-block-in-while.stderr
@@ -1,8 +1,11 @@
-error: expected expression, found reserved keyword `try`
-  --> $DIR/try-block-in-while.rs:6:11
+error[E0277]: the trait bound `bool: std::ops::Try` is not satisfied
+  --> $DIR/try-block-in-while.rs:6:15
    |
 LL |     while try { false } {}
-   |           ^^^ expected expression
+   |               ^^^^^^^^^ the trait `std::ops::Try` is not implemented for `bool`
+   |
+   = note: required by `std::ops::Try::from_ok`
 
 error: aborting due to previous error
 
+For more information about this error, try `rustc --explain E0277`.
diff --git a/src/test/ui/try-block/try-block-unused-delims.rs b/src/test/ui/try-block/try-block-unused-delims.rs
new file mode 100644
index 00000000000..0b767eb2dad
--- /dev/null
+++ b/src/test/ui/try-block/try-block-unused-delims.rs
@@ -0,0 +1,28 @@
+// check-pass
+// compile-flags: --edition 2018
+
+#![feature(try_blocks)]
+#![warn(unused_parens, unused_braces)]
+
+fn consume<T>(_: Result<T, T>) -> T { todo!() }
+
+fn main() {
+    consume((try {}));
+    //~^ WARN unnecessary parentheses
+
+    consume({ try {} });
+    //~^ WARN unnecessary braces
+
+    match (try {}) {
+        //~^ WARN unnecessary parentheses
+        Ok(()) | Err(()) => (),
+    }
+
+    if let Err(()) = (try {}) {}
+    //~^ WARN unnecessary parentheses
+
+    match (try {}) {
+        //~^ WARN unnecessary parentheses
+        Ok(()) | Err(()) => (),
+    }
+}
diff --git a/src/test/ui/try-block/try-block-unused-delims.stderr b/src/test/ui/try-block/try-block-unused-delims.stderr
new file mode 100644
index 00000000000..5c7602ee0ab
--- /dev/null
+++ b/src/test/ui/try-block/try-block-unused-delims.stderr
@@ -0,0 +1,44 @@
+warning: unnecessary parentheses around function argument
+  --> $DIR/try-block-unused-delims.rs:10:13
+   |
+LL |     consume((try {}));
+   |             ^^^^^^^^ help: remove these parentheses
+   |
+note: the lint level is defined here
+  --> $DIR/try-block-unused-delims.rs:5:9
+   |
+LL | #![warn(unused_parens, unused_braces)]
+   |         ^^^^^^^^^^^^^
+
+warning: unnecessary braces around function argument
+  --> $DIR/try-block-unused-delims.rs:13:13
+   |
+LL |     consume({ try {} });
+   |             ^^^^^^^^^^ help: remove these braces
+   |
+note: the lint level is defined here
+  --> $DIR/try-block-unused-delims.rs:5:24
+   |
+LL | #![warn(unused_parens, unused_braces)]
+   |                        ^^^^^^^^^^^^^
+
+warning: unnecessary parentheses around `match` scrutinee expression
+  --> $DIR/try-block-unused-delims.rs:16:11
+   |
+LL |     match (try {}) {
+   |           ^^^^^^^^ help: remove these parentheses
+
+warning: unnecessary parentheses around `let` scrutinee expression
+  --> $DIR/try-block-unused-delims.rs:21:22
+   |
+LL |     if let Err(()) = (try {}) {}
+   |                      ^^^^^^^^ help: remove these parentheses
+
+warning: unnecessary parentheses around `match` scrutinee expression
+  --> $DIR/try-block-unused-delims.rs:24:11
+   |
+LL |     match (try {}) {
+   |           ^^^^^^^^ help: remove these parentheses
+
+warning: 5 warnings emitted
+