add track_caller to RefCell::{borrow, borrow_mut}
So panic messages point at the offending borrow.
This commit is contained in:
parent
98efae8760
commit
c596e01b8e
2 changed files with 10 additions and 1 deletions
|
@ -788,6 +788,7 @@ impl<T: ?Sized> RefCell<T> {
|
||||||
/// ```
|
/// ```
|
||||||
#[stable(feature = "rust1", since = "1.0.0")]
|
#[stable(feature = "rust1", since = "1.0.0")]
|
||||||
#[inline]
|
#[inline]
|
||||||
|
#[track_caller]
|
||||||
pub fn borrow(&self) -> Ref<'_, T> {
|
pub fn borrow(&self) -> Ref<'_, T> {
|
||||||
self.try_borrow().expect("already mutably borrowed")
|
self.try_borrow().expect("already mutably borrowed")
|
||||||
}
|
}
|
||||||
|
@ -863,6 +864,7 @@ impl<T: ?Sized> RefCell<T> {
|
||||||
/// ```
|
/// ```
|
||||||
#[stable(feature = "rust1", since = "1.0.0")]
|
#[stable(feature = "rust1", since = "1.0.0")]
|
||||||
#[inline]
|
#[inline]
|
||||||
|
#[track_caller]
|
||||||
pub fn borrow_mut(&self) -> RefMut<'_, T> {
|
pub fn borrow_mut(&self) -> RefMut<'_, T> {
|
||||||
self.try_borrow_mut().expect("already borrowed")
|
self.try_borrow_mut().expect("already borrowed")
|
||||||
}
|
}
|
||||||
|
|
|
@ -7,8 +7,10 @@
|
||||||
//! Test that panic locations for `#[track_caller]` functions in std have the correct
|
//! Test that panic locations for `#[track_caller]` functions in std have the correct
|
||||||
//! location reported.
|
//! location reported.
|
||||||
|
|
||||||
|
use std::cell::RefCell;
|
||||||
use std::collections::{BTreeMap, HashMap, VecDeque};
|
use std::collections::{BTreeMap, HashMap, VecDeque};
|
||||||
use std::ops::{Index, IndexMut};
|
use std::ops::{Index, IndexMut};
|
||||||
|
use std::panic::{AssertUnwindSafe, UnwindSafe};
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
// inspect the `PanicInfo` we receive to ensure the right file is the source
|
// 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();
|
std::panic::catch_unwind(f).unwrap_err();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -57,4 +59,9 @@ fn main() {
|
||||||
let weirdo: VecDeque<()> = Default::default();
|
let weirdo: VecDeque<()> = Default::default();
|
||||||
assert_panicked(|| { weirdo.index(1); });
|
assert_panicked(|| { weirdo.index(1); });
|
||||||
assert_panicked(|| { weirdo[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(); }));
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue