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

E0424 The self keyword was used in a static method. #59782

Closed
gnzlbg opened this issue Apr 7, 2019 · 11 comments · Fixed by #65542
Closed

E0424 The self keyword was used in a static method. #59782

gnzlbg opened this issue Apr 7, 2019 · 11 comments · Fixed by #65542
Labels
A-diagnostics Area: Messages for errors, warnings, and lints C-bug Category: This is a bug. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@gnzlbg
Copy link
Contributor

gnzlbg commented Apr 7, 2019

E0424 states:

The self keyword was used in a static method.

But static method is probably wrong, it should just say a non-method (I don't think we have a technical term to refer to methods that do not take an arbitrary self type as first argument).

It continues with:

Please check if the method's argument list should have contained self, &self, or &mut self (in case you didn't want to create a static method), and add it if so.

That's outdated, &mut Pin<Self> (and others) work too. So this should be updated to cover arbitrary Self types.

@gnzlbg
Copy link
Contributor Author

gnzlbg commented Apr 7, 2019

cc @Centril

@Centril Centril 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 Apr 7, 2019
@Centril
Copy link
Contributor

Centril commented Apr 7, 2019

Yea; the diagnostic is wrong. Methods are only those things that have receivers (self). Other things are functions (methods are too)... we don't have a word for "thing that is a function but not a method" so "non-method" works and is clear in its explanatory purpose.

@Centril Centril added the C-bug Category: This is a bug. label Apr 7, 2019
@petrochenkov
Copy link
Contributor

petrochenkov commented Apr 7, 2019

"Static method" is just an older terminology, which should be natural and familiar if you used any other popular language before, but, IIRC, the doc (or lang) team made a decision not to use it in documentation at one point without providing a good alternative.

If you want to "fix" this you may want to update a bunch of other docs as well, rust-by-example in particular.

@gnzlbg
Copy link
Contributor Author

gnzlbg commented Apr 7, 2019

@Centril I think it would be better to get the lang team to sign on how to call these. I agree with @petrochenkov that we used to have terminology for these (static methods, inherent functions, UFCS, ...) which have been phased out without a replacement.

From a grammar POV, an impl {} block contains associated items, so methods are just associated functions with a receiver, and these are just associated functions. I'd guess that non-methods would work as well, but I'm not too fond of that name.

@Centril
Copy link
Contributor

Centril commented Apr 7, 2019

@gnzlbg I don't view "non-method" as a name really; it's describing things explicitly by opposition to methods, which makes sense for this diagnostic since having a self is the defining characteristic of a method and the problem was that it wasn't a method.

cc @rust-lang/lang @rust-lang/docs

I don't want to FCP this... it's not important enough to get all the team to sign off.

@estebank
Copy link
Contributor

estebank commented Apr 7, 2019

Regarding arbitrary self types... There are a couple of places where suggestions or help text is technically incorrect by omission, like this one. We need to walk the line of being correct, thorough, and succinct. I'm not sure this case is a good place to teach people about this feature, depending on how much text/space it takes.

@Centril
Copy link
Contributor

Centril commented Apr 7, 2019

@estebank perhaps link to a more elaborate description in the message?

@gnzlbg
Copy link
Contributor Author

gnzlbg commented Apr 8, 2019

@estebank maybe using something like: "self receivers (like self, &self, or &mut self)", where "self receivers" links to their page in the reference would be correct (because of self receiver), succinct, and approachable for beginners (because it contains the three most common cases).

Centril added a commit to Centril/rust that referenced this issue Oct 19, 2019
…tril

Refer to "associated functions" instead of "static methods"

Fix rust-lang#59782.
Centril added a commit to Centril/rust that referenced this issue Oct 19, 2019
…tril

Refer to "associated functions" instead of "static methods"

Fix rust-lang#59782.
@bors bors closed this as completed in fab7404 Oct 19, 2019
@dayo777
Copy link

dayo777 commented Oct 4, 2024

Just came across this error in Rust edition 2021. I understand what the error means, but for anyone looking for a simpler explanation,
*it typically indicates that you are trying to reference a method, associated function, or field that is not defined or accessible in the current scope of that impl block. *

struct MyStruct {
    name: String
}

impl MyStruct {
    fn first_fn() {
        self.second_fn()
    }
    
    fn second_fn(&self) {
        println!("Second fn");
   }
}

The above code was similar to my use-case. To make the code work, you have to make the first_fn &self in other to add it to scope so that you can reference the second function.

@estebank
Copy link
Contributor

estebank commented Oct 5, 2024

Note that the current error for that example is

error[E0424]: expected value, found module `self`
 --> src/lib.rs:7:9
  |
6 |     fn first_fn() {
  |        -------- this function doesn't have a `self` parameter
7 |         self.second_fn()
  |         ^^^^ `self` value is a keyword only available in methods with a `self` parameter
  |
help: add a `self` receiver parameter to make the associated `fn` a method
  |
6 |     fn first_fn(&self) {
  |                 +++++

which I feel provides the context needed. @dayo777 is there anything there that you'd want to add?

@dayo777
Copy link

dayo777 commented Oct 6, 2024

Hello @estebank , nothing.

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 C-bug Category: This is a bug. 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.

5 participants