about summary refs log tree commit diff
diff options
context:
space:
mode:
authorOrson Peters <orsonpeters@gmail.com>2025-05-28 17:09:15 +0200
committerOrson Peters <orsonpeters@gmail.com>2025-05-28 17:10:50 +0200
commit8785f7b122bbb83d308035565f243ceb95ce4736 (patch)
tree308812561d3e51b3a2c3be8f2f4b49204e87e006
parent13bce27e378267203f681470f947208b3e267558 (diff)
downloadrust-8785f7b122bbb83d308035565f243ceb95ce4736.tar.gz
rust-8785f7b122bbb83d308035565f243ceb95ce4736.zip
Add same unsafe bound on get_or_init_slow
-rw-r--r--library/std/src/sys/thread_local/native/lazy.rs10
1 files changed, 8 insertions, 2 deletions
diff --git a/library/std/src/sys/thread_local/native/lazy.rs b/library/std/src/sys/thread_local/native/lazy.rs
index c5e2618e10f..2eb1c981edb 100644
--- a/library/std/src/sys/thread_local/native/lazy.rs
+++ b/library/std/src/sys/thread_local/native/lazy.rs
@@ -57,12 +57,18 @@ where
         if let State::Alive = self.state.get() {
             self.value.get().cast()
         } else {
-            self.get_or_init_slow(i, f)
+            unsafe { self.get_or_init_slow(i, f) }
         }
     }
 
+    /// # Safety
+    /// The `self` reference must remain valid until the TLS destructor is run.
     #[cold]
-    fn get_or_init_slow(&self, i: Option<&mut Option<T>>, f: impl FnOnce() -> T) -> *const T {
+    unsafe fn get_or_init_slow(
+        &self,
+        i: Option<&mut Option<T>>,
+        f: impl FnOnce() -> T,
+    ) -> *const T {
         match self.state.get() {
             State::Uninitialized => {}
             State::Alive => return self.value.get().cast(),