Skip to content

Commit c7ff921

Browse files
committed
Minor tweaks to refcell logging
- Fix the logging when debug_refcell is enabled so the location is output as a string rather than a sequence of bytes - Avoid logging an empty struct when debug_refcell is disabled - Make the panic error message consistent with the compiler borrow-checking error messages
1 parent c6768de commit c7ff921

File tree

1 file changed

+14
-6
lines changed

1 file changed

+14
-6
lines changed

library/core/src/cell.rs

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -743,7 +743,7 @@ impl Debug for BorrowError {
743743
let mut builder = f.debug_struct("BorrowError");
744744

745745
#[cfg(feature = "debug_refcell")]
746-
builder.field("location", self.location);
746+
builder.field("location", &format_args!("{}", self.location));
747747

748748
builder.finish()
749749
}
@@ -770,7 +770,7 @@ impl Debug for BorrowMutError {
770770
let mut builder = f.debug_struct("BorrowMutError");
771771

772772
#[cfg(feature = "debug_refcell")]
773-
builder.field("location", self.location);
773+
builder.field("location", &format_args!("{}", self.location));
774774

775775
builder.finish()
776776
}
@@ -787,16 +787,24 @@ impl Display for BorrowMutError {
787787
#[cfg_attr(not(feature = "panic_immediate_abort"), inline(never))]
788788
#[track_caller]
789789
#[cold]
790-
fn panic_already_borrowed(err: BorrowMutError) -> ! {
791-
panic!("already borrowed: {:?}", err)
790+
fn panic_already_borrowed(_err: BorrowMutError) -> ! {
791+
if cfg!(feature = "debug_refcell") {
792+
panic!("cannot borrow as mutable because it is also borrowed as immutable here {:?}", _err);
793+
} else {
794+
panic!("cannot borrow as mutable because it is also borrowed as immutable");
795+
}
792796
}
793797

794798
// This ensures the panicking code is outlined from `borrow` for `RefCell`.
795799
#[cfg_attr(not(feature = "panic_immediate_abort"), inline(never))]
796800
#[track_caller]
797801
#[cold]
798-
fn panic_already_mutably_borrowed(err: BorrowError) -> ! {
799-
panic!("already mutably borrowed: {:?}", err)
802+
fn panic_already_mutably_borrowed(_err: BorrowError) -> ! {
803+
if cfg!(feature = "debug_refcell") {
804+
panic!("cannot borrow as immutable because it is also borrowed as mutable here {:?}", _err);
805+
} else {
806+
panic!("cannot borrow as immutable because it is also borrowed as mutable");
807+
}
800808
}
801809

802810
// Positive values represent the number of `Ref` active. Negative values

0 commit comments

Comments
 (0)