Closed
Description
I tried this code:
struct Foo<'a>(&'a ());
fn foo(v: *const Foo<'_>) -> *const Foo<'static> {
v as *const Foo<'static>
}
I expected to see this happen: it compiles, since raw pointer casts should be allowed even for completely different types.
Instead, this happened: it fails with:
error: lifetime may not live long enough
--> src/lib.rs:4:5
|
3 | fn foo(v: *const Foo<'_>) -> *const Foo<'static> {
| - has type `*const Foo<'1>`
4 | v as *const Foo<'static>
| ^^^^^^^^^^^^^^^^^^^^^^^^ type annotation requires that `'1` must outlive `'static`
It looks like for some reason the compiler creates a lifetime obligation here.
Strangely, it works with cast()
:
fn foo(v: *const Foo<'_>) -> *const Foo<'static> {
v.cast::<Foo<'static>>()
}