]> git.lizzy.rs Git - rust.git/blob - src/test/ui/drop/issue-90752-raw-ptr-shenanigans.rs
Rollup merge of #92959 - asquared31415:test-non-fn-help, r=estebank
[rust.git] / src / test / ui / drop / issue-90752-raw-ptr-shenanigans.rs
1 // run-pass
2
3 use std::cell::RefCell;
4
5 struct S<'a>(i32, &'a RefCell<Vec<i32>>);
6
7 impl<'a> Drop for S<'a> {
8     fn drop(&mut self) {
9         self.1.borrow_mut().push(self.0);
10     }
11 }
12
13 fn test(drops: &RefCell<Vec<i32>>) {
14     let mut foo = None;
15     let pfoo: *mut _ = &mut foo;
16
17     match foo {
18         None => (),
19         _ => return,
20     }
21
22     // Both S(0) and S(1) should be dropped, but aren't.
23     unsafe { *pfoo = Some((S(0, drops), S(1, drops))); }
24
25     match foo {
26         Some((_x, _)) => {}
27         _ => {}
28     }
29 }
30
31 fn main() {
32     let drops = RefCell::new(Vec::new());
33     test(&drops);
34
35     // Ideally, we want this...
36     //assert_eq!(*drops.borrow(), &[0, 1]);
37
38     // But the delayed access through the raw pointer confuses drop elaboration,
39     // causing S(1) to be leaked.
40     assert_eq!(*drops.borrow(), &[0]);
41 }