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 short message weirdly confusing #51547

Closed
Havvy opened this issue Jun 14, 2018 · 2 comments · Fixed by #61087
Closed

E0424 short message weirdly confusing #51547

Havvy opened this issue Jun 14, 2018 · 2 comments · Fixed by #61087
Labels
A-diagnostics Area: Messages for errors, warnings, and lints

Comments

@Havvy
Copy link
Contributor

Havvy commented Jun 14, 2018

This invalid function gives two instances of E0424.

fn foo(self: i32) -> i32 { self }
error[E0424]: expected unit struct/variant or constant, found module `self`
 --> src/main.rs:8:8
  |
L | fn foo(self: i32) -> i32 { self }
  |        ^^^^ `self` value is only available in methods with `self` parameter

error[E0424]: expected value, found module `self`
 --> src/main.rs:8:28
  |
L | fn foo(self: i32) -> i32 { self }
  |                            ^^^^ `self` value is only available in methods with `self` parameter

The long diagnostic for this error states "The self keyword was used in a static method." which while slightly wrong in this case (it's a free function, not a method at all), it's more correct than what's emitted by the compiler.

Overall, this diagnostic needs to be cleaned up.

  1. In the first instance, the error message should state "self parameter can only be declared on methods".
  2. In the first instance, the help message should just be removed.
  3. In the second instance, it probably shouldn't even be emitted. Possibly check if a parameter was erroneously named self and if so, pretend it's a valid variable name in the function. Maybe a note pointing out its usage as part of the first emitted error would be okay here?
  4. The long error description (in the error index) should say "free function or method".
@sanxiyn sanxiyn added the A-diagnostics Area: Messages for errors, warnings, and lints label Jun 21, 2018
@Havvy Havvy changed the title E0242 short message weirdly confusing E0424 short message weirdly confusing Jun 30, 2018
@durka
Copy link
Contributor

durka commented Jun 30, 2018

I have some related gripes about this error message:

error[E0424]: expected unit struct/variant or constant, found local variable `self`
  --> expr.rs:18:50
   |
18 |     struct S; impl S { fn foo(self) { let self = 42; } }
   |                                           ^^^^ `self` value is only available in methods with `self` parameter

The error seems to have a second meaning, namely "self used as an identifier for binding but not in the argument list", and this fires whether or not the method has a self parameter. This isn't covered in the --explain either. Should it be split out to a different error code?

@estebank
Copy link
Contributor

Current output:

error: unexpected `self` argument in function
 --> src/lib.rs:1:14
  |
1 | fn foo(self: i32) -> i32 { self }
  |              ^^^ `self` is only valid as the first argument of an associated function

error[E0424]: expected value, found module `self`
 --> src/lib.rs:1:28
  |
1 | fn foo(self: i32) -> i32 { self }
  |                            ^^^^ `self` value is a keyword only available in methods with `self` parameter

The case in the comment still has the same output.

Centril added a commit to Centril/rust that referenced this issue May 25, 2019
Tweak `self` arg not as first argument of a method diagnostic

Mention that `self` is only valid on "associated functions"
```
error: unexpected `self` argument in function
  --> $DIR/self-in-function-arg.rs:1:15
   |
LL | fn foo(x:i32, self: i32) -> i32 { self }
   |               ^^^^ not valid as function argument
   |
   = note: `self` is only valid as the first argument of an associated function
```

When it is a method, mention it must be first
```
error: unexpected `self` argument in function
  --> $DIR/trait-fn.rs:4:20
   |
LL |     fn c(foo: u32, self) {}
   |                    ^^^^ must be the first associated function argument
```

Move a bunch of error recovery methods to `diagnostics.rs` away from `parser.rs`.

Fix rust-lang#51547. CC rust-lang#60015.
Centril added a commit to Centril/rust that referenced this issue May 26, 2019
Tweak `self` arg not as first argument of a method diagnostic

Mention that `self` is only valid on "associated functions"
```
error: unexpected `self` argument in function
  --> $DIR/self-in-function-arg.rs:1:15
   |
LL | fn foo(x:i32, self: i32) -> i32 { self }
   |               ^^^^ not valid as function argument
   |
   = note: `self` is only valid as the first argument of an associated function
```

When it is a method, mention it must be first
```
error: unexpected `self` argument in function
  --> $DIR/trait-fn.rs:4:20
   |
LL |     fn c(foo: u32, self) {}
   |                    ^^^^ must be the first associated function argument
```

Move a bunch of error recovery methods to `diagnostics.rs` away from `parser.rs`.

Fix rust-lang#51547. CC rust-lang#60015.
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
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants