Skip to content

Commit 2d582ad

Browse files
authored
Rollup merge of rust-lang#142668 - hkBst:less-static-mut, r=tgross35
vec_deque/fmt/vec tests: remove static mut More rust-lang#125035. r? `@tgross35`
2 parents b8e28b7 + 9c22768 commit 2d582ad

File tree

9 files changed

+139
-271
lines changed

9 files changed

+139
-271
lines changed

library/alloc/src/collections/linked_list/tests.rs

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
use std::cell::Cell;
21
use std::panic::{AssertUnwindSafe, catch_unwind};
32
use std::thread;
43

54
use rand::RngCore;
65

76
use super::*;
87
use crate::testing::crash_test::{CrashTestDummy, Panic};
8+
use crate::testing::macros::struct_with_counted_drop;
99
use crate::vec::Vec;
1010

1111
#[test]
@@ -1010,22 +1010,6 @@ fn extract_if_drop_panic_leak() {
10101010
assert_eq!(d7.dropped(), 1);
10111011
}
10121012

1013-
macro_rules! struct_with_counted_drop {
1014-
($struct_name:ident$(($elt_ty:ty))?, $drop_counter:ident $(=> $drop_stmt:expr)?) => {
1015-
thread_local! {static $drop_counter: Cell<u32> = Cell::new(0);}
1016-
1017-
struct $struct_name$(($elt_ty))?;
1018-
1019-
impl Drop for $struct_name {
1020-
fn drop(&mut self) {
1021-
$drop_counter.set($drop_counter.get() + 1);
1022-
1023-
$($drop_stmt(self))?
1024-
}
1025-
}
1026-
};
1027-
}
1028-
10291013
#[test]
10301014
#[cfg_attr(not(panic = "unwind"), ignore = "test requires unwinding support")]
10311015
fn extract_if_pred_panic_leak() {

library/alloc/src/collections/vec_deque/tests.rs

Lines changed: 12 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
1-
// FIXME(static_mut_refs): Do not allow `static_mut_refs` lint
2-
#![allow(static_mut_refs)]
3-
41
use core::iter::TrustedLen;
52

63
use super::*;
4+
use crate::testing::macros::struct_with_counted_drop;
75

86
#[bench]
97
fn bench_push_back_100(b: &mut test::Bencher) {
@@ -1086,36 +1084,24 @@ fn test_clone_from() {
10861084

10871085
#[test]
10881086
fn test_vec_deque_truncate_drop() {
1089-
static mut DROPS: u32 = 0;
1090-
#[derive(Clone)]
1091-
struct Elem(#[allow(dead_code)] i32);
1092-
impl Drop for Elem {
1093-
fn drop(&mut self) {
1094-
unsafe {
1095-
DROPS += 1;
1096-
}
1097-
}
1098-
}
1087+
struct_with_counted_drop!(Elem, DROPS);
10991088

1100-
let v = vec![Elem(1), Elem(2), Elem(3), Elem(4), Elem(5)];
1101-
for push_front in 0..=v.len() {
1102-
let v = v.clone();
1103-
let mut tester = VecDeque::with_capacity(5);
1104-
for (index, elem) in v.into_iter().enumerate() {
1089+
const LEN: usize = 5;
1090+
for push_front in 0..=LEN {
1091+
let mut tester = VecDeque::with_capacity(LEN);
1092+
for index in 0..LEN {
11051093
if index < push_front {
1106-
tester.push_front(elem);
1094+
tester.push_front(Elem);
11071095
} else {
1108-
tester.push_back(elem);
1096+
tester.push_back(Elem);
11091097
}
11101098
}
1111-
assert_eq!(unsafe { DROPS }, 0);
1099+
assert_eq!(DROPS.get(), 0);
11121100
tester.truncate(3);
1113-
assert_eq!(unsafe { DROPS }, 2);
1101+
assert_eq!(DROPS.get(), 2);
11141102
tester.truncate(0);
1115-
assert_eq!(unsafe { DROPS }, 5);
1116-
unsafe {
1117-
DROPS = 0;
1118-
}
1103+
assert_eq!(DROPS.get(), 5);
1104+
DROPS.set(0);
11191105
}
11201106
}
11211107

library/alloctests/testing/macros.rs

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
macro_rules! struct_with_counted_drop {
2+
($struct_name:ident $(( $( $elt_ty:ty ),+ ))?, $drop_counter:ident $( => $drop_stmt:expr )? ) => {
3+
thread_local! {static $drop_counter: ::core::cell::Cell<u32> = ::core::cell::Cell::new(0);}
4+
5+
#[derive(Clone, Debug, PartialEq)]
6+
struct $struct_name $(( $( $elt_ty ),+ ))?;
7+
8+
impl ::std::ops::Drop for $struct_name {
9+
fn drop(&mut self) {
10+
$drop_counter.set($drop_counter.get() + 1);
11+
12+
$($drop_stmt(self))?
13+
}
14+
}
15+
};
16+
($struct_name:ident $(( $( $elt_ty:ty ),+ ))?, $drop_counter:ident[ $drop_key:expr,$key_ty:ty ] $( => $drop_stmt:expr )? ) => {
17+
thread_local! {
18+
static $drop_counter: ::core::cell::RefCell<::std::collections::HashMap<$key_ty, u32>> =
19+
::core::cell::RefCell::new(::std::collections::HashMap::new());
20+
}
21+
22+
#[derive(Clone, Debug, PartialEq)]
23+
struct $struct_name $(( $( $elt_ty ),+ ))?;
24+
25+
impl ::std::ops::Drop for $struct_name {
26+
fn drop(&mut self) {
27+
$drop_counter.with_borrow_mut(|counter| {
28+
*counter.entry($drop_key(self)).or_default() += 1;
29+
});
30+
31+
$($drop_stmt(self))?
32+
}
33+
}
34+
};
35+
}
36+
37+
pub(crate) use struct_with_counted_drop;

library/alloctests/testing/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
pub(crate) mod crash_test;
2+
pub(crate) mod macros;
23
pub(crate) mod ord_chaos;
34
pub(crate) mod rng;

library/alloctests/tests/fmt.rs

Lines changed: 26 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
11
#![deny(warnings)]
2-
// FIXME(static_mut_refs): Do not allow `static_mut_refs` lint
3-
#![allow(static_mut_refs)]
42
#![allow(unnecessary_transmutes)]
53

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

286+
macro_rules! counter_fn {
287+
($name:ident) => {
288+
fn $name() -> u32 {
289+
thread_local! {static COUNTER: ::core::cell::Cell<u32> = ::core::cell::Cell::new(0);}
290+
291+
COUNTER.set(COUNTER.get() + 1);
292+
COUNTER.get()
293+
}
294+
};
295+
}
296+
288297
#[test]
289298
fn test_order() {
290-
// Make sure format!() arguments are always evaluated in a left-to-right
291-
// ordering
292-
fn foo() -> isize {
293-
static mut FOO: isize = 0;
294-
unsafe {
295-
FOO += 1;
296-
FOO
297-
}
298-
}
299+
// Make sure format!() arguments are always evaluated in a left-to-right ordering
300+
counter_fn!(count);
301+
299302
assert_eq!(
300-
format!("{} {} {a} {b} {} {c}", foo(), foo(), foo(), a = foo(), b = foo(), c = foo()),
303+
format!(
304+
"{} {} {a} {b} {} {c}",
305+
count(),
306+
count(),
307+
count(),
308+
a = count(),
309+
b = count(),
310+
c = count()
311+
),
301312
"1 2 4 5 3 6".to_string()
302313
);
303314
}
@@ -306,14 +317,9 @@ fn test_order() {
306317
fn test_once() {
307318
// Make sure each argument are evaluated only once even though it may be
308319
// formatted multiple times
309-
fn foo() -> isize {
310-
static mut FOO: isize = 0;
311-
unsafe {
312-
FOO += 1;
313-
FOO
314-
}
315-
}
316-
assert_eq!(format!("{0} {0} {0} {a} {a} {a}", foo(), a = foo()), "1 1 1 2 2 2".to_string());
320+
counter_fn!(count);
321+
322+
assert_eq!(format!("{0} {0} {0} {a} {a} {a}", count(), a = count()), "1 1 1 2 2 2".to_string());
317323
}
318324

319325
#[test]
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
11
#[path = "../../testing/crash_test.rs"]
22
pub mod crash_test;
3+
#[path = "../../testing/macros.rs"]
4+
pub mod macros;

0 commit comments

Comments
 (0)