From 5c497cb3f08d729206726e659d17ce1168327949 Mon Sep 17 00:00:00 2001 From: Ralf Jung Date: Wed, 29 May 2024 21:53:44 +0200 Subject: [PATCH 1/2] drop_in_place: weaken the claim of equivalence with drop(ptr.read()) --- library/core/src/ptr/mod.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/library/core/src/ptr/mod.rs b/library/core/src/ptr/mod.rs index d2bbdc84d4dd1..51eb2bb4f0e08 100644 --- a/library/core/src/ptr/mod.rs +++ b/library/core/src/ptr/mod.rs @@ -450,7 +450,7 @@ mod mut_ptr; /// Executes the destructor (if any) of the pointed-to value. /// -/// This is semantically equivalent to calling [`ptr::read`] and discarding +/// This is almost the same as calling [`ptr::read`] and discarding /// the result, but has the following advantages: /// /// * It is *required* to use `drop_in_place` to drop unsized types like From 5c68a15e41a4e2fe792508ec130c3659b198db3d Mon Sep 17 00:00:00 2001 From: Ralf Jung Date: Thu, 30 May 2024 09:07:06 +0200 Subject: [PATCH 2/2] explain what the open questions are, and add a Miri test for that --- library/core/src/ptr/mod.rs | 5 +++++ src/tools/miri/tests/pass/drop_in_place.rs | 12 ++++++++++++ 2 files changed, 17 insertions(+) create mode 100644 src/tools/miri/tests/pass/drop_in_place.rs diff --git a/library/core/src/ptr/mod.rs b/library/core/src/ptr/mod.rs index 51eb2bb4f0e08..335fc861ae86f 100644 --- a/library/core/src/ptr/mod.rs +++ b/library/core/src/ptr/mod.rs @@ -452,6 +452,11 @@ mod mut_ptr; /// /// This is almost the same as calling [`ptr::read`] and discarding /// the result, but has the following advantages: +// FIXME: say something more useful than "almost the same"? +// There are open questions here: `read` requires the value to be fully valid, e.g. if `T` is a +// `bool` it must be 0 or 1, if it is a reference then it must be dereferenceable. `drop_in_place` +// only requires that `*to_drop` be "valid for dropping" and we have not defined what that means. In +// Miri it currently (May 2024) requires nothing at all for types without drop glue. /// /// * It is *required* to use `drop_in_place` to drop unsized types like /// trait objects, because they can't be read out onto the stack and diff --git a/src/tools/miri/tests/pass/drop_in_place.rs b/src/tools/miri/tests/pass/drop_in_place.rs new file mode 100644 index 0000000000000..0615a43c80003 --- /dev/null +++ b/src/tools/miri/tests/pass/drop_in_place.rs @@ -0,0 +1,12 @@ +// Miri currently doesn't require types without drop glue to be +// valid when dropped. This test confirms that behavior. +// This is not a stable guarantee! + +use std::ptr; + +fn main() { + let mut not_a_bool = 13u8; + unsafe { + ptr::drop_in_place(&mut not_a_bool as *mut u8 as *mut bool) + }; +}