rust/hg-cpython/src/ref_sharing.rs
changeset 42939 06080afd0565
parent 42891 5ccc08d02280
child 43082 fdfe5cfb3723
--- a/rust/hg-cpython/src/ref_sharing.rs	Sat Sep 14 12:11:03 2019 -0400
+++ b/rust/hg-cpython/src/ref_sharing.rs	Sun Sep 15 22:43:32 2019 +0900
@@ -85,10 +85,14 @@
     /// It's unsafe to update the reference count without knowing the
     /// reference is deleted. Do not call this function directly.
     pub unsafe fn decrease_leak_count(&self, _py: Python, mutable: bool) {
-        self.leak_count
-            .replace(self.leak_count.get().saturating_sub(1));
         if mutable {
+            assert_eq!(self.leak_count.get(), 0);
+            assert!(self.mutably_borrowed.get());
             self.mutably_borrowed.replace(false);
+        } else {
+            let count = self.leak_count.get();
+            assert!(count > 0);
+            self.leak_count.replace(count - 1);
         }
     }
 }