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

"cycle detected" with impl Self {} #93952

Closed
matthiaskrgr opened this issue Feb 12, 2022 · 4 comments
Closed

"cycle detected" with impl Self {} #93952

matthiaskrgr opened this issue Feb 12, 2022 · 4 comments
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

@matthiaskrgr
Copy link
Member

matthiaskrgr commented Feb 12, 2022

Given the following code:

impl Self {}

The current output is:

error[E0391]: cycle detected when computing type of `<impl at src/main.rs:1:1: 1:13>`
 --> src/main.rs:1:6
  |
1 | impl Self {}
  |      ^^^^
  |
  = note: ...which immediately requires computing type of `<impl at src/main.rs:1:1: 1:13>` again
note: cycle used when collecting item types in top-level module
 --> src/main.rs:1:1
  |
1 | impl Self {}
  | ^^^^^^^^^

For more information about this error, try `rustc --explain E0391`.

Ideally the output should look like:

Something more like

error[E0412]: cannot find type `Self` in this scope
 --> src/main.rs:1:6
  |
1 | impl Self {}
  |      ^^^^ not found in this scope

Or even a better message explaining that there should be an actual type there instead of just "Self" ..?

@matthiaskrgr matthiaskrgr 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 Feb 12, 2022
@ibraheemdev
Copy link
Member

A potentially confusing case:

struct Foo;

impl Foo {
    fn foo() {
        impl Self { fn bar() {} }
    }
}
error[E0391]: cycle detected when computing type of `main::<impl at src/main.rs:4:1: 8:2>::foo::<impl at src/main.rs:6:9: 6:34>`
 --> src/main.rs:6:14
  |
6 |         impl Self { fn bar() {} }
  |              ^^^^
  |
  = note: ...which immediately requires computing type of `main::<impl at src/main.rs:4:1: 8:2>::foo::<impl at src/main.rs:6:9: 6:34>` again
note: cycle used when collecting item types in top-level module
 --> src/main.rs:1:1
  |
1 | fn main() {
  | ^^^^^^^^^

@compiler-errors
Copy link
Member

@rustbot claim

@edward-shen
Copy link
Contributor

No longer repros in 1.68.0-nightly (2023-01-09 3020239) as it now returns a a better message (playground):

error: `Self` is not valid in the self type of an impl block
 --> src/lib.rs:5:14
  |
5 |         impl Self { fn bar() {} }
  |              ^^^^
  |
  = note: replace `Self` with a different type

error: could not compile `playground` due to previous error

Also found this issue via a similar usecase, which also no longer repros:

impl TryFrom<String> for Self {
    type Error = String;
    fn try_from(s: String) -> Result<Self, Self::Error> {
        Self::new(s)
    }
}

@compiler-errors
Copy link
Member

Yeah @BoxyUwU fixed 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 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.

4 participants