@@ -40,7 +40,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
4040 self . suggest_missing_parentheses ( err, expr) ;
4141 self . note_need_for_fn_pointer ( err, expected, expr_ty) ;
4242 self . note_internal_mutation_in_method ( err, expr, expected, expr_ty) ;
43- self . report_closure_infered_return_type ( err, expected)
43+ self . report_closure_inferred_return_type ( err, expected) ;
4444 }
4545
4646 // Requires that the two types unify, and prints an error message if
@@ -1106,29 +1106,26 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
11061106 }
11071107
11081108 // Report the type inferred by the return statement.
1109- fn report_closure_infered_return_type (
1109+ fn report_closure_inferred_return_type (
11101110 & self ,
11111111 err : & mut DiagnosticBuilder < ' _ > ,
11121112 expected : Ty < ' tcx > ,
11131113 ) {
11141114 if let Some ( sp) = self . ret_coercion_span . get ( ) {
1115- // If the closure has an explicit return type annotation,
1116- // then a type error may occur at the first return expression we
1117- // see in the closure (if it conflicts with the declared
1118- // return type). Skip adding a note in this case, since it
1119- // would be incorrect.
1120- if !err. span . primary_spans ( ) . iter ( ) . any ( |& span| span == sp) {
1121- let hir = self . tcx . hir ( ) ;
1122- let body_owner = hir. body_owned_by ( hir. enclosing_body_owner ( self . body_id ) ) ;
1123- if self . tcx . is_closure ( hir. body_owner_def_id ( body_owner) . to_def_id ( ) ) {
1124- err. span_note (
1125- sp,
1126- & format ! (
1127- "return type inferred to be `{}` here" ,
1128- self . resolve_vars_if_possible( expected)
1129- ) ,
1130- ) ;
1131- }
1115+ // If the closure has an explicit return type annotation, or if
1116+ // the closure's return type has been inferred from outside
1117+ // requirements (such as an Fn* trait bound), then a type error
1118+ // may occur at the first return expression we see in the closure
1119+ // (if it conflicts with the declared return type). Skip adding a
1120+ // note in this case, since it would be incorrect.
1121+ if !self . return_type_pre_known {
1122+ err. span_note (
1123+ sp,
1124+ & format ! (
1125+ "return type inferred to be `{}` here" ,
1126+ self . resolve_vars_if_possible( expected)
1127+ ) ,
1128+ ) ;
11321129 }
11331130 }
11341131 }
0 commit comments