-
Notifications
You must be signed in to change notification settings - Fork 13.6k
Description
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:
use std::cell::UnsafeCell;
use std::rc::Rc;
type Invariant<T> = UnsafeCell<T>;
type Covariant<T> = T;
//type Wrapper<T> = Invariant<T>;
type Wrapper<T> = Covariant<T>;
trait AsSelf<'slf> {
type Ref: ?Sized;
fn as_self(&'slf self) -> &Self::Ref;
}
struct Foo<'a>(Wrapper<&'a str>);
impl<'slf, 'd: 'slf> AsSelf<'slf> for Foo<'d> {
type Ref = Foo<'slf>;
fn as_self(&'slf self) -> &Self::Ref {
self
}
}
This compiles, but if Wrapper<T>
is changed to the Invariant version, then it refuses to compile with this error:
error[E0495]: cannot infer an appropriate lifetime for lifetime parameter `'slf` due to conflicting requirements
--> src/lib.rs:21:42
|
21 | fn as_self(&'slf self) -> &Self::Ref {
| __________________________________________^
22 | | self
23 | | }
| |_____^
|
note: first, the lifetime cannot outlive the lifetime 'slf as defined on the impl at 18:6...
--> src/lib.rs:18:6
|
18 | impl<'slf, 'd: 'slf> AsSelf<'slf> for Foo<'d> {
| ^^^^
= note: ...so that the types are compatible:
expected AsSelf<'slf>
found AsSelf<'_>
note: but, the lifetime must be valid for the lifetime 'd as defined on the impl at 18:12...
--> src/lib.rs:18:12
|
18 | impl<'slf, 'd: 'slf> AsSelf<'slf> for Foo<'d> {
| ^^
= note: ...so that the expression is assignable:
expected &'slf Foo<'_>
found &'slf Foo<'d>
error: aborting due to previous 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 considers Wrapper<T>
invariant because it contains an UnsafeCell<T>
(or &mut T
) and maybe even where and link to the variance docs? This would help tremendously understanding what's happening here.
Activity
estebank commentedon Sep 5, 2019
Output with nll enabled:
Giving the return type an explicit lifetime
'd
makes it compile.Aaron1011 commentedon Dec 4, 2019
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
unsafe
at all (as this issue demonstrates). I think it would be a bad idea to have the compiler emit an error message (when nounsafe
is involved) that you can't understand without reading that Nomicon.compiler-errors commentedon Jan 7, 2023
Error message now:
... which explains variance. Closing this.
mitsuhiko commentedon Jan 8, 2023
@compiler-errors in that example the suggestion
"consider adding the following bound: `'slf: 'd`"
does not seem very helpful, particularly because'd: 'slf
. I wonder if that could be removed?