Skip to content

Commit ece0e6a

Browse files
Add raw pointer variant of #90752 with incorrect behavior
1 parent d846fe0 commit ece0e6a

File tree

1 file changed

+41
-0
lines changed

1 file changed

+41
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
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+
}

0 commit comments

Comments
 (0)