fix: fix issue 143740, Wrong messages from compiler confusing methods with the same name from different traits

This commit is contained in:
codedump 2025-07-16 23:21:47 +08:00
parent a9fb6103b0
commit f204801971
6 changed files with 70 additions and 28 deletions

View file

@ -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)));
}

View file

@ -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::Pointer;
|
LL + use std::fmt::LowerExp;
|
= and 5 other candidates
error: aborting due to 2 previous errors

View file

@ -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();

View file

@ -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<Self, u64>) -> u64 {
@ -38,14 +33,9 @@ LL | fn foo(self: Smaht<Self, u64>) -> 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);

View file

@ -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
}

View file

@ -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`.