From f81b07e9470728a311a5d816616762e37b00f29f Mon Sep 17 00:00:00 2001 From: Giacomo Stevanato Date: Sat, 10 Sep 2022 11:27:21 +0200 Subject: [PATCH] Adapt inplace collection leak test to check for no leaks --- library/alloc/tests/vec.rs | 36 ++++++++++++++++++++---------------- 1 file changed, 20 insertions(+), 16 deletions(-) diff --git a/library/alloc/tests/vec.rs b/library/alloc/tests/vec.rs index d94da8f5f5a..ebf5cbd0e60 100644 --- a/library/alloc/tests/vec.rs +++ b/library/alloc/tests/vec.rs @@ -1110,11 +1110,13 @@ fn test_from_iter_specialization_panic_during_iteration_drops() { } #[test] -fn test_from_iter_specialization_panic_during_drop_leaks() { - static mut DROP_COUNTER: usize = 0; +fn test_from_iter_specialization_panic_during_drop_doesnt_leak() { + static mut DROP_COUNTER_SHOULD_BE_DROPPED: usize = 0; + static mut DROP_COUNTER_DROPPED_TWICE: usize = 0; #[derive(Debug)] enum Droppable { + ShouldBeDropped, DroppedTwice(Box), PanicOnDrop, } @@ -1122,11 +1124,17 @@ fn test_from_iter_specialization_panic_during_drop_leaks() { impl Drop for Droppable { fn drop(&mut self) { match self { + Droppable::ShouldBeDropped => { + unsafe { + DROP_COUNTER_SHOULD_BE_DROPPED += 1; + } + println!("Dropping ShouldBeDropped!") + } Droppable::DroppedTwice(_) => { unsafe { - DROP_COUNTER += 1; + DROP_COUNTER_DROPPED_TWICE += 1; } - println!("Dropping!") + println!("Dropping DroppedTwice!") } Droppable::PanicOnDrop => { 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 mut v = vec![Droppable::DroppedTwice(Box::new(123)), Droppable::PanicOnDrop]; - to_free = v.as_mut_ptr(); - cap = v.capacity(); - let _ = v.into_iter().take(0).collect::>(); + let v = vec![ + Droppable::ShouldBeDropped, + Droppable::DroppedTwice(Box::new(123)), + Droppable::PanicOnDrop, + ]; + let _ = v.into_iter().take(1).collect::>(); })); - assert_eq!(unsafe { DROP_COUNTER }, 1); - // clean up the leak to keep miri happy - unsafe { - drop(Vec::from_raw_parts(to_free, 0, cap)); - } + assert_eq!(unsafe { DROP_COUNTER_SHOULD_BE_DROPPED }, 1); + assert_eq!(unsafe { DROP_COUNTER_DROPPED_TWICE }, 1); } // regression test for issue #85322. Peekable previously implemented InPlaceIterable,