Do not create dangling &T in Weak<T>::drop #80488
Merged
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.
Since at this point all strong pointers have been dropped, the wrapped
T
has also been dropped. As such, creating a&T
to the dropped place is negligent at best (language UB at worst). Since we haveLayout::for_value_raw
now, use that instead ofLayout::for_value
to avoid creating the&T
.This does have implications for custom (potentially thin) DSTs, though much less severe than those discussed in #80407. Specifically, one of two things has to be true:
*const T
to a dropped (potentially custom, potentially thin) unsized tailed object to determine the layout (size/align) of the object. This is what is currently implemented (though with&T
instead of&T
). The validity of reading some location after it has been dropped is an open question IIUC (What about: use-after-move and (maybe) use-after-drop unsafe-code-guidelines#188) (except when the whole type isCopy
, perdrop_in_place
's docs).In this design, custom DSTs would get a
*mut T
and use that to return layout, and must be able to do so while in the "zombie" (post-drop, pre-free) state.RcBox
/ArcInner
compute and store layout eagerly, so that they don't have to ask the type for its layout after dropping it.Importantly, this is already true today, as you can construct
Rc<DST>
, create aWeak<DST>
, and drop theRc
before theWeak
. This PR is a strict improvement over the status quo, and the above question about potentially thin DSTs will need to be resolved by any custom DST proposal.