Skip to content

Commit f588463

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 6c8138d commit f588463

File tree

2 files changed

+16
-6
lines changed

2 files changed

+16
-6
lines changed

library/core/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ test = false
1616
bench = false
1717

1818
[features]
19+
default = ["debug_refcell"]
1920
# Make panics and failed asserts immediately abort without formatting any message
2021
panic_immediate_abort = []
2122
# Choose algorithms that are optimized for binary size instead of runtime performance

library/core/src/cell.rs

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -767,12 +767,13 @@ pub struct BorrowMutError {
767767
#[stable(feature = "try_borrow", since = "1.13.0")]
768768
impl Debug for BorrowMutError {
769769
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
770-
let mut builder = f.debug_struct("BorrowMutError");
770+
write!(f, "{}", self.location)
771+
// let mut builder = f.debug_struct("BorrowMutError");
771772

772-
#[cfg(feature = "debug_refcell")]
773-
builder.field("location", self.location);
773+
// #[cfg(feature = "debug_refcell")]
774+
// builder.field("location", self.location);
774775

775-
builder.finish()
776+
// builder.finish()
776777
}
777778
}
778779

@@ -788,15 +789,23 @@ impl Display for BorrowMutError {
788789
#[track_caller]
789790
#[cold]
790791
fn panic_already_borrowed(err: BorrowMutError) -> ! {
791-
panic!("already borrowed: {:?}", err)
792+
if cfg!(feature = "debug_refcell") {
793+
panic!("already borrowed here: {:?}", err);
794+
} else {
795+
panic!("already borrowed");
796+
}
792797
}
793798

794799
// This ensures the panicking code is outlined from `borrow` for `RefCell`.
795800
#[cfg_attr(not(feature = "panic_immediate_abort"), inline(never))]
796801
#[track_caller]
797802
#[cold]
798803
fn panic_already_mutably_borrowed(err: BorrowError) -> ! {
799-
panic!("already mutably borrowed: {:?}", err)
804+
if cfg!(feature = "debug_refcell") {
805+
panic!("already mutably borrowed here: {:?}", err);
806+
} else {
807+
panic!("already mutably borrowed");
808+
}
800809
}
801810

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

0 commit comments

Comments
 (0)