From dbc2866bb5f564d3be3fa5efdda769f2946ee968 Mon Sep 17 00:00:00 2001 From: Karoy Lorentey Date: Thu, 15 May 2025 21:37:11 -0700 Subject: [PATCH] [Heap] removeAll(where:): Fix exception safety --- Sources/HeapModule/Heap.swift | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/Sources/HeapModule/Heap.swift b/Sources/HeapModule/Heap.swift index fbb6e40e..a4304c8d 100644 --- a/Sources/HeapModule/Heap.swift +++ b/Sources/HeapModule/Heap.swift @@ -280,13 +280,15 @@ extension Heap { public mutating func removeAll( where shouldBeRemoved: (Element) throws -> Bool ) rethrows { - defer { _checkInvariants() } - try _storage.removeAll(where: shouldBeRemoved) - if _storage.count > 1 { - _update { handle in - handle.heapify() + defer { + if _storage.count > 1 { + _update { handle in + handle.heapify() + } } + _checkInvariants() } + try _storage.removeAll(where: shouldBeRemoved) } /// Replaces the maximum value in the heap with the given replacement,