Skip to content

Commit 16ae56c

Browse files
committed
Add information to Display implementation for BorrowError/BorrowMutError
This change adds the location field of BorrowError/BorrowMutError to the the output when it is present and also rewords the error message. The change also uses the Display trait for outputting the error message instead of Debug.
1 parent 92e4c39 commit 16ae56c

File tree

2 files changed

+25
-14
lines changed

2 files changed

+25
-14
lines changed

library/core/Cargo.toml

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

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

library/core/src/cell.rs

Lines changed: 25 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
//!
1313
//! Shareable mutable containers exist to permit mutability in a controlled manner, even in the
1414
//! presence of aliasing. [`Cell<T>`], [`RefCell<T>`], and [`OnceCell<T>`] allow doing this in
15-
//! a single-threaded waythey do not implement [`Sync`]. (If you need to do aliasing and
15+
//! a single-threaded way-they do not implement [`Sync`]. (If you need to do aliasing and
1616
//! mutation among multiple threads, [`Mutex<T>`], [`RwLock<T>`], [`OnceLock<T>`] or [`atomic`]
1717
//! types are the correct data structures to do so).
1818
//!
@@ -741,7 +741,17 @@ pub struct BorrowError {
741741
#[stable(feature = "try_borrow", since = "1.13.0")]
742742
impl Display for BorrowError {
743743
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
744-
Display::fmt("already mutably borrowed", f)
744+
#[cfg(feature = "debug_refcell")]
745+
let res = write!(
746+
f,
747+
"RefCell already mutably borrowed, a previous borrow was at this location: {}",
748+
self._location
749+
);
750+
751+
#[cfg(not(feature = "debug_refcell"))]
752+
let res = Display::fmt("RefCell already mutably borrowed", f);
753+
754+
res
745755
}
746756
}
747757

@@ -757,7 +767,17 @@ pub struct BorrowMutError {
757767
#[stable(feature = "try_borrow", since = "1.13.0")]
758768
impl Display for BorrowMutError {
759769
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
760-
Display::fmt("already borrowed", f)
770+
#[cfg(feature = "debug_refcell")]
771+
let res = write!(
772+
f,
773+
"RefCell already borrowed, a previous borrow was at this location: {}",
774+
self._location
775+
);
776+
777+
#[cfg(not(feature = "debug_refcell"))]
778+
let res = Display::fmt("RefCell already borrowed", f);
779+
780+
res
761781
}
762782
}
763783

@@ -766,23 +786,15 @@ impl Display for BorrowMutError {
766786
#[track_caller]
767787
#[cold]
768788
fn panic_already_borrowed(err: BorrowMutError) -> ! {
769-
if cfg!(feature = "debug_refcell") {
770-
panic!("already borrowed here: {:?}", err);
771-
} else {
772-
panic!("already borrowed");
773-
}
789+
panic!("{}", err)
774790
}
775791

776792
// This ensures the panicking code is outlined from `borrow` for `RefCell`.
777793
#[cfg_attr(not(feature = "panic_immediate_abort"), inline(never))]
778794
#[track_caller]
779795
#[cold]
780796
fn panic_already_mutably_borrowed(err: BorrowError) -> ! {
781-
if cfg!(feature = "debug_refcell") {
782-
panic!("already mutably borrowed here: {:?}", err);
783-
} else {
784-
panic!("already mutably borrowed");
785-
}
797+
panic!("{}", err)
786798
}
787799

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

0 commit comments

Comments
 (0)