Skip to content

Commit 383bf02

Browse files
committed
Auto merge of rust-lang#117848 - compiler-errors:method-ambiguity-no-rcvr, r=TaKO8Ki
Don't expect a rcvr in `print_disambiguation_help` We don't necessarily have a receiver when we are both accidentally using the `.` operator *AND* we have more than one ambiguous method candidate. Fixes rust-lang#117728
2 parents ee85f7f + 99664b0 commit 383bf02

File tree

3 files changed

+52
-2
lines changed

3 files changed

+52
-2
lines changed

compiler/rustc_hir_typeck/src/method/suggest.rs

+6-2
Original file line numberDiff line numberDiff line change
@@ -3296,8 +3296,12 @@ fn print_disambiguation_help<'tcx>(
32963296
{
32973297
let def_kind_descr = tcx.def_kind_descr(item.kind.as_def_kind(), item.def_id);
32983298
let item_name = item.ident(tcx);
3299-
let rcvr_ref = tcx.fn_sig(item.def_id).skip_binder().skip_binder().inputs()[0]
3300-
.ref_mutability()
3299+
let rcvr_ref = tcx.fn_sig(item.def_id)
3300+
.skip_binder()
3301+
.skip_binder()
3302+
.inputs()
3303+
.get(0)
3304+
.and_then(|ty| ty.ref_mutability())
33013305
.map_or("", |mutbl| mutbl.ref_prefix_str());
33023306
let args = format!(
33033307
"({}{})",
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
struct Qux;
2+
3+
trait Foo {
4+
fn foo();
5+
}
6+
7+
trait FooBar {
8+
fn foo() {}
9+
}
10+
11+
fn main() {
12+
Qux.foo();
13+
//~^ ERROR no method named `foo` found for struct `Qux` in the current scope
14+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
error[E0599]: no method named `foo` found for struct `Qux` in the current scope
2+
--> $DIR/method-ambiguity-no-rcvr.rs:12:9
3+
|
4+
LL | struct Qux;
5+
| ---------- method `foo` not found for this struct
6+
...
7+
LL | Qux.foo();
8+
| ^^^ this is an associated function, not a method
9+
|
10+
= note: found the following associated functions; to be used as methods, functions must have a `self` parameter
11+
note: candidate #1 is defined in the trait `Foo`
12+
--> $DIR/method-ambiguity-no-rcvr.rs:4:5
13+
|
14+
LL | fn foo();
15+
| ^^^^^^^^^
16+
note: candidate #2 is defined in the trait `FooBar`
17+
--> $DIR/method-ambiguity-no-rcvr.rs:8:5
18+
|
19+
LL | fn foo() {}
20+
| ^^^^^^^^
21+
help: disambiguate the associated function for candidate #1
22+
|
23+
LL | <Qux as Foo>::foo(Qux);
24+
| ~~~~~~~~~~~~~~~~~~~~~~
25+
help: disambiguate the associated function for candidate #2
26+
|
27+
LL | <Qux as FooBar>::foo(Qux);
28+
| ~~~~~~~~~~~~~~~~~~~~~~~~~
29+
30+
error: aborting due to previous error
31+
32+
For more information about this error, try `rustc --explain E0599`.

0 commit comments

Comments
 (0)