#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).
#60611 shows that the
Dropimpl ofVecis not idempotent. That is, ifDropfails, theVecis 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
Dropimpl 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
Vectype, but maybe this can also be something that can be documented at the top of thelibcore/liballoc/libstdcrates for all types (e.g.Dropimpls of standard types are not idempotent).[0] Maybe setting the vector
lenfield to zero before dropping the slice elements and having theDropimpl ofRawVecset 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).