# HG changeset patch # User Yuya Nishihara # Date 1568555012 -32400 # Node ID 06080afd05650a9d614d6b69a22dd1cd627ecd92 # Parent a2dffe68b4eac19250cb3eb2a14ff765bb10a011 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. diff -r a2dffe68b4ea -r 06080afd0565 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); } } }