-
Notifications
You must be signed in to change notification settings - Fork 13.6k
Description
Rust fails to properly infer types when there is a type parameter that implements are trait that contains associated types that have relationships between each other.
Suppose there are two structs Wrapper1
and Wrapper2
that each hold a type I1
and I2
respectively.
We want to express the relationship that I2 : Deref<Target = I1>
So there's a 2 ways to do this:
-
define
Wrapper1
andWrapper2
with 2 types,T1
andT2
whereT2 : Deref<Target = T1>
e.g.
https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=5c8869c7499b8cf628695b8b8a66ad40 -
define a type
WT
whereWT
has typesInner1
andInner2
such thatInner2 : Deref<Target = Self::Inner1>
.
e.g.
https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=892489013b67f5f8ef5de1e60dcdb1ef
However, in the second case, the compiler cannot infer types correctly for code to use the properties of the associated types Inner2 : Deref<Target = Self::Inner1>
.
In the above code examples, foo2
does not compile in 2.
It looks like the compiler is unable to infer the types correctly and apply the Deref, since for foo1
, where we explicitly indicate the type, it compiles properly.
This ends up causing the Deref traits to not be always inferred properly by other users too.