Skip to content

Commit de0ae42

Browse files
matthiaskrgrgitbot
authored and
gitbot
committed
Rollup merge of rust-lang#133669 - RalfJung:const_swap_splitup, r=dtolnay
Move some functions out of const_swap feature gate - `swap_unchecked` is still unstable as a regular fn, so that feature gate can also cover its constness. - `swap_nonoverlapping` isn't ready to be stabilized yet, so make it a different feature gate. Part of rust-lang#83163, rust-lang#88539, rust-lang#133668
2 parents 23f7ead + fe08c18 commit de0ae42

File tree

4 files changed

+23
-3
lines changed

4 files changed

+23
-3
lines changed

Diff for: core/src/ptr/mod.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1009,6 +1009,7 @@ pub const fn slice_from_raw_parts_mut<T>(data: *mut T, len: usize) -> *mut [T] {
10091009
#[stable(feature = "rust1", since = "1.0.0")]
10101010
#[rustc_const_unstable(feature = "const_swap", issue = "83163")]
10111011
#[rustc_diagnostic_item = "ptr_swap"]
1012+
#[rustc_const_stable_indirect]
10121013
pub const unsafe fn swap<T>(x: *mut T, y: *mut T) {
10131014
// Give ourselves some scratch space to work with.
10141015
// We do not have to worry about drops: `MaybeUninit` does nothing when dropped.
@@ -1069,7 +1070,7 @@ pub const unsafe fn swap<T>(x: *mut T, y: *mut T) {
10691070
/// ```
10701071
#[inline]
10711072
#[stable(feature = "swap_nonoverlapping", since = "1.27.0")]
1072-
#[rustc_const_unstable(feature = "const_swap", issue = "83163")]
1073+
#[rustc_const_unstable(feature = "const_swap_nonoverlapping", issue = "133668")]
10731074
#[rustc_diagnostic_item = "ptr_swap_nonoverlapping"]
10741075
pub const unsafe fn swap_nonoverlapping<T>(x: *mut T, y: *mut T, count: usize) {
10751076
#[allow(unused)]
@@ -1129,7 +1130,6 @@ pub const unsafe fn swap_nonoverlapping<T>(x: *mut T, y: *mut T, count: usize) {
11291130
/// LLVM can vectorize this (at least it can for the power-of-two-sized types
11301131
/// `swap_nonoverlapping` tries to use) so no need to manually SIMD it.
11311132
#[inline]
1132-
#[rustc_const_unstable(feature = "const_swap", issue = "83163")]
11331133
const unsafe fn swap_nonoverlapping_simple_untyped<T>(x: *mut T, y: *mut T, count: usize) {
11341134
let x = x.cast::<MaybeUninit<T>>();
11351135
let y = y.cast::<MaybeUninit<T>>();

Diff for: core/src/slice/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -959,7 +959,7 @@ impl<T> [T] {
959959
/// [`swap`]: slice::swap
960960
/// [undefined behavior]: https://doc.rust-lang.org/reference/behavior-considered-undefined.html
961961
#[unstable(feature = "slice_swap_unchecked", issue = "88539")]
962-
#[rustc_const_unstable(feature = "const_swap", issue = "83163")]
962+
#[rustc_const_unstable(feature = "slice_swap_unchecked", issue = "88539")]
963963
pub const unsafe fn swap_unchecked(&mut self, a: usize, b: usize) {
964964
assert_unsafe_precondition!(
965965
check_library_ub,

Diff for: core/tests/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#![feature(const_eval_select)]
1919
#![feature(const_heap)]
2020
#![feature(const_nonnull_new)]
21+
#![feature(const_swap)]
2122
#![feature(const_trait_impl)]
2223
#![feature(core_intrinsics)]
2324
#![feature(core_io_borrowed_buf)]

Diff for: core/tests/ptr.rs

+19
Original file line numberDiff line numberDiff line change
@@ -897,6 +897,25 @@ fn test_const_copy() {
897897
};
898898
}
899899

900+
#[test]
901+
fn test_const_swap() {
902+
const {
903+
let mut ptr1 = &1;
904+
let mut ptr2 = &666;
905+
906+
// Swap ptr1 and ptr2, bytewise. `swap` does not take a count
907+
// so the best we can do is use an array.
908+
type T = [u8; mem::size_of::<&i32>()];
909+
unsafe {
910+
ptr::swap(ptr::from_mut(&mut ptr1).cast::<T>(), ptr::from_mut(&mut ptr2).cast::<T>());
911+
}
912+
913+
// Make sure they still work.
914+
assert!(*ptr1 == 666);
915+
assert!(*ptr2 == 1);
916+
};
917+
}
918+
900919
#[test]
901920
fn test_null_array_as_slice() {
902921
let arr: *mut [u8; 4] = null_mut();

0 commit comments

Comments
 (0)