Repository navigation
Behavior of panicking Drop::drop is not properly documented #60611
Description
Activity
@rustbot modify labels: T-lang T-doc
- addedA-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 toolsT-langRelevant to the language teamRelevant to the language team
on May 7, 2019 - addedA-destructorsArea: Destructors (`Drop`, …)Area: Destructors (`Drop`, …)C-enhancementCategory: An issue proposing an enhancement or a PR with one.Category: An issue proposing an enhancement or a PR with one.
on May 7, 2019 cc @rust-lang/wg-unsafe-code-guidelines @rust-lang/lang
Previous issue: #50765
And also: rust-lang/reference#348
Related: #60840 (comment)
The assumption this PR is making is that once [MIR]
dropreturns to a function, whether it succeeds or panics, it is UB for the function to then access that local.- addedI-needs-decisionIssue: In need of a decision.Issue: In need of a decision.
on Sep 9, 2024 https://doc.rust-lang.org/nightly/std/ops/trait.Drop.html#tymethod.drop currently includes
"""
Note that even if this panics, the value is considered to be dropped; you must not cause drop to be called again. This is normally automatically handled by the compiler, but when using unsafe code, can sometimes occur unintentionally, particularly when using ptr::drop_in_place.
"""This was added in ##67559
So I think this can be closed.
Agreed, thanks for gathering the references.
There are some discussions for further changes here, but those are already tracked elsewhere:
Reacted by Marijn Schouten
It was decided in, I think, #14875, that
Drop::dropcan panic, and if this happens, the value must be leaked (at least in a generic context), that is, it cannot be re-dropped again and doing that could invoke UB (that's at least what generic unsafe code needs to assume).This does not appear to be documented anywhere. These semantics make the following snippet have undefined behavior due to double-drops (playground uses
T = Vec<HasDrop>):To avoid UB, that snippet must be changed to unconditionally leak the value independently of whether
drop_in_placesucceeded or failed:cc @Centril - this might be a T-lang issue, I don't know the best way to word this, and I can't find any RFC designing this part of the language.