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
This does basically the same as the above, it unsafely assertions that our underlying type satisfies the 'static bound. So I would expect both of these to be accepted. But somehow the borrow checker is getting in the way and says something about variance:
error: lifetime may not live long enough
--> src/lib.rs:8:5
|
7 | fn assert_static<'a>(ptr: *mut (dyn Trait + 'a)) -> *mut (dyn Trait + 'static) {
| -- lifetime `'a` defined here
8 | ptr as _
| ^^^^^^^^ returning this value requires that `'a` must outlive `'static`
|
= note: requirement occurs because of a mutable pointer to `dyn Trait`
= note: mutable pointers are invariant over their type parameter
= help: see <https://doc.rust-lang.org/nomicon/subtyping.html> for more information about variance
help: consider changing the trait object's explicit `'static` bound to the lifetime of argument `ptr`
|
7 | fn assert_static<'a>(ptr: *mut (dyn Trait + 'a)) -> *mut (dyn Trait + 'a) {
| ~~
help: alternatively, add an explicit `'static` bound to this reference
|
7 | fn assert_static<'a>(ptr: *mut (dyn Trait + 'static)) -> *mut (dyn Trait + 'static) {
| ~~~~~~~~~~~~~~~~~~~~~~~~~~
To work around this, one has to use a transmute, and that's always a bad sign. This even happens in the standard library.
Cc @rust-lang/types
The text was updated successfully, but these errors were encountered:
The following code compiles just fine:
This basically unsafely asserts that our underlying type is actually
Send
.However, strangely, the following code is rejected:
This does basically the same as the above, it unsafely assertions that our underlying type satisfies the
'static
bound. So I would expect both of these to be accepted. But somehow the borrow checker is getting in the way and says something about variance:To work around this, one has to use a
transmute
, and that's always a bad sign. This even happens in the standard library.Cc @rust-lang/types
The text was updated successfully, but these errors were encountered: