2024-06-04 08:21:09 -05:00
|
|
|
|
//@ check-fail
|
2024-01-22 13:08:19 -06:00
|
|
|
|
//
|
|
|
|
|
// issue: <https://github.com/rust-lang/rust/issues/120217>
|
|
|
|
|
|
2024-03-30 14:25:22 -05:00
|
|
|
|
#![feature(arbitrary_self_types_pointers)]
|
2024-01-22 13:08:19 -06:00
|
|
|
|
|
|
|
|
|
trait Static<'a> {
|
|
|
|
|
fn proof(self: *const Self, s: &'a str) -> &'static str;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn bad_cast<'a>(x: *const dyn Static<'static>) -> *const dyn Static<'a> {
|
2024-02-12 15:24:48 -06:00
|
|
|
|
x as _ //~ error: lifetime may not live long enough
|
2024-01-22 13:08:19 -06:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl Static<'static> for () {
|
|
|
|
|
fn proof(self: *const Self, s: &'static str) -> &'static str {
|
|
|
|
|
s
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn extend_lifetime(s: &str) -> &'static str {
|
|
|
|
|
bad_cast(&()).proof(s)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn main() {
|
|
|
|
|
let s = String::from("Hello World");
|
|
|
|
|
let slice = extend_lifetime(&s);
|
|
|
|
|
println!("Now it exists: {slice}");
|
|
|
|
|
drop(s);
|
|
|
|
|
println!("Now it’s gone: {slice}");
|
|
|
|
|
}
|