diff options
| author | Marijn Schouten <mhkbst@gmail.com> | 2025-06-18 11:51:40 +0000 |
|---|---|---|
| committer | Marijn Schouten <mhkbst@gmail.com> | 2025-06-18 11:51:40 +0000 |
| commit | 5bd918fcd8be416600026a61df47a12c00788bfc (patch) | |
| tree | a31ae1539661e2a1d3724baeeddfe7db1f8b76ad | |
| parent | 1bb335244c311a07cee165c28c553c869e6f64a9 (diff) | |
| download | rust-5bd918fcd8be416600026a61df47a12c00788bfc.tar.gz rust-5bd918fcd8be416600026a61df47a12c00788bfc.zip | |
vec_deque tests: remove static mut
| -rw-r--r-- | library/alloc/src/collections/linked_list/tests.rs | 18 | ||||
| -rw-r--r-- | library/alloc/src/collections/vec_deque/tests.rs | 38 | ||||
| -rw-r--r-- | library/alloctests/testing/macros.rs | 17 | ||||
| -rw-r--r-- | library/alloctests/testing/mod.rs | 1 |
4 files changed, 31 insertions, 43 deletions
diff --git a/library/alloc/src/collections/linked_list/tests.rs b/library/alloc/src/collections/linked_list/tests.rs index 410e67d3fdb..3d6c740e80b 100644 --- a/library/alloc/src/collections/linked_list/tests.rs +++ b/library/alloc/src/collections/linked_list/tests.rs @@ -1,4 +1,3 @@ -use std::cell::Cell; use std::panic::{AssertUnwindSafe, catch_unwind}; use std::thread; @@ -6,6 +5,7 @@ use rand::RngCore; use super::*; use crate::testing::crash_test::{CrashTestDummy, Panic}; +use crate::testing::macros::struct_with_counted_drop; use crate::vec::Vec; #[test] @@ -1010,22 +1010,6 @@ fn extract_if_drop_panic_leak() { assert_eq!(d7.dropped(), 1); } -macro_rules! struct_with_counted_drop { - ($struct_name:ident$(($elt_ty:ty))?, $drop_counter:ident $(=> $drop_stmt:expr)?) => { - thread_local! {static $drop_counter: Cell<u32> = Cell::new(0);} - - struct $struct_name$(($elt_ty))?; - - impl Drop for $struct_name { - fn drop(&mut self) { - $drop_counter.set($drop_counter.get() + 1); - - $($drop_stmt(self))? - } - } - }; -} - #[test] #[cfg_attr(not(panic = "unwind"), ignore = "test requires unwinding support")] fn extract_if_pred_panic_leak() { diff --git a/library/alloc/src/collections/vec_deque/tests.rs b/library/alloc/src/collections/vec_deque/tests.rs index c90679f1797..ad76cb14deb 100644 --- a/library/alloc/src/collections/vec_deque/tests.rs +++ b/library/alloc/src/collections/vec_deque/tests.rs @@ -1,9 +1,7 @@ -// FIXME(static_mut_refs): Do not allow `static_mut_refs` lint -#![allow(static_mut_refs)] - use core::iter::TrustedLen; use super::*; +use crate::testing::macros::struct_with_counted_drop; #[bench] fn bench_push_back_100(b: &mut test::Bencher) { @@ -1086,36 +1084,24 @@ fn test_clone_from() { #[test] fn test_vec_deque_truncate_drop() { - static mut DROPS: u32 = 0; - #[derive(Clone)] - struct Elem(#[allow(dead_code)] i32); - impl Drop for Elem { - fn drop(&mut self) { - unsafe { - DROPS += 1; - } - } - } + struct_with_counted_drop!(Elem, DROPS); - let v = vec![Elem(1), Elem(2), Elem(3), Elem(4), Elem(5)]; - for push_front in 0..=v.len() { - let v = v.clone(); - let mut tester = VecDeque::with_capacity(5); - for (index, elem) in v.into_iter().enumerate() { + const LEN: usize = 5; + for push_front in 0..=LEN { + let mut tester = VecDeque::with_capacity(LEN); + for index in 0..LEN { if index < push_front { - tester.push_front(elem); + tester.push_front(Elem); } else { - tester.push_back(elem); + tester.push_back(Elem); } } - assert_eq!(unsafe { DROPS }, 0); + assert_eq!(DROPS.get(), 0); tester.truncate(3); - assert_eq!(unsafe { DROPS }, 2); + assert_eq!(DROPS.get(), 2); tester.truncate(0); - assert_eq!(unsafe { DROPS }, 5); - unsafe { - DROPS = 0; - } + assert_eq!(DROPS.get(), 5); + DROPS.set(0); } } diff --git a/library/alloctests/testing/macros.rs b/library/alloctests/testing/macros.rs new file mode 100644 index 00000000000..37cf59bc169 --- /dev/null +++ b/library/alloctests/testing/macros.rs @@ -0,0 +1,17 @@ +macro_rules! struct_with_counted_drop { + ($struct_name:ident$(($elt_ty:ty))?, $drop_counter:ident $(=> $drop_stmt:expr)?) => { + thread_local! {static $drop_counter: ::core::cell::Cell<u32> = ::core::cell::Cell::new(0);} + + struct $struct_name$(($elt_ty))?; + + impl ::std::ops::Drop for $struct_name { + fn drop(&mut self) { + $drop_counter.set($drop_counter.get() + 1); + + $($drop_stmt(self))? + } + } + }; +} + +pub(crate) use struct_with_counted_drop; diff --git a/library/alloctests/testing/mod.rs b/library/alloctests/testing/mod.rs index c8457daf93e..66a4f6682b9 100644 --- a/library/alloctests/testing/mod.rs +++ b/library/alloctests/testing/mod.rs @@ -1,3 +1,4 @@ pub(crate) mod crash_test; +pub(crate) mod macros; pub(crate) mod ord_chaos; pub(crate) mod rng; |
