More or less tightly coupled traits fail in where clause trying to establish a tight coupling
Nobody has claimed this yet.
- Dominant language
- Rust
- Stars
- 119k
- Forks
- 16.1k
- PR merge metrics
- PR metrics pending
Description
I tried this code playground:
trait A {
type TC: C<TA = Self>;
}
trait B {
type TA: A;
}
trait C: B where
// adding this breaks it when using the trait
Self::TA: A<TC = Self>,
{
}
#[allow(dead_code)]
fn use_c<X>()
where
X: C,
{
}
The compiler (e.g. rustc 1.74 stable, but also nightly) fails with:
error[E0271]: type mismatch resolving `<<X as B>::TA as A>::TC == X`
--> src/lib.rs:18:8
|
16 | fn use_c<X>()
| - this type parameter
17 | where
18 | X: C,
| ^ expected type parameter `X`, found associated type
|
= note: expected type parameter `X`
found associated type `<<X as B>::TA as A>::TC`
= note: you might be missing a type parameter or trait bound
note: required by a bound in `C`
--> src/lib.rs:11:17
|
9 | trait C: B where
| - required by a bound in this trait
10 | // adding this breaks it when using the trait
11 | Self::TA: A<TC = Self>,
| ^^^^^^^^^ required by this bound in `C`
For more information about this error, try `rustc --explain E0271`.
The idea is to have "coupled" types through traits (in my case: various IP-address based types that are linked by address family, see cidr::Address - I'd like to add a GenericCidr supertrait to Cidr which just includes the Address associated type).
In the example A and C are supposed to strictly coupled, while B simply points to an A type (but B also needs to be a base for C) - so there could be a type implementing B but not C, but it'd point to a type implementing A with a partner type for C (that also implements B).
The where could be moved to the function (X::TA: A<TC = X>), but I don't see why it can't work in the trait, and the error seems to indicate the compiler is quite confused about what the problem is.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with the linked Rust Playground reproduction and the A, B, C, and use_c trait definitions. Test the example on stable and nightly rustc, then compare the trait-level where clause with the function-level equivalent described in the issue. Done means determining whether the trait bound should be accepted and, if not, clarifying or correcting the compiler behavior and diagnostic.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- rust
- Domain
- compilers
- Issue type
- Bug
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100