about summary refs log tree commit diff
path: root/library
diff options
context:
space:
mode:
authorbors <bors@rust-lang.org>2023-09-03 23:03:03 +0000
committerbors <bors@rust-lang.org>2023-09-03 23:03:03 +0000
commitabfc6c44381fb033c6b3b0a6bfb804a799f39afd (patch)
treeebfc7c336933056636ad536a2df2b324c7698881 /library
parent58e967a9cc3bd39122e8cb728e8cec6e3a4eeef2 (diff)
parent00c251134d49383b6d69d8707886ab1114aa0c33 (diff)
downloadrust-abfc6c44381fb033c6b3b0a6bfb804a799f39afd.tar.gz
rust-abfc6c44381fb033c6b3b0a6bfb804a799f39afd.zip
Auto merge of #115491 - Zoxc:refcell-tweak, r=Mark-Simulacrum
Outline panicking code for `RefCell::borrow` and `RefCell::borrow_mut`

This outlines panicking code for `RefCell::borrow` and `RefCell::borrow_mut` to reduce code size.
Diffstat (limited to 'library')
-rw-r--r--library/core/src/cell.rs26
1 files changed, 24 insertions, 2 deletions
diff --git a/library/core/src/cell.rs b/library/core/src/cell.rs
index 28950a43d2d..20dce6516bf 100644
--- a/library/core/src/cell.rs
+++ b/library/core/src/cell.rs
@@ -753,6 +753,22 @@ impl Display for BorrowMutError {
     }
 }
 
+// This ensures the panicking code is outlined from `borrow_mut` for `RefCell`.
+#[inline(never)]
+#[track_caller]
+#[cold]
+fn panic_already_borrowed(err: BorrowMutError) -> ! {
+    panic!("already borrowed: {:?}", err)
+}
+
+// This ensures the panicking code is outlined from `borrow` for `RefCell`.
+#[inline(never)]
+#[track_caller]
+#[cold]
+fn panic_already_mutably_borrowed(err: BorrowError) -> ! {
+    panic!("already mutably borrowed: {:?}", err)
+}
+
 // Positive values represent the number of `Ref` active. Negative values
 // represent the number of `RefMut` active. Multiple `RefMut`s can only be
 // active at a time if they refer to distinct, nonoverlapping components of a
@@ -934,7 +950,10 @@ impl<T: ?Sized> RefCell<T> {
     #[inline]
     #[track_caller]
     pub fn borrow(&self) -> Ref<'_, T> {
-        self.try_borrow().expect("already mutably borrowed")
+        match self.try_borrow() {
+            Ok(b) => b,
+            Err(err) => panic_already_mutably_borrowed(err),
+        }
     }
 
     /// Immutably borrows the wrapped value, returning an error if the value is currently mutably
@@ -1027,7 +1046,10 @@ impl<T: ?Sized> RefCell<T> {
     #[inline]
     #[track_caller]
     pub fn borrow_mut(&self) -> RefMut<'_, T> {
-        self.try_borrow_mut().expect("already borrowed")
+        match self.try_borrow_mut() {
+            Ok(b) => b,
+            Err(err) => panic_already_borrowed(err),
+        }
     }
 
     /// Mutably borrows the wrapped value, returning an error if the value is currently borrowed.