]> git.lizzy.rs Git - rust.git/blob - tests/compile-fail/rc_as_raw.rs
Fix merge conflicts
[rust.git] / tests / compile-fail / rc_as_raw.rs
1 // This should fail even without validation
2 // compile-flags: -Zmiri-disable-validation
3 #![feature(weak_into_raw)]
4
5 use std::rc::{Rc, Weak};
6 use std::ptr;
7
8 /// Taken from the `Weak::as_raw` doctest.
9 fn main() {
10     let strong = Rc::new(Box::new(42));
11     let weak = Rc::downgrade(&strong);
12     // Both point to the same object
13     assert!(ptr::eq(&*strong, Weak::as_raw(&weak)));
14     // The strong here keeps it alive, so we can still access the object.
15     assert_eq!(42, **unsafe { &*Weak::as_raw(&weak) });
16     
17     drop(strong);
18     // But not any more. We can do Weak::as_raw(&weak), but accessing the pointer would lead to
19     // undefined behaviour.
20     assert_eq!(42, **unsafe { &*Weak::as_raw(&weak) }); //~ ERROR dangling pointer
21 }