Improve docs

This commit is contained in:
Ben Kimock 2024-03-09 10:49:26 -05:00
parent 5a93a59fd5
commit 50d0bea153
2 changed files with 10 additions and 8 deletions

View File

@ -2799,6 +2799,10 @@ const fn precondition_check($($name:$ty),*) {
/// Checks whether `ptr` is properly aligned with respect to /// Checks whether `ptr` is properly aligned with respect to
/// `align_of::<T>()`. /// `align_of::<T>()`.
///
/// In `const` this is approximate and can fail spuriously. It is primarily intended
/// for `assert_unsafe_precondition!` with `check_language_ub`, in which case the
/// check is anyway not executed in `const`.
#[inline] #[inline]
pub(crate) const fn is_aligned_and_not_null(ptr: *const (), align: usize) -> bool { pub(crate) const fn is_aligned_and_not_null(ptr: *const (), align: usize) -> bool {
!ptr.is_null() && ptr.is_aligned_to(align) !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 {
/// Checks whether the regions of memory starting at `src` and `dst` of size /// Checks whether the regions of memory starting at `src` and `dst` of size
/// `count * size` do *not* overlap. /// `count * size` do *not* overlap.
/// ///
/// # Safety /// Note that in const-eval this function just returns `true` and therefore must
/// This function must only be called such that if it returns false, we will execute UB. /// only be used with `assert_unsafe_precondition!`, similar to `is_aligned_and_not_null`.
#[inline] #[inline]
pub(crate) const unsafe fn is_nonoverlapping( pub(crate) const fn is_nonoverlapping(
src: *const (), src: *const (),
dst: *const (), dst: *const (),
size: usize, size: usize,
@ -2842,7 +2846,7 @@ const fn comptime(_: *const (), _: *const (), _: usize, _: usize) -> bool {
true true
} }
#[cfg_attr(not(bootstrap), allow(unused_unsafe))] #[cfg_attr(not(bootstrap), allow(unused_unsafe))] // on bootstrap bump, remove unsafe block
// SAFETY: This function's precondition is equivalent to that of `const_eval_select`. // SAFETY: This function's precondition is equivalent to that of `const_eval_select`.
// Programs which do not execute UB will only see this function return `true`, which makes the // Programs which do not execute UB will only see this function return `true`, which makes the
// const and runtime implementation indistinguishable. // const and runtime implementation indistinguishable.
@ -2962,8 +2966,7 @@ const fn comptime(_: *const (), _: *const (), _: usize, _: usize) -> bool {
) => ) =>
is_aligned_and_not_null(src, align) is_aligned_and_not_null(src, align)
&& is_aligned_and_not_null(dst, align) && is_aligned_and_not_null(dst, align)
// SAFETY: If this returns false, we're about to execute UB. && is_nonoverlapping(src, dst, size, count)
&& unsafe { is_nonoverlapping(src, dst, size, count) }
); );
// SAFETY: the safety contract for `copy_nonoverlapping` must be // SAFETY: the safety contract for `copy_nonoverlapping` must be

View File

@ -1032,8 +1032,7 @@ macro_rules! attempt_swap_as_chunks {
) => ) =>
is_aligned_and_not_null(x, align) is_aligned_and_not_null(x, align)
&& is_aligned_and_not_null(y, align) && is_aligned_and_not_null(y, align)
// SAFETY: If this returns false, we're about to execute UB. && is_nonoverlapping(x, y, size, count)
&& unsafe { is_nonoverlapping(x, y, size, count) }
); );
// Split up the slice into small power-of-two-sized chunks that LLVM is able // Split up the slice into small power-of-two-sized chunks that LLVM is able