about summary refs log tree commit diff
diff options
context:
space:
mode:
authorLukas Bergdoll <lukas.bergdoll@gmail.com>2024-04-16 20:44:01 +0200
committerLukas Bergdoll <lukas.bergdoll@gmail.com>2024-05-16 17:08:55 +0200
commit1a6b0e410e3b32f8641b9ee2bcf992712c8fa72f (patch)
tree540432904ca256d76f1cf995885f0b60ad4b7040
parente49be415cdc24d876403e8493e28ec1bfee62427 (diff)
downloadrust-1a6b0e410e3b32f8641b9ee2bcf992712c8fa72f.tar.gz
rust-1a6b0e410e3b32f8641b9ee2bcf992712c8fa72f.zip
Fix tidy errors
-rw-r--r--library/core/src/slice/sort/select.rs5
-rw-r--r--library/core/src/slice/sort/shared/smallsort.rs15
-rw-r--r--library/core/src/slice/sort/stable/quicksort.rs9
-rw-r--r--library/core/src/slice/sort/unstable/quicksort.rs4
-rw-r--r--library/core/tests/slice.rs1
-rw-r--r--src/doc/unstable-book/src/library-features/sort-internals.md5
6 files changed, 13 insertions, 26 deletions
diff --git a/library/core/src/slice/sort/select.rs b/library/core/src/slice/sort/select.rs
index e0c1085916e..6212def3041 100644
--- a/library/core/src/slice/sort/select.rs
+++ b/library/core/src/slice/sort/select.rs
@@ -92,7 +92,10 @@ fn partition_at_index_loop<'a, T, F>(
         // slice. Partition the slice into elements equal to and elements greater than the pivot.
         // This case is usually hit when the slice contains many duplicate elements.
         if let Some(p) = ancestor_pivot {
-            if !is_less(p, unsafe { v.get_unchecked(pivot_pos) }) {
+            // SAFETY: choose_pivot promises to return a valid pivot position.
+            let pivot = unsafe { v.get_unchecked(pivot_pos) };
+
+            if !is_less(p, pivot) {
                 let num_lt = partition(v, pivot_pos, &mut |a, b| !is_less(b, a));
 
                 // Continue sorting elements greater than the pivot. We know that `mid` contains
diff --git a/library/core/src/slice/sort/shared/smallsort.rs b/library/core/src/slice/sort/shared/smallsort.rs
index 8dbd45a389c..6e4424310e8 100644
--- a/library/core/src/slice/sort/shared/smallsort.rs
+++ b/library/core/src/slice/sort/shared/smallsort.rs
@@ -177,6 +177,8 @@ fn small_sort_fallback<T, F: FnMut(&T, &T) -> bool>(v: &mut [T], is_less: &mut F
 fn small_sort_general<T: FreezeMarker, F: FnMut(&T, &T) -> bool>(v: &mut [T], is_less: &mut F) {
     let mut stack_array = MaybeUninit::<[T; SMALL_SORT_GENERAL_SCRATCH_LEN]>::uninit();
 
+    // SAFETY: The memory is backed by `stack_array`, and the operation is safe as long as the len
+    // is the same.
     let scratch = unsafe {
         slice::from_raw_parts_mut(
             stack_array.as_mut_ptr() as *mut MaybeUninit<T>,
@@ -327,8 +329,9 @@ where
         }
 
         // SAFETY: The right side of `v` based on `len_div_2` is guaranteed in-bounds.
-        region =
-            unsafe { &mut *ptr::slice_from_raw_parts_mut(v_base.add(len_div_2), len - len_div_2) };
+        unsafe {
+            region = &mut *ptr::slice_from_raw_parts_mut(v_base.add(len_div_2), len - len_div_2)
+        };
     }
 
     // SAFETY: We checked that T is Freeze and thus observation safe.
@@ -812,14 +815,6 @@ pub(crate) const fn has_efficient_in_place_swap<T>() -> bool {
     mem::size_of::<T>() <= 8 // mem::size_of::<u64>()
 }
 
-#[test]
-fn type_info() {
-    assert!(has_efficient_in_place_swap::<i32>());
-    assert!(has_efficient_in_place_swap::<u64>());
-    assert!(!has_efficient_in_place_swap::<u128>());
-    assert!(!has_efficient_in_place_swap::<String>());
-}
-
 /// SAFETY: Only used for run-time optimization heuristic.
 #[rustc_unsafe_specialization_marker]
 trait CopyMarker {}
diff --git a/library/core/src/slice/sort/stable/quicksort.rs b/library/core/src/slice/sort/stable/quicksort.rs
index b6d6d3ec8ea..e1734ce8d8b 100644
--- a/library/core/src/slice/sort/stable/quicksort.rs
+++ b/library/core/src/slice/sort/stable/quicksort.rs
@@ -256,12 +256,3 @@ const fn has_direct_interior_mutability<T>() -> bool {
     // Otherwise a type like Mutex<Option<Box<str>>> could lead to double free.
     !T::is_freeze()
 }
-
-#[test]
-fn freeze_check() {
-    assert!(!has_direct_interior_mutability::<u32>());
-    assert!(!has_direct_interior_mutability::<[u128; 2]>());
-
-    assert!(has_direct_interior_mutability::<crate::cell::Cell<u32>>());
-    assert!(has_direct_interior_mutability::<crate::sync::Mutex<u32>>());
-}
diff --git a/library/core/src/slice/sort/unstable/quicksort.rs b/library/core/src/slice/sort/unstable/quicksort.rs
index c3b4339d704..533b5b0eec7 100644
--- a/library/core/src/slice/sort/unstable/quicksort.rs
+++ b/library/core/src/slice/sort/unstable/quicksort.rs
@@ -325,6 +325,8 @@ struct GapGuard<T> {
 
 impl<T> Drop for GapGuard<T> {
     fn drop(&mut self) {
+        // SAFETY: `self` MUST be constructed in a way that makes copying the gap value into
+        // `self.pos` sound.
         unsafe {
             ptr::copy_nonoverlapping(&*self.value, self.pos, 1);
         }
@@ -340,6 +342,8 @@ struct GapGuardRaw<T> {
 
 impl<T> Drop for GapGuardRaw<T> {
     fn drop(&mut self) {
+        // SAFETY: `self` MUST be constructed in a way that makes copying the gap value into
+        // `self.pos` sound.
         unsafe {
             ptr::copy_nonoverlapping(self.value, self.pos, 1);
         }
diff --git a/library/core/tests/slice.rs b/library/core/tests/slice.rs
index 3870fcb5f0f..1fe92afb6bb 100644
--- a/library/core/tests/slice.rs
+++ b/library/core/tests/slice.rs
@@ -1803,7 +1803,6 @@ fn brute_force_rotate_test_1() {
 #[test]
 #[cfg(not(target_arch = "wasm32"))]
 fn sort_unstable() {
-    // use core::cmp::Ordering::{Equal, Greater, Less};
     use rand::Rng;
 
     // Miri is too slow (but still need to `chain` to make the types match)
diff --git a/src/doc/unstable-book/src/library-features/sort-internals.md b/src/doc/unstable-book/src/library-features/sort-internals.md
deleted file mode 100644
index 6f2385e5300..00000000000
--- a/src/doc/unstable-book/src/library-features/sort-internals.md
+++ /dev/null
@@ -1,5 +0,0 @@
-# `sort_internals`
-
-This feature is internal to the Rust compiler and is not intended for general use.
-
-------------------------