]> git.lizzy.rs Git - rust.git/blob - src/test/ui/traits/negative-impls/pin-unsound-issue-66544-clone.rs
Auto merge of #87284 - Aaron1011:remove-paren-special, r=petrochenkov
[rust.git] / src / test / ui / traits / negative-impls / pin-unsound-issue-66544-clone.rs
1 use std::cell::Cell;
2 use std::marker::PhantomPinned;
3 use std::pin::Pin;
4
5 struct MyType<'a>(Cell<Option<&'a mut MyType<'a>>>, PhantomPinned);
6
7 impl<'a> Clone for &'a mut MyType<'a> {
8     //~^ ERROR E0751
9     fn clone(&self) -> &'a mut MyType<'a> {
10         self.0.take().unwrap()
11     }
12 }
13
14 fn main() {
15     let mut unpinned = MyType(Cell::new(None), PhantomPinned);
16     let bad_addr = &unpinned as *const MyType<'_> as usize;
17     let mut p = Box::pin(MyType(Cell::new(Some(&mut unpinned)), PhantomPinned));
18
19     // p_mut1 is okay: it does not point to the bad_addr
20     let p_mut1: Pin<&mut MyType<'_>> = p.as_mut();
21     assert_ne!(bad_addr, &*p_mut1 as *const _ as usize);
22
23     // but p_mut2 does point to bad_addr! this is unsound
24     let p_mut2: Pin<&mut MyType<'_>> = p_mut1.clone();
25     assert_eq!(bad_addr, &*p_mut2 as *const _ as usize);
26 }