]> git.proxmox.com Git - rustc.git/blame - src/librustc_error_codes/error_codes/E0424.md
New upstream version 1.47.0+dfsg1
[rustc.git] / src / librustc_error_codes / error_codes / E0424.md
CommitLineData
60c5eb7d
XL
1The `self` keyword was used inside of an associated function without a "`self`
2receiver" parameter.
3
4Erroneous code example:
5
6```compile_fail,E0424
7struct Foo;
8
9impl Foo {
10 // `bar` is a method, because it has a receiver parameter.
11 fn bar(&self) {}
12
13 // `foo` is not a method, because it has no receiver parameter.
14 fn foo() {
15 self.bar(); // error: `self` value is a keyword only available in
16 // methods with a `self` parameter
17 }
18}
19```
20
21The `self` keyword can only be used inside methods, which are associated
22functions (functions defined inside of a `trait` or `impl` block) that have a
23`self` receiver as its first parameter, like `self`, `&self`, `&mut self` or
24`self: &mut Pin<Self>` (this last one is an example of an ["abitrary `self`
25type"](https://github.com/rust-lang/rust/issues/44874)).
26
27Check if the associated function's parameter list should have contained a `self`
28receiver for it to be a method, and add it if so. Example:
29
30```
31struct Foo;
32
33impl Foo {
34 fn bar(&self) {}
35
36 fn foo(self) { // `foo` is now a method.
37 self.bar(); // ok!
38 }
39}
40```