Skip to content

vec_deque/fmt/vec tests: remove static mut #142668

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Jun 20, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 1 addition & 17 deletions library/alloc/src/collections/linked_list/tests.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
use std::cell::Cell;
use std::panic::{AssertUnwindSafe, catch_unwind};
use std::thread;

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]
Expand Down Expand Up @@ -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() {
Expand Down
38 changes: 12 additions & 26 deletions library/alloc/src/collections/vec_deque/tests.rs
Original file line number Diff line number Diff line change
@@ -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) {
Expand Down Expand Up @@ -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);
}
}

Expand Down
37 changes: 37 additions & 0 deletions library/alloctests/testing/macros.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
macro_rules! struct_with_counted_drop {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not necessarily something to do here, but could vecdeque just use these too?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure what you mean here.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, nevermind, I was thinking #142520 was for dequeue rather than linked list

($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);}

#[derive(Clone, Debug, PartialEq)]
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))?
}
}
};
($struct_name:ident $(( $( $elt_ty:ty ),+ ))?, $drop_counter:ident[ $drop_key:expr,$key_ty:ty ] $( => $drop_stmt:expr )? ) => {
thread_local! {
static $drop_counter: ::core::cell::RefCell<::std::collections::HashMap<$key_ty, u32>> =
::core::cell::RefCell::new(::std::collections::HashMap::new());
}

#[derive(Clone, Debug, PartialEq)]
struct $struct_name $(( $( $elt_ty ),+ ))?;

impl ::std::ops::Drop for $struct_name {
fn drop(&mut self) {
$drop_counter.with_borrow_mut(|counter| {
*counter.entry($drop_key(self)).or_default() += 1;
});

$($drop_stmt(self))?
}
}
};
}

pub(crate) use struct_with_counted_drop;
1 change: 1 addition & 0 deletions library/alloctests/testing/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
pub(crate) mod crash_test;
pub(crate) mod macros;
pub(crate) mod ord_chaos;
pub(crate) mod rng;
46 changes: 26 additions & 20 deletions library/alloctests/tests/fmt.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
#![deny(warnings)]
// FIXME(static_mut_refs): Do not allow `static_mut_refs` lint
#![allow(static_mut_refs)]
#![allow(unnecessary_transmutes)]

use std::cell::RefCell;
Expand Down Expand Up @@ -285,19 +283,32 @@ fn test_format_args() {
t!(s, "args were: hello world");
}

macro_rules! counter_fn {
($name:ident) => {
fn $name() -> u32 {
thread_local! {static COUNTER: ::core::cell::Cell<u32> = ::core::cell::Cell::new(0);}

COUNTER.set(COUNTER.get() + 1);
COUNTER.get()
}
};
}

#[test]
fn test_order() {
// Make sure format!() arguments are always evaluated in a left-to-right
// ordering
fn foo() -> isize {
static mut FOO: isize = 0;
unsafe {
FOO += 1;
FOO
}
}
// Make sure format!() arguments are always evaluated in a left-to-right ordering
counter_fn!(count);

assert_eq!(
format!("{} {} {a} {b} {} {c}", foo(), foo(), foo(), a = foo(), b = foo(), c = foo()),
format!(
"{} {} {a} {b} {} {c}",
count(),
count(),
count(),
a = count(),
b = count(),
c = count()
),
"1 2 4 5 3 6".to_string()
);
}
Expand All @@ -306,14 +317,9 @@ fn test_order() {
fn test_once() {
// Make sure each argument are evaluated only once even though it may be
// formatted multiple times
fn foo() -> isize {
static mut FOO: isize = 0;
unsafe {
FOO += 1;
FOO
}
}
assert_eq!(format!("{0} {0} {0} {a} {a} {a}", foo(), a = foo()), "1 1 1 2 2 2".to_string());
counter_fn!(count);

assert_eq!(format!("{0} {0} {0} {a} {a} {a}", count(), a = count()), "1 1 1 2 2 2".to_string());
}

#[test]
Expand Down
2 changes: 2 additions & 0 deletions library/alloctests/tests/testing/mod.rs
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
pub mod crash_test;
#[path = "../../testing/macros.rs"]
pub mod macros;
Loading
Loading