Skip to content

Commit 50d0bea

Browse files
committed
Improve docs
1 parent 5a93a59 commit 50d0bea

File tree

2 files changed

+10
-8
lines changed

2 files changed

+10
-8
lines changed

library/core/src/intrinsics.rs

+9-6
Original file line numberDiff line numberDiff line change
@@ -2799,6 +2799,10 @@ pub(crate) use assert_unsafe_precondition;
27992799

28002800
/// Checks whether `ptr` is properly aligned with respect to
28012801
/// `align_of::<T>()`.
2802+
///
2803+
/// In `const` this is approximate and can fail spuriously. It is primarily intended
2804+
/// for `assert_unsafe_precondition!` with `check_language_ub`, in which case the
2805+
/// check is anyway not executed in `const`.
28022806
#[inline]
28032807
pub(crate) const fn is_aligned_and_not_null(ptr: *const (), align: usize) -> bool {
28042808
!ptr.is_null() && ptr.is_aligned_to(align)
@@ -2813,10 +2817,10 @@ pub(crate) const fn is_valid_allocation_size(size: usize, len: usize) -> bool {
28132817
/// Checks whether the regions of memory starting at `src` and `dst` of size
28142818
/// `count * size` do *not* overlap.
28152819
///
2816-
/// # Safety
2817-
/// This function must only be called such that if it returns false, we will execute UB.
2820+
/// Note that in const-eval this function just returns `true` and therefore must
2821+
/// only be used with `assert_unsafe_precondition!`, similar to `is_aligned_and_not_null`.
28182822
#[inline]
2819-
pub(crate) const unsafe fn is_nonoverlapping(
2823+
pub(crate) const fn is_nonoverlapping(
28202824
src: *const (),
28212825
dst: *const (),
28222826
size: usize,
@@ -2842,7 +2846,7 @@ pub(crate) const unsafe fn is_nonoverlapping(
28422846
true
28432847
}
28442848

2845-
#[cfg_attr(not(bootstrap), allow(unused_unsafe))]
2849+
#[cfg_attr(not(bootstrap), allow(unused_unsafe))] // on bootstrap bump, remove unsafe block
28462850
// SAFETY: This function's precondition is equivalent to that of `const_eval_select`.
28472851
// Programs which do not execute UB will only see this function return `true`, which makes the
28482852
// const and runtime implementation indistinguishable.
@@ -2962,8 +2966,7 @@ pub const unsafe fn copy_nonoverlapping<T>(src: *const T, dst: *mut T, count: us
29622966
) =>
29632967
is_aligned_and_not_null(src, align)
29642968
&& is_aligned_and_not_null(dst, align)
2965-
// SAFETY: If this returns false, we're about to execute UB.
2966-
&& unsafe { is_nonoverlapping(src, dst, size, count) }
2969+
&& is_nonoverlapping(src, dst, size, count)
29672970
);
29682971

29692972
// SAFETY: the safety contract for `copy_nonoverlapping` must be

library/core/src/ptr/mod.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -1032,8 +1032,7 @@ pub const unsafe fn swap_nonoverlapping<T>(x: *mut T, y: *mut T, count: usize) {
10321032
) =>
10331033
is_aligned_and_not_null(x, align)
10341034
&& is_aligned_and_not_null(y, align)
1035-
// SAFETY: If this returns false, we're about to execute UB.
1036-
&& unsafe { is_nonoverlapping(x, y, size, count) }
1035+
&& is_nonoverlapping(x, y, size, count)
10371036
);
10381037

10391038
// Split up the slice into small power-of-two-sized chunks that LLVM is able

0 commit comments

Comments
 (0)