Skip to content

Add help suggestion for object instance mistakenly used with associated function #101542

Closed
@Rageking8

Description

@Rageking8

Given the following code: link

struct S {

}

impl S {
    fn func() {

    }
}

fn main() {
    let x = S{};
    x::func();
}

The current output is:

Compiling playground v0.0.1 (/playground)
error[[E0433]](https://doc.rust-lang.org/nightly/error-index.html#E0433): failed to resolve: use of undeclared crate or module `x`
  --> src/main.rs:13:5
   |
13 |     x::func();
   |     ^ use of undeclared crate or module `x`

For more information about this error, try `rustc --explain E0433`.
error: could not compile `playground` due to previous error

Since methods are called using an object instance, there is a chance that an associated function will be mistakenly called using an object instance. Therefore, the ideal output would suggest changing the object instance to the type of said instance.

Note: if mistake is changed to x.func(), the current error message already suggest the correct way to fix that error, shown below:

Compiling playground v0.0.1 (/playground)
error[[E0599]](https://doc.rust-lang.org/nightly/error-index.html#E0599): no method named `func` found for struct `S` in the current scope
  --> src/main.rs:13:7
   |
1  | struct S {
   | -------- method `func` not found for this struct
...
13 |     x.func();
   |     --^^^^
   |     | |
   |     | this is an associated function, not a method
   |     help: use associated function syntax instead: `S::func`
   |
   = note: found the following associated functions; to be used as methods, functions must have a `self` parameter
note: the candidate is defined in an impl for the type `S`
  --> src/main.rs:6:5
   |
6  |     fn func() {
   |     ^^^^^^^^^

For more information about this error, try `rustc --explain E0599`.
error: could not compile `playground` due to previous error

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