-
Notifications
You must be signed in to change notification settings - Fork 13k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Covariance combined with contravariance is not invariance unless observed. #31200
Comments
This is just plain silly. If you don't provide type inference hints, Of course, if you supply a type inference hint both lifetimes are forced to be the same - this also happens if you wrap |
Just to clear things up for future readers (because I had to re-read the two messages here a couple times before I understood @arielb1's diagnosis of what was "silly" here): The first line of the issue description:
is not a correct conclusion to draw from the code presented, because removing the type annotations (i.e. Update: just to complete my elaboration of the points being made above, here is a playpen illustrating what @arielb1 was saying about wrapping |
Thanks for clearing this up, I can see it now. I should've gotten rid of |
This is a curiosity from a discussion about different ways to write an Invariant type parameter marker; since we need to express it via candidates were basically (although this was only used for
Which all have different drawbacks and incidental other implications. The source of the confusion was (3) case not passing my simple test for invariance. Because of the drawbacks, it seems it would be useful to have a real invariance marker in rust's libstd. @reem pointed out some of these (1) and (2) make the type |
@bluss This is not a bug, you have |
Id<'a>
does not appear to be invariant over'a
if the whole type is inferred (run on playpen):This testcase was derived from a (misbehaving) invariance check using
fn(T) -> T
by @bluss.I'm not sure this is a soundness issue, because the regionck error appears as soon as the lifetime parameter is instantiated, even from trait impls, so only code completely generic over the type compiles.
However, specialization would allow observing
Id<'a>
through a completely generic function (likesame
) without explicit bounds, so it might be interesting to see what happens on #30652.cc @nikomatsakis @arielb1 @aturon
The text was updated successfully, but these errors were encountered: