1
Fork 0

libs: use assert_unchecked instead of intrinsic

This commit is contained in:
joboet 2024-01-13 20:10:00 +01:00
parent 174e73a3f6
commit fa9a911a57
No known key found for this signature in database
GPG key ID: 704E0149B0194B3C
15 changed files with 28 additions and 22 deletions

View file

@ -3,7 +3,7 @@
#![stable(feature = "alloc_module", since = "1.28.0")] #![stable(feature = "alloc_module", since = "1.28.0")]
#[cfg(not(test))] #[cfg(not(test))]
use core::intrinsics; use core::hint;
#[cfg(not(test))] #[cfg(not(test))]
use core::ptr::{self, NonNull}; use core::ptr::{self, NonNull};
@ -208,7 +208,7 @@ impl Global {
let new_size = new_layout.size(); let new_size = new_layout.size();
// `realloc` probably checks for `new_size >= old_layout.size()` or something similar. // `realloc` probably checks for `new_size >= old_layout.size()` or something similar.
intrinsics::assume(new_size >= old_layout.size()); hint::assert_unchecked(new_size >= old_layout.size());
let raw_ptr = realloc(ptr.as_ptr(), old_layout, new_size); let raw_ptr = realloc(ptr.as_ptr(), old_layout, new_size);
let ptr = NonNull::new(raw_ptr).ok_or(AllocError)?; let ptr = NonNull::new(raw_ptr).ok_or(AllocError)?;
@ -299,7 +299,7 @@ unsafe impl Allocator for Global {
// SAFETY: `new_size` is non-zero. Other conditions must be upheld by the caller // SAFETY: `new_size` is non-zero. Other conditions must be upheld by the caller
new_size if old_layout.align() == new_layout.align() => unsafe { new_size if old_layout.align() == new_layout.align() => unsafe {
// `realloc` probably checks for `new_size <= old_layout.size()` or something similar. // `realloc` probably checks for `new_size <= old_layout.size()` or something similar.
intrinsics::assume(new_size <= old_layout.size()); hint::assert_unchecked(new_size <= old_layout.size());
let raw_ptr = realloc(ptr.as_ptr(), old_layout, new_size); let raw_ptr = realloc(ptr.as_ptr(), old_layout, new_size);
let ptr = NonNull::new(raw_ptr).ok_or(AllocError)?; let ptr = NonNull::new(raw_ptr).ok_or(AllocError)?;

View file

@ -129,6 +129,7 @@
#![feature(fmt_internals)] #![feature(fmt_internals)]
#![feature(fn_traits)] #![feature(fn_traits)]
#![feature(hasher_prefixfree_extras)] #![feature(hasher_prefixfree_extras)]
#![feature(hint_assert_unchecked)]
#![feature(inline_const)] #![feature(inline_const)]
#![feature(inplace_iteration)] #![feature(inplace_iteration)]
#![feature(iter_advance_by)] #![feature(iter_advance_by)]

View file

@ -2,7 +2,7 @@
use core::alloc::LayoutError; use core::alloc::LayoutError;
use core::cmp; use core::cmp;
use core::intrinsics; use core::hint;
use core::mem::{self, ManuallyDrop, MaybeUninit, SizedTypeProperties}; use core::mem::{self, ManuallyDrop, MaybeUninit, SizedTypeProperties};
use core::ptr::{self, NonNull, Unique}; use core::ptr::{self, NonNull, Unique};
use core::slice; use core::slice;
@ -325,7 +325,7 @@ impl<T, A: Allocator> RawVec<T, A> {
} }
unsafe { unsafe {
// Inform the optimizer that the reservation has succeeded or wasn't needed // Inform the optimizer that the reservation has succeeded or wasn't needed
core::intrinsics::assume(!self.needs_to_grow(len, additional)); hint::assert_unchecked(!self.needs_to_grow(len, additional));
} }
Ok(()) Ok(())
} }
@ -363,7 +363,7 @@ impl<T, A: Allocator> RawVec<T, A> {
} }
unsafe { unsafe {
// Inform the optimizer that the reservation has succeeded or wasn't needed // Inform the optimizer that the reservation has succeeded or wasn't needed
core::intrinsics::assume(!self.needs_to_grow(len, additional)); hint::assert_unchecked(!self.needs_to_grow(len, additional));
} }
Ok(()) Ok(())
} }
@ -514,7 +514,7 @@ where
debug_assert_eq!(old_layout.align(), new_layout.align()); debug_assert_eq!(old_layout.align(), new_layout.align());
unsafe { unsafe {
// The allocator checks for alignment equality // The allocator checks for alignment equality
intrinsics::assume(old_layout.align() == new_layout.align()); hint::assert_unchecked(old_layout.align() == new_layout.align());
alloc.grow(ptr, old_layout, new_layout) alloc.grow(ptr, old_layout, new_layout)
} }
} else { } else {

View file

@ -252,6 +252,7 @@ use core::cell::Cell;
use core::cmp::Ordering; use core::cmp::Ordering;
use core::fmt; use core::fmt;
use core::hash::{Hash, Hasher}; use core::hash::{Hash, Hasher};
use core::hint;
use core::intrinsics::abort; use core::intrinsics::abort;
#[cfg(not(no_global_oom_handling))] #[cfg(not(no_global_oom_handling))]
use core::iter; use core::iter;
@ -3268,7 +3269,7 @@ trait RcInnerPtr {
// SAFETY: The reference count will never be zero when this is // SAFETY: The reference count will never be zero when this is
// called. // called.
unsafe { unsafe {
core::intrinsics::assume(strong != 0); hint::assert_unchecked(strong != 0);
} }
let strong = strong.wrapping_add(1); let strong = strong.wrapping_add(1);
@ -3301,7 +3302,7 @@ trait RcInnerPtr {
// SAFETY: The reference count will never be zero when this is // SAFETY: The reference count will never be zero when this is
// called. // called.
unsafe { unsafe {
core::intrinsics::assume(weak != 0); hint::assert_unchecked(weak != 0);
} }
let weak = weak.wrapping_add(1); let weak = weak.wrapping_add(1);

View file

@ -1993,7 +1993,7 @@ impl<T, A: Allocator> Vec<T, A> {
} else { } else {
unsafe { unsafe {
self.len -= 1; self.len -= 1;
core::intrinsics::assume(self.len < self.capacity()); core::hint::assert_unchecked(self.len < self.capacity());
Some(ptr::read(self.as_ptr().add(self.len()))) Some(ptr::read(self.as_ptr().add(self.len())))
} }
} }

View file

@ -110,7 +110,7 @@ use crate::ptr;
/// ```rust,ignore (unsound and has placeholders) /// ```rust,ignore (unsound and has placeholders)
/// drop(Box::new(42)); /// drop(Box::new(42));
/// let number_of_heap_allocs = /* call private allocator API */; /// let number_of_heap_allocs = /* call private allocator API */;
/// unsafe { std::intrinsics::assume(number_of_heap_allocs > 0); } /// unsafe { std::hint::assert_unchecked(number_of_heap_allocs > 0); }
/// ``` /// ```
/// ///
/// Note that the optimizations mentioned above are not the only /// Note that the optimizations mentioned above are not the only

View file

@ -131,6 +131,7 @@
#![feature(const_fmt_arguments_new)] #![feature(const_fmt_arguments_new)]
#![feature(const_hash)] #![feature(const_hash)]
#![feature(const_heap)] #![feature(const_heap)]
#![feature(const_hint_assert_unchecked)]
#![feature(const_index_range_slice_index)] #![feature(const_index_range_slice_index)]
#![feature(const_int_unchecked_arith)] #![feature(const_int_unchecked_arith)]
#![feature(const_intrinsic_forget)] #![feature(const_intrinsic_forget)]

View file

@ -3,6 +3,7 @@
#![stable(feature = "rust1", since = "1.0.0")] #![stable(feature = "rust1", since = "1.0.0")]
use crate::ascii; use crate::ascii;
use crate::hint;
use crate::intrinsics; use crate::intrinsics;
use crate::mem; use crate::mem;
use crate::ops::{Add, Mul, Sub}; use crate::ops::{Add, Mul, Sub};

View file

@ -2036,8 +2036,8 @@ macro_rules! uint_impl {
// SAFETY: the result is positive and fits in an integer with half as many bits. // SAFETY: the result is positive and fits in an integer with half as many bits.
// Inform the optimizer about it. // Inform the optimizer about it.
unsafe { unsafe {
intrinsics::assume(0 < res); hint::assert_unchecked(0 < res);
intrinsics::assume(res < 1 << (Self::BITS / 2)); hint::assert_unchecked(res < 1 << (Self::BITS / 2));
} }
res res

View file

@ -234,7 +234,7 @@ unsafe impl<T> SliceIndex<[T]> for usize {
// `self` is in bounds of `slice` so `self` cannot overflow an `isize`, // `self` is in bounds of `slice` so `self` cannot overflow an `isize`,
// so the call to `add` is safe. // so the call to `add` is safe.
unsafe { unsafe {
crate::intrinsics::assume(self < slice.len()); crate::hint::assert_unchecked(self < slice.len());
slice.as_ptr().add(self) slice.as_ptr().add(self)
} }
} }

View file

@ -6,7 +6,7 @@ mod macros;
use crate::cmp; use crate::cmp;
use crate::cmp::Ordering; use crate::cmp::Ordering;
use crate::fmt; use crate::fmt;
use crate::intrinsics::assume; use crate::hint::assert_unchecked;
use crate::iter::{ use crate::iter::{
FusedIterator, TrustedLen, TrustedRandomAccess, TrustedRandomAccessNoCoerce, UncheckedIterator, FusedIterator, TrustedLen, TrustedRandomAccess, TrustedRandomAccessNoCoerce, UncheckedIterator,
}; };

View file

@ -338,7 +338,7 @@ macro_rules! iterator {
if predicate(x) { if predicate(x) {
// SAFETY: we are guaranteed to be in bounds by the loop invariant: // SAFETY: we are guaranteed to be in bounds by the loop invariant:
// when `i >= n`, `self.next()` returns `None` and the loop breaks. // when `i >= n`, `self.next()` returns `None` and the loop breaks.
unsafe { assume(i < n) }; unsafe { assert_unchecked(i < n) };
return Some(i); return Some(i);
} }
i += 1; i += 1;
@ -361,7 +361,7 @@ macro_rules! iterator {
if predicate(x) { if predicate(x) {
// SAFETY: `i` must be lower than `n` since it starts at `n` // SAFETY: `i` must be lower than `n` since it starts at `n`
// and is only decreasing. // and is only decreasing.
unsafe { assume(i < n) }; unsafe { assert_unchecked(i < n) };
return Some(i); return Some(i);
} }
} }

View file

@ -8,6 +8,7 @@
use crate::cmp::Ordering::{self, Equal, Greater, Less}; use crate::cmp::Ordering::{self, Equal, Greater, Less};
use crate::fmt; use crate::fmt;
use crate::hint;
use crate::intrinsics::exact_div; use crate::intrinsics::exact_div;
use crate::mem::{self, SizedTypeProperties}; use crate::mem::{self, SizedTypeProperties};
use crate::num::NonZeroUsize; use crate::num::NonZeroUsize;
@ -2850,7 +2851,7 @@ impl<T> [T] {
right = if cmp == Greater { mid } else { right }; right = if cmp == Greater { mid } else { right };
if cmp == Equal { if cmp == Equal {
// SAFETY: same as the `get_unchecked` above // SAFETY: same as the `get_unchecked` above
unsafe { crate::intrinsics::assume(mid < self.len()) }; unsafe { hint::assert_unchecked(mid < self.len()) };
return Ok(mid); return Ok(mid);
} }
@ -2859,7 +2860,7 @@ impl<T> [T] {
// SAFETY: directly true from the overall invariant. // SAFETY: directly true from the overall invariant.
// Note that this is `<=`, unlike the assume in the `Ok` path. // Note that this is `<=`, unlike the assume in the `Ok` path.
unsafe { crate::intrinsics::assume(left <= self.len()) }; unsafe { hint::assert_unchecked(left <= self.len()) };
Err(left) Err(left)
} }

View file

@ -56,7 +56,7 @@
#![deny(unsafe_op_in_unsafe_fn)] #![deny(unsafe_op_in_unsafe_fn)]
#![stable(feature = "alloc_module", since = "1.28.0")] #![stable(feature = "alloc_module", since = "1.28.0")]
use core::intrinsics; use core::hint;
use core::ptr::NonNull; use core::ptr::NonNull;
use core::sync::atomic::{AtomicPtr, Ordering}; use core::sync::atomic::{AtomicPtr, Ordering};
use core::{mem, ptr}; use core::{mem, ptr};
@ -172,7 +172,7 @@ impl System {
let new_size = new_layout.size(); let new_size = new_layout.size();
// `realloc` probably checks for `new_size >= old_layout.size()` or something similar. // `realloc` probably checks for `new_size >= old_layout.size()` or something similar.
intrinsics::assume(new_size >= old_layout.size()); hint::assert_unchecked(new_size >= old_layout.size());
let raw_ptr = GlobalAlloc::realloc(self, ptr.as_ptr(), old_layout, new_size); let raw_ptr = GlobalAlloc::realloc(self, ptr.as_ptr(), old_layout, new_size);
let ptr = NonNull::new(raw_ptr).ok_or(AllocError)?; let ptr = NonNull::new(raw_ptr).ok_or(AllocError)?;
@ -264,7 +264,7 @@ unsafe impl Allocator for System {
// SAFETY: `new_size` is non-zero. Other conditions must be upheld by the caller // SAFETY: `new_size` is non-zero. Other conditions must be upheld by the caller
new_size if old_layout.align() == new_layout.align() => unsafe { new_size if old_layout.align() == new_layout.align() => unsafe {
// `realloc` probably checks for `new_size <= old_layout.size()` or something similar. // `realloc` probably checks for `new_size <= old_layout.size()` or something similar.
intrinsics::assume(new_size <= old_layout.size()); hint::assert_unchecked(new_size <= old_layout.size());
let raw_ptr = GlobalAlloc::realloc(self, ptr.as_ptr(), old_layout, new_size); let raw_ptr = GlobalAlloc::realloc(self, ptr.as_ptr(), old_layout, new_size);
let ptr = NonNull::new(raw_ptr).ok_or(AllocError)?; let ptr = NonNull::new(raw_ptr).ok_or(AllocError)?;

View file

@ -325,6 +325,7 @@
#![feature(float_next_up_down)] #![feature(float_next_up_down)]
#![feature(hasher_prefixfree_extras)] #![feature(hasher_prefixfree_extras)]
#![feature(hashmap_internals)] #![feature(hashmap_internals)]
#![feature(hint_assert_unchecked)]
#![feature(ip)] #![feature(ip)]
#![feature(maybe_uninit_slice)] #![feature(maybe_uninit_slice)]
#![feature(maybe_uninit_uninit_array)] #![feature(maybe_uninit_uninit_array)]