From f70cf59fc19b7717397e9701b4783f744983275f Mon Sep 17 00:00:00 2001 From: Orson Peters Date: Wed, 28 May 2025 14:51:52 +0200 Subject: [PATCH] Improve safety comment, double-drop is not relevant here --- library/std/src/sys/thread_local/native/lazy.rs | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/library/std/src/sys/thread_local/native/lazy.rs b/library/std/src/sys/thread_local/native/lazy.rs index 0cb7fa0ef248..7cf2ba5eed84 100644 --- a/library/std/src/sys/thread_local/native/lazy.rs +++ b/library/std/src/sys/thread_local/native/lazy.rs @@ -109,9 +109,10 @@ unsafe extern "C" fn destroy(ptr: *mut u8) { abort_on_dtor_unwind(|| { let storage = unsafe { &*(ptr as *const Storage) }; if let State::Alive = storage.state.replace(State::Destroyed(())) { - // SAFETY: we ensured the state was Alive, and prevented running the destructor - // twice by updating the state to Destroyed. This is necessary as the destructor - // may attempt to access the variable. + // SAFETY: we ensured the state was Alive so the value was initialized. + // We also updated the state to Destroyed to prevent the destructor + // from accessing the thread-local variable, as this would violate + // the exclusive access provided by &mut T in Drop::drop. unsafe { crate::ptr::drop_in_place(storage.value.get().cast::()); }