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::()); }