Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add track_caller to RefCell::{borrow, borrow_mut} #74526

Merged
merged 1 commit into from
Aug 3, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions library/core/src/cell.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
Expand Down Expand Up @@ -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")
}
Expand Down
9 changes: 8 additions & 1 deletion src/test/ui/rfc-2091-track-caller/std-panic-locations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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();
}

Expand Down Expand Up @@ -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(); }));
}