rust/compiler
Yutaro Ohno 0e42298674 parser: provide better 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:

```
fn main() {
    let _x = Box::new(|x|x+1;);
}
```

the current output is like this:

```
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 | }
  | ^
  |

...

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
```

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
```
2023-02-23 19:05:13 +09:00
..
rustc
rustc_abi
rustc_apfloat
rustc_arena
rustc_ast Use ThinVec in a few more AST types. 2023-02-21 11:51:56 +11:00
rustc_ast_lowering errors: generate typed identifiers in each crate 2023-02-22 09:15:53 +00:00
rustc_ast_passes errors: generate typed identifiers in each crate 2023-02-22 09:15:53 +00:00
rustc_ast_pretty Use ThinVec in various AST types. 2023-02-21 11:51:56 +11:00
rustc_attr errors: generate typed identifiers in each crate 2023-02-22 09:15:53 +00:00
rustc_baked_icu_data
rustc_borrowck Auto merge of #108324 - notriddle:notriddle/assoc-fn-method, r=compiler-errors,davidtwco,estebank,oli-obk 2023-02-23 00:19:12 +00:00
rustc_builtin_macros errors: generate typed identifiers in each crate 2023-02-22 09:15:53 +00:00
rustc_codegen_cranelift Auto merge of #108340 - eggyal:remove_traversal_trait_aliases, r=oli-obk 2023-02-22 18:26:51 +00:00
rustc_codegen_gcc Auto merge of #108340 - eggyal:remove_traversal_trait_aliases, r=oli-obk 2023-02-22 18:26:51 +00:00
rustc_codegen_llvm Auto merge of #108340 - eggyal:remove_traversal_trait_aliases, r=oli-obk 2023-02-22 18:26:51 +00:00
rustc_codegen_ssa Auto merge of #108340 - eggyal:remove_traversal_trait_aliases, r=oli-obk 2023-02-22 18:26:51 +00:00
rustc_const_eval Auto merge of #108340 - eggyal:remove_traversal_trait_aliases, r=oli-obk 2023-02-22 18:26:51 +00:00
rustc_data_structures Auto merge of #108300 - oli-obk:elsa, r=eholk 2023-02-22 08:44:30 +00:00
rustc_driver
rustc_driver_impl various: translation resources from cg backend 2023-02-22 09:15:54 +00:00
rustc_error_codes
rustc_error_messages various: translation resources from cg backend 2023-02-22 09:15:54 +00:00
rustc_errors various: translation resources from cg backend 2023-02-22 09:15:54 +00:00
rustc_expand various: translation resources from cg backend 2023-02-22 09:15:54 +00:00
rustc_feature
rustc_fs_util
rustc_graphviz
rustc_hir diagnostics: if AssocFn has self argument, describe as method 2023-02-22 08:40:33 -07:00
rustc_hir_analysis Auto merge of #108324 - notriddle:notriddle/assoc-fn-method, r=compiler-errors,davidtwco,estebank,oli-obk 2023-02-23 00:19:12 +00:00
rustc_hir_pretty
rustc_hir_typeck Auto merge of #108324 - notriddle:notriddle/assoc-fn-method, r=compiler-errors,davidtwco,estebank,oli-obk 2023-02-23 00:19:12 +00:00
rustc_incremental errors: generate typed identifiers in each crate 2023-02-22 09:15:53 +00:00
rustc_index
rustc_infer Auto merge of #108324 - notriddle:notriddle/assoc-fn-method, r=compiler-errors,davidtwco,estebank,oli-obk 2023-02-23 00:19:12 +00:00
rustc_interface Auto merge of #103042 - davidtwco:translation-distributed-ftl, r=oli-obk 2023-02-22 15:14:22 +00:00
rustc_lexer
rustc_lint Auto merge of #108340 - eggyal:remove_traversal_trait_aliases, r=oli-obk 2023-02-22 18:26:51 +00:00
rustc_lint_defs
rustc_llvm
rustc_log
rustc_macros Auto merge of #108340 - eggyal:remove_traversal_trait_aliases, r=oli-obk 2023-02-22 18:26:51 +00:00
rustc_metadata errors: generate typed identifiers in each crate 2023-02-22 09:15:53 +00:00
rustc_middle Auto merge of #108324 - notriddle:notriddle/assoc-fn-method, r=compiler-errors,davidtwco,estebank,oli-obk 2023-02-23 00:19:12 +00:00
rustc_mir_build Auto merge of #108340 - eggyal:remove_traversal_trait_aliases, r=oli-obk 2023-02-22 18:26:51 +00:00
rustc_mir_dataflow errors: generate typed identifiers in each crate 2023-02-22 09:15:53 +00:00
rustc_mir_transform Remove type-traversal trait aliases 2023-02-22 17:04:58 +00:00
rustc_monomorphize Auto merge of #108340 - eggyal:remove_traversal_trait_aliases, r=oli-obk 2023-02-22 18:26:51 +00:00
rustc_parse parser: provide better errors on closures with braces missing 2023-02-23 19:05:13 +09:00
rustc_parse_format
rustc_passes Auto merge of #108324 - notriddle:notriddle/assoc-fn-method, r=compiler-errors,davidtwco,estebank,oli-obk 2023-02-23 00:19:12 +00:00
rustc_plugin_impl errors: generate typed identifiers in each crate 2023-02-22 09:15:53 +00:00
rustc_privacy Auto merge of #108324 - notriddle:notriddle/assoc-fn-method, r=compiler-errors,davidtwco,estebank,oli-obk 2023-02-23 00:19:12 +00:00
rustc_query_impl Upgrade thin-vec from 0.2.9 to 0.2.12. 2023-02-21 11:51:55 +11:00
rustc_query_system Auto merge of #103042 - davidtwco:translation-distributed-ftl, r=oli-obk 2023-02-22 15:14:22 +00:00
rustc_resolve Auto merge of #108324 - notriddle:notriddle/assoc-fn-method, r=compiler-errors,davidtwco,estebank,oli-obk 2023-02-23 00:19:12 +00:00
rustc_serialize Auto merge of #104754 - nnethercote:more-ThinVec-in-ast, r=the8472 2023-02-21 07:02:57 +00:00
rustc_session Auto merge of #103042 - davidtwco:translation-distributed-ftl, r=oli-obk 2023-02-22 15:14:22 +00:00
rustc_smir
rustc_span Add test for precise algorithm used 2023-02-19 22:59:22 +00:00
rustc_symbol_mangling Auto merge of #108340 - eggyal:remove_traversal_trait_aliases, r=oli-obk 2023-02-22 18:26:51 +00:00
rustc_target
rustc_trait_selection Auto merge of #108324 - notriddle:notriddle/assoc-fn-method, r=compiler-errors,davidtwco,estebank,oli-obk 2023-02-23 00:19:12 +00:00
rustc_traits Remove type-traversal trait aliases 2023-02-22 17:04:58 +00:00
rustc_transmute Remove type-traversal trait aliases 2023-02-22 17:04:58 +00:00
rustc_ty_utils Auto merge of #108340 - eggyal:remove_traversal_trait_aliases, r=oli-obk 2023-02-22 18:26:51 +00:00
rustc_type_ir make first component of dyn* use pointer layout+type, and adjust DynStar comment 2023-02-18 19:24:55 +00:00