Do not create dangling &T in Weak::drop by CAD97 · Pull Request #80488 · rust-lang/rust (original) (raw)

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 have Layout::for_value_raw now, use that instead of Layout::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:

Importantly, this is already true today, as you can construct Rc<DST>, create a Weak<DST>, and drop the Rc before the Weak. 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.