Skip to content

Exception for cyclic-safe traits #29826

@WildCryptoFox

Description

@WildCryptoFox

Thy cyclic supertrait lint and super-trait tracing code should have an exception to allow for cyclic-safe traits.

https://play.rust-lang.org/?gist=cc794942ed0663d530d8&version=nightly

A trait definition like this is currently invalid.

trait Mode<M>: Mode<A> { type Return; }

Instead of being invalid, this should cut the recursion at the point that M == RequiredMode.

Any type that implements Mode<B> must also implement Mode<A> and when selecting specific traits with <T as Trait>, the exact trait should be respected.

<T as Mode<B>>::Return // B
<T as Mode<A>>::Return // A
<<T as Mode<B>> as Mode<A>>::Return // A

As demonstrated in #12511, I would also say that trait T: T {} should be the identity of T, and consequently be the identity of the exception to permit cyclic-safe types.

Cyclic safe traits

trait Trait: Trait { ... } // Trait == Trait, stop recurring // lint redundant trait?
trait Mode<M>: Mode<A> { ... } // When M == A, stop recurring

Cyclic unsafe traits

trait A: B { ... }
trait B: A { ... } 

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions