@@ -1141,14 +1141,19 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
11411141 #[ instrument( level = "debug" , skip( self , diag, secondary_span, prefer_label) ) ]
11421142 pub fn note_type_err (
11431143 & self ,
1144- span : Span ,
11451144 diag : & mut Diag < ' _ > ,
11461145 cause : & ObligationCause < ' tcx > ,
11471146 secondary_span : Option < ( Span , Cow < ' static , str > , bool ) > ,
11481147 mut values : Option < ty:: ParamEnvAnd < ' tcx , ValuePairs < ' tcx > > > ,
11491148 terr : TypeError < ' tcx > ,
11501149 prefer_label : bool ,
1150+ override_span : Option < Span > ,
11511151 ) {
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 ) ;
11521157 // For some types of errors, expected-found does not make
11531158 // sense, so just ignore the values we were given.
11541159 if let TypeError :: CyclicTy ( _) = terr {
@@ -1799,13 +1804,13 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
17991804 ) ;
18001805 let mut diag = self . dcx ( ) . create_err ( failure_code) ;
18011806 self . note_type_err (
1802- span,
18031807 & mut diag,
18041808 & trace. cause ,
18051809 None ,
18061810 Some ( param_env. and ( trace. values ) ) ,
18071811 terr,
18081812 false ,
1813+ None ,
18091814 ) ;
18101815 diag
18111816 }
0 commit comments