From 89086f7d36e3a692b48cae8a408057734f044567 Mon Sep 17 00:00:00 2001 From: Michael Goulet Date: Wed, 4 Jan 2023 00:26:53 +0000 Subject: [PATCH] Restore Fn trait note --- compiler/rustc_hir_analysis/src/collect.rs | 6 +++--- src/test/ui/fn/issue-80179.rs | 1 + src/test/ui/fn/issue-80179.stderr | 2 ++ src/test/ui/fn/suggest-return-closure.rs | 3 +++ src/test/ui/fn/suggest-return-closure.stderr | 10 ++++++++-- 5 files changed, 17 insertions(+), 5 deletions(-) diff --git a/compiler/rustc_hir_analysis/src/collect.rs b/compiler/rustc_hir_analysis/src/collect.rs index 53a58df0916f..cf847047c906 100644 --- a/compiler/rustc_hir_analysis/src/collect.rs +++ b/compiler/rustc_hir_analysis/src/collect.rs @@ -1231,10 +1231,10 @@ fn infer_return_ty_for_fn_sig<'tcx>( Applicability::MachineApplicable, ); } else if ret_ty.is_closure() { - // 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"); + } + // Also note how `Fn` traits work just in case! + if ret_ty.is_closure() { diag.note( "for more information on `Fn` traits and closure types, see \ https://doc.rust-lang.org/book/ch13-01-closures.html", diff --git a/src/test/ui/fn/issue-80179.rs b/src/test/ui/fn/issue-80179.rs index bea8deadc898..35e39bebb29a 100644 --- a/src/test/ui/fn/issue-80179.rs +++ b/src/test/ui/fn/issue-80179.rs @@ -20,6 +20,7 @@ fn returns_closure() -> _ { //~| NOTE not allowed in type signatures //~| HELP replace with an appropriate return type //~| SUGGESTION impl Fn() -> i32 +//~| NOTE for more information on `Fn` traits and closure types || 0 } diff --git a/src/test/ui/fn/issue-80179.stderr b/src/test/ui/fn/issue-80179.stderr index dff8252b3715..f5d6c44db751 100644 --- a/src/test/ui/fn/issue-80179.stderr +++ b/src/test/ui/fn/issue-80179.stderr @@ -15,6 +15,8 @@ LL | fn returns_closure() -> _ { | | | not allowed in type signatures | help: replace with an appropriate return type: `impl Fn() -> i32` + | + = 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 diff --git a/src/test/ui/fn/suggest-return-closure.rs b/src/test/ui/fn/suggest-return-closure.rs index 1b9094cea38e..33daa1ea0b40 100644 --- a/src/test/ui/fn/suggest-return-closure.rs +++ b/src/test/ui/fn/suggest-return-closure.rs @@ -3,6 +3,7 @@ fn fn_once() -> _ { //~| NOTE not allowed in type signatures //~| HELP replace with an appropriate return type //~| SUGGESTION impl FnOnce() + //~| NOTE for more information on `Fn` traits and closure types let x = String::new(); || { drop(x); @@ -14,6 +15,7 @@ fn fn_mut() -> _ { //~| NOTE not allowed in type signatures //~| HELP replace with an appropriate return type //~| SUGGESTION impl FnMut(char) + //~| NOTE for more information on `Fn` traits and closure types let x = String::new(); |c| { x.push(c); @@ -25,6 +27,7 @@ fn fun() -> _ { //~| NOTE not allowed in type signatures //~| HELP replace with an appropriate return type //~| SUGGESTION impl Fn() -> i32 + //~| NOTE for more information on `Fn` traits and closure types || 1i32 } diff --git a/src/test/ui/fn/suggest-return-closure.stderr b/src/test/ui/fn/suggest-return-closure.stderr index f4b2c7f52740..341044469ea3 100644 --- a/src/test/ui/fn/suggest-return-closure.stderr +++ b/src/test/ui/fn/suggest-return-closure.stderr @@ -6,24 +6,30 @@ LL | fn fn_once() -> _ { | | | not allowed in type signatures | help: replace with an appropriate return type: `impl FnOnce()` + | + = note: for more information on `Fn` traits and closure types, see https://doc.rust-lang.org/book/ch13-01-closures.html error[E0121]: the placeholder `_` is not allowed within types on item signatures for return types - --> $DIR/suggest-return-closure.rs:12:16 + --> $DIR/suggest-return-closure.rs:13:16 | LL | fn fn_mut() -> _ { | ^ | | | not allowed in type signatures | help: replace with an appropriate return type: `impl FnMut(char)` + | + = note: for more information on `Fn` traits and closure types, see https://doc.rust-lang.org/book/ch13-01-closures.html error[E0121]: the placeholder `_` is not allowed within types on item signatures for return types - --> $DIR/suggest-return-closure.rs:23:13 + --> $DIR/suggest-return-closure.rs:25:13 | LL | fn fun() -> _ { | ^ | | | not allowed in type signatures | help: replace with an appropriate return type: `impl Fn() -> i32` + | + = 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 3 previous errors