Skip to content

Commit f81b07e

Browse files
committed
Adapt inplace collection leak test to check for no leaks
1 parent fa61678 commit f81b07e

File tree

1 file changed

+20
-16
lines changed

1 file changed

+20
-16
lines changed

library/alloc/tests/vec.rs

+20-16
Original file line numberDiff line numberDiff line change
@@ -1110,23 +1110,31 @@ fn test_from_iter_specialization_panic_during_iteration_drops() {
11101110
}
11111111

11121112
#[test]
1113-
fn test_from_iter_specialization_panic_during_drop_leaks() {
1114-
static mut DROP_COUNTER: usize = 0;
1113+
fn test_from_iter_specialization_panic_during_drop_doesnt_leak() {
1114+
static mut DROP_COUNTER_SHOULD_BE_DROPPED: usize = 0;
1115+
static mut DROP_COUNTER_DROPPED_TWICE: usize = 0;
11151116

11161117
#[derive(Debug)]
11171118
enum Droppable {
1119+
ShouldBeDropped,
11181120
DroppedTwice(Box<i32>),
11191121
PanicOnDrop,
11201122
}
11211123

11221124
impl Drop for Droppable {
11231125
fn drop(&mut self) {
11241126
match self {
1127+
Droppable::ShouldBeDropped => {
1128+
unsafe {
1129+
DROP_COUNTER_SHOULD_BE_DROPPED += 1;
1130+
}
1131+
println!("Dropping ShouldBeDropped!")
1132+
}
11251133
Droppable::DroppedTwice(_) => {
11261134
unsafe {
1127-
DROP_COUNTER += 1;
1135+
DROP_COUNTER_DROPPED_TWICE += 1;
11281136
}
1129-
println!("Dropping!")
1137+
println!("Dropping DroppedTwice!")
11301138
}
11311139
Droppable::PanicOnDrop => {
11321140
if !std::thread::panicking() {
@@ -1137,21 +1145,17 @@ fn test_from_iter_specialization_panic_during_drop_leaks() {
11371145
}
11381146
}
11391147

1140-
let mut to_free: *mut Droppable = core::ptr::null_mut();
1141-
let mut cap = 0;
1142-
11431148
let _ = std::panic::catch_unwind(AssertUnwindSafe(|| {
1144-
let mut v = vec![Droppable::DroppedTwice(Box::new(123)), Droppable::PanicOnDrop];
1145-
to_free = v.as_mut_ptr();
1146-
cap = v.capacity();
1147-
let _ = v.into_iter().take(0).collect::<Vec<_>>();
1149+
let v = vec![
1150+
Droppable::ShouldBeDropped,
1151+
Droppable::DroppedTwice(Box::new(123)),
1152+
Droppable::PanicOnDrop,
1153+
];
1154+
let _ = v.into_iter().take(1).collect::<Vec<_>>();
11481155
}));
11491156

1150-
assert_eq!(unsafe { DROP_COUNTER }, 1);
1151-
// clean up the leak to keep miri happy
1152-
unsafe {
1153-
drop(Vec::from_raw_parts(to_free, 0, cap));
1154-
}
1157+
assert_eq!(unsafe { DROP_COUNTER_SHOULD_BE_DROPPED }, 1);
1158+
assert_eq!(unsafe { DROP_COUNTER_DROPPED_TWICE }, 1);
11551159
}
11561160

11571161
// regression test for issue #85322. Peekable previously implemented InPlaceIterable,

0 commit comments

Comments
 (0)