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

Outline panicking code for RefCell::borrow and RefCell::borrow_mut #115491

Merged
merged 1 commit into from
Sep 4, 2023
Merged
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
26 changes: 24 additions & 2 deletions library/core/src/cell.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Copy link
Contributor

@klensy klensy Sep 3, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Now this will print on release "already borrowed: BorrowMutError" instead of "already borrowed", which is useless.
Next one similar.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's done to match the existing output. BorrowMutError may also contain the location of the original borrower.

Copy link
Contributor

@klensy klensy Sep 3, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

https://play.rust-lang.org/?version=nightly&mode=release&edition=2021&gist=4db78fa88be92de81e74933887a7a140
Oh, my bad, with this pr and previously that code outputs "already borrowed: BorrowMutError".

Sad to not to have test coverage. Can you add tests with panic outputs?

Can this be simple strings like "already borrowed" instead? If user wants more info from error he will use try_borrow/try_borrow_mut instead of borrow/borrow_mut, that fn should be as slim as possible (but that cold code anyway, except pulling debug impl for error). And location available only via debug_refcell which require rebuild std anyway?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would make sense to me to use simpler strings with debug_refcell disabled.

}

// 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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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.
Expand Down