@@ -377,8 +377,11 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
377
377
let Some ( arg_ty) = self . node_ty_opt ( args[ idx] . hir_id ) else {
378
378
return false ;
379
379
} ;
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| {
381
381
let possible_rcvr_ty = self . node_ty_opt ( binding. hir_id ) ?;
382
+ if possible_rcvr_ty. is_ty_var ( ) {
383
+ return None ;
384
+ }
382
385
// Fudge the receiver, so we can do new inference on it.
383
386
let possible_rcvr_ty = possible_rcvr_ty. fold_with ( & mut fudger) ;
384
387
let method = self
@@ -390,6 +393,12 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
390
393
binding,
391
394
)
392
395
. 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
+ }
393
402
// Unify the method signature with our incompatible arg, to
394
403
// do inference in the *opposite* direction and to find out
395
404
// what our ideal rcvr ty would look like.
@@ -460,6 +469,12 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
460
469
) else {
461
470
continue ;
462
471
} ;
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
+ }
463
478
464
479
let ideal_rcvr_ty = rcvr_ty. fold_with ( & mut fudger) ;
465
480
let ideal_method = self
@@ -509,13 +524,19 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
509
524
// blame arg, if possible. Don't do this if we're coming from
510
525
// arg mismatch code, because we'll possibly suggest a mutually
511
526
// 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.
512
531
if matches ! ( source, TypeMismatchSource :: Ty ( _) )
513
532
&& 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 ] )
519
540
&& !ideal_arg_ty. has_non_region_infer ( )
520
541
{
521
542
self . emit_type_mismatch_suggestions (
0 commit comments