@@ -1386,14 +1386,19 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
1386
1386
#[ instrument( level = "debug" , skip( self , diag, secondary_span, prefer_label) ) ]
1387
1387
pub fn note_type_err (
1388
1388
& self ,
1389
- span : Span ,
1390
1389
diag : & mut Diag < ' _ > ,
1391
1390
cause : & ObligationCause < ' tcx > ,
1392
1391
secondary_span : Option < ( Span , Cow < ' static , str > , bool ) > ,
1393
1392
mut values : Option < ty:: ParamEnvAnd < ' tcx , ValuePairs < ' tcx > > > ,
1394
1393
terr : TypeError < ' tcx > ,
1395
1394
prefer_label : bool ,
1395
+ override_span : Option < Span > ,
1396
1396
) {
1397
+ // We use `override_span` when we want the error to point at a `Span` other than
1398
+ // `cause.span`. This is used in E0271, when a closure is passed in where the return type
1399
+ // isn't what was expected. We want to point at the closure's return type (or expression),
1400
+ // instead of the expression where the closure is passed as call argument.
1401
+ let span = override_span. unwrap_or ( cause. span ) ;
1397
1402
// For some types of errors, expected-found does not make
1398
1403
// sense, so just ignore the values we were given.
1399
1404
if let TypeError :: CyclicTy ( _) = terr {
@@ -2052,13 +2057,13 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
2052
2057
) ;
2053
2058
let mut diag = self . dcx ( ) . create_err ( failure_code) ;
2054
2059
self . note_type_err (
2055
- span,
2056
2060
& mut diag,
2057
2061
& trace. cause ,
2058
2062
None ,
2059
2063
Some ( param_env. and ( trace. values ) ) ,
2060
2064
terr,
2061
2065
false ,
2066
+ None ,
2062
2067
) ;
2063
2068
diag
2064
2069
}
0 commit comments