@@ -41,7 +41,7 @@ use rustc_middle::ty::subst::InternalSubsts;
4141use rustc_middle:: ty:: util:: Discr ;
4242use rustc_middle:: ty:: util:: IntTypeExt ;
4343use rustc_middle:: ty:: { self , AdtKind , Const , DefIdTree , Ty , TyCtxt } ;
44- use rustc_middle:: ty:: { ReprOptions , ToPredicate , TypeFoldable } ;
44+ use rustc_middle:: ty:: { ReprOptions , ToPredicate } ;
4545use rustc_session:: lint;
4646use rustc_session:: parse:: feature_err;
4747use rustc_span:: symbol:: { kw, sym, Ident , Symbol } ;
@@ -2004,28 +2004,29 @@ fn infer_return_ty_for_fn_sig<'tcx>(
20042004 visitor. visit_ty ( ty) ;
20052005 let mut diag = bad_placeholder ( tcx, visitor. 0 , "return type" ) ;
20062006 let ret_ty = fn_sig. skip_binder ( ) . output ( ) ;
2007- if !ret_ty. references_error ( ) {
2008- if !ret_ty. is_closure ( ) {
2009- let ret_ty_str = match ret_ty. kind ( ) {
2010- // Suggest a function pointer return type instead of a unique function definition
2011- // (e.g. `fn() -> i32` instead of `fn() -> i32 { f }`, the latter of which is invalid
2012- // syntax)
2013- ty:: FnDef ( ..) => ret_ty. fn_sig ( tcx) . to_string ( ) ,
2014- _ => ret_ty. to_string ( ) ,
2015- } ;
2007+ if ret_ty. is_suggestable ( ) {
2008+ diag. span_suggestion (
2009+ ty. span ,
2010+ "replace with the correct return type" ,
2011+ ret_ty. to_string ( ) ,
2012+ Applicability :: MachineApplicable ,
2013+ ) ;
2014+ } else if matches ! ( ret_ty. kind( ) , ty:: FnDef ( ..) ) {
2015+ let fn_sig = ret_ty. fn_sig ( tcx) ;
2016+ if fn_sig. skip_binder ( ) . inputs_and_output . iter ( ) . all ( |t| t. is_suggestable ( ) ) {
20162017 diag. span_suggestion (
20172018 ty. span ,
20182019 "replace with the correct return type" ,
2019- ret_ty_str ,
2020- Applicability :: MaybeIncorrect ,
2020+ fn_sig . to_string ( ) ,
2021+ Applicability :: MachineApplicable ,
20212022 ) ;
2022- } else {
2023- // We're dealing with a closure, so we should suggest using `impl Fn` or trait bounds
2024- // to prevent the user from getting a papercut while trying to use the unique closure
2025- // syntax (e.g. `[closure@src/lib.rs:2:5: 2:9]`).
2026- diag. help ( "consider using an `Fn`, `FnMut`, or `FnOnce` trait bound" ) ;
2027- diag. note ( "for more information on `Fn` traits and closure types, see https://doc.rust-lang.org/book/ch13-01-closures.html" ) ;
20282023 }
2024+ } else if ret_ty. is_closure ( ) {
2025+ // We're dealing with a closure, so we should suggest using `impl Fn` or trait bounds
2026+ // to prevent the user from getting a papercut while trying to use the unique closure
2027+ // syntax (e.g. `[closure@src/lib.rs:2:5: 2:9]`).
2028+ diag. help ( "consider using an `Fn`, `FnMut`, or `FnOnce` trait bound" ) ;
2029+ diag. note ( "for more information on `Fn` traits and closure types, see https://doc.rust-lang.org/book/ch13-01-closures.html" ) ;
20292030 }
20302031 diag. emit ( ) ;
20312032
0 commit comments