@@ -61,7 +61,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
6161 || self . suggest_into ( err, expr, expr_ty, expected)
6262 || self . suggest_floating_point_literal ( err, expr, expected) ;
6363 if !suggested {
64- self . point_at_expr_source_of_inferred_type ( err, expr, expr_ty, expected) ;
64+ self . point_at_expr_source_of_inferred_type ( err, expr, expr_ty, expected, expr . span ) ;
6565 }
6666 }
6767
@@ -222,6 +222,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
222222 expr : & hir:: Expr < ' _ > ,
223223 found : Ty < ' tcx > ,
224224 expected : Ty < ' tcx > ,
225+ mismatch_span : Span ,
225226 ) -> bool {
226227 let map = self . tcx . hir ( ) ;
227228
@@ -281,7 +282,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
281282 } ,
282283 } ;
283284 let mut prev = eraser. fold_ty ( ty) ;
284- let mut prev_span = None ;
285+ let mut prev_span: Option < Span > = None ;
285286
286287 for binding in expr_finder. uses {
287288 // In every expression where the binding is referenced, we will look at that
@@ -334,13 +335,13 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
334335 let arg = & args[ i] ;
335336 let arg_ty = self . node_ty ( arg. hir_id ) ;
336337 if !arg. span . overlaps ( mismatch_span) {
337- err. span_label (
338- arg. span ,
339- & format ! (
340- "this is of type `{arg_ty}`, which causes `{ident}` to be \
341- inferred as `{ty}`",
342- ) ,
343- ) ;
338+ err. span_label (
339+ arg. span ,
340+ & format ! (
341+ "this is of type `{arg_ty}`, which causes `{ident}` to be \
342+ inferred as `{ty}`",
343+ ) ,
344+ ) ;
344345 }
345346 param_args. insert ( param_ty, ( arg, arg_ty) ) ;
346347 }
@@ -384,12 +385,13 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
384385 && self . can_eq ( self . param_env , ty, found) . is_ok ( )
385386 {
386387 // We only point at the first place where the found type was inferred.
388+ if !segment. ident . span . overlaps ( mismatch_span) {
387389 err. span_label (
388390 segment. ident . span ,
389391 with_forced_trimmed_paths ! ( format!(
390392 "here the type of `{ident}` is inferred to be `{ty}`" ,
391393 ) ) ,
392- ) ;
394+ ) ; }
393395 break ;
394396 } else if !param_args. is_empty ( ) {
395397 break ;
@@ -408,12 +410,14 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
408410 // We use the *previous* span because if the type is known *here* it means
409411 // it was *evaluated earlier*. We don't do this for method calls because we
410412 // evaluate the method's self type eagerly, but not in any other case.
411- err. span_label (
412- span,
413- with_forced_trimmed_paths ! ( format!(
414- "here the type of `{ident}` is inferred to be `{ty}`" ,
415- ) ) ,
416- ) ;
413+ if !span. overlaps ( mismatch_span) {
414+ err. span_label (
415+ span,
416+ with_forced_trimmed_paths ! ( format!(
417+ "here the type of `{ident}` is inferred to be `{ty}`" ,
418+ ) ) ,
419+ ) ;
420+ }
417421 break ;
418422 }
419423 prev = ty;
0 commit comments