forked from rust-lang/rust
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Rollup merge of rust-lang#88546 - scrabsha:scrabsha/closure-missing-b…
…races, r=estebank 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. Closes rust-lang#88065. r? `@estebank`
- Loading branch information
Showing
7 changed files
with
234 additions
and
9 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 bodies 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 bodies that contain statements must be surrounded by braces | ||
println!("{}", a); | ||
a * b | ||
).unwrap(); | ||
} |
38 changes: 38 additions & 0 deletions
38
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,38 @@ | ||
error: closure bodies that contain statements must be surrounded by braces | ||
--> $DIR/missing_braces_around_block.rs:14:26 | ||
| | ||
LL | (1..num).reduce(|a, b| | ||
| ^ | ||
... | ||
LL | ).unwrap(); | ||
| ^ | ||
| | ||
note: statement found outside of a block | ||
--> $DIR/missing_braces_around_block.rs:16: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 | ||
| | ||
LL | (1..num).reduce(|a, b| | ||
| _____________________^ | ||
LL | | | ||
LL | | println!("{}", a); | ||
| |_________________________^ this is the parsed closure... | ||
LL | a * b | ||
LL | ).unwrap(); | ||
| - ...but likely you meant the closure to end here | ||
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 | ||
|
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,16 @@ | ||
// Part of issue #27300. | ||
// The problem here is that ruby-style closures are parsed as blocks whose | ||
// first statement is a closure. See the issue for more details: | ||
// https://github.com/rust-lang/rust/issues/27300 | ||
|
||
// Note: this test represents what the compiler currently emits. The error | ||
// message will be improved later. | ||
|
||
fn main() { | ||
let p = Some(45).and_then({ | ||
//~^ expected a `FnOnce<({integer},)>` closure, found `Option<_>` | ||
|x| println!("doubling {}", x); | ||
Some(x * 2) | ||
//~^ ERROR: cannot find value `x` in this scope | ||
}); | ||
} |
18 changes: 18 additions & 0 deletions
18
src/test/ui/expr/malformed_closure/ruby_style_closure.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,18 @@ | ||
error[E0425]: cannot find value `x` in this scope | ||
--> $DIR/ruby_style_closure.rs:13:14 | ||
| | ||
LL | Some(x * 2) | ||
| ^ not found in this scope | ||
|
||
error[E0277]: expected a `FnOnce<({integer},)>` closure, found `Option<_>` | ||
--> $DIR/ruby_style_closure.rs:10:22 | ||
| | ||
LL | let p = Some(45).and_then({ | ||
| ^^^^^^^^ expected an `FnOnce<({integer},)>` closure, found `Option<_>` | ||
| | ||
= help: the trait `FnOnce<({integer},)>` is not implemented for `Option<_>` | ||
|
||
error: aborting due to 2 previous errors | ||
|
||
Some errors have detailed explanations: E0277, E0425. | ||
For more information about an error, try `rustc --explain E0277`. |