From 352c4408020cf3341135d9bd8cd846771288c325 Mon Sep 17 00:00:00 2001 From: Gary Guo Date: Thu, 18 Nov 2021 17:09:44 +0000 Subject: [PATCH 1/2] Try to clarify destructor not being run scenario. This is being discussed in https://rust-lang.zulipchat.com/#narrow/stream/131828-t-compiler/topic/Guarantees.20around.20Drop.20.28.2390752.29. --- src/destructors.md | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/src/destructors.md b/src/destructors.md index cf7ab0aca..3c616cec2 100644 --- a/src/destructors.md +++ b/src/destructors.md @@ -345,10 +345,14 @@ let x = (&temp()).use_temp(); // ERROR ## Not running destructors -Not running destructors in Rust is safe even if it has a type that isn't -`'static`. [`std::mem::ManuallyDrop`] provides a wrapper to prevent a +[`std::mem::forget`] can be used to prevent the destructor of a variable from being run, +and [`std::mem::ManuallyDrop`] provides a wrapper to prevent a variable or field from being dropped automatically. +> Note: Preventing a destructor from being run via `forget` or other means is safe in Rust +> even if it has a type that isn't `'static`. This means that publicly exposed APIs cannot +> rely on destructor being run for soundness. + [Assignment]: expressions/operator-expr.md#assignment-expressions [binding modes]: patterns.md#binding-modes [closure]: types/closure.md @@ -395,4 +399,5 @@ variable or field from being dropped automatically. [`::drop`]: ../std/ops/trait.Drop.html#tymethod.drop [`std::ptr::drop_in_place`]: ../std/ptr/fn.drop_in_place.html +[`std::mem::forget`]: ../std/mem/fn.forget.html [`std::mem::ManuallyDrop`]: ../std/mem/struct.ManuallyDrop.html From f1a8b53ad6ad9c09e7b621549683429ce60a22f1 Mon Sep 17 00:00:00 2001 From: Gary Guo Date: Sat, 12 Mar 2022 18:55:57 +0000 Subject: [PATCH 2/2] Apply suggestion --- src/destructors.md | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/destructors.md b/src/destructors.md index 3c616cec2..6d616b3e7 100644 --- a/src/destructors.md +++ b/src/destructors.md @@ -349,9 +349,8 @@ let x = (&temp()).use_temp(); // ERROR and [`std::mem::ManuallyDrop`] provides a wrapper to prevent a variable or field from being dropped automatically. -> Note: Preventing a destructor from being run via `forget` or other means is safe in Rust -> even if it has a type that isn't `'static`. This means that publicly exposed APIs cannot -> rely on destructor being run for soundness. +> Note: Preventing a destructor from being run via [`std::mem::forget`] or other means is safe even if it has a type that isn't `'static`. +> Besides the places where destructors are guaranteed to run as defined by this document, types may *not* safely rely on a destructor being run for soundness. [Assignment]: expressions/operator-expr.md#assignment-expressions [binding modes]: patterns.md#binding-modes