Skip to content

Commit f00897e

Browse files
authored
Rollup merge of #103531 - chenyukang:yukang/fix-103474, r=estebank
Suggest calling the instance method of the same name when method not found Fixes #103474
2 parents fd5ff82 + 2716449 commit f00897e

File tree

6 files changed

+88
-16
lines changed

6 files changed

+88
-16
lines changed

compiler/rustc_resolve/src/late/diagnostics.rs

+12-12
Original file line numberDiff line numberDiff line change
@@ -219,26 +219,26 @@ impl<'a: 'ast, 'ast> LateResolutionVisitor<'a, '_, 'ast> {
219219
let (mod_prefix, mod_str, suggestion) = if path.len() == 1 {
220220
debug!(?self.diagnostic_metadata.current_impl_items);
221221
debug!(?self.diagnostic_metadata.current_function);
222-
let suggestion = if let Some(items) = self.diagnostic_metadata.current_impl_items
222+
let suggestion = if self.current_trait_ref.is_none()
223223
&& let Some((fn_kind, _)) = self.diagnostic_metadata.current_function
224-
&& self.current_trait_ref.is_none()
225224
&& let Some(FnCtxt::Assoc(_)) = fn_kind.ctxt()
225+
&& let Some(items) = self.diagnostic_metadata.current_impl_items
226226
&& let Some(item) = items.iter().find(|i| {
227-
if let AssocItemKind::Fn(fn_) = &i.kind
228-
&& !fn_.sig.decl.has_self()
229-
&& i.ident.name == item_str.name
227+
if let AssocItemKind::Fn(_) = &i.kind && i.ident.name == item_str.name
230228
{
231229
debug!(?item_str.name);
232-
debug!(?fn_.sig.decl.inputs);
233230
return true
234231
}
235232
false
236233
})
234+
&& let AssocItemKind::Fn(fn_) = &item.kind
237235
{
236+
debug!(?fn_);
237+
let self_sugg = if fn_.sig.decl.has_self() { "self." } else { "Self::" };
238238
Some((
239-
item_span,
239+
item_span.shrink_to_lo(),
240240
"consider using the associated function",
241-
format!("Self::{}", item.ident)
241+
self_sugg.to_string()
242242
))
243243
} else {
244244
None
@@ -396,11 +396,13 @@ impl<'a: 'ast, 'ast> LateResolutionVisitor<'a, '_, 'ast> {
396396
}
397397

398398
fn suggest_self_or_self_ref(&mut self, err: &mut Diagnostic, path: &[Segment], span: Span) {
399-
let is_assoc_fn = self.self_type_is_available();
399+
if !self.self_type_is_available() {
400+
return;
401+
}
400402
let Some(path_last_segment) = path.last() else { return };
401403
let item_str = path_last_segment.ident;
402404
// Emit help message for fake-self from other languages (e.g., `this` in Javascript).
403-
if ["this", "my"].contains(&item_str.as_str()) && is_assoc_fn {
405+
if ["this", "my"].contains(&item_str.as_str()) {
404406
err.span_suggestion_short(
405407
span,
406408
"you might have meant to use `self` here instead",
@@ -451,7 +453,6 @@ impl<'a: 'ast, 'ast> LateResolutionVisitor<'a, '_, 'ast> {
451453
let is_enum_variant = &|res| matches!(res, Res::Def(DefKind::Variant, _));
452454
let path_str = Segment::names_to_string(path);
453455
let ident_span = path.last().map_or(span, |ident| ident.ident.span);
454-
455456
let mut candidates = self
456457
.r
457458
.lookup_import_candidates(ident, ns, &self.parent_scope, is_expected)
@@ -1542,7 +1543,6 @@ impl<'a: 'ast, 'ast> LateResolutionVisitor<'a, '_, 'ast> {
15421543
_ => None,
15431544
}
15441545
}
1545-
15461546
// Fields are generally expected in the same contexts as locals.
15471547
if filter_fn(Res::Local(ast::DUMMY_NODE_ID)) {
15481548
if let Some(node_id) =

src/test/ui/resolve/issue-103474.rs

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
struct S {}
2+
impl S {
3+
fn first(&self) {}
4+
5+
fn second(&self) {
6+
first()
7+
//~^ ERROR cannot find function `first` in this scope
8+
}
9+
10+
fn third(&self) {
11+
no_method_err()
12+
//~^ ERROR cannot find function `no_method_err` in this scope
13+
}
14+
}
15+
16+
// https://github.com/rust-lang/rust/pull/103531#discussion_r1004728080
17+
struct Foo {
18+
i: i32,
19+
}
20+
21+
impl Foo {
22+
fn needs_self() {
23+
this.i
24+
//~^ ERROR cannot find value `this` in this scope
25+
}
26+
}
27+
28+
fn main() {}
+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
error[E0425]: cannot find value `this` in this scope
2+
--> $DIR/issue-103474.rs:23:9
3+
|
4+
LL | this.i
5+
| ^^^^ not found in this scope
6+
|
7+
help: you might have meant to use `self` here instead
8+
|
9+
LL | self.i
10+
| ~~~~
11+
help: if you meant to use `self`, you are also missing a `self` receiver argument
12+
|
13+
LL | fn needs_self(&self) {
14+
| +++++
15+
16+
error[E0425]: cannot find function `first` in this scope
17+
--> $DIR/issue-103474.rs:6:9
18+
|
19+
LL | first()
20+
| ^^^^^ not found in this scope
21+
|
22+
help: consider using the associated function
23+
|
24+
LL | self.first()
25+
| +++++
26+
27+
error[E0425]: cannot find function `no_method_err` in this scope
28+
--> $DIR/issue-103474.rs:11:9
29+
|
30+
LL | no_method_err()
31+
| ^^^^^^^^^^^^^ not found in this scope
32+
33+
error: aborting due to 3 previous errors
34+
35+
For more information about this error, try `rustc --explain E0425`.

src/test/ui/resolve/issue-2356.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ LL | static_method();
8585
help: consider using the associated function
8686
|
8787
LL | Self::static_method();
88-
| ~~~~~~~~~~~~~~~~~~~
88+
| ++++++
8989

9090
error[E0425]: cannot find function `purr` in this scope
9191
--> $DIR/issue-2356.rs:54:9
@@ -114,7 +114,7 @@ LL | grow_older();
114114
help: consider using the associated function
115115
|
116116
LL | Self::grow_older();
117-
| ~~~~~~~~~~~~~~~~
117+
| ++++++
118118

119119
error[E0425]: cannot find function `shave` in this scope
120120
--> $DIR/issue-2356.rs:74:5

src/test/ui/self/class-missing-self.stderr

+4
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@ error[E0425]: cannot find function `sleep` in this scope
1010
LL | sleep();
1111
| ^^^^^ not found in this scope
1212
|
13+
help: consider using the associated function
14+
|
15+
LL | self.sleep();
16+
| +++++
1317
help: consider importing this function
1418
|
1519
LL | use std::thread::sleep;

src/test/ui/suggestions/assoc_fn_without_self.stderr

+7-2
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,18 @@ LL | foo();
77
help: consider using the associated function
88
|
99
LL | Self::foo();
10-
| ~~~~~~~~~
10+
| ++++++
1111

1212
error[E0425]: cannot find function `bar` in this scope
1313
--> $DIR/assoc_fn_without_self.rs:17:9
1414
|
1515
LL | bar();
1616
| ^^^ not found in this scope
17+
|
18+
help: consider using the associated function
19+
|
20+
LL | self.bar();
21+
| +++++
1722

1823
error[E0425]: cannot find function `baz` in this scope
1924
--> $DIR/assoc_fn_without_self.rs:18:9
@@ -24,7 +29,7 @@ LL | baz(2, 3);
2429
help: consider using the associated function
2530
|
2631
LL | Self::baz(2, 3);
27-
| ~~~~~~~~~
32+
| ++++++
2833

2934
error[E0425]: cannot find function `foo` in this scope
3035
--> $DIR/assoc_fn_without_self.rs:14:13

0 commit comments

Comments
 (0)