From 8fe08f6e3e24b26901e28f363093f104d8d0eb29 Mon Sep 17 00:00:00 2001 From: "Tim (Theemathas) Chirananthavat" Date: Tue, 8 Jul 2025 10:26:53 +0700 Subject: [PATCH] `#[may_dangle]` types can only be dropped, not moved Moving a `#[may_dangle]` type in `Drop::drop` asserts the validity of references stored in that type, which is unsound. For example, the following code is UB according to Miri: ```rust #![feature(dropck_eyepatch)] #![allow(unused)] struct Thing(Option); unsafe impl<#[may_dangle] T> Drop for Thing { fn drop(&mut self) { let _ = self.0.take(); } } fn main() { let thing; { let a = 1; thing = Thing(Some(&a)); } // thing is dropped here } ``` --- src/dropck.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/dropck.md b/src/dropck.md index 4063d561..7db6ed93 100644 --- a/src/dropck.md +++ b/src/dropck.md @@ -260,7 +260,7 @@ compiler is not checking the implicit assertion that no potentially expired data The attribute can be applied to any number of lifetime and type parameters. In the following example, we assert that we access no data behind a reference of -lifetime `'b` and that the only uses of `T` will be moves or drops, but omit +lifetime `'b` and that the only uses of `T` will be drops, but omit the attribute from `'a` and `U`, because we do access data with that lifetime and that type: