Skip to content

Commit c6f0a5c

Browse files
authored
Rollup merge of #119957 - Young-Flash:fix, r=fmease
fix: correct suggestion arg for impl trait follow up #118502, close #119775
2 parents b35a3f8 + 8b3a681 commit c6f0a5c

File tree

4 files changed

+137
-9
lines changed

4 files changed

+137
-9
lines changed

compiler/rustc_hir_typeck/src/method/suggest.rs

+19-9
Original file line numberDiff line numberDiff line change
@@ -1601,23 +1601,33 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
16011601
self.ty_to_value_string(rcvr_ty.peel_refs())
16021602
};
16031603
if let SelfSource::MethodCall(_) = source {
1604-
let first_arg = if let Some(CandidateSource::Impl(impl_did)) = static_candidates.get(0)
1605-
&& let Some(assoc) = self.associated_value(*impl_did, item_name)
1606-
&& assoc.kind == ty::AssocKind::Fn
1607-
{
1604+
let first_arg = static_candidates.get(0).and_then(|candidate_source| {
1605+
let (assoc_did, self_ty) = match candidate_source {
1606+
CandidateSource::Impl(impl_did) => {
1607+
(*impl_did, self.tcx.type_of(*impl_did).instantiate_identity())
1608+
}
1609+
CandidateSource::Trait(trait_did) => (*trait_did, rcvr_ty),
1610+
};
1611+
1612+
let assoc = self.associated_value(assoc_did, item_name)?;
1613+
if assoc.kind != ty::AssocKind::Fn {
1614+
return None;
1615+
}
1616+
1617+
// for CandidateSource::Impl, `Self` will be instantiated to a concrete type
1618+
// but for CandidateSource::Trait, `Self` is still `Self`
16081619
let sig = self.tcx.fn_sig(assoc.def_id).instantiate_identity();
16091620
sig.inputs().skip_binder().get(0).and_then(|first| {
1610-
let impl_ty = self.tcx.type_of(*impl_did).instantiate_identity();
16111621
// if the type of first arg is the same as the current impl type, we should take the first arg into assoc function
1612-
if first.peel_refs() == impl_ty {
1622+
let first_ty = first.peel_refs();
1623+
if first_ty == self_ty || first_ty == self.tcx.types.self_param {
16131624
Some(first.ref_mutability().map_or("", |mutbl| mutbl.ref_prefix_str()))
16141625
} else {
16151626
None
16161627
}
16171628
})
1618-
} else {
1619-
None
1620-
};
1629+
});
1630+
16211631
let mut applicability = Applicability::MachineApplicable;
16221632
let args = if let SelfSource::MethodCall(receiver) = source
16231633
&& let Some(args) = args
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// run-rustfix
2+
3+
struct A {
4+
5+
}
6+
7+
trait M {
8+
fn foo(_a: Self);
9+
fn bar(_a: Self);
10+
fn baz(_a: i32);
11+
}
12+
13+
impl M for A {
14+
fn foo(_a: Self) {}
15+
fn bar(_a: A) {}
16+
fn baz(_a: i32) {}
17+
}
18+
19+
fn main() {
20+
let _a = A {};
21+
A::foo(_a);
22+
//~^ ERROR no method named `foo` found
23+
A::baz(0);
24+
//~^ ERROR no method named `baz` found
25+
26+
let _b = A {};
27+
A::bar(_b);
28+
//~^ ERROR no method named `bar` found
29+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// run-rustfix
2+
3+
struct A {
4+
5+
}
6+
7+
trait M {
8+
fn foo(_a: Self);
9+
fn bar(_a: Self);
10+
fn baz(_a: i32);
11+
}
12+
13+
impl M for A {
14+
fn foo(_a: Self) {}
15+
fn bar(_a: A) {}
16+
fn baz(_a: i32) {}
17+
}
18+
19+
fn main() {
20+
let _a = A {};
21+
_a.foo();
22+
//~^ ERROR no method named `foo` found
23+
_a.baz(0);
24+
//~^ ERROR no method named `baz` found
25+
26+
let _b = A {};
27+
_b.bar();
28+
//~^ ERROR no method named `bar` found
29+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
error[E0599]: no method named `foo` found for struct `A` in the current scope
2+
--> $DIR/suggest-assoc-fn-call-for-impl-trait.rs:21:8
3+
|
4+
LL | struct A {
5+
| -------- method `foo` not found for this struct
6+
...
7+
LL | _a.foo();
8+
| ---^^^--
9+
| | |
10+
| | this is an associated function, not a method
11+
| help: use associated function syntax instead: `A::foo(_a)`
12+
|
13+
= note: found the following associated functions; to be used as methods, functions must have a `self` parameter
14+
note: the candidate is defined in the trait `M`
15+
--> $DIR/suggest-assoc-fn-call-for-impl-trait.rs:8:5
16+
|
17+
LL | fn foo(_a: Self);
18+
| ^^^^^^^^^^^^^^^^^
19+
20+
error[E0599]: no method named `baz` found for struct `A` in the current scope
21+
--> $DIR/suggest-assoc-fn-call-for-impl-trait.rs:23:8
22+
|
23+
LL | struct A {
24+
| -------- method `baz` not found for this struct
25+
...
26+
LL | _a.baz(0);
27+
| ---^^^---
28+
| | |
29+
| | this is an associated function, not a method
30+
| help: use associated function syntax instead: `A::baz(0)`
31+
|
32+
= note: found the following associated functions; to be used as methods, functions must have a `self` parameter
33+
note: the candidate is defined in the trait `M`
34+
--> $DIR/suggest-assoc-fn-call-for-impl-trait.rs:10:5
35+
|
36+
LL | fn baz(_a: i32);
37+
| ^^^^^^^^^^^^^^^^
38+
39+
error[E0599]: no method named `bar` found for struct `A` in the current scope
40+
--> $DIR/suggest-assoc-fn-call-for-impl-trait.rs:27:8
41+
|
42+
LL | struct A {
43+
| -------- method `bar` not found for this struct
44+
...
45+
LL | _b.bar();
46+
| ---^^^--
47+
| | |
48+
| | this is an associated function, not a method
49+
| help: use associated function syntax instead: `A::bar(_b)`
50+
|
51+
= note: found the following associated functions; to be used as methods, functions must have a `self` parameter
52+
note: the candidate is defined in the trait `M`
53+
--> $DIR/suggest-assoc-fn-call-for-impl-trait.rs:9:5
54+
|
55+
LL | fn bar(_a: Self);
56+
| ^^^^^^^^^^^^^^^^^
57+
58+
error: aborting due to 3 previous errors
59+
60+
For more information about this error, try `rustc --explain E0599`.

0 commit comments

Comments
 (0)