Skip to content

Commit f0038a7

Browse files
committed
Auto merge of #124227 - compiler-errors:hack-check-method-res, r=estebank
Make sure that the method resolution matches in `note_source_of_type_mismatch_constraint` `note_source_of_type_mismatch_constraint` is a pile of hacks that I implemented to cover up another pile of hacks. It does a bunch of re-confirming methods, but it wasn't previously checking that the methods it was looking (back) up were equal to the methods we previously had. This PR adds those checks. Fixes #118185
2 parents bec1029 + 483f13c commit f0038a7

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
@@ -373,8 +373,11 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
373373
let Some(arg_ty) = self.node_ty_opt(args[idx].hir_id) else {
374374
return false;
375375
};
376-
let possible_rcvr_ty = expr_finder.uses.iter().find_map(|binding| {
376+
let possible_rcvr_ty = expr_finder.uses.iter().rev().find_map(|binding| {
377377
let possible_rcvr_ty = self.node_ty_opt(binding.hir_id)?;
378+
if possible_rcvr_ty.is_ty_var() {
379+
return None;
380+
}
378381
// Fudge the receiver, so we can do new inference on it.
379382
let possible_rcvr_ty = possible_rcvr_ty.fold_with(&mut fudger);
380383
let method = self
@@ -386,6 +389,12 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
386389
binding,
387390
)
388391
.ok()?;
392+
// Make sure we select the same method that we started with...
393+
if Some(method.def_id)
394+
!= self.typeck_results.borrow().type_dependent_def_id(call_expr.hir_id)
395+
{
396+
return None;
397+
}
389398
// Unify the method signature with our incompatible arg, to
390399
// do inference in the *opposite* direction and to find out
391400
// what our ideal rcvr ty would look like.
@@ -456,6 +465,12 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
456465
) else {
457466
continue;
458467
};
468+
// Make sure we select the same method that we started with...
469+
if Some(method.def_id)
470+
!= self.typeck_results.borrow().type_dependent_def_id(parent_expr.hir_id)
471+
{
472+
continue;
473+
}
459474

460475
let ideal_rcvr_ty = rcvr_ty.fold_with(&mut fudger);
461476
let ideal_method = self
@@ -505,13 +520,19 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
505520
// blame arg, if possible. Don't do this if we're coming from
506521
// arg mismatch code, because we'll possibly suggest a mutually
507522
// incompatible fix at the original mismatch site.
523+
// HACK(compiler-errors): We don't actually consider the implications
524+
// of our inference guesses in `emit_type_mismatch_suggestions`, so
525+
// only suggest things when we know our type error is precisely due to
526+
// a type mismatch, and not via some projection or something. See #116155.
508527
if matches!(source, TypeMismatchSource::Ty(_))
509528
&& let Some(ideal_method) = ideal_method
510-
&& let ideal_arg_ty = self.resolve_vars_if_possible(ideal_method.sig.inputs()[idx + 1])
511-
// HACK(compiler-errors): We don't actually consider the implications
512-
// of our inference guesses in `emit_type_mismatch_suggestions`, so
513-
// only suggest things when we know our type error is precisely due to
514-
// a type mismatch, and not via some projection or something. See #116155.
529+
&& Some(ideal_method.def_id)
530+
== self
531+
.typeck_results
532+
.borrow()
533+
.type_dependent_def_id(parent_expr.hir_id)
534+
&& let ideal_arg_ty =
535+
self.resolve_vars_if_possible(ideal_method.sig.inputs()[idx + 1])
515536
&& !ideal_arg_ty.has_non_region_infer()
516537
{
517538
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)