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

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

Open
LucaCappelletti94 opened this issue Apr 7, 2025 · 0 comments
Labels
A-trait-system Area: Trait system C-bug Category: This is a bug. fixed-by-next-solver Fixed by the next-generation trait solver, `-Znext-solver`. T-types Relevant to the types team, which will review and decide on the PR/issue.

Comments

@LucaCappelletti94
Copy link

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 == _`

@LucaCappelletti94 LucaCappelletti94 added the C-bug Category: This is a bug. label Apr 7, 2025
@rustbot rustbot added the needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. label Apr 7, 2025
@fmease fmease added A-trait-system Area: Trait system T-types Relevant to the types team, which will review and decide on the PR/issue. fixed-by-next-solver Fixed by the next-generation trait solver, `-Znext-solver`. and removed needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. labels Apr 7, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-trait-system Area: Trait system C-bug Category: This is a bug. fixed-by-next-solver Fixed by the next-generation trait solver, `-Znext-solver`. T-types Relevant to the types team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

3 participants