Skip to content

Commit d5f930f

Browse files
authored
Rollup merge of #139164 - xizheyin:issue-139034, r=joboet
std: improve documentation for get_mut() methods regarding forgotten guards Fixes #139034 This PR improves the documentation for `get_mut()` methods in `Mutex`, `RefCell`, and `RwLock` to clarify their behavior when lock guards are forgotten (e.g., via std::mem::forget). The current documentation for these methods states that a mutable borrow "statically guarantees no locks exist", which is not entirely accurate. While a mutable borrow prevents new locks from being created, it does not clear or detect previously abandoned locks through `forget()`. This can lead to counterintuitive behavior: https://play.rust-lang.org/?version=stable&mode=debug&edition=2024&gist=e68cefec12dcd435daf2237c16824ed3 https://play.rust-lang.org/?version=nightly&mode=debug&edition=2024&gist=81263ad652c752afd63c903113d3082c https://play.rust-lang.org/?version=stable&mode=debug&edition=2024&gist=311baa4edb3abf82a25c8d7bf21a4a52 r? libs
2 parents 3507f35 + 0162f29 commit d5f930f

File tree

3 files changed

+13
-3
lines changed

3 files changed

+13
-3
lines changed

Diff for: library/core/src/cell.rs

+5-1
Original file line numberDiff line numberDiff line change
@@ -1156,7 +1156,9 @@ impl<T: ?Sized> RefCell<T> {
11561156
/// Since this method borrows `RefCell` mutably, it is statically guaranteed
11571157
/// that no borrows to the underlying data exist. The dynamic checks inherent
11581158
/// in [`borrow_mut`] and most other methods of `RefCell` are therefore
1159-
/// unnecessary.
1159+
/// unnecessary. Note that this method does not reset the borrowing state if borrows were previously leaked
1160+
/// (e.g., via [`forget()`] on a [`Ref`] or [`RefMut`]). For that purpose,
1161+
/// consider using the unstable [`undo_leak`] method.
11601162
///
11611163
/// This method can only be called if `RefCell` can be mutably borrowed,
11621164
/// which in general is only the case directly after the `RefCell` has
@@ -1167,6 +1169,8 @@ impl<T: ?Sized> RefCell<T> {
11671169
/// Use [`borrow_mut`] to get mutable access to the underlying data then.
11681170
///
11691171
/// [`borrow_mut`]: RefCell::borrow_mut()
1172+
/// [`forget()`]: mem::forget
1173+
/// [`undo_leak`]: RefCell::undo_leak()
11701174
///
11711175
/// # Examples
11721176
///

Diff for: library/std/src/sync/poison/mutex.rs

+5-1
Original file line numberDiff line numberDiff line change
@@ -582,7 +582,9 @@ impl<T: ?Sized> Mutex<T> {
582582
/// Returns a mutable reference to the underlying data.
583583
///
584584
/// Since this call borrows the `Mutex` mutably, no actual locking needs to
585-
/// take place -- the mutable borrow statically guarantees no locks exist.
585+
/// take place -- the mutable borrow statically guarantees no new locks can be acquired
586+
/// while this reference exists. Note that this method does not clear any previous abandoned locks
587+
/// (e.g., via [`forget()`] on a [`MutexGuard`]).
586588
///
587589
/// # Errors
588590
///
@@ -599,6 +601,8 @@ impl<T: ?Sized> Mutex<T> {
599601
/// *mutex.get_mut().unwrap() = 10;
600602
/// assert_eq!(*mutex.lock().unwrap(), 10);
601603
/// ```
604+
///
605+
/// [`forget()`]: mem::forget
602606
#[stable(feature = "mutex_get_mut", since = "1.6.0")]
603607
pub fn get_mut(&mut self) -> LockResult<&mut T> {
604608
let data = self.data.get_mut();

Diff for: library/std/src/sync/poison/rwlock.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -608,7 +608,9 @@ impl<T: ?Sized> RwLock<T> {
608608
/// Returns a mutable reference to the underlying data.
609609
///
610610
/// Since this call borrows the `RwLock` mutably, no actual locking needs to
611-
/// take place -- the mutable borrow statically guarantees no locks exist.
611+
/// take place -- the mutable borrow statically guarantees no new locks can be acquired
612+
/// while this reference exists. Note that this method does not clear any previously abandoned locks
613+
/// (e.g., via [`forget()`] on a [`RwLockReadGuard`] or [`RwLockWriteGuard`]).
612614
///
613615
/// # Errors
614616
///

0 commit comments

Comments
 (0)