Skip to content

Commit e35a56d

Browse files
committed
Auto merge of rust-lang#119892 - joboet:libs_use_assert_unchecked, r=Nilstrieb,cuviper
Use `assert_unchecked` instead of `assume` intrinsic in the standard library Now that a public wrapper for the `assume` intrinsic exists, we can use it in the standard library. CC rust-lang#119131
2 parents 0011fac + 638439a commit e35a56d

File tree

17 files changed

+32
-22
lines changed

17 files changed

+32
-22
lines changed

Diff for: library/alloc/src/alloc.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
#![stable(feature = "alloc_module", since = "1.28.0")]
44

55
#[cfg(not(test))]
6-
use core::intrinsics;
6+
use core::hint;
77

88
#[cfg(not(test))]
99
use core::ptr::{self, NonNull};
@@ -208,7 +208,7 @@ impl Global {
208208
let new_size = new_layout.size();
209209

210210
// `realloc` probably checks for `new_size >= old_layout.size()` or something similar.
211-
intrinsics::assume(new_size >= old_layout.size());
211+
hint::assert_unchecked(new_size >= old_layout.size());
212212

213213
let raw_ptr = realloc(ptr.as_ptr(), old_layout, new_size);
214214
let ptr = NonNull::new(raw_ptr).ok_or(AllocError)?;
@@ -299,7 +299,7 @@ unsafe impl Allocator for Global {
299299
// SAFETY: `new_size` is non-zero. Other conditions must be upheld by the caller
300300
new_size if old_layout.align() == new_layout.align() => unsafe {
301301
// `realloc` probably checks for `new_size <= old_layout.size()` or something similar.
302-
intrinsics::assume(new_size <= old_layout.size());
302+
hint::assert_unchecked(new_size <= old_layout.size());
303303

304304
let raw_ptr = realloc(ptr.as_ptr(), old_layout, new_size);
305305
let ptr = NonNull::new(raw_ptr).ok_or(AllocError)?;

Diff for: library/alloc/src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,7 @@
129129
#![feature(fmt_internals)]
130130
#![feature(fn_traits)]
131131
#![feature(hasher_prefixfree_extras)]
132+
#![feature(hint_assert_unchecked)]
132133
#![feature(inline_const)]
133134
#![feature(inplace_iteration)]
134135
#![feature(iter_advance_by)]

Diff for: library/alloc/src/raw_vec.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
use core::alloc::LayoutError;
44
use core::cmp;
5-
use core::intrinsics;
5+
use core::hint;
66
use core::mem::{self, ManuallyDrop, MaybeUninit, SizedTypeProperties};
77
use core::ptr::{self, NonNull, Unique};
88
use core::slice;
@@ -325,7 +325,7 @@ impl<T, A: Allocator> RawVec<T, A> {
325325
}
326326
unsafe {
327327
// Inform the optimizer that the reservation has succeeded or wasn't needed
328-
core::intrinsics::assume(!self.needs_to_grow(len, additional));
328+
hint::assert_unchecked(!self.needs_to_grow(len, additional));
329329
}
330330
Ok(())
331331
}
@@ -363,7 +363,7 @@ impl<T, A: Allocator> RawVec<T, A> {
363363
}
364364
unsafe {
365365
// Inform the optimizer that the reservation has succeeded or wasn't needed
366-
core::intrinsics::assume(!self.needs_to_grow(len, additional));
366+
hint::assert_unchecked(!self.needs_to_grow(len, additional));
367367
}
368368
Ok(())
369369
}
@@ -514,7 +514,7 @@ where
514514
debug_assert_eq!(old_layout.align(), new_layout.align());
515515
unsafe {
516516
// The allocator checks for alignment equality
517-
intrinsics::assume(old_layout.align() == new_layout.align());
517+
hint::assert_unchecked(old_layout.align() == new_layout.align());
518518
alloc.grow(ptr, old_layout, new_layout)
519519
}
520520
} else {

Diff for: library/alloc/src/rc.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,7 @@ use core::cell::Cell;
252252
use core::cmp::Ordering;
253253
use core::fmt;
254254
use core::hash::{Hash, Hasher};
255+
use core::hint;
255256
use core::intrinsics::abort;
256257
#[cfg(not(no_global_oom_handling))]
257258
use core::iter;
@@ -3268,7 +3269,7 @@ trait RcInnerPtr {
32683269
// SAFETY: The reference count will never be zero when this is
32693270
// called.
32703271
unsafe {
3271-
core::intrinsics::assume(strong != 0);
3272+
hint::assert_unchecked(strong != 0);
32723273
}
32733274

32743275
let strong = strong.wrapping_add(1);
@@ -3301,7 +3302,7 @@ trait RcInnerPtr {
33013302
// SAFETY: The reference count will never be zero when this is
33023303
// called.
33033304
unsafe {
3304-
core::intrinsics::assume(weak != 0);
3305+
hint::assert_unchecked(weak != 0);
33053306
}
33063307

33073308
let weak = weak.wrapping_add(1);

Diff for: library/alloc/src/vec/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1996,7 +1996,7 @@ impl<T, A: Allocator> Vec<T, A> {
19961996
} else {
19971997
unsafe {
19981998
self.len -= 1;
1999-
core::intrinsics::assume(self.len < self.capacity());
1999+
core::hint::assert_unchecked(self.len < self.capacity());
20002000
Some(ptr::read(self.as_ptr().add(self.len())))
20012001
}
20022002
}

Diff for: library/core/src/alloc/global.rs

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

Diff for: library/core/src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,7 @@
132132
#![feature(const_fmt_arguments_new)]
133133
#![feature(const_hash)]
134134
#![feature(const_heap)]
135+
#![feature(const_hint_assert_unchecked)]
135136
#![feature(const_index_range_slice_index)]
136137
#![feature(const_int_unchecked_arith)]
137138
#![feature(const_intrinsic_forget)]

Diff for: library/core/src/num/mod.rs

+1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#![stable(feature = "rust1", since = "1.0.0")]
44

55
use crate::ascii;
6+
use crate::hint;
67
use crate::intrinsics;
78
use crate::mem;
89
use crate::ops::{Add, Mul, Sub};

Diff for: library/core/src/num/uint_macros.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -2452,8 +2452,8 @@ macro_rules! uint_impl {
24522452
// SAFETY: the result is positive and fits in an integer with half as many bits.
24532453
// Inform the optimizer about it.
24542454
unsafe {
2455-
intrinsics::assume(0 < res);
2456-
intrinsics::assume(res < 1 << (Self::BITS / 2));
2455+
hint::assert_unchecked(0 < res);
2456+
hint::assert_unchecked(res < 1 << (Self::BITS / 2));
24572457
}
24582458

24592459
res

Diff for: library/core/src/slice/index.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,7 @@ unsafe impl<T> SliceIndex<[T]> for usize {
234234
// `self` is in bounds of `slice` so `self` cannot overflow an `isize`,
235235
// so the call to `add` is safe.
236236
unsafe {
237-
crate::intrinsics::assume(self < slice.len());
237+
crate::hint::assert_unchecked(self < slice.len());
238238
slice.as_ptr().add(self)
239239
}
240240
}

Diff for: library/core/src/slice/iter.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ mod macros;
55

66
use crate::cmp;
77
use crate::fmt;
8-
use crate::intrinsics::assume;
8+
use crate::hint::assert_unchecked;
99
use crate::iter::{
1010
FusedIterator, TrustedLen, TrustedRandomAccess, TrustedRandomAccessNoCoerce, UncheckedIterator,
1111
};

Diff for: library/core/src/slice/iter/macros.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -338,7 +338,7 @@ macro_rules! iterator {
338338
if predicate(x) {
339339
// SAFETY: we are guaranteed to be in bounds by the loop invariant:
340340
// when `i >= n`, `self.next()` returns `None` and the loop breaks.
341-
unsafe { assume(i < n) };
341+
unsafe { assert_unchecked(i < n) };
342342
return Some(i);
343343
}
344344
i += 1;
@@ -361,7 +361,7 @@ macro_rules! iterator {
361361
if predicate(x) {
362362
// SAFETY: `i` must be lower than `n` since it starts at `n`
363363
// and is only decreasing.
364-
unsafe { assume(i < n) };
364+
unsafe { assert_unchecked(i < n) };
365365
return Some(i);
366366
}
367367
}

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

+3-2
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
use crate::cmp::Ordering::{self, Equal, Greater, Less};
1010
use crate::fmt;
11+
use crate::hint;
1112
use crate::intrinsics::exact_div;
1213
use crate::mem::{self, SizedTypeProperties};
1314
use crate::num::NonZeroUsize;
@@ -2799,7 +2800,7 @@ impl<T> [T] {
27992800
right = if cmp == Greater { mid } else { right };
28002801
if cmp == Equal {
28012802
// SAFETY: same as the `get_unchecked` above
2802-
unsafe { crate::intrinsics::assume(mid < self.len()) };
2803+
unsafe { hint::assert_unchecked(mid < self.len()) };
28032804
return Ok(mid);
28042805
}
28052806

@@ -2808,7 +2809,7 @@ impl<T> [T] {
28082809

28092810
// SAFETY: directly true from the overall invariant.
28102811
// Note that this is `<=`, unlike the assume in the `Ok` path.
2811-
unsafe { crate::intrinsics::assume(left <= self.len()) };
2812+
unsafe { hint::assert_unchecked(left <= self.len()) };
28122813
Err(left)
28132814
}
28142815

Diff for: library/std/src/alloc.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@
5656
#![deny(unsafe_op_in_unsafe_fn)]
5757
#![stable(feature = "alloc_module", since = "1.28.0")]
5858

59-
use core::intrinsics;
59+
use core::hint;
6060
use core::ptr::NonNull;
6161
use core::sync::atomic::{AtomicPtr, Ordering};
6262
use core::{mem, ptr};
@@ -172,7 +172,7 @@ impl System {
172172
let new_size = new_layout.size();
173173

174174
// `realloc` probably checks for `new_size >= old_layout.size()` or something similar.
175-
intrinsics::assume(new_size >= old_layout.size());
175+
hint::assert_unchecked(new_size >= old_layout.size());
176176

177177
let raw_ptr = GlobalAlloc::realloc(self, ptr.as_ptr(), old_layout, new_size);
178178
let ptr = NonNull::new(raw_ptr).ok_or(AllocError)?;
@@ -264,7 +264,7 @@ unsafe impl Allocator for System {
264264
// SAFETY: `new_size` is non-zero. Other conditions must be upheld by the caller
265265
new_size if old_layout.align() == new_layout.align() => unsafe {
266266
// `realloc` probably checks for `new_size <= old_layout.size()` or something similar.
267-
intrinsics::assume(new_size <= old_layout.size());
267+
hint::assert_unchecked(new_size <= old_layout.size());
268268

269269
let raw_ptr = GlobalAlloc::realloc(self, ptr.as_ptr(), old_layout, new_size);
270270
let ptr = NonNull::new(raw_ptr).ok_or(AllocError)?;

Diff for: library/std/src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -325,6 +325,7 @@
325325
#![feature(float_next_up_down)]
326326
#![feature(hasher_prefixfree_extras)]
327327
#![feature(hashmap_internals)]
328+
#![feature(hint_assert_unchecked)]
328329
#![feature(ip)]
329330
#![feature(maybe_uninit_slice)]
330331
#![feature(maybe_uninit_uninit_array)]

Diff for: tests/codegen/vec-reserve-extend.rs

+2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
// compile-flags: -O
2+
// ignore-debug
3+
// (with debug assertions turned on, `assert_unchecked` generates a real assertion)
24

35
#![crate_type = "lib"]
46

Diff for: tests/codegen/vec_pop_push_noop.rs

+2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
// compile-flags: -O
2+
// ignore-debug
3+
// (with debug assertions turned on, `assert_unchecked` generates a real assertion)
24

35
#![crate_type = "lib"]
46

0 commit comments

Comments
 (0)