1
Fork 0

Adapt inplace collection leak test to check for no leaks

This commit is contained in:
Giacomo Stevanato 2022-09-10 11:27:21 +02:00
parent fa61678a7d
commit f81b07e947

View file

@ -1110,11 +1110,13 @@ fn test_from_iter_specialization_panic_during_iteration_drops() {
} }
#[test] #[test]
fn test_from_iter_specialization_panic_during_drop_leaks() { fn test_from_iter_specialization_panic_during_drop_doesnt_leak() {
static mut DROP_COUNTER: usize = 0; static mut DROP_COUNTER_SHOULD_BE_DROPPED: usize = 0;
static mut DROP_COUNTER_DROPPED_TWICE: usize = 0;
#[derive(Debug)] #[derive(Debug)]
enum Droppable { enum Droppable {
ShouldBeDropped,
DroppedTwice(Box<i32>), DroppedTwice(Box<i32>),
PanicOnDrop, PanicOnDrop,
} }
@ -1122,11 +1124,17 @@ fn test_from_iter_specialization_panic_during_drop_leaks() {
impl Drop for Droppable { impl Drop for Droppable {
fn drop(&mut self) { fn drop(&mut self) {
match self { match self {
Droppable::ShouldBeDropped => {
unsafe {
DROP_COUNTER_SHOULD_BE_DROPPED += 1;
}
println!("Dropping ShouldBeDropped!")
}
Droppable::DroppedTwice(_) => { Droppable::DroppedTwice(_) => {
unsafe { unsafe {
DROP_COUNTER += 1; DROP_COUNTER_DROPPED_TWICE += 1;
} }
println!("Dropping!") println!("Dropping DroppedTwice!")
} }
Droppable::PanicOnDrop => { Droppable::PanicOnDrop => {
if !std::thread::panicking() { if !std::thread::panicking() {
@ -1137,21 +1145,17 @@ fn test_from_iter_specialization_panic_during_drop_leaks() {
} }
} }
let mut to_free: *mut Droppable = core::ptr::null_mut();
let mut cap = 0;
let _ = std::panic::catch_unwind(AssertUnwindSafe(|| { let _ = std::panic::catch_unwind(AssertUnwindSafe(|| {
let mut v = vec![Droppable::DroppedTwice(Box::new(123)), Droppable::PanicOnDrop]; let v = vec![
to_free = v.as_mut_ptr(); Droppable::ShouldBeDropped,
cap = v.capacity(); Droppable::DroppedTwice(Box::new(123)),
let _ = v.into_iter().take(0).collect::<Vec<_>>(); Droppable::PanicOnDrop,
];
let _ = v.into_iter().take(1).collect::<Vec<_>>();
})); }));
assert_eq!(unsafe { DROP_COUNTER }, 1); assert_eq!(unsafe { DROP_COUNTER_SHOULD_BE_DROPPED }, 1);
// clean up the leak to keep miri happy assert_eq!(unsafe { DROP_COUNTER_DROPPED_TWICE }, 1);
unsafe {
drop(Vec::from_raw_parts(to_free, 0, cap));
}
} }
// regression test for issue #85322. Peekable previously implemented InPlaceIterable, // regression test for issue #85322. Peekable previously implemented InPlaceIterable,