Description
#60611 shows that the Drop
impl of Vec
is not idempotent. That is, if Drop
fails, the Vec
is left in an "un-droppable" state, and trying to re-drop the vector invokes undefined behavior - that is, the vector must be leaked.
It might be possible to make it idempotent without adding significant overhead [0], but I don't know whether we should do this. I think we should be clearer about whether the Drop
impl of a type is idempotent or not, since making the wrong assumption can lead to UB, so I believe we should document this somewhere.
We could document this for the Vec
type, but maybe this can also be something that can be documented at the top of the libcore
/liballoc
/libstd
crates for all types (e.g. Drop
impls of standard types are not idempotent).
[0] Maybe setting the vector len
field to zero before dropping the slice elements and having the Drop
impl of RawVec
set the capacity to zero before exiting is enough to avoid trying to re-drop the elements or trying to deallocate the memory (which is always freed on the first drop attempt).