Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit f3de343

Browse files
committedJan 22, 2024
Add a test for *const Trait<'a> to *const Trait<'b> casts
1 parent 3141b78 commit f3de343

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed
 
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// check-pass
2+
//
3+
// issue: <https://github.com/rust-lang/rust/issues/120217>
4+
5+
#![feature(arbitrary_self_types)]
6+
7+
trait Static<'a> {
8+
fn proof(self: *const Self, s: &'a str) -> &'static str;
9+
}
10+
11+
fn bad_cast<'a>(x: *const dyn Static<'static>) -> *const dyn Static<'a> {
12+
x as _
13+
}
14+
15+
impl Static<'static> for () {
16+
fn proof(self: *const Self, s: &'static str) -> &'static str {
17+
s
18+
}
19+
}
20+
21+
fn extend_lifetime(s: &str) -> &'static str {
22+
bad_cast(&()).proof(s)
23+
}
24+
25+
fn main() {
26+
let s = String::from("Hello World");
27+
let slice = extend_lifetime(&s);
28+
println!("Now it exists: {slice}");
29+
drop(s);
30+
println!("Now it’s gone: {slice}");
31+
}

0 commit comments

Comments
 (0)
Failed to load comments.