Closed
Description
rustc currently compiles the following with no warnings or errors:
struct Foo;
impl Foo {
pub fn get_mut(&'static self, x: &mut u8) -> &mut u8 {
unsafe { &mut *(x as *mut _) }
}
}
However, attempting to make this function safe reveals that the elided lifetime of x
isn't the lifetime in the return type-- it's instead copying over the 'static
lifetime into the return type! Rustdoc actually documents that the signature of this method is fn get_mut(&'static self, x: &mut u8) -> &'static mut u8
.
The following code produces an error:
struct Foo;
impl Foo {
pub fn get_mut(&'static self, x: &mut u8) -> &mut u8 {
x
}
}
error[E0312]: lifetime of reference outlives lifetime of borrowed content...
--> src/main.rs:7:9
|
7 | x
| ^
|
= note: ...the reference is valid for the static lifetime...
note: ...but the borrowed content is only valid for the anonymous lifetime #1 defined on the method body at 6:5
--> src/main.rs:6:5
|
6 | / pub fn get_mut(&'static self, x: &mut u8) -> &mut u8 {
7 | | x
8 | | }
| |_____^
error: aborting due to previous error
error: Could not compile `playground`.
This seems like it could be fixed by disallowing elision in this case via a lint.
cc @aturon