diff options
| author | Dylan DPC <99973273+Dylan-DPC@users.noreply.github.com> | 2023-02-24 12:02:44 +0530 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2023-02-24 12:02:44 +0530 |
| commit | 8acbfe27d62b40186a0566f0e2bc8dbe3da5b407 (patch) | |
| tree | 4f9b39ed8ad94c45ed6f4eae043e89c84230a468 /compiler/rustc_query_impl/src | |
| parent | 4aff2c5ff89e378357d8fa00c4f465d79b26dddb (diff) | |
| parent | 0e42298674757bbb3563e0deda477044cce8271d (diff) | |
| download | rust-8acbfe27d62b40186a0566f0e2bc8dbe3da5b407.tar.gz rust-8acbfe27d62b40186a0566f0e2bc8dbe3da5b407.zip | |
Rollup merge of #108388 - ohno418:better-suggestion-on-malformed-closure, r=davidtwco
parser: provide better suggestions and errors on closures with braces missing
We currently provide wrong suggestions and unhelpful errors on closure bodies with braces missing.
For example, given the following code:
```rust
fn main() {
let _x = Box::new(|x|x+1;);
}
```
the current output is:
```
error: expected expression, found `)`
--> ./main.rs:2:30
|
2 | let _x = Box::new(|x|x+1;);
| ^ expected expression
error: closure bodies that contain statements must be surrounded by braces
--> ./main.rs:2:25
|
2 | let _x = Box::new(|x|x+1;);
| ^
3 | }
| ^
|
note: statement found outside of a block
--> ./main.rs:2:29
|
2 | let _x = Box::new(|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
--> ./main.rs:2:23
|
2 | let _x = Box::new(|x|x+1;);
| ^^^^^^ this is the parsed closure...
3 | }
| - ...but likely you meant the closure to end here
help: try adding braces
|
2 ~ let _x = Box::new(|x| {x+1;);
3 ~ }}
|
error: expected `;`, found `}`
--> ./main.rs:2:32
|
2 | let _x = Box::new(|x|x+1;);
| ^ help: add `;` here
3 | }
| - unexpected token
error: aborting due to 3 previous errors
```
We got 3 errors, but all but the second are unnecessary or just wrong.
This commit allows outputting correct suggestions and errors. The above code would output like this:
```
error: closure bodies that contain statements must be surrounded by braces
--> ./main.rs:2:25
|
2 | let _x = Box::new(|x|x+1;);
| ^ ^
|
note: statement found outside of a block
--> ./main.rs:2:29
|
2 | let _x = Box::new(|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
--> ./main.rs:2:23
|
2 | let _x = Box::new(|x|x+1;);
| ^^^^^^ - ...but likely you meant the closure to end here
| |
| this is the parsed closure...
help: try adding braces
|
2 | let _x = Box::new(|x| {x+1;});
| + +
error: aborting due to previous error
```
Fixes https://github.com/rust-lang/rust/issues/107959.
r? diagnostics
Diffstat (limited to 'compiler/rustc_query_impl/src')
0 files changed, 0 insertions, 0 deletions
