-
Notifications
You must be signed in to change notification settings - Fork 13k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Emit proper errors when on missing closure braces
This commit focuses on emitting clean errors for the following syntax error: ``` Some(42).map(|a| dbg!(a); a ); ``` Previous implementation tried to recover after parsing the closure body (the `dbg` expression) by replacing the next `;` with a `,`, which made the next expression belong to the next function argument. As such, the following errors were emitted (among others): - the semicolon token was not expected, - a is not in scope, - Option::map is supposed to take one argument, not two. This commit allows us to gracefully handle this situation by adding giving the parser the ability to remember when it has just parsed a closure body inside a function call. When this happens, we can treat the unexpected `;` specifically and try to parse as much statements as possible in order to eat the whole block. When we can't parse statements anymore, we generate a clean error indicating that the braces are missing, and return an ExprKind::Err.
- Loading branch information
Sasha Pourcelot
committed
Aug 31, 2021
1 parent
0a84708
commit 1f5568d
Showing
5 changed files
with
154 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
19 changes: 19 additions & 0 deletions
19
src/test/ui/expr/malformed_closure/missing_braces_around_block.fixed
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
// This snippet ensures that no attempt to recover on a semicolon instead of | ||
// comma is made next to a closure body. | ||
// | ||
// If this recovery happens, then plenty of errors are emitted. Here, we expect | ||
// only one error. | ||
// | ||
// This is part of issue #88065: | ||
// https://github.com/rust-lang/rust/issues/88065 | ||
|
||
// run-rustfix | ||
|
||
fn main() { | ||
let num = 5; | ||
(1..num).reduce(|a, b| { | ||
//~^ ERROR: closure body that contain statements must be surrounded by braces | ||
println!("{}", a); | ||
a * b | ||
}).unwrap(); | ||
} |
19 changes: 19 additions & 0 deletions
19
src/test/ui/expr/malformed_closure/missing_braces_around_block.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
// This snippet ensures that no attempt to recover on a semicolon instead of | ||
// comma is made next to a closure body. | ||
// | ||
// If this recovery happens, then plenty of errors are emitted. Here, we expect | ||
// only one error. | ||
// | ||
// This is part of issue #88065: | ||
// https://github.com/rust-lang/rust/issues/88065 | ||
|
||
// run-rustfix | ||
|
||
fn main() { | ||
let num = 5; | ||
(1..num).reduce(|a, b| | ||
//~^ ERROR: closure body that contain statements must be surrounded by braces | ||
println!("{}", a); | ||
a * b | ||
).unwrap(); | ||
} |
25 changes: 25 additions & 0 deletions
25
src/test/ui/expr/malformed_closure/missing_braces_around_block.stderr
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
error: closure body that contain statements must be surrounded by braces | ||
--> $DIR/missing_braces_around_block.rs:14:26 | ||
| | ||
LL | (1..num).reduce(|a, b| | ||
| ^ | ||
... | ||
LL | ).unwrap(); | ||
| ^ | ||
| | ||
help: This `;` turns the expression into a statement, which must be placed in a block | ||
--> $DIR/missing_braces_around_block.rs:16:26 | ||
| | ||
LL | println!("{}", a); | ||
| ^ | ||
help: try adding braces | ||
| | ||
LL ~ (1..num).reduce(|a, b| { | ||
LL | | ||
LL | println!("{}", a); | ||
LL | a * b | ||
LL ~ }).unwrap(); | ||
| | ||
|
||
error: aborting due to previous error | ||
|