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 way— they 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" ) ]
742742impl 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" ) ]
758768impl 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]
768788fn 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]
780796fn 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