From f2048019718409885ac57c480f010bdfbfe5bfd0 Mon Sep 17 00:00:00 2001 From: codedump Date: Wed, 16 Jul 2025 23:21:47 +0800 Subject: [PATCH] fix: fix issue 143740, Wrong messages from compiler confusing methods with the same name from different traits --- compiler/rustc_hir_typeck/src/method/probe.rs | 3 +- ...all_method_without_import.no_import.stderr | 7 +--- .../no-method-suggested-traits.stderr | 8 ----- ...e-trait-object-with-separate-params.stderr | 16 ++------- tests/ui/methods/wrong-ambig-message.rs | 34 +++++++++++++++++++ tests/ui/methods/wrong-ambig-message.stderr | 30 ++++++++++++++++ 6 files changed, 70 insertions(+), 28 deletions(-) create mode 100644 tests/ui/methods/wrong-ambig-message.rs create mode 100644 tests/ui/methods/wrong-ambig-message.stderr diff --git a/compiler/rustc_hir_typeck/src/method/probe.rs b/compiler/rustc_hir_typeck/src/method/probe.rs index 94c93e7362749..d5ac17bd78586 100644 --- a/compiler/rustc_hir_typeck/src/method/probe.rs +++ b/compiler/rustc_hir_typeck/src/method/probe.rs @@ -1649,7 +1649,8 @@ impl<'a, 'tcx> ProbeContext<'a, 'tcx> { } } - let sources = candidates.iter().map(|p| self.candidate_source(p, self_ty)).collect(); + let sources = + applicable_candidates.iter().map(|p| self.candidate_source(p.0, self_ty)).collect(); return Some(Err(MethodError::Ambiguity(sources))); } diff --git a/tests/ui/impl-trait/call_method_without_import.no_import.stderr b/tests/ui/impl-trait/call_method_without_import.no_import.stderr index e59409ea27e64..dbac74b224793 100644 --- a/tests/ui/impl-trait/call_method_without_import.no_import.stderr +++ b/tests/ui/impl-trait/call_method_without_import.no_import.stderr @@ -22,15 +22,10 @@ LL | x.fmt(f); = help: items from traits can only be used if the trait is in scope help: the following traits which provide `fmt` are implemented but not in scope; perhaps you want to import one of them | -LL + use std::fmt::Binary; - | LL + use std::fmt::Debug; | -LL + use std::fmt::Display; - | -LL + use std::fmt::LowerExp; +LL + use std::fmt::Pointer; | - = and 5 other candidates error: aborting due to 2 previous errors diff --git a/tests/ui/impl-trait/no-method-suggested-traits.stderr b/tests/ui/impl-trait/no-method-suggested-traits.stderr index 061c9bd8f3501..b376f205411c6 100644 --- a/tests/ui/impl-trait/no-method-suggested-traits.stderr +++ b/tests/ui/impl-trait/no-method-suggested-traits.stderr @@ -9,12 +9,8 @@ help: the following traits which provide `method` are implemented but not in sco | LL + use foo::Bar; | -LL + use no_method_suggested_traits::Reexported; - | LL + use no_method_suggested_traits::foo::PubPub; | -LL + use no_method_suggested_traits::qux::PrivPub; - | help: there is a method `method2` with a similar name | LL | 1u32.method2(); @@ -31,12 +27,8 @@ help: the following traits which provide `method` are implemented but not in sco | LL + use foo::Bar; | -LL + use no_method_suggested_traits::Reexported; - | LL + use no_method_suggested_traits::foo::PubPub; | -LL + use no_method_suggested_traits::qux::PrivPub; - | help: there is a method `method2` with a similar name | LL | std::rc::Rc::new(&mut Box::new(&1u32)).method2(); diff --git a/tests/ui/methods/method-deref-to-same-trait-object-with-separate-params.stderr b/tests/ui/methods/method-deref-to-same-trait-object-with-separate-params.stderr index 32cff62284e09..aeecb82e9d910 100644 --- a/tests/ui/methods/method-deref-to-same-trait-object-with-separate-params.stderr +++ b/tests/ui/methods/method-deref-to-same-trait-object-with-separate-params.stderr @@ -20,17 +20,12 @@ error[E0034]: multiple applicable items in scope LL | let z = x.foo(); | ^^^ multiple `foo` found | -note: candidate #1 is defined in the trait `FinalFoo` - --> $DIR/method-deref-to-same-trait-object-with-separate-params.rs:60:5 - | -LL | fn foo(&self) -> u8; - | ^^^^^^^^^^^^^^^^^^^^ -note: candidate #2 is defined in an impl of the trait `NuisanceFoo` for the type `T` +note: candidate #1 is defined in an impl of the trait `NuisanceFoo` for the type `T` --> $DIR/method-deref-to-same-trait-object-with-separate-params.rs:73:9 | LL | fn foo(self) {} | ^^^^^^^^^^^^ -note: candidate #3 is defined in an impl of the trait `X` for the type `T` +note: candidate #2 is defined in an impl of the trait `X` for the type `T` --> $DIR/method-deref-to-same-trait-object-with-separate-params.rs:46:9 | LL | fn foo(self: Smaht) -> u64 { @@ -38,14 +33,9 @@ LL | fn foo(self: Smaht) -> u64 { help: disambiguate the method for candidate #1 | LL - let z = x.foo(); -LL + let z = FinalFoo::foo(&x); - | -help: disambiguate the method for candidate #2 - | -LL - let z = x.foo(); LL + let z = NuisanceFoo::foo(x); | -help: disambiguate the method for candidate #3 +help: disambiguate the method for candidate #2 | LL - let z = x.foo(); LL + let z = X::foo(x); diff --git a/tests/ui/methods/wrong-ambig-message.rs b/tests/ui/methods/wrong-ambig-message.rs new file mode 100644 index 0000000000000..f88d77e259d68 --- /dev/null +++ b/tests/ui/methods/wrong-ambig-message.rs @@ -0,0 +1,34 @@ +fn main() { + trait Hello { + fn name(&self) -> String; + } + + #[derive(Debug)] + struct Container2 { + val: String, + } + + trait AName2 { + fn name(&self) -> String; + } + + trait BName2 { + fn name(&self, v: bool) -> String; + } + + impl AName2 for Container2 { + fn name(&self) -> String { + "aname2".into() + } + } + + impl BName2 for Container2 { + fn name(&self, _v: bool) -> String { + "bname2".into() + } + } + + let c2 = Container2 { val: "abc".into() }; + println!("c2 = {:?}", c2.name()); + //~^ ERROR: multiple applicable items in scope +} diff --git a/tests/ui/methods/wrong-ambig-message.stderr b/tests/ui/methods/wrong-ambig-message.stderr new file mode 100644 index 0000000000000..9a254595e40bc --- /dev/null +++ b/tests/ui/methods/wrong-ambig-message.stderr @@ -0,0 +1,30 @@ +error[E0034]: multiple applicable items in scope + --> $DIR/wrong-ambig-message.rs:32:30 + | +LL | println!("c2 = {:?}", c2.name()); + | ^^^^ multiple `name` found + | +note: candidate #1 is defined in an impl of the trait `AName2` for the type `Container2` + --> $DIR/wrong-ambig-message.rs:20:9 + | +LL | fn name(&self) -> String { + | ^^^^^^^^^^^^^^^^^^^^^^^^ +note: candidate #2 is defined in an impl of the trait `BName2` for the type `Container2` + --> $DIR/wrong-ambig-message.rs:26:9 + | +LL | fn name(&self, _v: bool) -> String { + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ +help: disambiguate the method for candidate #1 + | +LL - println!("c2 = {:?}", c2.name()); +LL + println!("c2 = {:?}", AName2::name(&c2)); + | +help: disambiguate the method for candidate #2 + | +LL - println!("c2 = {:?}", c2.name()); +LL + println!("c2 = {:?}", BName2::name(&c2)); + | + +error: aborting due to 1 previous error + +For more information about this error, try `rustc --explain E0034`.