Skip to content

Commit c0297cc

Browse files
committed
alloc: less static mut
1 parent 015c777 commit c0297cc

File tree

1 file changed

+30
-63
lines changed
  • library/alloc/src/collections/linked_list

1 file changed

+30
-63
lines changed

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

Lines changed: 30 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// FIXME(static_mut_refs): Do not allow `static_mut_refs` lint
22
#![allow(static_mut_refs)]
33

4+
use std::cell::Cell;
45
use std::panic::{AssertUnwindSafe, catch_unwind};
56
use std::thread;
67

@@ -1027,21 +1028,26 @@ fn extract_if_drop_panic_leak() {
10271028
assert_eq!(d7.dropped(), 1);
10281029
}
10291030

1030-
#[test]
1031-
#[cfg_attr(not(panic = "unwind"), ignore = "test requires unwinding support")]
1032-
fn extract_if_pred_panic_leak() {
1033-
static mut DROPS: i32 = 0;
1031+
macro_rules! struct_with_counted_drop {
1032+
($struct_name:ident$(($elt_ty:ty))?, $drop_counter:ident $(=> $drop_stmt:expr)?) => {
1033+
thread_local! {static $drop_counter: Cell<i32> = Cell::new(0);}
1034+
1035+
struct $struct_name$(($elt_ty))?;
10341036

1035-
#[derive(Debug)]
1036-
struct D(u32);
1037+
impl Drop for $struct_name {
1038+
fn drop(&mut self) {
1039+
$drop_counter.set($drop_counter.get() + 1);
10371040

1038-
impl Drop for D {
1039-
fn drop(&mut self) {
1040-
unsafe {
1041-
DROPS += 1;
1041+
$($drop_stmt(self))?
10421042
}
10431043
}
1044-
}
1044+
};
1045+
}
1046+
1047+
#[test]
1048+
#[cfg_attr(not(panic = "unwind"), ignore = "test requires unwinding support")]
1049+
fn extract_if_pred_panic_leak() {
1050+
struct_with_counted_drop!(D(u32), DROPS);
10451051

10461052
let mut q = LinkedList::new();
10471053
q.push_back(D(3));
@@ -1053,26 +1059,17 @@ fn extract_if_pred_panic_leak() {
10531059
q.push_front(D(1));
10541060
q.push_front(D(0));
10551061

1056-
catch_unwind(AssertUnwindSafe(|| {
1062+
_ = catch_unwind(AssertUnwindSafe(|| {
10571063
q.extract_if(|item| if item.0 >= 2 { panic!() } else { true }).for_each(drop)
1058-
}))
1059-
.ok();
1064+
}));
10601065

1061-
assert_eq!(unsafe { DROPS }, 2); // 0 and 1
1066+
assert_eq!(DROPS.get(), 2); // 0 and 1
10621067
assert_eq!(q.len(), 6);
10631068
}
10641069

10651070
#[test]
10661071
fn test_drop() {
1067-
static mut DROPS: i32 = 0;
1068-
struct Elem;
1069-
impl Drop for Elem {
1070-
fn drop(&mut self) {
1071-
unsafe {
1072-
DROPS += 1;
1073-
}
1074-
}
1075-
}
1072+
struct_with_counted_drop!(Elem, DROPS);
10761073

10771074
let mut ring = LinkedList::new();
10781075
ring.push_back(Elem);
@@ -1081,20 +1078,12 @@ fn test_drop() {
10811078
ring.push_front(Elem);
10821079
drop(ring);
10831080

1084-
assert_eq!(unsafe { DROPS }, 4);
1081+
assert_eq!(DROPS.get(), 4);
10851082
}
10861083

10871084
#[test]
10881085
fn test_drop_with_pop() {
1089-
static mut DROPS: i32 = 0;
1090-
struct Elem;
1091-
impl Drop for Elem {
1092-
fn drop(&mut self) {
1093-
unsafe {
1094-
DROPS += 1;
1095-
}
1096-
}
1097-
}
1086+
struct_with_counted_drop!(Elem, DROPS);
10981087

10991088
let mut ring = LinkedList::new();
11001089
ring.push_back(Elem);
@@ -1104,54 +1093,32 @@ fn test_drop_with_pop() {
11041093

11051094
drop(ring.pop_back());
11061095
drop(ring.pop_front());
1107-
assert_eq!(unsafe { DROPS }, 2);
1096+
assert_eq!(DROPS.get(), 2);
11081097

11091098
drop(ring);
1110-
assert_eq!(unsafe { DROPS }, 4);
1099+
assert_eq!(DROPS.get(), 4);
11111100
}
11121101

11131102
#[test]
11141103
fn test_drop_clear() {
1115-
static mut DROPS: i32 = 0;
1116-
struct Elem;
1117-
impl Drop for Elem {
1118-
fn drop(&mut self) {
1119-
unsafe {
1120-
DROPS += 1;
1121-
}
1122-
}
1123-
}
1104+
struct_with_counted_drop!(Elem, DROPS);
11241105

11251106
let mut ring = LinkedList::new();
11261107
ring.push_back(Elem);
11271108
ring.push_front(Elem);
11281109
ring.push_back(Elem);
11291110
ring.push_front(Elem);
11301111
ring.clear();
1131-
assert_eq!(unsafe { DROPS }, 4);
1112+
assert_eq!(DROPS.get(), 4);
11321113

11331114
drop(ring);
1134-
assert_eq!(unsafe { DROPS }, 4);
1115+
assert_eq!(DROPS.get(), 4);
11351116
}
11361117

11371118
#[test]
11381119
#[cfg_attr(not(panic = "unwind"), ignore = "test requires unwinding support")]
11391120
fn test_drop_panic() {
1140-
static mut DROPS: i32 = 0;
1141-
1142-
struct D(bool);
1143-
1144-
impl Drop for D {
1145-
fn drop(&mut self) {
1146-
unsafe {
1147-
DROPS += 1;
1148-
}
1149-
1150-
if self.0 {
1151-
panic!("panic in `drop`");
1152-
}
1153-
}
1154-
}
1121+
struct_with_counted_drop!(D(bool), DROPS => |this: &D| if this.0 { panic!("panic in `drop`"); } );
11551122

11561123
let mut q = LinkedList::new();
11571124
q.push_back(D(false));
@@ -1165,7 +1132,7 @@ fn test_drop_panic() {
11651132

11661133
catch_unwind(move || drop(q)).ok();
11671134

1168-
assert_eq!(unsafe { DROPS }, 8);
1135+
assert_eq!(DROPS.get(), 8);
11691136
}
11701137

11711138
#[test]

0 commit comments

Comments
 (0)