]> git.lizzy.rs Git - rust.git/blob - src/tools/miri/tests/fail/rc_as_ptr.rs
Rollup merge of #102187 - b-naber:inline-const-source-info, r=eholk
[rust.git] / src / tools / miri / tests / fail / rc_as_ptr.rs
1 // This should fail even without validation
2 //@compile-flags: -Zmiri-disable-validation
3
4 use std::ptr;
5 use std::rc::{Rc, Weak};
6
7 /// Taken from the `Weak::as_ptr` doctest.
8 fn main() {
9     let strong = Rc::new(Box::new(42));
10     let weak = Rc::downgrade(&strong);
11     // Both point to the same object
12     assert!(ptr::eq(&*strong, Weak::as_ptr(&weak)));
13     // The strong here keeps it alive, so we can still access the object.
14     assert_eq!(42, **unsafe { &*Weak::as_ptr(&weak) });
15
16     drop(strong);
17     // But not any more. We can do Weak::as_raw(&weak), but accessing the pointer would lead to
18     // undefined behaviour.
19     assert_eq!(42, **unsafe { &*Weak::as_ptr(&weak) }); //~ ERROR: dereferenced after this allocation got freed
20 }