Skip to content

Commit bfb8479

Browse files
susitsmepage
authored andcommitted
Abstract away an assertions details
This will make it easier to change the implementation in the future.
1 parent b624a44 commit bfb8479

File tree

3 files changed

+17
-9
lines changed

3 files changed

+17
-9
lines changed

src/cargo/core/registry.rs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -800,11 +800,9 @@ impl<'gctx> Registry for PackageRegistry<'gctx> {
800800

801801
#[tracing::instrument(skip_all)]
802802
fn block_until_ready(&mut self) -> CargoResult<()> {
803-
if cfg!(debug_assertions) {
804-
// Force borrow to catch invalid borrows, regardless of which source is used and how it
805-
// happens to behave this time
806-
self.gctx.shell().verbosity();
807-
}
803+
// Ensure `shell` is not already in use,
804+
// regardless of which source is used and how it happens to behave this time
805+
self.gctx.debug_assert_shell_not_borrowed();
808806
for (source_id, source) in self.sources.sources_mut() {
809807
source
810808
.block_until_ready()

src/cargo/util/context/mod.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -412,6 +412,17 @@ impl GlobalContext {
412412
self.shell.borrow_mut()
413413
}
414414

415+
/// Assert [`Self::shell`] is not in use
416+
///
417+
/// Testing might not identify bugs with two accesses to `shell` at once
418+
/// due to conditional logic,
419+
/// so place this outside of the conditions to catch these bugs in more situations.
420+
pub fn debug_assert_shell_not_borrowed(&self) {
421+
if cfg!(debug_assertions) {
422+
self.shell().verbosity();
423+
}
424+
}
425+
415426
/// Gets the path to the `rustdoc` executable.
416427
pub fn rustdoc(&self) -> CargoResult<&Path> {
417428
self.rustdoc

src/cargo/util/flock.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -389,10 +389,9 @@ fn acquire(
389389
lock_try: &dyn Fn() -> Result<(), TryLockError>,
390390
lock_block: &dyn Fn() -> io::Result<()>,
391391
) -> CargoResult<()> {
392-
if cfg!(debug_assertions) {
393-
// Force borrow to catch invalid borrows outside of contention situations
394-
gctx.shell().verbosity();
395-
}
392+
// Ensure `shell` is not already in use,
393+
// regardless of whether we hit contention or not
394+
gctx.debug_assert_shell_not_borrowed();
396395
if try_acquire(path, lock_try)? {
397396
return Ok(());
398397
}

0 commit comments

Comments
 (0)