-
Notifications
You must be signed in to change notification settings - Fork 12.6k
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
Lack of variance information for subtypes in error messages (E0495) #58771
Comments
Giving the return type an explicit lifetime |
From what I can tell, we never use the word 'variance' in any compiler output, or in the Rust Book. The only references appear in the Nomicon and rustc-guide. If we want to have the compiler explain these kinds of error messages in terms of variance (and I don't really know how else we would do it), we might want to consider moving the 'Subtyping and Variance' chapter into the Rust Book itself. While variance is an advanced topic, users can encounter it without using any |
Error message now:
... which explains variance. Closing this. |
@compiler-errors in that example the suggestion |
We tried to build some more complex traits and this lead to a situation where the compiler would refuse to compile the trait implementation for a certain type. The reason for this turned out to be that an embedded unsafe cell somewhere deep inside the type made it invariant.
Here is an example of this:
This compiles, but if
Wrapper<T>
is changed to the Invariant version, then it refuses to compile with this error:This is a pretty gnarly situation one finds itself in because a) the compiler says
rustc --explain E0495
but there is no extended help for E0495 and b) the actual problem here is rooted in variance and subtyping and not necessarily lifetimes as such. Would it be possible to indicate that the compiler considersWrapper<T>
invariant because it contains anUnsafeCell<T>
(or&mut T
) and maybe even where and link to the variance docs? This would help tremendously understanding what's happening here.I think this is somewhat related to #51243 and #42516
The text was updated successfully, but these errors were encountered: