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

When a function name within a method is not found, suggest calling the method of the same name #103474

Closed
jruderman opened this issue Oct 24, 2022 · 2 comments · Fixed by #103531
Assignees
Labels
A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@jruderman
Copy link
Contributor

jruderman commented Oct 24, 2022

Given the following code (playground):

struct S { }
impl S {
    fn first(&self) { }
    fn second(&self) { first() }
}

The current output is:

error[[E0425]](https://doc.rust-lang.org/nightly/error-index.html#E0425): cannot find function `first` in this scope
 --> src/lib.rs:4:24
  |
4 |     fn second(&self) { first() }
  |                        ^^^^^ not found in this scope

Ideally the output should mention the fact that a method with this name was found on the same struct. And perhaps suggest calling the method on self, especially if the self-param types are compatible (e.g. both take an immutable &self reference).

note: A method named `first` was found on `S`.
help:
  |
4 |     fn second(&self) { self.first() }
  |                        +++++   specify which instance of `S` to call `first` on

Motivation

This may be a common error for programmers coming from C++, because C++ does not require specifying the instance for method-to-method calls.

C++ example

#include <iostream>
using namespace std;

class C {
    public:
        int jj() { return 1; }
        int kk() { return 5 + jj(); }
};


int main() {
    C *c = new C();
    printf("%d", c->kk());
}

@jruderman jruderman added A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Oct 24, 2022
@kpreid
Copy link
Contributor

kpreid commented Oct 24, 2022

A similar issue: #102518, about suggesting methods when trying free functions in general, not specifically within an impl. An impl gives even more reason to prioritize the suggestion.

This may be a common error for programmers coming from C++,

Java and C# also do this.

@chenyukang
Copy link
Member

@rustbot claim

Manishearth added a commit to Manishearth/rust that referenced this issue Nov 11, 2022
…tebank

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

Fixes rust-lang#103474
Manishearth added a commit to Manishearth/rust that referenced this issue Nov 11, 2022
…tebank

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

Fixes rust-lang#103474
@bors bors closed this as completed in f00897e Nov 11, 2022
Aaron1011 pushed a commit to Aaron1011/rust that referenced this issue Jan 6, 2023
…tebank

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

Fixes rust-lang#103474
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants