]> git.lizzy.rs Git - rust.git/commitdiff
Suggest fn ptr rather than fn item and suggest to use `Fn` trait bounds rather than...
authorThePuzzlemaker <tpzker@thepuzzlemaker.info>
Tue, 22 Dec 2020 02:23:21 +0000 (20:23 -0600)
committerThePuzzlemaker <tpzker@thepuzzlemaker.info>
Tue, 22 Dec 2020 19:26:28 +0000 (13:26 -0600)
This is a squash of the titular commit along with these minor commits:
- Improve note
- Improve note pt2

compiler/rustc_typeck/src/collect.rs

index bc6b2037c184ef652993a56059d60ba87d137cae..ebbcf6304b65a00878400928e1049f79db3a2efd 100644 (file)
@@ -1544,12 +1544,27 @@ fn fn_sig(tcx: TyCtxt<'_>, def_id: DefId) -> ty::PolyFnSig<'_> {
                     let mut diag = bad_placeholder_type(tcx, visitor.0);
                     let ret_ty = fn_sig.output();
                     if ret_ty != tcx.ty_error() {
-                        diag.span_suggestion(
-                            ty.span,
-                            "replace with the correct return type",
-                            ret_ty.to_string(),
-                            Applicability::MaybeIncorrect,
-                        );
+                        if !ret_ty.is_closure() {
+                            let ret_ty_str = match ret_ty.kind() {
+                                // Suggest a function pointer return type instead of a unique function definition
+                                // (e.g. `fn() -> i32` instead of `fn() -> i32 { f }`, the latter of which is invalid
+                                // syntax)
+                                ty::FnDef(..) => ret_ty.fn_sig(tcx).to_string(),
+                                _ => ret_ty.to_string(),
+                            };
+                            diag.span_suggestion(
+                                ty.span,
+                                "replace with the correct return type",
+                                ret_ty_str,
+                                Applicability::MaybeIncorrect,
+                            );
+                        } else {
+                            // We're dealing with a closure, so we should suggest using `impl Fn` or trait bounds
+                            // to prevent the user from getting a papercut while trying to use the unique closure
+                            // syntax (e.g. `[closure@src/lib.rs:2:5: 2:9]`).
+                            diag.help("consider using an `Fn`, `FnMut`, or `FnOnce` trait bound");
+                            diag.note("for more information on `Fn` traits and closure types, see https://doc.rust-lang.org/book/ch13-01-closures.html");
+                        }
                     }
                     diag.emit();
                     ty::Binder::bind(fn_sig)