rust/compiler
Dylan DPC 12ac312351
Rollup merge of #80284 - ThePuzzlemaker:issue-80179-fix, r=varkor
Suggest fn ptr rather than fn item and suggest to use `Fn` trait bounds rather than the unique closure type in E0121

Previously, using `_` as a return type in a function that returned a function/closure would provide a diagnostic that would cause a papercut. For example:
```rust
fn f() -> i32 { 0 }
fn fn_ptr() -> _ { f }
fn closure() -> _ { || 0 }
```
would result in this diagnostic:
```rust
error[E0121]: the type placeholder `_` is not allowed within types on item signatures
 --> <anon>:2:16
  |
2 | fn fn_ptr() -> _ { f }
  |                ^
  |                |
  |                not allowed in type signatures
  |                help: replace with the correct return type: `fn() -> i32 {f}`

error[E0121]: the type placeholder `_` is not allowed within types on item signatures
 --> <anon>:3:17
  |
3 | fn closure() -> _ { || 0 }
  |                 ^
  |                 |
  |                 not allowed in type signatures
  |                 help: replace with the correct return type: `[closure@<anon>:3:21: 3:25]`

error: aborting due to 2 previous errors

For more information about this error, try `rustc --explain E0121`.
```
As can be seen, it was suggested to use the function definition return type `fn() -> i32 { f }` which is not valid syntax as a return type. Additionally, closures cause a papercut as unique closure types (notated in this case as `[closure@<anon>:3:21: 3:25]`) are not valid syntax either.

Instead, this PR implements this version of the diagnostic (this example is for the same code featured above):
```rust
error[E0121]: the type placeholder `_` is not allowed within types on item signatures
 --> <anon>:2:16
  |
2 | fn fn_ptr() -> _ { f }
  |                ^
  |                |
  |                not allowed in type signatures
  |                help: replace with the correct return type: `fn() -> i32`

error[E0121]: the type placeholder `_` is not allowed within types on item signatures
 --> <anon>:3:17
  |
3 | fn closure() -> _ { || 0 }
  |                 ^ not allowed in type signatures
  |
  = help: consider using an `Fn`, `FnMut`, or `FnOnce` trait bound
  = note: for more information on `Fn` traits and closure types, see https://doc.rust-lang.org/book/ch13-01-closures.html

error: aborting due to 2 previous errors

For more information about this error, try `rustc --explain E0121`.
```
As can be seen in this diagnostic, the papercut for returning a function item is fixed by suggesting the usage of a function pointer as the return type. As for closures, it's suggested to use an `Fn`, `FnMut`, or `FnOnce` trait bound (with further reading on closures and `Fn` traits in *The Book* for beginners). I did not implement a suggestion to use `impl Fn() -> i32` syntax as that was out-of-scope for my abilities at the moment, therefore someone in the future may want to implement that. Also, it's possible to use either `impl Trait` syntax, generics, or generics with a `where` clause, and some users may not want to use `impl Trait` syntax for their own reasons.

This PR fixes #80179.
2020-12-28 14:13:08 +01:00
..
rustc
rustc_apfloat
rustc_arena stabilize min_const_generics 2020-12-26 18:24:10 +01:00
rustc_ast stabilize min_const_generics 2020-12-26 18:24:10 +01:00
rustc_ast_lowering Rollup merge of #79051 - LeSeulArtichaut:if-let-guard, r=matthewjasper 2020-12-17 11:43:55 +09:00
rustc_ast_passes stabilize min_const_generics 2020-12-26 18:24:10 +01:00
rustc_ast_pretty Fix pretty printing an AST representing &(mut ident) 2020-12-20 13:11:07 +01:00
rustc_attr Get rid of clean::Deprecation 2020-12-14 22:00:46 -05:00
rustc_builtin_macros Add some intra-doc links to compiler docs 2020-12-22 09:54:23 -05:00
rustc_codegen_cranelift Make BoundRegion have a kind of BoungRegionKind 2020-12-18 15:27:28 -05:00
rustc_codegen_llvm Rollup merge of #79662 - bjorn3:move_more_code_out_of_codegen_backend, r=oli-obk 2020-12-28 14:12:59 +01:00
rustc_codegen_ssa Rollup merge of #79662 - bjorn3:move_more_code_out_of_codegen_backend, r=oli-obk 2020-12-28 14:12:59 +01:00
rustc_data_structures stabilize min_const_generics 2020-12-26 18:24:10 +01:00
rustc_driver
rustc_error_codes update error codes 2020-12-26 18:24:10 +01:00
rustc_errors Switch compiler/ to intra-doc links 2020-12-18 15:22:51 -05:00
rustc_expand Revert "Promote missing_fragment_specifier to hard error" 2020-12-22 09:33:16 -05:00
rustc_feature stabilize min_const_generics 2020-12-26 18:24:10 +01:00
rustc_fs_util
rustc_graphviz
rustc_hir Auto merge of #79073 - davidtwco:issue-78957-const-param-attrs, r=lcnr 2020-12-19 04:32:50 +00:00
rustc_hir_pretty Rollup merge of #79051 - LeSeulArtichaut:if-let-guard, r=matthewjasper 2020-12-17 11:43:55 +09:00
rustc_incremental
rustc_index Switch compiler/ to intra-doc links 2020-12-18 15:22:51 -05:00
rustc_infer Prevent caching projections in the case of cycles 2020-12-20 21:47:51 +00:00
rustc_interface Rollup merge of #79662 - bjorn3:move_more_code_out_of_codegen_backend, r=oli-obk 2020-12-28 14:12:59 +01:00
rustc_lexer Fix typo 2020-12-18 22:13:25 +09:00
rustc_lint stabilize min_const_generics 2020-12-26 18:24:10 +01:00
rustc_lint_defs Add example to lint docs 2020-12-22 09:33:16 -05:00
rustc_llvm llvm: update ffi bindings for split dwarf 2020-12-16 10:31:42 +00:00
rustc_macros Stop using intermediate macros in definition of symbols 2020-12-17 15:20:45 -08:00
rustc_metadata Rollup merge of #80039 - LeSeulArtichaut:rm-tyencoder-tcx, r=matthewjasper 2020-12-17 11:44:03 +09:00
rustc_middle stabilize min_const_generics 2020-12-26 18:24:10 +01:00
rustc_mir Auto merge of #80235 - RalfJung:validate-promoteds, r=oli-obk 2020-12-25 18:25:48 +00:00
rustc_mir_build Fix a comment 2020-12-22 15:20:24 +00:00
rustc_parse stabilize min_const_generics 2020-12-26 18:24:10 +01:00
rustc_parse_format
rustc_passes Auto merge of #79073 - davidtwco:issue-78957-const-param-attrs, r=lcnr 2020-12-19 04:32:50 +00:00
rustc_plugin_impl
rustc_privacy Move binder for dyn to each list item 2020-12-11 15:02:46 -05:00
rustc_query_system rustc_query_system: avoid race condition when using edge_count 2020-12-22 14:12:57 -08:00
rustc_resolve stabilize min_const_generics 2020-12-26 18:24:10 +01:00
rustc_save_analysis Rework beautify_doc_string so that it returns a Symbol instead of a String 2020-12-22 16:05:38 +01:00
rustc_serialize stabilize min_const_generics 2020-12-26 18:24:10 +01:00
rustc_session Revert "Promote missing_fragment_specifier to hard error" 2020-12-22 09:33:16 -05:00
rustc_span Auto merge of #79762 - Swatinem:remap-doctest-coverage, r=Swatinem 2020-12-25 02:37:08 +00:00
rustc_symbol_mangling Make BoundRegion have a kind of BoungRegionKind 2020-12-18 15:27:28 -05:00
rustc_target Add support for target aliases 2020-12-16 10:41:07 +01:00
rustc_trait_selection Auto merge of #80246 - matthewjasper:projection-cycle-caching, r=Mark-Simulacrum 2020-12-26 00:11:30 +00:00
rustc_traits Make BoundRegion have a kind of BoungRegionKind 2020-12-18 15:27:28 -05:00
rustc_ty_utils
rustc_type_ir Fix typo in DebruijnIndex documentation 2020-12-12 16:13:06 +01:00
rustc_typeck Rollup merge of #80284 - ThePuzzlemaker:issue-80179-fix, r=varkor 2020-12-28 14:13:08 +01:00