about summary refs log tree commit diff
diff options
context:
space:
mode:
authorErik Desjardins <erikdesjardins@users.noreply.github.com>2020-07-19 14:07:21 -0400
committerErik Desjardins <erikdesjardins@users.noreply.github.com>2020-07-28 15:45:25 -0400
commitc596e01b8ea34bb46444005425cd5aa825515f7b (patch)
tree2f13ceb8d18f2f177767f5b4d043e9f02f55a570
parent98efae87607aabd7c30b879befe61bf9c29eb978 (diff)
downloadrust-c596e01b8ea34bb46444005425cd5aa825515f7b.tar.gz
rust-c596e01b8ea34bb46444005425cd5aa825515f7b.zip
add track_caller to RefCell::{borrow, borrow_mut}
So panic messages point at the offending borrow.
-rw-r--r--library/core/src/cell.rs2
-rw-r--r--src/test/ui/rfc-2091-track-caller/std-panic-locations.rs9
2 files changed, 10 insertions, 1 deletions
diff --git a/library/core/src/cell.rs b/library/core/src/cell.rs
index 51d9695687f..cbbfcb46113 100644
--- a/library/core/src/cell.rs
+++ b/library/core/src/cell.rs
@@ -788,6 +788,7 @@ impl<T: ?Sized> RefCell<T> {
     /// ```
     #[stable(feature = "rust1", since = "1.0.0")]
     #[inline]
+    #[track_caller]
     pub fn borrow(&self) -> Ref<'_, T> {
         self.try_borrow().expect("already mutably borrowed")
     }
@@ -863,6 +864,7 @@ impl<T: ?Sized> RefCell<T> {
     /// ```
     #[stable(feature = "rust1", since = "1.0.0")]
     #[inline]
+    #[track_caller]
     pub fn borrow_mut(&self) -> RefMut<'_, T> {
         self.try_borrow_mut().expect("already borrowed")
     }
diff --git a/src/test/ui/rfc-2091-track-caller/std-panic-locations.rs b/src/test/ui/rfc-2091-track-caller/std-panic-locations.rs
index 35a2956ee26..d6a3a760b3e 100644
--- a/src/test/ui/rfc-2091-track-caller/std-panic-locations.rs
+++ b/src/test/ui/rfc-2091-track-caller/std-panic-locations.rs
@@ -7,8 +7,10 @@
 //! Test that panic locations for `#[track_caller]` functions in std have the correct
 //! location reported.
 
+use std::cell::RefCell;
 use std::collections::{BTreeMap, HashMap, VecDeque};
 use std::ops::{Index, IndexMut};
+use std::panic::{AssertUnwindSafe, UnwindSafe};
 
 fn main() {
     // inspect the `PanicInfo` we receive to ensure the right file is the source
@@ -20,7 +22,7 @@ fn main() {
         }
     }));
 
-    fn assert_panicked(f: impl FnOnce() + std::panic::UnwindSafe) {
+    fn assert_panicked(f: impl FnOnce() + UnwindSafe) {
         std::panic::catch_unwind(f).unwrap_err();
     }
 
@@ -57,4 +59,9 @@ fn main() {
     let weirdo: VecDeque<()> = Default::default();
     assert_panicked(|| { weirdo.index(1); });
     assert_panicked(|| { weirdo[1]; });
+
+    let refcell: RefCell<()> = Default::default();
+    let _conflicting = refcell.borrow_mut();
+    assert_panicked(AssertUnwindSafe(|| { refcell.borrow(); }));
+    assert_panicked(AssertUnwindSafe(|| { refcell.borrow_mut(); }));
 }