Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Suggest calling the instance method of the same name when method not found #103531

Merged
merged 2 commits into from
Nov 11, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 12 additions & 12 deletions compiler/rustc_resolve/src/late/diagnostics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -213,26 +213,26 @@ impl<'a: 'ast, 'ast> LateResolutionVisitor<'a, '_, 'ast> {
let (mod_prefix, mod_str, suggestion) = if path.len() == 1 {
debug!(?self.diagnostic_metadata.current_impl_items);
debug!(?self.diagnostic_metadata.current_function);
let suggestion = if let Some(items) = self.diagnostic_metadata.current_impl_items
let suggestion = if self.current_trait_ref.is_none()
estebank marked this conversation as resolved.
Show resolved Hide resolved
&& let Some((fn_kind, _)) = self.diagnostic_metadata.current_function
&& self.current_trait_ref.is_none()
&& let Some(FnCtxt::Assoc(_)) = fn_kind.ctxt()
&& let Some(items) = self.diagnostic_metadata.current_impl_items
&& let Some(item) = items.iter().find(|i| {
if let AssocItemKind::Fn(fn_) = &i.kind
&& !fn_.sig.decl.has_self()
&& i.ident.name == item_str.name
if let AssocItemKind::Fn(_) = &i.kind && i.ident.name == item_str.name
{
debug!(?item_str.name);
debug!(?fn_.sig.decl.inputs);
return true
}
false
})
&& let AssocItemKind::Fn(fn_) = &item.kind
{
debug!(?fn_);
let self_sugg = if fn_.sig.decl.has_self() { "self." } else { "Self::" };
Some((
item_span,
item_span.shrink_to_lo(),
"consider using the associated function",
format!("Self::{}", item.ident)
self_sugg.to_string()
))
} else {
None
Expand Down Expand Up @@ -381,11 +381,13 @@ impl<'a: 'ast, 'ast> LateResolutionVisitor<'a, '_, 'ast> {
}

fn suggest_self_or_self_ref(&mut self, err: &mut Diagnostic, path: &[Segment], span: Span) {
let is_assoc_fn = self.self_type_is_available();
if !self.self_type_is_available() {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This probably causes a regression in the suggestions for the code:

struct Foo {
    i: i32,
}

impl Foo {
    fn needs_self() {
        this.i
    }
}

Which currently looks like:

error[[E0425]](https://doc.rust-lang.org/stable/error-index.html#E0425): cannot find value `this` in this scope
 --> src/lib.rs:7:9
  |
7 |         this.i
  |         ^^^^ not found in this scope
  |
help: you might have meant to use `self` here instead
  |
7 |         self.i
  |         ~~~~
help: if you meant to use `self`, you are also missing a `self` receiver argument
  |
6 |     fn needs_self(&self) {
  |                   +++++

Can you check that it still works? Also useful to commit a test for it.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This testcase still works, I didn't change the meaning of suggest_self_or_self_ref, I moved the check from L388 to beginning of this function, removed a variable is_assoc_fn.

I will add this code into tests.

return;
}
let Some(path_last_segment) = path.last() else { return };
let item_str = path_last_segment.ident;
// Emit help message for fake-self from other languages (e.g., `this` in Javascript).
if ["this", "my"].contains(&item_str.as_str()) && is_assoc_fn {
if ["this", "my"].contains(&item_str.as_str()) {
err.span_suggestion_short(
span,
"you might have meant to use `self` here instead",
Expand Down Expand Up @@ -436,7 +438,6 @@ impl<'a: 'ast, 'ast> LateResolutionVisitor<'a, '_, 'ast> {
let is_enum_variant = &|res| matches!(res, Res::Def(DefKind::Variant, _));
let path_str = Segment::names_to_string(path);
let ident_span = path.last().map_or(span, |ident| ident.ident.span);

let mut candidates = self
.r
.lookup_import_candidates(ident, ns, &self.parent_scope, is_expected)
Expand Down Expand Up @@ -1512,7 +1513,6 @@ impl<'a: 'ast, 'ast> LateResolutionVisitor<'a, '_, 'ast> {
_ => None,
}
}

// Fields are generally expected in the same contexts as locals.
if filter_fn(Res::Local(ast::DUMMY_NODE_ID)) {
if let Some(node_id) =
Expand Down
16 changes: 16 additions & 0 deletions src/test/ui/resolve/issue-103474.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
struct S {}
impl S {
fn first(&self) {}

fn second(&self) {
first()
//~^ ERROR cannot find function `first` in this scope
}

fn third(&self) {
no_method_err()
//~^ ERROR cannot find function `no_method_err` in this scope
}
}

fn main() {}
20 changes: 20 additions & 0 deletions src/test/ui/resolve/issue-103474.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
error[E0425]: cannot find function `first` in this scope
--> $DIR/issue-103474.rs:6:9
|
LL | first()
| ^^^^^ not found in this scope
|
help: consider using the associated function
|
LL | self.first()
| +++++

error[E0425]: cannot find function `no_method_err` in this scope
--> $DIR/issue-103474.rs:11:9
|
LL | no_method_err()
| ^^^^^^^^^^^^^ not found in this scope

error: aborting due to 2 previous errors

For more information about this error, try `rustc --explain E0425`.
4 changes: 2 additions & 2 deletions src/test/ui/resolve/issue-2356.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ LL | static_method();
help: consider using the associated function
|
LL | Self::static_method();
| ~~~~~~~~~~~~~~~~~~~
| ++++++

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

error[E0425]: cannot find function `shave` in this scope
--> $DIR/issue-2356.rs:74:5
Expand Down
4 changes: 4 additions & 0 deletions src/test/ui/self/class-missing-self.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ error[E0425]: cannot find function `sleep` in this scope
LL | sleep();
| ^^^^^ not found in this scope
|
help: consider using the associated function
|
LL | self.sleep();
| +++++
help: consider importing this function
|
LL | use std::thread::sleep;
Expand Down
9 changes: 7 additions & 2 deletions src/test/ui/suggestions/assoc_fn_without_self.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,18 @@ LL | foo();
help: consider using the associated function
|
LL | Self::foo();
| ~~~~~~~~~
| ++++++

error[E0425]: cannot find function `bar` in this scope
--> $DIR/assoc_fn_without_self.rs:17:9
|
LL | bar();
| ^^^ not found in this scope
|
help: consider using the associated function
|
LL | self.bar();
| +++++

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

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