Skip to content

Type system limitations with double-diamond trait dependencies with bounded associated types #139486

Open
@LucaCappelletti94

Description

@LucaCappelletti94

Hi - apparently whenever there is a trait dependency chain which included bounded associated types and forms a double diamond pattern, such as the one illustrated in this figure, the type system fails with the error cannot satisfy `<Self as Red>::R == <Self as RedGreenBlue>::RGB` , with the full backtrace reported below.

double diamong

I tried to simplify as much as possible the issue and I documented it in this GitHub repository.

I boiled it down to this code, which you can try out at Rust playground:

pub trait Ancestor {
    type A;
}

pub trait Red: Ancestor<A = <Self as Red>::R> {
    type R;
}

pub trait Blue: Ancestor<A = <Self as Blue>::B> {
    type B;
}

pub trait Green: Ancestor<A = <Self as Green>::G> {
    type G;
}

pub trait RedGreen: Red<R = <Self as RedGreen>::RG> + Blue<B = <Self as RedGreen>::RG> {
    type RG;
}

pub trait GreenBlue: Red<R = <Self as GreenBlue>::GB> + Green<G = <Self as GreenBlue>::GB> {
    type GB;
}

pub trait RedGreenBlue:
    RedGreen<RG = <Self as RedGreenBlue>::RGB> + GreenBlue<GB = <Self as RedGreenBlue>::RGB>
{
    type RGB;
}

Meta

After having switched to nightly mode with rustup default nightly and executed rustup update, the error persists. Here is the output of executing rustc --version --verbose on my system:

rustc 1.88.0-nightly (2fa8b11f0 2025-04-06)
binary: rustc
commit-hash: 2fa8b11f0933dae9b4e5d287cc10c989218e8b36
commit-date: 2025-04-06
host: x86_64-unknown-linux-gnu
release: 1.88.0-nightly
LLVM version: 20.1.2
Backtrace

error[E0284]: type annotations needed
  --> src/lib.rs:27:1
   |
27 | / pub trait RedGreenBlue:
28 | |     RedGreen<RG = <Self as RedGreenBlue>::RGB> + GreenBlue<GB = <Self as RedGreenBlue>::RGB>
   | |____________________________________________________________________________________________^ cannot infer type
   |
   = note: cannot satisfy `<Self as Red>::R == _`

error[E0284]: type annotations needed: cannot satisfy `<Self as Red>::R == <Self as RedGreenBlue>::RGB`
  --> src/lib.rs:28:5
   |
28 |     RedGreen<RG = <Self as RedGreenBlue>::RGB> + GreenBlue<GB = <Self as RedGreenBlue>::RGB>
   |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ cannot satisfy `<Self as Red>::R == <Self as RedGreenBlue>::RGB`
   |
note: required by a bound in `RedGreen`
  --> src/lib.rs:19:25
   |
19 | pub trait RedGreen: Red<R = <Self as RedGreen>::RG> + Blue<B = <Self as RedGreen>::RG> {
   |                         ^^^^^^^^^^^^^^^^^^^^^^^^^^ required by this bound in `RedGreen`

error[E0284]: type annotations needed
  --> src/lib.rs:30:5
   |
30 |     type RGB;
   |     ^^^^^^^^ cannot infer type
   |
   = note: cannot satisfy `<Self as Red>::R == _`

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-trait-systemArea: Trait systemC-bugCategory: This is a bug.T-typesRelevant to the types team, which will review and decide on the PR/issue.fixed-by-next-solverFixed by the next-generation trait solver, `-Znext-solver`.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions