Reputation: 13
Just getting into rust and it's been a super interesting journey so far, but I've come across my first compiler error that all my best google and stack overflow skills haven't been able to fix.
The problem arose in a slightly more complicated bit of code but I've managed to distil it into this little snippet:
pub trait FooTrait {}
pub trait BarTrait {
type Foo
where
Self::Foo: FooTrait;
}
struct FooImpl {}
impl FooTrait for FooImpl {}
struct BarImpl {}
impl BarTrait for BarImpl {
type Foo = FooImpl;
}
When I try and compile this I get the error
error[E0275]: overflow evaluating the requirement `<BarImpl as BarTrait>::Foo == _`
--> src/main.rs:13:16
|
13 | type Foo = FooImpl;
| ^^^^^^^
I've been scratching my head about this for a couple of days now so any input would be very welcome; it really doesn't feel like I am asking something to do something unreasonable... I have a trait with an associated type in it that is bounded to have a different trait.
If you think what I'm trying to do is stupid in the first place I can supply a more concrete example of the actual problem at hand; I just figured this was easier to read.
This stack overflow question seems a bit similar, but I cannot figure out how what I am trying to do is recursive in any way
just about every combination of type bounding I could conceive; what I expected it to do was compile without an error.. what it actually did was refuse to compile.
There are a few similar questions about this error on stack overflow but none of them seem to be the same to my inexperienced eyes
Upvotes: 1
Views: 408
Reputation: 59827
Use this syntax instead:
pub trait BarTrait {
type Foo: FooTrait;
}
My naive understanding is that specifying a constraint on Self::Foo
requires the compiler to look up if Self
implements BarTrait
. However this constraint appears while it is currently evaluating if BarImpl
implements BarTrait
and therefore recurses without bound. The where
clause is designed for cases where you can't use the above syntax.
This is reported as issue #87755. Whether intentional behavior or not, hopefully we will at least get a better diagnostic message.
Upvotes: 3