Skip to content

Commit f320f42

Browse files
committed
Auto merge of rust-lang#114969 - kpreid:dropdoc, r=Mark-Simulacrum
Go into more detail about panicking in drop. This patch was sitting around in my drafts. I don't recall the motivation, but I think it was someone expressing confusion over “will likely abort” (since, in fact, a panicking drop _not_ caused by dropping while panicking will predictably _not_ abort). I hope that the new text will leave people well-informed about why not to panic and when it is reasonable to panic.
2 parents e877e2a + 2bccf1e commit f320f42

File tree

1 file changed

+8
-2
lines changed

1 file changed

+8
-2
lines changed

Diff for: library/core/src/ops/drop.rs

+8-2
Original file line numberDiff line numberDiff line change
@@ -217,8 +217,13 @@ pub trait Drop {
217217
///
218218
/// # Panics
219219
///
220-
/// Given that a [`panic!`] will call `drop` as it unwinds, any [`panic!`]
221-
/// in a `drop` implementation will likely abort.
220+
/// Implementations should generally avoid [`panic!`]ing, because `drop()` may itself be called
221+
/// during unwinding due to a panic, and if the `drop()` panics in that situation (a “double
222+
/// panic”), this will likely abort the program. It is possible to check [`panicking()`] first,
223+
/// which may be desirable for a `Drop` implementation that is reporting a bug of the kind
224+
/// “you didn't finish using this before it was dropped”; but most types should simply clean up
225+
/// their owned allocations or other resources and return normally from `drop()`, regardless of
226+
/// what state they are in.
222227
///
223228
/// Note that even if this panics, the value is considered to be dropped;
224229
/// you must not cause `drop` to be called again. This is normally automatically
@@ -227,6 +232,7 @@ pub trait Drop {
227232
///
228233
/// [E0040]: ../../error_codes/E0040.html
229234
/// [`panic!`]: crate::panic!
235+
/// [`panicking()`]: ../../std/thread/fn.panicking.html
230236
/// [`mem::drop`]: drop
231237
/// [`ptr::drop_in_place`]: crate::ptr::drop_in_place
232238
#[stable(feature = "rust1", since = "1.0.0")]

0 commit comments

Comments
 (0)