Skip to content

Commit 5d29954

Browse files
committed
Improve some SAFETY comments following suggestions
1 parent a83b79e commit 5d29954

File tree

1 file changed

+17
-6
lines changed

1 file changed

+17
-6
lines changed

library/std/src/thread/local.rs

+17-6
Original file line numberDiff line numberDiff line change
@@ -315,12 +315,23 @@ mod lazy {
315315
// value (an aliasing violation). To avoid setting the "I'm running a
316316
// destructor" flag we just use `mem::replace` which should sequence the
317317
// operations a little differently and make this safe to call.
318+
//
319+
// `ptr` can be dereferenced safely since it was obtained from
320+
// `UnsafeCell::get`, which should not return a non-aligned or NUL pointer.
321+
// What's more a `LazyKeyInner` can only be created with `new`, which ensures
322+
// `inner` is correctly initialized and all calls to methods on `LazyKeyInner`
323+
// will leave `inner` initialized too.
318324
unsafe {
319325
let _ = mem::replace(&mut *ptr, Some(value));
320326
}
321327

322-
// SAFETY: the *ptr operation is made safe by the `mem::replace`
323-
// call above that made sure a valid value is present behind it.
328+
// SAFETY: the `*ptr` operation is made safe by the `mem::replace`
329+
// call above combined with `ptr` being correct from the beginning
330+
// (see previous SAFETY: comment above).
331+
//
332+
// Plus, with the call to `mem::replace` it is guaranteed there is
333+
// a `Some` behind `ptr`, not a `None` so `unreachable_unchecked`
334+
// will never be reached.
324335
unsafe {
325336
// After storing `Some` we want to get a reference to the contents of
326337
// what we just stored. While we could use `unwrap` here and it should
@@ -337,8 +348,8 @@ mod lazy {
337348
#[allow(unused)]
338349
pub unsafe fn take(&mut self) -> Option<T> {
339350
// SAFETY: The other methods hand out references while taking &self.
340-
// As such, calling this method when such references are still alive
341-
// will fail because it takes a &mut self, conflicting with them.
351+
// As such, callers of this method must ensure no `&` and `&mut` are
352+
// available and used at the same time.
342353
unsafe { (*self.inner.get()).take() }
343354
}
344355
}
@@ -451,9 +462,9 @@ pub mod fast {
451462
// LLVM issue: https://bugs.llvm.org/show_bug.cgi?id=41722
452463
#[inline(never)]
453464
unsafe fn try_initialize<F: FnOnce() -> T>(&self, init: F) -> Option<&'static T> {
454-
// SAFETY: See comment above.
465+
// SAFETY: See comment above (this function doc).
455466
if !mem::needs_drop::<T>() || unsafe { self.try_register_dtor() } {
456-
// SAFETY: See comment above.
467+
// SAFETY: See comment above (his function doc).
457468
Some(unsafe { self.inner.initialize(init) })
458469
} else {
459470
None

0 commit comments

Comments
 (0)