Skip to content

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

Open

Description

@tage64

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.

Activity

  1. Centril commented on Oct 29, 2019

    @Centril
    Contributor

    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

  2. added
    T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.
    on Oct 29, 2019
  3. eddyb commented on Oct 29, 2019

    @eddyb
    Contributor

    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.

  4. alexreg commented on Oct 29, 2019

    @alexreg
    Contributor

    @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.

  5. jendrikw commented on Jun 27, 2020

    @jendrikw
    Contributor

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

  6. compiler-errors commented on May 7, 2023

    @compiler-errors
    Contributor

    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.

  7. fmease commented on Aug 21, 2023

    @fmease
    Member

    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

  8. added
    E-needs-mcveCall for participation: This issue has a repro, but needs a Minimal Complete and Verifiable Example
    I-hangIssue: The compiler never terminates, due to infinite loops, deadlock, livelock, etc.
    E-needs-bisectionCall for participation: This issue needs bisection: https://github.com/rust-lang/cargo-bisect-rustc
    and removed
    E-needs-mcveCall for participation: This issue has a repro, but needs a Minimal Complete and Verifiable Example
    on Aug 21, 2023
  9. fmease commented on Aug 21, 2023

    @fmease
    Member

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

  10. removed
    E-needs-bisectionCall for participation: This issue needs bisection: https://github.com/rust-lang/cargo-bisect-rustc
    on Aug 21, 2023
  11. added a commit that references this issue on Feb 25, 2024
  12. added a commit that references this issue on Feb 25, 2024
  13. added
    I-cycleIssue: A query cycle occurred while none was expected
    and removed
    I-hangIssue: The compiler never terminates, due to infinite loops, deadlock, livelock, etc.
    on Mar 6, 2024
  14. compiler-errors commented on Mar 6, 2024

    @compiler-errors
    Contributor

    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.

  15. added
    A-diagnosticsArea: Messages for errors, warnings, and lints
    D-confusingDiagnostics: Confusing error or lint that should be reworked.
    D-terseDiagnostics: An error or lint that doesn't give enough information about the problem at hand.
    on Mar 6, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsA-trait-systemArea: Trait systemD-confusingDiagnostics: Confusing error or lint that should be reworked.D-terseDiagnostics: An error or lint that doesn't give enough information about the problem at hand.F-associated_type_bounds`#![feature(associated_type_bounds)]`I-cycleIssue: A query cycle occurred while none was expectedT-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions