-
-
Notifications
You must be signed in to change notification settings - Fork 15k
Drop impl of Vec is not idempotent and this is not documented #60822
Copy link
Copy link
Closed
Labels
A-collectionsArea: `std::collections`Area: `std::collections`A-docsArea: Documentation for any part of the project, including the compiler, standard library, and toolsArea: Documentation for any part of the project, including the compiler, standard library, and toolsC-enhancementCategory: An issue proposing an enhancement or a PR with one.Category: An issue proposing an enhancement or a PR with one.T-libs-apiRelevant to the library API team, which will review and decide on the PR/issue.Relevant to the library API team, which will review and decide on the PR/issue.
Metadata
Metadata
Assignees
Labels
A-collectionsArea: `std::collections`Area: `std::collections`A-docsArea: Documentation for any part of the project, including the compiler, standard library, and toolsArea: Documentation for any part of the project, including the compiler, standard library, and toolsC-enhancementCategory: An issue proposing an enhancement or a PR with one.Category: An issue proposing an enhancement or a PR with one.T-libs-apiRelevant to the library API team, which will review and decide on the PR/issue.Relevant to the library API team, which will review and decide on the PR/issue.
Type
Fields
Give feedbackNo fields configured for issues without a type.
#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).