diff options
| author | The8472 <git@infinite-source.de> | 2021-03-29 04:22:34 +0200 |
|---|---|---|
| committer | The8472 <git@infinite-source.de> | 2021-03-29 04:39:23 +0200 |
| commit | fa89c0fbcfa8f4d44f153b1195ec5a305540ffc4 (patch) | |
| tree | 5ee709cce5bebac2402bdd5f90fba11bf0a6167a | |
| parent | cb0e0db641bb3a929b0608ece1dd93a181725ff4 (diff) | |
| download | rust-fa89c0fbcfa8f4d44f153b1195ec5a305540ffc4.tar.gz rust-fa89c0fbcfa8f4d44f153b1195ec5a305540ffc4.zip | |
add testcase for double-drop during Vec in-place collection
| -rw-r--r-- | library/alloc/tests/vec.rs | 38 |
1 files changed, 37 insertions, 1 deletions
diff --git a/library/alloc/tests/vec.rs b/library/alloc/tests/vec.rs index c142536cd2d..b926c697d58 100644 --- a/library/alloc/tests/vec.rs +++ b/library/alloc/tests/vec.rs @@ -1027,7 +1027,7 @@ fn test_from_iter_specialization_head_tail_drop() { } #[test] -fn test_from_iter_specialization_panic_drop() { +fn test_from_iter_specialization_panic_during_iteration_drops() { let drop_count: Vec<_> = (0..=2).map(|_| Rc::new(())).collect(); let src: Vec<_> = drop_count.iter().cloned().collect(); let iter = src.into_iter(); @@ -1051,6 +1051,42 @@ fn test_from_iter_specialization_panic_drop() { } #[test] +fn test_from_iter_specialization_panic_during_drop_leaks() { + static mut DROP_COUNTER: usize = 0; + + #[derive(Debug)] + enum Droppable { + DroppedTwice(Box<i32>), + PanicOnDrop, + } + + impl Drop for Droppable { + fn drop(&mut self) { + match self { + Droppable::DroppedTwice(_) => { + unsafe { + DROP_COUNTER += 1; + } + println!("Dropping!") + } + Droppable::PanicOnDrop => { + if !std::thread::panicking() { + panic!(); + } + } + } + } + } + + let _ = std::panic::catch_unwind(AssertUnwindSafe(|| { + let v = vec![Droppable::DroppedTwice(Box::new(123)), Droppable::PanicOnDrop]; + let _ = v.into_iter().take(0).collect::<Vec<_>>(); + })); + + assert_eq!(unsafe { DROP_COUNTER }, 1); +} + +#[test] fn test_cow_from() { let borrowed: &[_] = &["borrowed", "(slice)"]; let owned = vec!["owned", "(vec)"]; |
