@@ -40,7 +40,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
40
40
self . suggest_missing_parentheses ( err, expr) ;
41
41
self . note_need_for_fn_pointer ( err, expected, expr_ty) ;
42
42
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) ;
44
44
}
45
45
46
46
// Requires that the two types unify, and prints an error message if
@@ -1106,29 +1106,26 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
1106
1106
}
1107
1107
1108
1108
// Report the type inferred by the return statement.
1109
- fn report_closure_infered_return_type (
1109
+ fn report_closure_inferred_return_type (
1110
1110
& self ,
1111
1111
err : & mut DiagnosticBuilder < ' _ > ,
1112
1112
expected : Ty < ' tcx > ,
1113
1113
) {
1114
1114
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
+ ) ;
1132
1129
}
1133
1130
}
1134
1131
}
0 commit comments