Description
Spun off of #89196
Code example
trait MiniYokeable<'a> {
type Output;
}
struct MiniYoke<Y: for<'a> MiniYokeable<'a>> {
pub yokeable: Y,
}
impl<Y> Clone for MiniYoke<Y>
where
Y: for<'a> MiniYokeable<'a>,
for<'a> <Y as MiniYokeable<'a>>::Output: Clone
{
fn clone(&self) -> Self {
todo!()
}
}
trait MiniDataMarker {
type Yokeable: for<'a> MiniYokeable<'a>;
}
trait MiniDataProvider<M>
where
M: MiniDataMarker
{
fn mini_load_payload(&self) -> MiniYoke<M::Yokeable>;
}
struct MiniStructProvider<M>
where
M: MiniDataMarker,
{
pub yoke: MiniYoke<M::Yokeable>,
}
impl<M> MiniDataProvider<M> for MiniStructProvider<M>
where
M: MiniDataMarker,
for<'a> <M::Yokeable as MiniYokeable<'a>>::Output: Clone,
{
fn mini_load_payload(&self) -> MiniYoke<M::Yokeable> {
self.yoke.clone()
}
}
#[derive(Clone)]
struct SimpleStruct(pub u32);
impl<'a> MiniYokeable<'a> for SimpleStruct {
type Output = SimpleStruct;
}
impl MiniDataMarker for SimpleStruct {
type Yokeable = SimpleStruct;
}
fn main() {
let provider = MiniStructProvider {
yoke: MiniYoke {
yokeable: SimpleStruct(42)
}
};
let yoke: MiniYoke<SimpleStruct> = provider.mini_load_payload();
assert_eq!(yoke.yokeable.0, 42);
}
(playpen)
This fails to compile (needs beta or nightly to avoid #85636 , fixed by #85499 ) with the following error:
error[E0599]: the method `mini_load_payload` exists for struct `MiniStructProvider<_>`, but its trait bounds were not satisfied
--> src/main.rs:65:49
|
30 | / struct MiniStructProvider<M>
31 | | where
32 | | M: MiniDataMarker,
33 | | {
34 | | pub yoke: MiniYoke<M::Yokeable>,
35 | | }
| | -
| | |
| |_method `mini_load_payload` not found for this
| doesn't satisfy `MiniStructProvider<_>: MiniDataProvider<_>`
...
65 | let yoke: MiniYoke<SimpleStruct> = provider.mini_load_payload();
| ^^^^^^^^^^^^^^^^^ method cannot be called on `MiniStructProvider<_>` due to unsatisfied trait bounds
|
= note: the following trait bounds were not satisfied:
`<_ as MiniYokeable<'a>>::Output: Clone`
which is required by `MiniStructProvider<_>: MiniDataProvider<_>`
This error is incorrect, the trait bounds were satisfied. What's going on here is that the explicit type of provider
is not known, and inference is unable to figure it out since there can be multiple M: MiniDataMarker
types for the same M::Yokeable
value. The fix is to be explicit on line 59 and use let provider = MiniStructProvider::<SimpleStruct> {
instead, which avoids this issue.
Another version of this error can be seen in the code in this comment (playpen), which has a similar error due to inference bailing early (there are "Broken" and "Fixed" versions in the example so that you can see the specific thing inference needs to avoid the error)
cc @estebank