@@ -95,7 +95,7 @@ use rustc::middle::region::CodeExtent;
9595use rustc:: ty:: subst:: { Kind , Subst , Substs } ;
9696use rustc:: traits:: { self , FulfillmentContext , ObligationCause , ObligationCauseCode } ;
9797use rustc:: ty:: { ParamTy , LvaluePreference , NoPreference , PreferMutLvalue } ;
98- use rustc:: ty:: { self , Ty , TyCtxt , Visibility } ;
98+ use rustc:: ty:: { self , Ty , TyCtxt , Visibility , TypeVariants } ;
9999use rustc:: ty:: adjustment:: { Adjust , Adjustment , AutoBorrow } ;
100100use rustc:: ty:: fold:: { BottomUpFolder , TypeFoldable } ;
101101use rustc:: ty:: maps:: Providers ;
@@ -4302,7 +4302,6 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
43024302 expected : Ty < ' tcx > ,
43034303 found : Ty < ' tcx > ,
43044304 can_suggest : bool ) {
4305-
43064305 // Only suggest changing the return type for methods that
43074306 // haven't set a return type at all (and aren't `fn main()` or an impl).
43084307 match ( & fn_decl. output , found. is_suggestable ( ) , can_suggest) {
@@ -4316,13 +4315,31 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
43164315 }
43174316 ( & hir:: FunctionRetTy :: DefaultReturn ( span) , _, _) => {
43184317 // `fn main()` must return `()`, do not suggest changing return type
4319- err. span_label ( span, "expected `()` because of default return type" ) ;
4318+ err. span_label ( span, "expected () because of default return type" ) ;
43204319 }
43214320 ( & hir:: FunctionRetTy :: Return ( ref ty) , _, _) => {
43224321 // Only point to return type if the expected type is the return type, as if they
43234322 // are not, the expectation must have been caused by something else.
4324- err. span_label ( ty. span ,
4325- format ! ( "expected `{}` because of return type" , expected) ) ;
4323+ debug ! ( "suggest_missing_return_type: return type {:?} node {:?}" , ty, ty. node) ;
4324+ let sp = ty. span ;
4325+ let ty = AstConv :: ast_ty_to_ty ( self , ty) ;
4326+ debug ! ( "suggest_missing_return_type: return type sty {:?}" , ty. sty) ;
4327+ debug ! ( "suggest_missing_return_type: expected type sty {:?}" , ty. sty) ;
4328+ if ty. sty == expected. sty {
4329+ let quote = if let TypeVariants :: TyTuple ( ref slice, _) = expected. sty {
4330+ if slice. len ( ) == 0 { // don't use backtics for ()
4331+ ""
4332+ } else {
4333+ "`"
4334+ }
4335+ } else {
4336+ "`"
4337+ } ;
4338+ err. span_label ( sp, format ! ( "expected {}{}{} because of return type" ,
4339+ quote,
4340+ expected,
4341+ quote) ) ;
4342+ }
43264343 }
43274344 }
43284345 }
0 commit comments