-
Notifications
You must be signed in to change notification settings - Fork 13.6k
Closed
Labels
A-coercionsArea: implicit and explicit `expr as Type` coercionsArea: implicit and explicit `expr as Type` coercionsA-raw-pointersArea: raw pointers, MaybeUninit, NonNullArea: raw pointers, MaybeUninit, NonNullC-bugCategory: This is a bug.Category: This is a bug.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.Relevant to the compiler team, which will review and decide on the PR/issue.
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>>()
}
Metadata
Metadata
Assignees
Labels
A-coercionsArea: implicit and explicit `expr as Type` coercionsArea: implicit and explicit `expr as Type` coercionsA-raw-pointersArea: raw pointers, MaybeUninit, NonNullArea: raw pointers, MaybeUninit, NonNullC-bugCategory: This is a bug.Category: This is a bug.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.Relevant to the compiler team, which will review and decide on the PR/issue.