rust-cpython: add sanity check to PySharedState::decrease_leak_count()
authorYuya Nishihara <yuya@tcha.org>
Sun, 15 Sep 2019 22:43:32 +0900
changeset 42939 06080afd0565
parent 42938 a2dffe68b4ea
child 42940 c04e0836f039
rust-cpython: add sanity check to PySharedState::decrease_leak_count() If decrease_leak_count() were called unnecessarily, there must be a serious bug. It's better to not silently ignore such cases.
rust/hg-cpython/src/ref_sharing.rs
--- 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);
         }
     }
 }