runtime: remove misleading use of UnsafeCell::with_mut #6513
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
The code that we're removing calls UnsafeCell::with_mut with the argument
std::mem::drop
. This is misleading because the use ofdrop
has no effect.with_mut
takes an argument of typeimpl FnOnce(*mut T) -> R
. The argument to the argument function is a pointer. Dropping a pointer has no effect.The comment above the first instance of this pattern claims that this releases some resource. This is false because the call has no effect. The intention might have been to drop the value behind the pointer. If this did happen, it would be a bug because the resource (
waker
) would be dropped again at the end of the function when the containing object is dropped.I looked through the history of this code. This code originally called
with_mut
with the argument|_| ()
. Callingwith_mut
with an argument function that does nothing has a side effect when testing with loom. When testing with loom, the code uses loom's UnsafeCell type instead of std's. The intention of the code was likely to make use of that side effect because we expect to have exclusive access here as we are going to drop the containing object. The side effect is that loom checks that Rust's reference uniqueness properties are upheld.Instead of removing the whole code, I considered changing
drop
back to|_|()
and documenting what I wrote above. I decided on removing the code because nowhere else do we usewith_mut
in this way and the purpose of this check would be better served in loom directly as part of UnsafeCell's Drop implementation. I created an issue about this in loom [1].[1] tokio-rs/loom#349