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

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

Closed
Rageking8 opened this issue Sep 7, 2022 · 1 comment · Fixed by #101975
Closed
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

@Rageking8
Copy link
Contributor

Rageking8 commented Sep 7, 2022

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
@Rageking8 Rageking8 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 Sep 7, 2022
@chenyukang
Copy link
Member

@rustbot claim

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.

2 participants