You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Using associated type as struct field causes invariance w.r.t trait #57440
error[E0308]: mismatched types
--> src/lib.rs:31:81
|
31 | pub fn assert_covariance_s2<'new, 'old: 'new>(s2: S2<I<'old>>) -> S2<I<'new>> { s2 }
| ^^ lifetime mismatch
|
= note: expected type `S2<I<'new>>`
found type `S2<I<'old>>`
note: the lifetime 'new as defined on the function body at 31:29...
--> src/lib.rs:31:29
|
31 | pub fn assert_covariance_s2<'new, 'old: 'new>(s2: S2<I<'old>>) -> S2<I<'new>> { s2 }
| ^^^^
note: ...does not necessarily outlive the lifetime 'old as defined on the function body at 31:35
--> src/lib.rs:31:35
|
31 | pub fn assert_covariance_s2<'new, 'old: 'new>(s2: S2<I<'old>>) -> S2<I<'new>> { s2 }
| ^^^^
I would expect the variance of S1 and S2 to be the same w.r.t T. And even if this case can't be handled in general for all associated types, shouldn't the Assoc: 'static bound allow it?
I've been using S1 as a workaround for this issue, but the problem with that is the extra type parameter propagates upwards into all containing types, and it gets quite ugly and verbose.
You cannot expect to have variance for associated types in general. If T1 is a subtype of T2, there is no reason for T1::Item to be a subtype of T2::Item, as shown e.g. in this example: #21726 (comment)
Knowing that Trait::Assoc must outlive 'static, you can indeed deduce that the only lifetime that can appear in I<'a>::Assoc is 'static hence this family of associated types ought to be a constant family, but this example seems quite specific and I don’t know if we could draw a useful general rule out of this observation.
You can still write a hand-written « cast » between S2<I<'b>> and S2<I<'a>> when there is a relation between ’a and ’b. However this would incur an additional cost if you want to cast for example Vec<S<I<'a>>> to Vec<S<I<'b>>>: if you care about performances and you know for sure the relation holds (because you see « through » the associated types), you can still use transmute (well I think this is safe, right? cc @RalfJung)
You cannot expect to have variance for associated types in general. If T1 is a subtype of T2, there is no reason for T1::Item to be a subtype of T2::Item,
I understand that, and wasn't expecting that. Instead, I was hoping that internally rust could treat S2 as though it had an implicit A = T::Assoc type parameter, and given that, its variance could be handled exactly the same as S1.
You can still write a hand-written « cast » between S2<I<'b>> and S2<I<'a>> when there is a relation between ’a and ’b.
Notice that you can have trait impls like impl Trait for I<'static>, and pick a specific associated type there.
I don't want I<'static>. I'll edit the original post to remove uses of 'static for I.
In general, I don't see any reason why associated types should have any variance, ever. But I am also not an expert on this.
I don't want variance of the associated types. For my use case, they will never have a lifetime. But using the associated type as a field prevents variance w.r.t the trait type, which I do want.
Uh, I am not sure. Notice that you can have trait impls like impl Trait for I<'static>, and pick a specific associated type there.
Yep, but if there is a generic impl impl<'a> Trait for I<'a>, and the associated type must outlive 'static, the associated type value must be the same among all I<'a> types.
But actually what I was asking is, if you know that I<'a> is indeed a subtype of I<'b> (but the compiler cannot enforce it because there are associated types playing around), whether it was safe to transmuteVec<I<'a>> to Vec<I<'b>>.
For that to be useful, I'd have to be able to write the cast to be generic in T: Trait, but then I don't see how to specify the lifetime relation.
Yes that's what I was meaning. Annoying, but the only solution I guess. Basically you have to manually write casts for all your types that need sub-typing. If there are only a few, that might work.
But actually what I was asking is, if you know that I<'a> is indeed a subtype of I<'b> (but the compiler cannot enforce it because there are associated types playing around), whether it was safe to transmute Vec<I<'a>> to Vec<I<'b>>.
Well, that's pretty much the definition of subtype -- "it is safe to do the transmute". So, yes -- but now the hard part is making sure that it's really a subtype.
changed the title [-]Using associated type as struct field causes invariance[/-][+]Using associated type as struct field causes invariance w.r.t trait[/+]on Jan 17, 2019
Changing the workaround to the following avoids most of the downsides, since it lets me omit the extra type parameter in uses of the struct. It's still something I would expect the compiler to allow automatically, but I think this is good enough for me for now.
Activity
scalexm commentedon Jan 8, 2019
You cannot expect to have variance for associated types in general. If
T1is a subtype ofT2, there is no reason forT1::Itemto be a subtype ofT2::Item, as shown e.g. in this example: #21726 (comment)Knowing that
Trait::Assocmust outlive'static, you can indeed deduce that the only lifetime that can appear inI<'a>::Associs'statichence this family of associated types ought to be a constant family, but this example seems quite specific and I don’t know if we could draw a useful general rule out of this observation.You can still write a hand-written « cast » between
S2<I<'b>>andS2<I<'a>>when there is a relation between’aand’b. However this would incur an additional cost if you want to cast for exampleVec<S<I<'a>>>toVec<S<I<'b>>>: if you care about performances and you know for sure the relation holds (because you see « through » the associated types), you can still usetransmute(well I think this is safe, right? cc @RalfJung)philipc commentedon Jan 8, 2019
I understand that, and wasn't expecting that. Instead, I was hoping that internally rust could treat
S2as though it had an implicitA = T::Assoctype parameter, and given that, its variance could be handled exactly the same asS1.Do you mean like this?
For that to be useful, I'd have to be able to write the cast to be generic in
T: Trait, but then I don't see how to specify the lifetime relation.RalfJung commentedon Jan 8, 2019
Uh, I am not sure. Notice that you can have trait impls like
impl Trait for I<'static>, and pick a specific associated type there.In general, I don't see any reason why associated types should have any variance, ever. But I am also not an expert on this.
philipc commentedon Jan 8, 2019
I don't want
I<'static>. I'll edit the original post to remove uses of'staticforI.I don't want variance of the associated types. For my use case, they will never have a lifetime. But using the associated type as a field prevents variance w.r.t the trait type, which I do want.
scalexm commentedon Jan 8, 2019
@RalfJung
Yep, but if there is a generic impl
impl<'a> Trait for I<'a>, and the associated type must outlive'static, the associated type value must be the same among allI<'a>types.But actually what I was asking is, if you know that
I<'a>is indeed a subtype ofI<'b>(but the compiler cannot enforce it because there are associated types playing around), whether it was safe totransmuteVec<I<'a>>toVec<I<'b>>.@philipc
Yes that's what I was meaning. Annoying, but the only solution I guess. Basically you have to manually write casts for all your types that need sub-typing. If there are only a few, that might work.
RalfJung commentedon Jan 8, 2019
Well, that's pretty much the definition of subtype -- "it is safe to do the transmute". So, yes -- but now the hard part is making sure that it's really a subtype.
[-]Using associated type as struct field causes invariance[/-][+]Using associated type as struct field causes invariance w.r.t trait[/+]philipc commentedon Feb 22, 2019
Changing the workaround to the following avoids most of the downsides, since it lets me omit the extra type parameter in uses of the struct. It's still something I would expect the compiler to allow automatically, but I think this is good enough for me for now.
Uses a simple fix to enable arraybase to be covariant.
Uses a simple fix to enable arraybase to be covariant. (#1480)