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 when using associated type bounds in super trait #65913

Open
tage64 opened this issue Oct 28, 2019 · 8 comments
Open

Cycle detected when using associated type bounds in super trait #65913

tage64 opened this issue Oct 28, 2019 · 8 comments
Labels
A-diagnostics Area: Messages for errors, warnings, and lints A-traits Area: Trait system D-confusing Diagnostics: Confusing error or lint that should be reworked. D-terse Diagnostics: An error or lint that doesn't give enough information about the problem at hand. F-associated_type_bounds `#![feature(associated_type_bounds)]` I-cycle Issue: A query cycle occurred while none was expected T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@tage64
Copy link

tage64 commented Oct 28, 2019

I don't know if this is a bug or expected behaviour. But I'm encountering the following error when compiling the following code.

#![feature(associated_type_bounds)]

trait A {
    type T;
}

trait B: A<T: B> {}
error[E0391]: cycle detected when computing the supertraits of `B`
 --> src/main.rs:6:15
  |
6 | trait B: A<T: B> {}
  |               ^
  |
  = note: ...which again requires computing the supertraits of `B`, completing the cycle
note: cycle used when collecting item types in top-level module
 --> src/main.rs:6:1
  |
6 | trait B: A<T: B> {}
  | ^^^^^^^^^^^^^^^^

But if I merge the traits A and B into the trait C it works.

trait C {
    type T: C;
}

I think the first example should work as good as the second since the trait C is only split into two traits A and B with same functionality.

@jonas-schievink jonas-schievink added A-traits Area: Trait system F-associated_type_bounds `#![feature(associated_type_bounds)]` labels Oct 28, 2019
@Centril
Copy link
Contributor

Centril commented Oct 29, 2019

When we write:

trait B: A<T: B> {}

this is equivalent to:

trait B
where
    Self: A<T: B>
{}

which is equivalent to:

trait B
where
    Self: A,
    <Self as A>::T: B,
{}

This results in an error:

error[E0277]: the trait bound `<<Self as A>::T as A>::T: B` is not satisfied

One might think that this should compile because <<Self as A>::T as A>::T: B should flow from the mere fact that Self::T: B holds which should hold by Self: B. However, the compiler isn't smart enough to take these implied bounds into account. When it comes to the special case of type T: C;, the compiler is able to do that however.

What is curious here is that rather than getting the error message about "not satisfied", we are getting a cycle error. cc @alexreg @nikomatsakis @tmandry

@Centril Centril added the T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. label Oct 29, 2019
@eddyb
Copy link
Member

eddyb commented Oct 29, 2019

I wonder if the supertraits query is including the associated type bound - AFAIK it should only include the bounds specified on Self itself, not associated types of Self or other types.

@alexreg
Copy link
Contributor

alexreg commented Oct 29, 2019

@eddyb I know that was a problem previously, though I introduced a filter in relevant places to combat it. We may need to introduce a filter in another place.

@jendrikw
Copy link
Contributor

This issues still happens, are there any updates on fixes?

@compiler-errors
Copy link
Member

As of #110512, this is no longer a cycle error, and the code above errors with:

error[E0277]: the trait bound `<<Self as A>::T as A>::T: B` is not satisfied
 --> /home/../test.rs:7:15
  |
7 | trait B: A<T: B> {}
  |               ^ the trait `B` is not implemented for `<<Self as A>::T as A>::T`
  |
note: required by a bound in `B`
 --> /home/../test.rs:7:15
  |
7 | trait B: A<T: B> {}
  |               ^ required by this bound in `B`
help: consider further restricting the associated type
  |
7 | trait B: A<T: B> where <<Self as A>::T as A>::T: B {}
  |                  +++++++++++++++++++++++++++++++++

The fact that this still errors is a somewhat unfortunate side-effect of #65913 (comment), since the <Self as A>::T: B bound is not implied by the supertrait bounds.

Fixing this in a way that doesn't cause the compiler to overflow generating an infinite nested set of <<<<<<... as A>::T as A>::T as A>::T as A>::T as A>::T as A>::T: B bounds is gonna be pretty difficult, though.

@fmease
Copy link
Member

fmease commented Aug 21, 2023

This now hangs on nightly (not only the sugared form but also the desugared one) with both the old and the new solver.

@rustbot label I-hang E-needs-bisection

@rustbot rustbot added E-needs-mcve Call for participation: This issue has a repro, but needs a Minimal Complete and Verifiable Example I-hang Issue: The compiler never terminates, due to infinite loops, deadlock, livelock, etc. E-needs-bisection Call for participation: This issue needs bisection: https://github.com/rust-lang/cargo-bisect-rustc and removed E-needs-mcve Call for participation: This issue has a repro, but needs a Minimal Complete and Verifiable Example labels Aug 21, 2023
@fmease
Copy link
Member

fmease commented Aug 21, 2023

Regressed in #112629 (git-revert: hang → errors). CC @compiler-errors
@rustbot label -E-needs-bisection

@rustbot rustbot removed the E-needs-bisection Call for participation: This issue needs bisection: https://github.com/rust-lang/cargo-bisect-rustc label Aug 21, 2023
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this issue Feb 25, 2024
Prevent cycle in implied predicates computation

Makes rust-lang#65913 from hang -> fail. I believe fail is the correct state for this test to remain for the long term.
rust-timer added a commit to rust-lang-ci/rust that referenced this issue Feb 25, 2024
Rollup merge of rust-lang#121409 - compiler-errors:atb-cycle, r=cjgillot

Prevent cycle in implied predicates computation

Makes rust-lang#65913 from hang -> fail. I believe fail is the correct state for this test to remain for the long term.
@fmease fmease added I-cycle Issue: A query cycle occurred while none was expected and removed I-hang Issue: The compiler never terminates, due to infinite loops, deadlock, livelock, etc. labels Mar 6, 2024
@compiler-errors
Copy link
Member

I don't believe this will ever be made to compile. A cycle error IMO is the correct state for this. Diagnostics could perhaps be improved, but they would need to explain some details about how supertrait bound elaboration works.

@compiler-errors compiler-errors added A-diagnostics Area: Messages for errors, warnings, and lints D-confusing Diagnostics: Confusing error or lint that should be reworked. D-terse Diagnostics: An error or lint that doesn't give enough information about the problem at hand. labels Mar 6, 2024
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-traits Area: Trait system D-confusing Diagnostics: Confusing error or lint that should be reworked. D-terse Diagnostics: An error or lint that doesn't give enough information about the problem at hand. F-associated_type_bounds `#![feature(associated_type_bounds)]` I-cycle Issue: A query cycle occurred while none was expected 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

9 participants