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

Improve error message for impl Self #65784

Closed
sunjay opened this issue Oct 25, 2019 · 4 comments
Closed

Improve error message for impl Self #65784

sunjay opened this issue Oct 25, 2019 · 4 comments
Assignees
Labels
A-diagnostics Area: Messages for errors, warnings, and lints C-enhancement Category: An issue proposing an enhancement or a PR with one. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@sunjay
Copy link
Member

sunjay commented Oct 25, 2019

Suppose you have the following reduced code example:

impl Self {   
}

This gives you the error message:

error[E0391]: cycle detected when processing `<impl at src/lib.rs:1:1: 3:2>`
 --> src/lib.rs:1:6
  |
1 | impl Self {
  |      ^^^^
  |
  = note: ...which again requires processing `<impl at src/lib.rs:1:1: 3:2>`, completing the cycle
note: cycle used when collecting item types in top-level module
 --> src/lib.rs:1:1
  |
1 | impl Self {
  | ^^^^^^^^^

This is confusing because there isn't actually any obvious cycle (unless you know things about compilers). E0391 actually describes itself with an example like this:

trait FirstTrait : SecondTrait {

}

trait SecondTrait : FirstTrait {

}

This looks nothing like the original code example.

We should probably detect this case and provide a more useful message that indicates that Self is a special keyword that can be used within the impl block, but not as the type of the impl block itself.

@Centril
Copy link
Contributor

Centril commented Oct 25, 2019

I'm curious how impl Self { ... } happened -- can you describe the scenario?

@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 Oct 25, 2019
@petar-dambovaliev
Copy link
Contributor

@Centril I am also curious. I don't know of a language that supports that.
We should improve error messages when there is an actual real world example.

@JohnTitor JohnTitor added the C-enhancement Category: An issue proposing an enhancement or a PR with one. label Dec 8, 2019
@TaKO8Ki TaKO8Ki self-assigned this Mar 17, 2022
@compiler-errors
Copy link
Member

@BoxyUwU fixed this, the error message on nightly looks like this now:

error: `Self` is not valid in the self type of an impl block
 --> src/lib.rs:1:6
  |
1 | impl Self {   
  |      ^^^^
  |
  = note: replace `Self` with a different type

Further improvements on the error message would be nice as a new issue, so newcomers can discover the issue, since it'd probably be a good E-easy task.

@BoxyUwU
Copy link
Member

BoxyUwU commented Nov 4, 2022

Oh I didn't realise there was an issue open for this..

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-enhancement Category: An issue proposing an enhancement or a PR with one. 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.

7 participants