@@ -4347,11 +4347,15 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
4347
4347
struct_span_err ! ( self . tcx. sess, expr. span, E0572 ,
4348
4348
"return statement outside of function body" ) . emit ( ) ;
4349
4349
} else if let Some ( ref e) = * expr_opt {
4350
- * self . ret_coercion_span . borrow_mut ( ) = Some ( e. span ) ;
4350
+ if self . ret_coercion_span . borrow ( ) . is_none ( ) {
4351
+ * self . ret_coercion_span . borrow_mut ( ) = Some ( e. span ) ;
4352
+ }
4351
4353
self . check_return_expr ( e) ;
4352
4354
} else {
4353
4355
let mut coercion = self . ret_coercion . as_ref ( ) . unwrap ( ) . borrow_mut ( ) ;
4354
- * self . ret_coercion_span . borrow_mut ( ) = Some ( expr. span ) ;
4356
+ if self . ret_coercion_span . borrow ( ) . is_none ( ) {
4357
+ * self . ret_coercion_span . borrow_mut ( ) = Some ( expr. span ) ;
4358
+ }
4355
4359
let cause = self . cause ( expr. span , ObligationCauseCode :: ReturnNoExpression ) ;
4356
4360
if let Some ( ( fn_decl, _) ) = self . get_fn_decl ( expr. id ) {
4357
4361
coercion. coerce_forced_unit (
@@ -5081,12 +5085,15 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
5081
5085
found : Ty < ' tcx > ,
5082
5086
cause_span : Span ,
5083
5087
blk_id : ast:: NodeId ,
5084
- ) {
5088
+ ) -> bool {
5085
5089
self . suggest_missing_semicolon ( err, expression, expected, cause_span) ;
5090
+ let mut pointing_at_return_type = false ;
5086
5091
if let Some ( ( fn_decl, can_suggest) ) = self . get_fn_decl ( blk_id) {
5087
- self . suggest_missing_return_type ( err, & fn_decl, expected, found, can_suggest) ;
5092
+ pointing_at_return_type = self . suggest_missing_return_type (
5093
+ err, & fn_decl, expected, found, can_suggest) ;
5088
5094
}
5089
5095
self . suggest_ref_or_into ( err, expression, expected, found) ;
5096
+ pointing_at_return_type
5090
5097
}
5091
5098
5092
5099
pub fn suggest_ref_or_into (
@@ -5185,12 +5192,14 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
5185
5192
/// This routine checks if the return type is left as default, the method is not part of an
5186
5193
/// `impl` block and that it isn't the `main` method. If so, it suggests setting the return
5187
5194
/// type.
5188
- fn suggest_missing_return_type ( & self ,
5189
- err : & mut DiagnosticBuilder < ' tcx > ,
5190
- fn_decl : & hir:: FnDecl ,
5191
- expected : Ty < ' tcx > ,
5192
- found : Ty < ' tcx > ,
5193
- can_suggest : bool ) {
5195
+ fn suggest_missing_return_type (
5196
+ & self ,
5197
+ err : & mut DiagnosticBuilder < ' tcx > ,
5198
+ fn_decl : & hir:: FnDecl ,
5199
+ expected : Ty < ' tcx > ,
5200
+ found : Ty < ' tcx > ,
5201
+ can_suggest : bool ,
5202
+ ) -> bool {
5194
5203
// Only suggest changing the return type for methods that
5195
5204
// haven't set a return type at all (and aren't `fn main()` or an impl).
5196
5205
match ( & fn_decl. output , found. is_suggestable ( ) , can_suggest, expected. is_unit ( ) ) {
@@ -5200,16 +5209,19 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
5200
5209
"try adding a return type" ,
5201
5210
format ! ( "-> {} " , self . resolve_type_vars_with_obligations( found) ) ,
5202
5211
Applicability :: MachineApplicable ) ;
5212
+ true
5203
5213
}
5204
5214
( & hir:: FunctionRetTy :: DefaultReturn ( span) , false , true , true ) => {
5205
5215
err. span_label ( span, "possibly return type missing here?" ) ;
5216
+ true
5206
5217
}
5207
5218
( & hir:: FunctionRetTy :: DefaultReturn ( span) , _, false , true ) => {
5208
5219
// `fn main()` must return `()`, do not suggest changing return type
5209
5220
err. span_label ( span, "expected `()` because of default return type" ) ;
5221
+ true
5210
5222
}
5211
5223
// expectation was caused by something else, not the default return
5212
- ( & hir:: FunctionRetTy :: DefaultReturn ( _) , _, _, false ) => { }
5224
+ ( & hir:: FunctionRetTy :: DefaultReturn ( _) , _, _, false ) => false ,
5213
5225
( & hir:: FunctionRetTy :: Return ( ref ty) , _, _, _) => {
5214
5226
// Only point to return type if the expected type is the return type, as if they
5215
5227
// are not, the expectation must have been caused by something else.
@@ -5221,7 +5233,9 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
5221
5233
if ty. sty == expected. sty {
5222
5234
err. span_label ( sp, format ! ( "expected `{}` because of return type" ,
5223
5235
expected) ) ;
5236
+ return true ;
5224
5237
}
5238
+ false
5225
5239
}
5226
5240
}
5227
5241
}
0 commit comments