@@ -377,8 +377,11 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
377377 let Some ( arg_ty) = self . node_ty_opt ( args[ idx] . hir_id ) else {
378378 return false ;
379379 } ;
380- let possible_rcvr_ty = expr_finder. uses . iter ( ) . find_map ( |binding| {
380+ let possible_rcvr_ty = expr_finder. uses . iter ( ) . rev ( ) . find_map ( |binding| {
381381 let possible_rcvr_ty = self . node_ty_opt ( binding. hir_id ) ?;
382+ if possible_rcvr_ty. is_ty_var ( ) {
383+ return None ;
384+ }
382385 // Fudge the receiver, so we can do new inference on it.
383386 let possible_rcvr_ty = possible_rcvr_ty. fold_with ( & mut fudger) ;
384387 let method = self
@@ -390,6 +393,12 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
390393 binding,
391394 )
392395 . ok ( ) ?;
396+ // Make sure we select the same method that we started with...
397+ if Some ( method. def_id )
398+ != self . typeck_results . borrow ( ) . type_dependent_def_id ( call_expr. hir_id )
399+ {
400+ return None ;
401+ }
393402 // Unify the method signature with our incompatible arg, to
394403 // do inference in the *opposite* direction and to find out
395404 // what our ideal rcvr ty would look like.
@@ -460,6 +469,12 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
460469 ) else {
461470 continue ;
462471 } ;
472+ // Make sure we select the same method that we started with...
473+ if Some ( method. def_id )
474+ != self . typeck_results . borrow ( ) . type_dependent_def_id ( parent_expr. hir_id )
475+ {
476+ continue ;
477+ }
463478
464479 let ideal_rcvr_ty = rcvr_ty. fold_with ( & mut fudger) ;
465480 let ideal_method = self
@@ -509,13 +524,19 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
509524 // blame arg, if possible. Don't do this if we're coming from
510525 // arg mismatch code, because we'll possibly suggest a mutually
511526 // incompatible fix at the original mismatch site.
527+ // HACK(compiler-errors): We don't actually consider the implications
528+ // of our inference guesses in `emit_type_mismatch_suggestions`, so
529+ // only suggest things when we know our type error is precisely due to
530+ // a type mismatch, and not via some projection or something. See #116155.
512531 if matches ! ( source, TypeMismatchSource :: Ty ( _) )
513532 && let Some ( ideal_method) = ideal_method
514- && let ideal_arg_ty = self . resolve_vars_if_possible ( ideal_method. sig . inputs ( ) [ idx + 1 ] )
515- // HACK(compiler-errors): We don't actually consider the implications
516- // of our inference guesses in `emit_type_mismatch_suggestions`, so
517- // only suggest things when we know our type error is precisely due to
518- // a type mismatch, and not via some projection or something. See #116155.
533+ && Some ( ideal_method. def_id )
534+ == self
535+ . typeck_results
536+ . borrow ( )
537+ . type_dependent_def_id ( parent_expr. hir_id )
538+ && let ideal_arg_ty =
539+ self . resolve_vars_if_possible ( ideal_method. sig . inputs ( ) [ idx + 1 ] )
519540 && !ideal_arg_ty. has_non_region_infer ( )
520541 {
521542 self . emit_type_mismatch_suggestions (
0 commit comments