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