Skip to content

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

Closed
@jruderman

Description

@jruderman

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());
}

Metadata

Metadata

Assignees

Labels

A-diagnosticsArea: Messages for errors, warnings, and lintsT-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions