Skip to content

Commit a8a4d3d

Browse files
committed
fix: fix issue 143740, Wrong messages from compiler confusing methods with the same name from different traits
1 parent a9fb610 commit a8a4d3d

File tree

3 files changed

+73
-1
lines changed

3 files changed

+73
-1
lines changed

compiler/rustc_hir_typeck/src/method/probe.rs

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

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

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
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+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
error[E0034]: multiple applicable items in scope
2+
--> tests/ui/issues/issue-143740.rs:32:30
3+
|
4+
32 | 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+
--> tests/ui/issues/issue-143740.rs:20:9
9+
|
10+
20 | fn name(&self) -> String {
11+
| ^^^^^^^^^^^^^^^^^^^^^^^^
12+
note: candidate #2 is defined in an impl of the trait `BName2` for the type `Container2`
13+
--> tests/ui/issues/issue-143740.rs:26:9
14+
|
15+
26 | fn name(&self, v: bool) -> String {
16+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
17+
help: disambiguate the method for candidate #1
18+
|
19+
32 - println!("c2 = {:?}", c2.name());
20+
32 + println!("c2 = {:?}", AName2::name(&c2));
21+
|
22+
help: disambiguate the method for candidate #2
23+
|
24+
32 - println!("c2 = {:?}", c2.name());
25+
32 + println!("c2 = {:?}", BName2::name(&c2));
26+
|
27+
28+
warning: unused variable: `v`
29+
--> tests/ui/issues/issue-143740.rs:26:24
30+
|
31+
26 | fn name(&self, v: bool) -> String {
32+
| ^ help: if this is intentional, prefix it with an underscore: `_v`
33+
|
34+
= note: `#[warn(unused_variables)]` on by default
35+
36+
error: aborting due to 1 previous error; 1 warning emitted
37+
38+
For more information about this error, try `rustc --explain E0034`.

0 commit comments

Comments
 (0)