From 5e32da184933e4398a99a3be13d21db857f059b5 Mon Sep 17 00:00:00 2001 From: Jonas Schievink Date: Thu, 12 Dec 2019 00:14:09 +0100 Subject: LinkedList: drop remaining items when drop panics --- src/liballoc/collections/linked_list.rs | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) (limited to 'src/liballoc/collections') diff --git a/src/liballoc/collections/linked_list.rs b/src/liballoc/collections/linked_list.rs index a0c9263673d..6ee22834a46 100644 --- a/src/liballoc/collections/linked_list.rs +++ b/src/liballoc/collections/linked_list.rs @@ -808,7 +808,21 @@ impl LinkedList { #[stable(feature = "rust1", since = "1.0.0")] unsafe impl<#[may_dangle] T> Drop for LinkedList { fn drop(&mut self) { - while let Some(_) = self.pop_front_node() {} + struct DropGuard<'a, T>(&'a mut LinkedList); + + impl<'a, T> Drop for DropGuard<'a, T> { + fn drop(&mut self) { + // Continue the same loop we do below. This only runs when a destructor has + // panicked. If another one panics this will abort. + while let Some(_) = self.0.pop_front_node() {} + } + } + + while let Some(node) = self.pop_front_node() { + let guard = DropGuard(self); + drop(node); + mem::forget(guard); + } } } -- cgit 1.4.1-3-g733a5