Skip to content

Commit fa89c0f

Browse files
committed
add testcase for double-drop during Vec in-place collection
1 parent cb0e0db commit fa89c0f

File tree

1 file changed

+37
-1
lines changed

1 file changed

+37
-1
lines changed

library/alloc/tests/vec.rs

+37-1
Original file line numberDiff line numberDiff line change
@@ -1027,7 +1027,7 @@ fn test_from_iter_specialization_head_tail_drop() {
10271027
}
10281028

10291029
#[test]
1030-
fn test_from_iter_specialization_panic_drop() {
1030+
fn test_from_iter_specialization_panic_during_iteration_drops() {
10311031
let drop_count: Vec<_> = (0..=2).map(|_| Rc::new(())).collect();
10321032
let src: Vec<_> = drop_count.iter().cloned().collect();
10331033
let iter = src.into_iter();
@@ -1050,6 +1050,42 @@ fn test_from_iter_specialization_panic_drop() {
10501050
);
10511051
}
10521052

1053+
#[test]
1054+
fn test_from_iter_specialization_panic_during_drop_leaks() {
1055+
static mut DROP_COUNTER: usize = 0;
1056+
1057+
#[derive(Debug)]
1058+
enum Droppable {
1059+
DroppedTwice(Box<i32>),
1060+
PanicOnDrop,
1061+
}
1062+
1063+
impl Drop for Droppable {
1064+
fn drop(&mut self) {
1065+
match self {
1066+
Droppable::DroppedTwice(_) => {
1067+
unsafe {
1068+
DROP_COUNTER += 1;
1069+
}
1070+
println!("Dropping!")
1071+
}
1072+
Droppable::PanicOnDrop => {
1073+
if !std::thread::panicking() {
1074+
panic!();
1075+
}
1076+
}
1077+
}
1078+
}
1079+
}
1080+
1081+
let _ = std::panic::catch_unwind(AssertUnwindSafe(|| {
1082+
let v = vec![Droppable::DroppedTwice(Box::new(123)), Droppable::PanicOnDrop];
1083+
let _ = v.into_iter().take(0).collect::<Vec<_>>();
1084+
}));
1085+
1086+
assert_eq!(unsafe { DROP_COUNTER }, 1);
1087+
}
1088+
10531089
#[test]
10541090
fn test_cow_from() {
10551091
let borrowed: &[_] = &["borrowed", "(slice)"];

0 commit comments

Comments
 (0)