Skip to content

Commit d8cbd9c

Browse files
committed
Auto merge of #74526 - erikdesjardins:reftrack, r=Mark-Simulacrum
Add track_caller to RefCell::{borrow, borrow_mut} So panic messages point at the offending borrow. Fixes #74472
2 parents 829d69b + c596e01 commit d8cbd9c

File tree

2 files changed

+10
-1
lines changed

2 files changed

+10
-1
lines changed

library/core/src/cell.rs

+2
Original file line numberDiff line numberDiff line change
@@ -788,6 +788,7 @@ impl<T: ?Sized> RefCell<T> {
788788
/// ```
789789
#[stable(feature = "rust1", since = "1.0.0")]
790790
#[inline]
791+
#[track_caller]
791792
pub fn borrow(&self) -> Ref<'_, T> {
792793
self.try_borrow().expect("already mutably borrowed")
793794
}
@@ -863,6 +864,7 @@ impl<T: ?Sized> RefCell<T> {
863864
/// ```
864865
#[stable(feature = "rust1", since = "1.0.0")]
865866
#[inline]
867+
#[track_caller]
866868
pub fn borrow_mut(&self) -> RefMut<'_, T> {
867869
self.try_borrow_mut().expect("already borrowed")
868870
}

src/test/ui/rfc-2091-track-caller/std-panic-locations.rs

+8-1
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,10 @@
77
//! Test that panic locations for `#[track_caller]` functions in std have the correct
88
//! location reported.
99
10+
use std::cell::RefCell;
1011
use std::collections::{BTreeMap, HashMap, VecDeque};
1112
use std::ops::{Index, IndexMut};
13+
use std::panic::{AssertUnwindSafe, UnwindSafe};
1214

1315
fn main() {
1416
// inspect the `PanicInfo` we receive to ensure the right file is the source
@@ -20,7 +22,7 @@ fn main() {
2022
}
2123
}));
2224

23-
fn assert_panicked(f: impl FnOnce() + std::panic::UnwindSafe) {
25+
fn assert_panicked(f: impl FnOnce() + UnwindSafe) {
2426
std::panic::catch_unwind(f).unwrap_err();
2527
}
2628

@@ -57,4 +59,9 @@ fn main() {
5759
let weirdo: VecDeque<()> = Default::default();
5860
assert_panicked(|| { weirdo.index(1); });
5961
assert_panicked(|| { weirdo[1]; });
62+
63+
let refcell: RefCell<()> = Default::default();
64+
let _conflicting = refcell.borrow_mut();
65+
assert_panicked(AssertUnwindSafe(|| { refcell.borrow(); }));
66+
assert_panicked(AssertUnwindSafe(|| { refcell.borrow_mut(); }));
6067
}

0 commit comments

Comments
 (0)