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
fnnew_vec<T>() -> Vec<&'static T>{Vec::new()}fnfoo<'a,T>(){// Errors, as it cannot coerce Vec<T> to Vec<U> where U <: T.let _:Vec<&'a T> = new_vec();}
This can lead to the need to make functions generic over lifetimes in their return type, but that doesn't work so well when referencing a static (which cannot be generic over non-'static lifetimes like function can).
To show that this is not specific to Vec (or other unsafe-using abstractions):
structFoo<T>(T);fnnew_foo<T>() -> Foo<&'static T>{fail!()}fnfoo<'a,T>(){// Same error, Foo<T> is also invariant w.r.t T.let _:Foo<&'a T> = new_foo();}
Without the Foo newtype, it works as expected:
fnnew_foo<T:'static>() -> &'static T{fail!()}fnfoo<'a,T:'static>(){let _:&'a T = new_foo();}
It also works when the variance comes from a lifetime parameter:
I now see that this is pretty much #3598, which has been moved to rust-lang/rfcs#281.
I needed an issue to use in comments for changes which are only required because of this lack of variance, one of them requiring unsafe code to work around.
There are two FIXMEs mentioning #3598, not sure if I should use that or this issue.
Consider this:
This can lead to the need to make functions generic over lifetimes in their return type, but that doesn't work so well when referencing a
static
(which cannot be generic over non-'static
lifetimes like function can).To show that this is not specific to
Vec
(or other unsafe-using abstractions):Without the
Foo
newtype, it works as expected:It also works when the variance comes from a lifetime parameter:
The now-closed (awaiting a rewrite) rust-lang/rfcs#233 RFC PR would fix this.
cc @nikomatsakis
The text was updated successfully, but these errors were encountered: