Skip to content

Confusing diagnostic for supertrait cycle #80164

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

Open
RReverser opened this issue Dec 18, 2020 · 1 comment
Open

Confusing diagnostic for supertrait cycle #80164

RReverser opened this issue Dec 18, 2020 · 1 comment
Labels
A-diagnostics Area: Messages for errors, warnings, and lints A-trait-system Area: Trait system D-papercut Diagnostics: An error or lint that needs small tweaks. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@RReverser
Copy link
Contributor

pub trait Constant: From<Self::Value> + Into<Self::Value> + Copy + Eq + Ord {
    type Value;
}

(Playground)

Errors:

   Compiling playground v0.0.1 (/playground)
error[E0391]: cycle detected when computing the supertraits of `Constant`
 --> src/lib.rs:1:1
  |
1 | pub trait Constant: From<Self::Value> + Into<Self::Value> + Copy + Eq + Ord {
  | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  |
  = note: ...which again requires computing the supertraits of `Constant`, completing the cycle
note: cycle used when collecting item types in top-level module
 --> src/lib.rs:1:1
  |
1 | pub trait Constant: From<Self::Value> + Into<Self::Value> + Copy + Eq + Ord {
  | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

error: aborting due to previous error

For more information about this error, try `rustc --explain E0391`.
error: could not compile `playground`

To learn more, run the command again with --verbose.

Note: it's possible to reproduce the issue with a lot simpler repro, leaving just the From<Self::Value part, but example with more traits is more problematic, because in that case it's unclear which one of the trait clauses is actually causing the issue.

Note 2: Aside from improving diagnostics, why is this an error at all? There is no trait cycle as far as I can tell, it's just trying to use its own associated type which feels like should be fine...

cc @estebank

@estebank
Copy link
Contributor

Another set of cases that should be handled (perhaps separately) is trait bounds on type parameters like this and others:

fn foo<R>(_: R) where R: Trait<R::Assoc> {}
trait Trait<A> { }

why is this an error at all? There is no trait cycle as far as I can tell, it's just trying to use its own associated type which feels like should be fine...

This can be written this way to resolve the cycle error:

pub trait Constant: From<<Self as Constant>::Value> + Into<<Self as Constant>::Value> + Copy + Eq + Ord {
    type Value;
}

This is because rustc (currently) needs to evaluate all of Constant to figure out that it has a Value when invoked through Self, which in turn tries to evaluate Constant, causing the cycle.

We recently landed a PR that would have let this compile, but it had to be reverted. This will eventually land and (I'm pretty sure) make your original code compile. Even when that happens, the PR doesn't handle all cases, so there will be a need to improve the diagnostic regardless.

@estebank estebank added A-diagnostics Area: Messages for errors, warnings, and lints A-trait-system Area: Trait system D-papercut Diagnostics: An error or lint that needs small tweaks. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Dec 18, 2020
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 A-trait-system Area: Trait system D-papercut Diagnostics: An error or lint that needs small tweaks. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

2 participants