Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
98 changes: 53 additions & 45 deletions library/alloc/src/boxed/thin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -165,10 +165,10 @@ impl<T: ?Sized> DerefMut for ThinBox<T> {
#[unstable(feature = "thin_box", issue = "92791")]
impl<T: ?Sized> Drop for ThinBox<T> {
fn drop(&mut self) {
let value = self.deref_mut();
let value = value as *mut T;
// ignore-tidy-undocumented-unsafe
unsafe {
let value = self.deref_mut();
let value = value as *mut T;
self.with_header().drop::<T>(value);
}
}
Expand Down Expand Up @@ -240,34 +240,38 @@ impl<H> WithHeader<H> {
alloc::handle_alloc_error(Layout::new::<()>());
};

// ignore-tidy-undocumented-unsafe
unsafe {
// Note: It's UB to pass a layout with a zero size to `alloc::alloc`, so
// we use `layout.dangling()` for this case, which should have a valid
// alignment for both `T` and `H`.
let ptr = if layout.size() == 0 {
// Some paranoia checking, mostly so that the ThinBox tests are
// more able to catch issues.
debug_assert!(value_offset == 0 && T::IS_ZST && H::IS_ZST);
layout.dangling_ptr()
} else {
let ptr = alloc::alloc(layout);
if ptr.is_null() {
alloc::handle_alloc_error(layout);
}
// Safety:
// - The size is at least `aligned_header_size`.
// Note: It's UB to pass a layout with a zero size to `alloc::alloc`, so
// we use `layout.dangling()` for this case, which should have a valid
// alignment for both `T` and `H`.
let ptr = if layout.size() == 0 {
// Some paranoia checking, mostly so that the ThinBox tests are
// more able to catch issues.
debug_assert!(value_offset == 0 && T::IS_ZST && H::IS_ZST);
layout.dangling_ptr()
} else {
// ignore-tidy-undocumented-unsafe
let ptr = unsafe { alloc::alloc(layout) };
if ptr.is_null() {
alloc::handle_alloc_error(layout);
}
// SAFETY:
// - The size is at least `aligned_header_size`.
unsafe {
let ptr = ptr.add(value_offset) as *mut _;

NonNull::new_unchecked(ptr)
};
}
};

let result = WithHeader(ptr, PhantomData);
let result = WithHeader(ptr, PhantomData);

// ignore-tidy-undocumented-unsafe
unsafe {
ptr::write(result.header(), header);
ptr::write(result.value().cast(), value);

result
}

result
}

/// Non-panicking version of `new`.
Expand All @@ -278,35 +282,39 @@ impl<H> WithHeader<H> {
return Err(core::alloc::AllocError);
};

// ignore-tidy-undocumented-unsafe
unsafe {
// Note: It's UB to pass a layout with a zero size to `alloc::alloc`, so
// we use `layout.dangling()` for this case, which should have a valid
// alignment for both `T` and `H`.
let ptr = if layout.size() == 0 {
// Some paranoia checking, mostly so that the ThinBox tests are
// more able to catch issues.
debug_assert!(value_offset == 0 && T::IS_ZST && H::IS_ZST);
layout.dangling_ptr()
} else {
let ptr = alloc::alloc(layout);
if ptr.is_null() {
return Err(core::alloc::AllocError);
}

// Safety:
// - The size is at least `aligned_header_size`.
// Note: It's UB to pass a layout with a zero size to `alloc::alloc`, so
// we use `layout.dangling()` for this case, which should have a valid
// alignment for both `T` and `H`.
let ptr = if layout.size() == 0 {
// Some paranoia checking, mostly so that the ThinBox tests are
// more able to catch issues.
debug_assert!(value_offset == 0 && T::IS_ZST && H::IS_ZST);
layout.dangling_ptr()
} else {
// ignore-tidy-undocumented-unsafe
let ptr = unsafe { alloc::alloc(layout) };
if ptr.is_null() {
return Err(core::alloc::AllocError);
}

// SAFETY:
// - The size is at least `aligned_header_size`.
unsafe {
let ptr = ptr.add(value_offset) as *mut _;

NonNull::new_unchecked(ptr)
};
}
};

let result = WithHeader(ptr, PhantomData);

let result = WithHeader(ptr, PhantomData);
// ignore-tidy-undocumented-unsafe
unsafe {
ptr::write(result.header(), header);
ptr::write(result.value().cast(), value);

Ok(result)
}

Ok(result)
}

// `Dyn` is `?Sized` type like `[u32]`, and `T` is ZST type like `[u32; 0]`.
Expand Down
4 changes: 2 additions & 2 deletions library/alloc/src/collections/binary_heap/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1574,9 +1574,9 @@ impl<'a, T> Hole<'a, T> {
unsafe fn move_to(&mut self, index: usize) {
debug_assert!(index != self.pos);
debug_assert!(index < self.data.len());
let ptr = self.data.as_mut_ptr();
// ignore-tidy-undocumented-unsafe
unsafe {
let ptr = self.data.as_mut_ptr();
let index_ptr: *const _ = ptr.add(index);
let hole_ptr = ptr.add(self.pos);
ptr::copy_nonoverlapping(index_ptr, hole_ptr, 1);
Expand All @@ -1589,9 +1589,9 @@ impl<T> Drop for Hole<'_, T> {
#[inline]
fn drop(&mut self) {
// fill the hole again
let pos = self.pos;
// ignore-tidy-undocumented-unsafe
unsafe {
let pos = self.pos;
ptr::copy_nonoverlapping(&*self.elt, self.data.get_unchecked_mut(pos), 1);
}
}
Expand Down
6 changes: 3 additions & 3 deletions library/alloc/src/collections/btree/map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -280,10 +280,10 @@ impl<K: Clone, V: Clone, A: AllocatorClone> Clone for BTreeMap<K, V, A> {

// We can't destructure subtree directly
// because BTreeMap implements Drop
// ignore-tidy-undocumented-unsafe
let (subroot, sublength) = unsafe {
let (subroot, sublength) = {
let subtree = ManuallyDrop::new(subtree);
let root = ptr::read(&subtree.root);
// ignore-tidy-undocumented-unsafe
let root = unsafe { ptr::read(&subtree.root) };
let length = subtree.length;
(root, length)
};
Expand Down
16 changes: 8 additions & 8 deletions library/alloc/src/collections/btree/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1875,11 +1875,11 @@ pub(super) mod marker {
/// # Safety
/// The slice has more than `idx` elements.
unsafe fn slice_insert<T>(slice: &mut [MaybeUninit<T>], idx: usize, val: T) {
let len = slice.len();
debug_assert!(len > idx);
let slice_ptr = slice.as_mut_ptr();
// ignore-tidy-undocumented-unsafe
unsafe {
let len = slice.len();
debug_assert!(len > idx);
let slice_ptr = slice.as_mut_ptr();
if len > idx + 1 {
ptr::copy(slice_ptr.add(idx), slice_ptr.add(idx + 1), len - idx - 1);
}
Expand All @@ -1893,11 +1893,11 @@ unsafe fn slice_insert<T>(slice: &mut [MaybeUninit<T>], idx: usize, val: T) {
/// # Safety
/// The slice has more than `idx` elements.
unsafe fn slice_remove<T>(slice: &mut [MaybeUninit<T>], idx: usize) -> T {
let len = slice.len();
debug_assert!(idx < len);
let slice_ptr = slice.as_mut_ptr();
// ignore-tidy-undocumented-unsafe
unsafe {
let len = slice.len();
debug_assert!(idx < len);
let slice_ptr = slice.as_mut_ptr();
let ret = (*slice_ptr.add(idx)).assume_init_read();
ptr::copy(slice_ptr.add(idx + 1), slice_ptr.add(idx), len - idx - 1);
ret
Expand All @@ -1909,9 +1909,9 @@ unsafe fn slice_remove<T>(slice: &mut [MaybeUninit<T>], idx: usize) -> T {
/// # Safety
/// The slice has at least `distance` elements.
unsafe fn slice_shl<T>(slice: &mut [MaybeUninit<T>], distance: usize) {
let slice_ptr = slice.as_mut_ptr();
// ignore-tidy-undocumented-unsafe
unsafe {
let slice_ptr = slice.as_mut_ptr();
ptr::copy(slice_ptr.add(distance), slice_ptr, slice.len() - distance);
}
}
Expand All @@ -1921,9 +1921,9 @@ unsafe fn slice_shl<T>(slice: &mut [MaybeUninit<T>], distance: usize) {
/// # Safety
/// The slice has at least `distance` elements.
unsafe fn slice_shr<T>(slice: &mut [MaybeUninit<T>], distance: usize) {
let slice_ptr = slice.as_mut_ptr();
// ignore-tidy-undocumented-unsafe
unsafe {
let slice_ptr = slice.as_mut_ptr();
ptr::copy(slice_ptr, slice_ptr.add(distance), slice.len() - distance);
}
}
Expand Down
56 changes: 28 additions & 28 deletions library/alloc/src/collections/linked_list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1679,20 +1679,20 @@ impl<'a, T> CursorMut<'a, T> {
/// inserted at the start of the `LinkedList`.
#[unstable(feature = "linked_list_cursors", issue = "58533")]
pub fn splice_after(&mut self, list: LinkedList<T>) {
let Some((splice_head, splice_tail, splice_len)) = list.detach_all_nodes() else {
return;
};
// ignore-tidy-undocumented-unsafe
unsafe {
let Some((splice_head, splice_tail, splice_len)) = list.detach_all_nodes() else {
return;
};
let node_next = match self.current {
None => self.list.head,
Some(node) => node.as_ref().next,
};
self.list.splice_nodes(self.current, node_next, splice_head, splice_tail, splice_len);
if self.current.is_none() {
// The "ghost" non-element's index has changed.
self.index = self.list.len;
}
}
if self.current.is_none() {
// The "ghost" non-element's index has changed.
self.index = self.list.len;
}
}

Expand All @@ -1702,19 +1702,19 @@ impl<'a, T> CursorMut<'a, T> {
/// inserted at the end of the `LinkedList`.
#[unstable(feature = "linked_list_cursors", issue = "58533")]
pub fn splice_before(&mut self, list: LinkedList<T>) {
let (splice_head, splice_tail, splice_len) = match list.detach_all_nodes() {
Some(parts) => parts,
_ => return,
};
// ignore-tidy-undocumented-unsafe
unsafe {
let (splice_head, splice_tail, splice_len) = match list.detach_all_nodes() {
Some(parts) => parts,
_ => return,
};
let node_prev = match self.current {
None => self.list.tail,
Some(node) => node.as_ref().prev,
};
self.list.splice_nodes(node_prev, self.current, splice_head, splice_tail, splice_len);
self.index += splice_len;
}
self.index += splice_len;
}
}

Expand All @@ -1725,19 +1725,19 @@ impl<'a, T, A: Allocator> CursorMut<'a, T, A> {
/// inserted at the front of the `LinkedList`.
#[unstable(feature = "linked_list_cursors", issue = "58533")]
pub fn insert_after(&mut self, item: T) {
let spliced_node =
Box::into_non_null_with_allocator(Box::new_in(Node::new(item), &self.list.alloc)).0;
// ignore-tidy-undocumented-unsafe
unsafe {
let spliced_node =
Box::into_non_null_with_allocator(Box::new_in(Node::new(item), &self.list.alloc)).0;
let node_next = match self.current {
None => self.list.head,
Some(node) => node.as_ref().next,
};
self.list.splice_nodes(self.current, node_next, spliced_node, spliced_node, 1);
if self.current.is_none() {
// The "ghost" non-element's index has changed.
self.index = self.list.len;
}
}
if self.current.is_none() {
// The "ghost" non-element's index has changed.
self.index = self.list.len;
}
}

Expand All @@ -1747,17 +1747,17 @@ impl<'a, T, A: Allocator> CursorMut<'a, T, A> {
/// inserted at the end of the `LinkedList`.
#[unstable(feature = "linked_list_cursors", issue = "58533")]
pub fn insert_before(&mut self, item: T) {
let spliced_node =
Box::into_non_null_with_allocator(Box::new_in(Node::new(item), &self.list.alloc)).0;
// ignore-tidy-undocumented-unsafe
unsafe {
let spliced_node =
Box::into_non_null_with_allocator(Box::new_in(Node::new(item), &self.list.alloc)).0;
let node_prev = match self.current {
None => self.list.tail,
Some(node) => node.as_ref().prev,
};
self.list.splice_nodes(node_prev, self.current, spliced_node, spliced_node, 1);
self.index += 1;
}
self.index += 1;
}

/// Removes the current element from the `LinkedList`.
Expand Down Expand Up @@ -1799,14 +1799,14 @@ impl<'a, T, A: Allocator> CursorMut<'a, T, A> {

unlinked_node.as_mut().prev = None;
unlinked_node.as_mut().next = None;
Some(LinkedList {
head: Some(unlinked_node),
tail: Some(unlinked_node),
len: 1,
alloc: self.list.alloc.clone(),
marker: PhantomData,
})
}
Some(LinkedList {
head: Some(unlinked_node),
tail: Some(unlinked_node),
len: 1,
alloc: self.list.alloc.clone(),
marker: PhantomData,
})
}

/// Splits the list into two after the current element. This will return a
Expand Down
Loading
Loading