Skip to content

Commit a3b663f

Browse files
committed
const-eval: full support for pointer fragments
1 parent b671d55 commit a3b663f

File tree

2 files changed

+5
-45
lines changed

2 files changed

+5
-45
lines changed

core/src/ptr/mod.rs

Lines changed: 1 addition & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1345,40 +1345,6 @@ pub const unsafe fn swap<T>(x: *mut T, y: *mut T) {
13451345
/// assert_eq!(x, [7, 8, 3, 4]);
13461346
/// assert_eq!(y, [1, 2, 9]);
13471347
/// ```
1348-
///
1349-
/// # Const evaluation limitations
1350-
///
1351-
/// If this function is invoked during const-evaluation, the current implementation has a small (and
1352-
/// rarely relevant) limitation: if `count` is at least 2 and the data pointed to by `x` or `y`
1353-
/// contains a pointer that crosses the boundary of two `T`-sized chunks of memory, the function may
1354-
/// fail to evaluate (similar to a panic during const-evaluation). This behavior may change in the
1355-
/// future.
1356-
///
1357-
/// The limitation is illustrated by the following example:
1358-
///
1359-
/// ```
1360-
/// use std::mem::size_of;
1361-
/// use std::ptr;
1362-
///
1363-
/// const { unsafe {
1364-
/// const PTR_SIZE: usize = size_of::<*const i32>();
1365-
/// let mut data1 = [0u8; PTR_SIZE];
1366-
/// let mut data2 = [0u8; PTR_SIZE];
1367-
/// // Store a pointer in `data1`.
1368-
/// data1.as_mut_ptr().cast::<*const i32>().write_unaligned(&42);
1369-
/// // Swap the contents of `data1` and `data2` by swapping `PTR_SIZE` many `u8`-sized chunks.
1370-
/// // This call will fail, because the pointer in `data1` crosses the boundary
1371-
/// // between several of the 1-byte chunks that are being swapped here.
1372-
/// //ptr::swap_nonoverlapping(data1.as_mut_ptr(), data2.as_mut_ptr(), PTR_SIZE);
1373-
/// // Swap the contents of `data1` and `data2` by swapping a single chunk of size
1374-
/// // `[u8; PTR_SIZE]`. That works, as there is no pointer crossing the boundary between
1375-
/// // two chunks.
1376-
/// ptr::swap_nonoverlapping(&mut data1, &mut data2, 1);
1377-
/// // Read the pointer from `data2` and dereference it.
1378-
/// let ptr = data2.as_ptr().cast::<*const i32>().read_unaligned();
1379-
/// assert!(*ptr == 42);
1380-
/// } }
1381-
/// ```
13821348
#[inline]
13831349
#[stable(feature = "swap_nonoverlapping", since = "1.27.0")]
13841350
#[rustc_const_stable(feature = "const_swap_nonoverlapping", since = "1.88.0")]
@@ -1407,9 +1373,7 @@ pub const unsafe fn swap_nonoverlapping<T>(x: *mut T, y: *mut T, count: usize) {
14071373
const_eval_select!(
14081374
@capture[T] { x: *mut T, y: *mut T, count: usize }:
14091375
if const {
1410-
// At compile-time we want to always copy this in chunks of `T`, to ensure that if there
1411-
// are pointers inside `T` we will copy them in one go rather than trying to copy a part
1412-
// of a pointer (which would not work).
1376+
// At compile-time we don't need all the special code below.
14131377
// SAFETY: Same preconditions as this function
14141378
unsafe { swap_nonoverlapping_const(x, y, count) }
14151379
} else {

coretests/tests/ptr.rs

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -936,22 +936,18 @@ fn test_const_swap_ptr() {
936936
assert!(*s1.0.ptr == 666);
937937
assert!(*s2.0.ptr == 1);
938938

939-
// Swap them back, again as an array.
939+
// Swap them back, byte-for-byte
940940
unsafe {
941941
ptr::swap_nonoverlapping(
942-
ptr::from_mut(&mut s1).cast::<T>(),
943-
ptr::from_mut(&mut s2).cast::<T>(),
944-
1,
942+
ptr::from_mut(&mut s1).cast::<u8>(),
943+
ptr::from_mut(&mut s2).cast::<u8>(),
944+
size_of::<A>(),
945945
);
946946
}
947947

948948
// Make sure they still work.
949949
assert!(*s1.0.ptr == 1);
950950
assert!(*s2.0.ptr == 666);
951-
952-
// This is where we'd swap again using a `u8` type and a `count` of `size_of::<T>()` if it
953-
// were not for the limitation of `swap_nonoverlapping` around pointers crossing multiple
954-
// elements.
955951
};
956952
}
957953

0 commit comments

Comments
 (0)