From 773f88ff160ceabe42599de5073211791635aec5 Mon Sep 17 00:00:00 2001 From: Ed Page Date: Tue, 26 Mar 2024 11:12:57 -0500 Subject: [PATCH] test: Add asserts to catch BorrowMutError's This intentionally borrows from `RefCell`s before conditional code to try to prevent regressions like #13646 without exhaustively covering every case that could hit these `BorrowMutError`s with complicated tests. I tested this by ensuring the registry code path panicked before rebasing on top of #13647. Once I rebased, the panic went away. --- src/cargo/core/registry.rs | 5 +++++ src/cargo/util/flock.rs | 4 ++++ 2 files changed, 9 insertions(+) diff --git a/src/cargo/core/registry.rs b/src/cargo/core/registry.rs index 724035bf1ef..b038635c328 100644 --- a/src/cargo/core/registry.rs +++ b/src/cargo/core/registry.rs @@ -739,6 +739,11 @@ impl<'gctx> Registry for PackageRegistry<'gctx> { } fn block_until_ready(&mut self) -> CargoResult<()> { + if cfg!(debug_assertions) { + // Force borrow to catch invalid borrows, regardless of which source is used and how it + // happens to behave this time + self.gctx.shell().verbosity(); + } for (source_id, source) in self.sources.sources_mut() { source .block_until_ready() diff --git a/src/cargo/util/flock.rs b/src/cargo/util/flock.rs index 627ddbc4be5..62821d85e59 100644 --- a/src/cargo/util/flock.rs +++ b/src/cargo/util/flock.rs @@ -392,6 +392,10 @@ fn acquire( lock_try: &dyn Fn() -> io::Result<()>, lock_block: &dyn Fn() -> io::Result<()>, ) -> CargoResult<()> { + if cfg!(debug_assertions) { + // Force borrow to catch invalid borrows outside of contention situations + gctx.shell().verbosity(); + } if try_acquire(path, lock_try)? { return Ok(()); }