Skip to content

Commit 483f13c

Browse files
Make sure that the method resolution matches in note_source_of_type_mismatch_constraint
1 parent aa31bad commit 483f13c

File tree

4 files changed

+46
-35
lines changed

4 files changed

+46
-35
lines changed

compiler/rustc_hir_typeck/src/demand.rs

+27-6
Original file line numberDiff line numberDiff line change
@@ -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(

tests/crashes/118185.rs

-26
This file was deleted.

tests/crashes/118185-2.rs renamed to tests/ui/mismatched_types/diagnostic-method-lookup-returns-sig-with-fewer-args.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
1-
//@ known-bug: #118185
2-
31
fn main() {
42
let target: Target = create_target();
53
target.get(0); // correct arguments work
6-
target.get(10.0); // CRASH HERE
4+
target.get(10.0); // (used to crash here)
5+
//~^ ERROR mismatched types
76
}
87

98
// must be generic
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
error[E0308]: mismatched types
2+
--> $DIR/diagnostic-method-lookup-returns-sig-with-fewer-args.rs:4:16
3+
|
4+
LL | target.get(10.0); // (used to crash here)
5+
| --- ^^^^ expected `i32`, found floating-point number
6+
| |
7+
| arguments to this method are incorrect
8+
|
9+
note: method defined here
10+
--> $DIR/diagnostic-method-lookup-returns-sig-with-fewer-args.rs:22:12
11+
|
12+
LL | pub fn get(&self, data: i32) {
13+
| ^^^ ---------
14+
15+
error: aborting due to 1 previous error
16+
17+
For more information about this error, try `rustc --explain E0308`.

0 commit comments

Comments
 (0)