@@ -1141,14 +1141,19 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
1141
1141
#[ instrument( level = "debug" , skip( self , diag, secondary_span, prefer_label) ) ]
1142
1142
pub fn note_type_err (
1143
1143
& self ,
1144
- span : Span ,
1145
1144
diag : & mut Diag < ' _ > ,
1146
1145
cause : & ObligationCause < ' tcx > ,
1147
1146
secondary_span : Option < ( Span , Cow < ' static , str > , bool ) > ,
1148
1147
mut values : Option < ty:: ParamEnvAnd < ' tcx , ValuePairs < ' tcx > > > ,
1149
1148
terr : TypeError < ' tcx > ,
1150
1149
prefer_label : bool ,
1150
+ override_span : Option < Span > ,
1151
1151
) {
1152
+ // We use `override_span` when we want the error to point at a `Span` other than
1153
+ // `cause.span`. This is used in E0271, when a closure is passed in where the return type
1154
+ // isn't what was expected. We want to point at the closure's return type (or expression),
1155
+ // instead of the expression where the closure is passed as call argument.
1156
+ let span = override_span. unwrap_or ( cause. span ) ;
1152
1157
// For some types of errors, expected-found does not make
1153
1158
// sense, so just ignore the values we were given.
1154
1159
if let TypeError :: CyclicTy ( _) = terr {
@@ -1799,13 +1804,13 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
1799
1804
) ;
1800
1805
let mut diag = self . dcx ( ) . create_err ( failure_code) ;
1801
1806
self . note_type_err (
1802
- span,
1803
1807
& mut diag,
1804
1808
& trace. cause ,
1805
1809
None ,
1806
1810
Some ( param_env. and ( trace. values ) ) ,
1807
1811
terr,
1808
1812
false ,
1813
+ None ,
1809
1814
) ;
1810
1815
diag
1811
1816
}
0 commit comments