Skip to content

Commit 2d377d2

Browse files
authored
Rollup merge of #144029 - lichuang:fix_issue_143740, r=compiler-errors
Fix wrong messages from methods with the same name from different traits fix issue #143740
2 parents 999aa22 + f204801 commit 2d377d2

File tree

6 files changed

+70
-28
lines changed

6 files changed

+70
-28
lines changed

compiler/rustc_hir_typeck/src/method/probe.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1650,7 +1650,8 @@ impl<'a, 'tcx> ProbeContext<'a, 'tcx> {
16501650
}
16511651
}
16521652

1653-
let sources = candidates.iter().map(|p| self.candidate_source(p, self_ty)).collect();
1653+
let sources =
1654+
applicable_candidates.iter().map(|p| self.candidate_source(p.0, self_ty)).collect();
16541655
return Some(Err(MethodError::Ambiguity(sources)));
16551656
}
16561657

tests/ui/impl-trait/call_method_without_import.no_import.stderr

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,10 @@ LL | x.fmt(f);
2222
= help: items from traits can only be used if the trait is in scope
2323
help: the following traits which provide `fmt` are implemented but not in scope; perhaps you want to import one of them
2424
|
25-
LL + use std::fmt::Binary;
26-
|
2725
LL + use std::fmt::Debug;
2826
|
29-
LL + use std::fmt::Display;
30-
|
31-
LL + use std::fmt::LowerExp;
27+
LL + use std::fmt::Pointer;
3228
|
33-
= and 5 other candidates
3429

3530
error: aborting due to 2 previous errors
3631

tests/ui/impl-trait/no-method-suggested-traits.stderr

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,8 @@ help: the following traits which provide `method` are implemented but not in sco
99
|
1010
LL + use foo::Bar;
1111
|
12-
LL + use no_method_suggested_traits::Reexported;
13-
|
1412
LL + use no_method_suggested_traits::foo::PubPub;
1513
|
16-
LL + use no_method_suggested_traits::qux::PrivPub;
17-
|
1814
help: there is a method `method2` with a similar name
1915
|
2016
LL | 1u32.method2();
@@ -31,12 +27,8 @@ help: the following traits which provide `method` are implemented but not in sco
3127
|
3228
LL + use foo::Bar;
3329
|
34-
LL + use no_method_suggested_traits::Reexported;
35-
|
3630
LL + use no_method_suggested_traits::foo::PubPub;
3731
|
38-
LL + use no_method_suggested_traits::qux::PrivPub;
39-
|
4032
help: there is a method `method2` with a similar name
4133
|
4234
LL | std::rc::Rc::new(&mut Box::new(&1u32)).method2();

tests/ui/methods/method-deref-to-same-trait-object-with-separate-params.stderr

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -20,32 +20,22 @@ error[E0034]: multiple applicable items in scope
2020
LL | let z = x.foo();
2121
| ^^^ multiple `foo` found
2222
|
23-
note: candidate #1 is defined in the trait `FinalFoo`
24-
--> $DIR/method-deref-to-same-trait-object-with-separate-params.rs:60:5
25-
|
26-
LL | fn foo(&self) -> u8;
27-
| ^^^^^^^^^^^^^^^^^^^^
28-
note: candidate #2 is defined in an impl of the trait `NuisanceFoo` for the type `T`
23+
note: candidate #1 is defined in an impl of the trait `NuisanceFoo` for the type `T`
2924
--> $DIR/method-deref-to-same-trait-object-with-separate-params.rs:73:9
3025
|
3126
LL | fn foo(self) {}
3227
| ^^^^^^^^^^^^
33-
note: candidate #3 is defined in an impl of the trait `X` for the type `T`
28+
note: candidate #2 is defined in an impl of the trait `X` for the type `T`
3429
--> $DIR/method-deref-to-same-trait-object-with-separate-params.rs:46:9
3530
|
3631
LL | fn foo(self: Smaht<Self, u64>) -> u64 {
3732
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
3833
help: disambiguate the method for candidate #1
3934
|
4035
LL - let z = x.foo();
41-
LL + let z = FinalFoo::foo(&x);
42-
|
43-
help: disambiguate the method for candidate #2
44-
|
45-
LL - let z = x.foo();
4636
LL + let z = NuisanceFoo::foo(x);
4737
|
48-
help: disambiguate the method for candidate #3
38+
help: disambiguate the method for candidate #2
4939
|
5040
LL - let z = x.foo();
5141
LL + let z = X::foo(x);
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
fn main() {
2+
trait Hello {
3+
fn name(&self) -> String;
4+
}
5+
6+
#[derive(Debug)]
7+
struct Container2 {
8+
val: String,
9+
}
10+
11+
trait AName2 {
12+
fn name(&self) -> String;
13+
}
14+
15+
trait BName2 {
16+
fn name(&self, v: bool) -> String;
17+
}
18+
19+
impl AName2 for Container2 {
20+
fn name(&self) -> String {
21+
"aname2".into()
22+
}
23+
}
24+
25+
impl BName2 for Container2 {
26+
fn name(&self, _v: bool) -> String {
27+
"bname2".into()
28+
}
29+
}
30+
31+
let c2 = Container2 { val: "abc".into() };
32+
println!("c2 = {:?}", c2.name());
33+
//~^ ERROR: multiple applicable items in scope
34+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
error[E0034]: multiple applicable items in scope
2+
--> $DIR/wrong-ambig-message.rs:32:30
3+
|
4+
LL | println!("c2 = {:?}", c2.name());
5+
| ^^^^ multiple `name` found
6+
|
7+
note: candidate #1 is defined in an impl of the trait `AName2` for the type `Container2`
8+
--> $DIR/wrong-ambig-message.rs:20:9
9+
|
10+
LL | fn name(&self) -> String {
11+
| ^^^^^^^^^^^^^^^^^^^^^^^^
12+
note: candidate #2 is defined in an impl of the trait `BName2` for the type `Container2`
13+
--> $DIR/wrong-ambig-message.rs:26:9
14+
|
15+
LL | fn name(&self, _v: bool) -> String {
16+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
17+
help: disambiguate the method for candidate #1
18+
|
19+
LL - println!("c2 = {:?}", c2.name());
20+
LL + println!("c2 = {:?}", AName2::name(&c2));
21+
|
22+
help: disambiguate the method for candidate #2
23+
|
24+
LL - println!("c2 = {:?}", c2.name());
25+
LL + println!("c2 = {:?}", BName2::name(&c2));
26+
|
27+
28+
error: aborting due to 1 previous error
29+
30+
For more information about this error, try `rustc --explain E0034`.

0 commit comments

Comments
 (0)