Skip to content

Commit d3589aa

Browse files
authored
Rollup merge of rust-lang#66564 - foeb:66219-document-unsafe-sync-cell-str, r=Amanieu
Document unsafe blocks in core::{cell, str, sync} Split from rust-lang#66506 (issue rust-lang#66219). Hopefully doing a chunk at a time is more manageable! r? @RalfJung
2 parents 2480c9e + 022a7de commit d3589aa

File tree

5 files changed

+112
-35
lines changed

5 files changed

+112
-35
lines changed

src/libcore/cell.rs

+17-2
Original file line numberDiff line numberDiff line change
@@ -187,8 +187,6 @@
187187
//! ```
188188
//!
189189
190-
// ignore-tidy-undocumented-unsafe
191-
192190
#![stable(feature = "rust1", since = "1.0.0")]
193191

194192
use crate::cmp::Ordering;
@@ -368,6 +366,10 @@ impl<T> Cell<T> {
368366
if ptr::eq(self, other) {
369367
return;
370368
}
369+
// SAFETY: This can be risky if called from separate threads, but `Cell`
370+
// is `!Sync` so this won't happen. This also won't invalidate any
371+
// pointers since `Cell` makes sure nothing else will be pointing into
372+
// either of these `Cell`s.
371373
unsafe {
372374
ptr::swap(self.value.get(), other.value.get());
373375
}
@@ -387,6 +389,8 @@ impl<T> Cell<T> {
387389
/// ```
388390
#[stable(feature = "move_cell", since = "1.17.0")]
389391
pub fn replace(&self, val: T) -> T {
392+
// SAFETY: This can cause data races if called from a separate thread,
393+
// but `Cell` is `!Sync` so this won't happen.
390394
mem::replace(unsafe { &mut *self.value.get() }, val)
391395
}
392396

@@ -423,6 +427,8 @@ impl<T: Copy> Cell<T> {
423427
#[inline]
424428
#[stable(feature = "rust1", since = "1.0.0")]
425429
pub fn get(&self) -> T {
430+
// SAFETY: This can cause data races if called from a separate thread,
431+
// but `Cell` is `!Sync` so this won't happen.
426432
unsafe { *self.value.get() }
427433
}
428434

@@ -491,6 +497,9 @@ impl<T: ?Sized> Cell<T> {
491497
#[inline]
492498
#[stable(feature = "cell_get_mut", since = "1.11.0")]
493499
pub fn get_mut(&mut self) -> &mut T {
500+
// SAFETY: This can cause data races if called from a separate thread,
501+
// but `Cell` is `!Sync` so this won't happen, and `&mut` guarantees
502+
// unique access.
494503
unsafe { &mut *self.value.get() }
495504
}
496505

@@ -510,6 +519,7 @@ impl<T: ?Sized> Cell<T> {
510519
#[inline]
511520
#[stable(feature = "as_cell", since = "1.37.0")]
512521
pub fn from_mut(t: &mut T) -> &Cell<T> {
522+
// SAFETY: `&mut` ensures unique access.
513523
unsafe { &*(t as *mut T as *const Cell<T>) }
514524
}
515525
}
@@ -553,6 +563,7 @@ impl<T> Cell<[T]> {
553563
/// ```
554564
#[stable(feature = "as_cell", since = "1.37.0")]
555565
pub fn as_slice_of_cells(&self) -> &[Cell<T>] {
566+
// SAFETY: `Cell<T>` has the same memory layout as `T`.
556567
unsafe { &*(self as *const Cell<[T]> as *const [Cell<T>]) }
557568
}
558569
}
@@ -816,6 +827,8 @@ impl<T: ?Sized> RefCell<T> {
816827
#[inline]
817828
pub fn try_borrow(&self) -> Result<Ref<'_, T>, BorrowError> {
818829
match BorrowRef::new(&self.borrow) {
830+
// SAFETY: `BorrowRef` ensures that there is only immutable access
831+
// to the value while borrowed.
819832
Some(b) => Ok(Ref { value: unsafe { &*self.value.get() }, borrow: b }),
820833
None => Err(BorrowError { _private: () }),
821834
}
@@ -891,6 +904,7 @@ impl<T: ?Sized> RefCell<T> {
891904
#[inline]
892905
pub fn try_borrow_mut(&self) -> Result<RefMut<'_, T>, BorrowMutError> {
893906
match BorrowRefMut::new(&self.borrow) {
907+
// SAFETY: `BorrowRef` guarantees unique access.
894908
Some(b) => Ok(RefMut { value: unsafe { &mut *self.value.get() }, borrow: b }),
895909
None => Err(BorrowMutError { _private: () }),
896910
}
@@ -940,6 +954,7 @@ impl<T: ?Sized> RefCell<T> {
940954
#[inline]
941955
#[stable(feature = "cell_get_mut", since = "1.11.0")]
942956
pub fn get_mut(&mut self) -> &mut T {
957+
// SAFETY: `&mut` guarantees unique access.
943958
unsafe { &mut *self.value.get() }
944959
}
945960

src/libcore/str/lossy.rs

+5-2
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@ use crate::fmt::{self, Write};
33
use crate::mem;
44
use crate::str as core_str;
55

6-
// ignore-tidy-undocumented-unsafe
7-
86
/// Lossy UTF-8 string.
97
#[unstable(feature = "str_internals", issue = "none")]
108
pub struct Utf8Lossy {
@@ -17,6 +15,7 @@ impl Utf8Lossy {
1715
}
1816

1917
pub fn from_bytes(bytes: &[u8]) -> &Utf8Lossy {
18+
// SAFETY: Both use the same memory layout, and UTF-8 correctness isn't required.
2019
unsafe { mem::transmute(bytes) }
2120
}
2221

@@ -60,6 +59,8 @@ impl<'a> Iterator for Utf8LossyChunksIter<'a> {
6059
while i < self.source.len() {
6160
let i_ = i;
6261

62+
// SAFETY: `i` starts at `0`, is less than `self.source.len()`, and
63+
// only increases, so `0 <= i < self.source.len()`.
6364
let byte = unsafe { *self.source.get_unchecked(i) };
6465
i += 1;
6566

@@ -69,6 +70,7 @@ impl<'a> Iterator for Utf8LossyChunksIter<'a> {
6970

7071
macro_rules! error {
7172
() => {{
73+
// SAFETY: We have checked up to `i` that source is valid UTF-8.
7274
unsafe {
7375
let r = Utf8LossyChunk {
7476
valid: core_str::from_utf8_unchecked(&self.source[0..i_]),
@@ -130,6 +132,7 @@ impl<'a> Iterator for Utf8LossyChunksIter<'a> {
130132
}
131133

132134
let r = Utf8LossyChunk {
135+
// SAFETY: We have checked that the entire source is valid UTF-8.
133136
valid: unsafe { core_str::from_utf8_unchecked(self.source) },
134137
broken: &[],
135138
};

0 commit comments

Comments
 (0)