Skip to content

Fix wrong messages from methods with the same name from different traits #144029

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 18, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion compiler/rustc_hir_typeck/src/method/probe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
8 changes: 0 additions & 8 deletions tests/ui/impl-trait/no-method-suggested-traits.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,32 +20,22 @@ 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 {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
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);
Expand Down
34 changes: 34 additions & 0 deletions tests/ui/methods/wrong-ambig-message.rs
Original file line number Diff line number Diff line change
@@ -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
}
30 changes: 30 additions & 0 deletions tests/ui/methods/wrong-ambig-message.stderr
Original file line number Diff line number Diff line change
@@ -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`.
Loading