@@ -95,7 +95,7 @@ use rustc::middle::region::CodeExtent;
95
95
use rustc:: ty:: subst:: { Kind , Subst , Substs } ;
96
96
use rustc:: traits:: { self , FulfillmentContext , ObligationCause , ObligationCauseCode } ;
97
97
use rustc:: ty:: { ParamTy , LvaluePreference , NoPreference , PreferMutLvalue } ;
98
- use rustc:: ty:: { self , Ty , TyCtxt , Visibility } ;
98
+ use rustc:: ty:: { self , Ty , TyCtxt , Visibility , TypeVariants } ;
99
99
use rustc:: ty:: adjustment:: { Adjust , Adjustment , AutoBorrow } ;
100
100
use rustc:: ty:: fold:: { BottomUpFolder , TypeFoldable } ;
101
101
use rustc:: ty:: maps:: Providers ;
@@ -4302,7 +4302,6 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
4302
4302
expected : Ty < ' tcx > ,
4303
4303
found : Ty < ' tcx > ,
4304
4304
can_suggest : bool ) {
4305
-
4306
4305
// Only suggest changing the return type for methods that
4307
4306
// haven't set a return type at all (and aren't `fn main()` or an impl).
4308
4307
match ( & fn_decl. output , found. is_suggestable ( ) , can_suggest) {
@@ -4316,13 +4315,31 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
4316
4315
}
4317
4316
( & hir:: FunctionRetTy :: DefaultReturn ( span) , _, _) => {
4318
4317
// `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" ) ;
4320
4319
}
4321
4320
( & hir:: FunctionRetTy :: Return ( ref ty) , _, _) => {
4322
4321
// Only point to return type if the expected type is the return type, as if they
4323
4322
// 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
+ }
4326
4343
}
4327
4344
}
4328
4345
}
0 commit comments