]> git.lizzy.rs Git - rust.git/commit - src/tools/rust-analyzer
Auto merge of #85535 - dtolnay:weakdangle, r=kennytm
authorbors <bors@rust-lang.org>
Wed, 26 May 2021 01:17:02 +0000 (01:17 +0000)
committerbors <bors@rust-lang.org>
Wed, 26 May 2021 01:17:02 +0000 (01:17 +0000)
commit47a90f452050d4ea484206447babb07dd33c21d5
tree36cc6db15488f9ff8766259d39bb8f02be2eb63a
parentff2c947c00f867b9f012e28ba88cecfbe556f904
parent23a4050f7dbc8675df6305b9968a3d5648518ff4
Auto merge of #85535 - dtolnay:weakdangle, r=kennytm

Weak's type parameter may dangle on drop

Way back in https://github.com/rust-lang/rust/commit/34076bc0c9fb9ee718e1cebc407547eef730a080, #\[may_dangle\] was added to Rc\<T\> and Arc\<T\>'s Drop impls. That appears to have been because a test added in #28929 used Arc and Rc with dangling references at drop time. However, Weak was not covered by that test, and therefore no #\[may_dangle\] was forced to be added at the time.

As far as dropping, Weak has *even less need* to interact with the T than Rc and Arc do. Roughly speaking #\[may_dangle\] describes generic parameters that the outer type's Drop impl does not interact with except by possibly dropping them; no other interaction (such as trait method calls on the generic type) is permissible. It's clear this applies to Rc's and Arc's drop impl, which sometimes drop T but otherwise do not interact with one. It applies *even more* to Weak. Dropping a Weak cannot ever cause T's drop impl to run. Either there are strong references still in existence, in which case better not drop the T. Or there are no strong references still in existence, in which case the T would already have been dropped previously by the drop of the last strong count.