Skip to content

parser: provide better suggestions and errors on closures with braces missing #108388

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

Merged
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
6 changes: 5 additions & 1 deletion compiler/rustc_parse/src/parser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -982,7 +982,11 @@ impl<'a> Parser<'a> {
let initial_semicolon = self.token.span;

while self.eat(&TokenKind::Semi) {
let _ = self.parse_stmt(ForceCollect::Yes)?;
let _ =
self.parse_stmt_without_recovery(false, ForceCollect::Yes).unwrap_or_else(|e| {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Recovery within parse_stmt was doing something wrong. So changed it not to do.

e.cancel();
None
});
}

expect_err.set_primary_message(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,23 @@
// If this recovery happens, then plenty of errors are emitted. Here, we expect
// only one error.
//
// This is part of issue #88065:
// This is part of the following issues:
// https://github.com/rust-lang/rust/issues/88065
// https://github.com/rust-lang/rust/issues/107959

// run-rustfix

fn main() {
// Closure with multiple expressions delimited by semicolon.
let num = 5;
(1..num).reduce(|a, b| {
//~^ ERROR: closure bodies that contain statements must be surrounded by braces
println!("{}", a);
a * b
}).unwrap();

// Closure with a single expression ended by a semicolon.
let mut v = vec![1, 2, 3];
v.iter_mut().for_each(|x| {*x = *x+1;});
//~^ ERROR: closure bodies that contain statements must be surrounded by braces
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,23 @@
// If this recovery happens, then plenty of errors are emitted. Here, we expect
// only one error.
//
// This is part of issue #88065:
// This is part of the following issues:
// https://github.com/rust-lang/rust/issues/88065
// https://github.com/rust-lang/rust/issues/107959

// run-rustfix

fn main() {
// Closure with multiple expressions delimited by semicolon.
let num = 5;
(1..num).reduce(|a, b|
//~^ ERROR: closure bodies that contain statements must be surrounded by braces
println!("{}", a);
a * b
).unwrap();

// Closure with a single expression ended by a semicolon.
let mut v = vec![1, 2, 3];
v.iter_mut().for_each(|x|*x = *x+1;);
//~^ ERROR: closure bodies that contain statements must be surrounded by braces
}
33 changes: 29 additions & 4 deletions tests/ui/expr/malformed_closure/missing_braces_around_block.stderr
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
error: closure bodies that contain statements must be surrounded by braces
--> $DIR/missing_braces_around_block.rs:14:26
--> $DIR/missing_braces_around_block.rs:16:26
|
LL | (1..num).reduce(|a, b|
| ^
Expand All @@ -8,14 +8,14 @@ LL | ).unwrap();
| ^
|
note: statement found outside of a block
--> $DIR/missing_braces_around_block.rs:16:26
--> $DIR/missing_braces_around_block.rs:18:26
|
LL | println!("{}", a);
| -----------------^ this `;` turns the preceding closure into a statement
| |
| this expression is a statement because of the trailing semicolon
note: the closure body may be incorrectly delimited
--> $DIR/missing_braces_around_block.rs:14:21
--> $DIR/missing_braces_around_block.rs:16:21
|
LL | (1..num).reduce(|a, b|
| _____________________^
Expand All @@ -34,5 +34,30 @@ LL | a * b
LL ~ }).unwrap();
|

error: aborting due to previous error
error: closure bodies that contain statements must be surrounded by braces
--> $DIR/missing_braces_around_block.rs:24:29
|
LL | v.iter_mut().for_each(|x|*x = *x+1;);
| ^ ^
|
note: statement found outside of a block
--> $DIR/missing_braces_around_block.rs:24:39
|
LL | v.iter_mut().for_each(|x|*x = *x+1;);
| ---------^ this `;` turns the preceding closure into a statement
| |
| this expression is a statement because of the trailing semicolon
note: the closure body may be incorrectly delimited
--> $DIR/missing_braces_around_block.rs:24:27
|
LL | v.iter_mut().for_each(|x|*x = *x+1;);
| ^^^^^^^^^^^^ - ...but likely you meant the closure to end here
| |
| this is the parsed closure...
help: try adding braces
|
LL | v.iter_mut().for_each(|x| {*x = *x+1;});
| + +

error: aborting due to 2 previous errors