]> git.lizzy.rs Git - rust.git/blob - tests/ui/borrowck/borrowck-loan-of-static-data-issue-27616.rs
Rollup merge of #106726 - cmorin6:fix-comment-typos, r=Nilstrieb
[rust.git] / tests / ui / borrowck / borrowck-loan-of-static-data-issue-27616.rs
1 use std::mem;
2
3 fn leak<T>(mut b: Box<T>) -> &'static mut T {
4     // isn't this supposed to be safe?
5     let inner = &mut *b as *mut _;
6     mem::forget(b);
7     unsafe { &mut *inner }
8 }
9
10 fn evil(mut s: &'static mut String)
11 {
12     // create alias
13     let alias: &'static mut String = s;
14     let inner: &str = &alias;
15     // free value
16     *s = String::new(); //~ ERROR cannot assign
17     let _spray = "0wned".to_owned();
18     // ... and then use it
19     println!("{}", inner);
20 }
21
22 fn main() {
23     evil(leak(Box::new("hello".to_owned())));
24 }