Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow try-blocks in places where an open delim is expected #70657

Merged
merged 2 commits into from
Apr 15, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/librustc_lint/unused.rs
Original file line number Diff line number Diff line change
Expand Up @@ -384,7 +384,7 @@ trait UnusedDelimLint {
fn is_expr_delims_necessary(inner: &ast::Expr, followed_by_block: bool) -> bool {
followed_by_block
&& match inner.kind {
ast::ExprKind::Ret(_) | ast::ExprKind::Break(..) => true,
ExprKind::Ret(_) | ExprKind::Break(..) => true,
_ => parser::contains_exterior_struct_lit(&inner),
}
}
Expand Down
8 changes: 3 additions & 5 deletions src/librustc_parse/parser/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
6 changes: 5 additions & 1 deletion src/test/ui/try-block/try-block-in-match.rs
Original file line number Diff line number Diff line change
@@ -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(()) => (),
}
}
10 changes: 0 additions & 10 deletions src/test/ui/try-block/try-block-in-match.stderr

This file was deleted.

3 changes: 2 additions & 1 deletion src/test/ui/try-block/try-block-in-while.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
9 changes: 6 additions & 3 deletions src/test/ui/try-block/try-block-in-while.stderr
Original file line number Diff line number Diff line change
@@ -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`.
28 changes: 28 additions & 0 deletions src/test/ui/try-block/try-block-unused-delims.rs
Original file line number Diff line number Diff line change
@@ -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(()) => (),
}
}
44 changes: 44 additions & 0 deletions src/test/ui/try-block/try-block-unused-delims.stderr
Original file line number Diff line number Diff line change
@@ -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