Skip to content

Commit 89d3b04

Browse files
committed
Convert many assert_unsafe_precondition to debug_assert_nounwind
1 parent 1372f5b commit 89d3b04

File tree

7 files changed

+98
-132
lines changed

7 files changed

+98
-132
lines changed

library/core/src/hint.rs

+5-4
Original file line numberDiff line numberDiff line change
@@ -98,12 +98,13 @@ use crate::intrinsics;
9898
#[rustc_const_stable(feature = "const_unreachable_unchecked", since = "1.57.0")]
9999
#[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces
100100
pub const unsafe fn unreachable_unchecked() -> ! {
101+
crate::panic::debug_assert_nounwind!(
102+
false,
103+
"hint::unreachable_unchecked must never be reached"
104+
);
101105
// SAFETY: the safety contract for `intrinsics::unreachable` must
102106
// be upheld by the caller.
103-
unsafe {
104-
intrinsics::assert_unsafe_precondition!("hint::unreachable_unchecked must never be reached", () => false);
105-
intrinsics::unreachable()
106-
}
107+
unsafe { intrinsics::unreachable() }
107108
}
108109

109110
/// Emits a machine instruction to signal the processor that it is running in

library/core/src/num/nonzero.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -61,12 +61,12 @@ macro_rules! nonzero_integers {
6161
#[must_use]
6262
#[inline]
6363
pub const unsafe fn new_unchecked(n: $Int) -> Self {
64+
crate::panic::debug_assert_nounwind!(
65+
n != 0,
66+
concat!(stringify!($Ty), "::new_unchecked requires a non-zero argument")
67+
);
6468
// SAFETY: this is guaranteed to be safe by the caller.
6569
unsafe {
66-
core::intrinsics::assert_unsafe_precondition!(
67-
concat!(stringify!($Ty), "::new_unchecked requires a non-zero argument"),
68-
(n: $Int) => n != 0
69-
);
7070
Self(n)
7171
}
7272
}

library/core/src/ops/index_range.rs

+5-8
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::intrinsics::{assert_unsafe_precondition, unchecked_add, unchecked_sub};
1+
use crate::intrinsics::{unchecked_add, unchecked_sub};
22
use crate::iter::{FusedIterator, TrustedLen};
33
use crate::num::NonZeroUsize;
44

@@ -19,13 +19,10 @@ impl IndexRange {
1919
/// - `start <= end`
2020
#[inline]
2121
pub const unsafe fn new_unchecked(start: usize, end: usize) -> Self {
22-
// SAFETY: comparisons on usize are pure
23-
unsafe {
24-
assert_unsafe_precondition!(
25-
"IndexRange::new_unchecked requires `start <= end`",
26-
(start: usize, end: usize) => start <= end
27-
)
28-
};
22+
crate::panic::debug_assert_nounwind!(
23+
start <= end,
24+
"IndexRange::new_unchecked requires `start <= end`"
25+
);
2926
IndexRange { start, end }
3027
}
3128

library/core/src/ptr/alignment.rs

+4-8
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
use crate::convert::{TryFrom, TryInto};
2-
use crate::intrinsics::assert_unsafe_precondition;
32
use crate::num::NonZeroUsize;
43
use crate::{cmp, fmt, hash, mem, num};
54

@@ -76,13 +75,10 @@ impl Alignment {
7675
#[rustc_const_unstable(feature = "ptr_alignment_type", issue = "102070")]
7776
#[inline]
7877
pub const unsafe fn new_unchecked(align: usize) -> Self {
79-
// SAFETY: Precondition passed to the caller.
80-
unsafe {
81-
assert_unsafe_precondition!(
82-
"Alignment::new_unchecked requires a power of two",
83-
(align: usize) => align.is_power_of_two()
84-
)
85-
};
78+
crate::panic::debug_assert_nounwind!(
79+
align.is_power_of_two(),
80+
"Alignment::new_unchecked requires a power of two"
81+
);
8682

8783
// SAFETY: By precondition, this must be a power of two, and
8884
// our variants encompass all possible powers of two.

library/core/src/slice/index.rs

+29-46
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
//! Indexing implementations for `[T]`.
22
3-
use crate::intrinsics::assert_unsafe_precondition;
43
use crate::intrinsics::const_eval_select;
54
use crate::intrinsics::unchecked_sub;
65
use crate::ops;
6+
use crate::panic::debug_assert_nounwind;
77
use crate::ptr;
88

99
#[stable(feature = "rust1", since = "1.0.0")]
@@ -228,31 +228,25 @@ unsafe impl<T> const SliceIndex<[T]> for usize {
228228

229229
#[inline]
230230
unsafe fn get_unchecked(self, slice: *const [T]) -> *const T {
231-
let this = self;
231+
debug_assert_nounwind!(
232+
self < slice.len(),
233+
"slice::get_unchecked requires that the index is within the slice",
234+
);
232235
// SAFETY: the caller guarantees that `slice` is not dangling, so it
233236
// cannot be longer than `isize::MAX`. They also guarantee that
234237
// `self` is in bounds of `slice` so `self` cannot overflow an `isize`,
235238
// so the call to `add` is safe.
236-
unsafe {
237-
assert_unsafe_precondition!(
238-
"slice::get_unchecked requires that the index is within the slice",
239-
[T](this: usize, slice: *const [T]) => this < slice.len()
240-
);
241-
slice.as_ptr().add(self)
242-
}
239+
unsafe { slice.as_ptr().add(self) }
243240
}
244241

245242
#[inline]
246243
unsafe fn get_unchecked_mut(self, slice: *mut [T]) -> *mut T {
247-
let this = self;
244+
debug_assert_nounwind!(
245+
self < slice.len(),
246+
"slice::get_unchecked_mut requires that the index is within the slice",
247+
);
248248
// SAFETY: see comments for `get_unchecked` above.
249-
unsafe {
250-
assert_unsafe_precondition!(
251-
"slice::get_unchecked_mut requires that the index is within the slice",
252-
[T](this: usize, slice: *mut [T]) => this < slice.len()
253-
);
254-
slice.as_mut_ptr().add(self)
255-
}
249+
unsafe { slice.as_mut_ptr().add(self) }
256250
}
257251

258252
#[inline]
@@ -296,32 +290,25 @@ unsafe impl<T> const SliceIndex<[T]> for ops::IndexRange {
296290

297291
#[inline]
298292
unsafe fn get_unchecked(self, slice: *const [T]) -> *const [T] {
299-
let end = self.end();
293+
debug_assert_nounwind!(
294+
self.end() <= slice.len(),
295+
"slice::get_unchecked requires that the index is within the slice"
296+
);
300297
// SAFETY: the caller guarantees that `slice` is not dangling, so it
301298
// cannot be longer than `isize::MAX`. They also guarantee that
302299
// `self` is in bounds of `slice` so `self` cannot overflow an `isize`,
303300
// so the call to `add` is safe.
304-
305-
unsafe {
306-
assert_unsafe_precondition!(
307-
"slice::get_unchecked requires that the index is within the slice",
308-
[T](end: usize, slice: *const [T]) => end <= slice.len()
309-
);
310-
ptr::slice_from_raw_parts(slice.as_ptr().add(self.start()), self.len())
311-
}
301+
unsafe { ptr::slice_from_raw_parts(slice.as_ptr().add(self.start()), self.len()) }
312302
}
313303

314304
#[inline]
315305
unsafe fn get_unchecked_mut(self, slice: *mut [T]) -> *mut [T] {
316-
let end = self.end();
306+
debug_assert_nounwind!(
307+
self.end() <= slice.len(),
308+
"slice::get_unchecked_mut requires that the index is within the slice",
309+
);
317310
// SAFETY: see comments for `get_unchecked` above.
318-
unsafe {
319-
assert_unsafe_precondition!(
320-
"slice::get_unchecked_mut requires that the index is within the slice",
321-
[T](end: usize, slice: *mut [T]) => end <= slice.len()
322-
);
323-
ptr::slice_from_raw_parts_mut(slice.as_mut_ptr().add(self.start()), self.len())
324-
}
311+
unsafe { ptr::slice_from_raw_parts_mut(slice.as_mut_ptr().add(self.start()), self.len()) }
325312
}
326313

327314
#[inline]
@@ -372,32 +359,28 @@ unsafe impl<T> const SliceIndex<[T]> for ops::Range<usize> {
372359

373360
#[inline]
374361
unsafe fn get_unchecked(self, slice: *const [T]) -> *const [T] {
375-
let this = ops::Range { ..self };
362+
debug_assert_nounwind!(
363+
self.end >= self.start && self.end <= slice.len(),
364+
"slice::get_unchecked requires that the range is within the slice",
365+
);
376366
// SAFETY: the caller guarantees that `slice` is not dangling, so it
377367
// cannot be longer than `isize::MAX`. They also guarantee that
378368
// `self` is in bounds of `slice` so `self` cannot overflow an `isize`,
379369
// so the call to `add` is safe and the length calculation cannot overflow.
380370
unsafe {
381-
assert_unsafe_precondition!(
382-
"slice::get_unchecked requires that the range is within the slice",
383-
[T](this: ops::Range<usize>, slice: *const [T]) =>
384-
this.end >= this.start && this.end <= slice.len()
385-
);
386371
let new_len = unchecked_sub(self.end, self.start);
387372
ptr::slice_from_raw_parts(slice.as_ptr().add(self.start), new_len)
388373
}
389374
}
390375

391376
#[inline]
392377
unsafe fn get_unchecked_mut(self, slice: *mut [T]) -> *mut [T] {
393-
let this = ops::Range { ..self };
378+
debug_assert_nounwind!(
379+
self.end >= self.start && self.end <= slice.len(),
380+
"slice::get_unchecked_mut requires that the range is within the slice",
381+
);
394382
// SAFETY: see comments for `get_unchecked` above.
395383
unsafe {
396-
assert_unsafe_precondition!(
397-
"slice::get_unchecked_mut requires that the range is within the slice",
398-
[T](this: ops::Range<usize>, slice: *mut [T]) =>
399-
this.end >= this.start && this.end <= slice.len()
400-
);
401384
let new_len = unchecked_sub(self.end, self.start);
402385
ptr::slice_from_raw_parts_mut(slice.as_mut_ptr().add(self.start), new_len)
403386
}

library/core/src/slice/mod.rs

+30-37
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,14 @@
88

99
use crate::cmp::Ordering::{self, Greater, Less};
1010
use crate::fmt;
11-
use crate::intrinsics::{assert_unsafe_precondition, exact_div};
11+
use crate::intrinsics::exact_div;
1212
use crate::marker::Copy;
1313
use crate::mem::{self, SizedTypeProperties};
1414
use crate::num::NonZeroUsize;
1515
use crate::ops::{Bound, FnMut, OneSidedRange, Range, RangeBounds};
1616
use crate::option::Option;
1717
use crate::option::Option::{None, Some};
18+
use crate::panic::debug_assert_nounwind;
1819
use crate::ptr;
1920
use crate::result::Result;
2021
use crate::result::Result::{Err, Ok};
@@ -656,14 +657,14 @@ impl<T> [T] {
656657
#[unstable(feature = "slice_swap_unchecked", issue = "88539")]
657658
#[rustc_const_unstable(feature = "const_swap", issue = "83163")]
658659
pub const unsafe fn swap_unchecked(&mut self, a: usize, b: usize) {
659-
let this = self;
660-
let ptr = this.as_mut_ptr();
660+
debug_assert_nounwind!(
661+
a < self.len() && b < self.len(),
662+
"slice::swap_unchecked requires that the indices are within the slice",
663+
);
664+
665+
let ptr = self.as_mut_ptr();
661666
// SAFETY: caller has to guarantee that `a < self.len()` and `b < self.len()`
662667
unsafe {
663-
assert_unsafe_precondition!(
664-
"slice::swap_unchecked requires that the indices are within the slice",
665-
[T](a: usize, b: usize, this: &mut [T]) => a < this.len() && b < this.len()
666-
);
667668
ptr::swap(ptr.add(a), ptr.add(b));
668669
}
669670
}
@@ -997,15 +998,12 @@ impl<T> [T] {
997998
#[inline]
998999
#[must_use]
9991000
pub unsafe fn as_chunks_unchecked<const N: usize>(&self) -> &[[T; N]] {
1000-
let this = self;
1001+
debug_assert_nounwind!(
1002+
N != 0 && self.len() % N == 0,
1003+
"slice::as_chunks_unchecked requires `N != 0` and the slice to split exactly into `N`-element chunks",
1004+
);
10011005
// SAFETY: Caller must guarantee that `N` is nonzero and exactly divides the slice length
1002-
let new_len = unsafe {
1003-
assert_unsafe_precondition!(
1004-
"slice::as_chunks_unchecked requires `N != 0` and the slice to split exactly into `N`-element chunks",
1005-
[T](this: &[T], N: usize) => N != 0 && this.len() % N == 0
1006-
);
1007-
exact_div(self.len(), N)
1008-
};
1006+
let new_len = unsafe { exact_div(self.len(), N) };
10091007
// SAFETY: We cast a slice of `new_len * N` elements into
10101008
// a slice of `new_len` many `N` elements chunks.
10111009
unsafe { from_raw_parts(self.as_ptr().cast(), new_len) }
@@ -1154,15 +1152,12 @@ impl<T> [T] {
11541152
#[inline]
11551153
#[must_use]
11561154
pub unsafe fn as_chunks_unchecked_mut<const N: usize>(&mut self) -> &mut [[T; N]] {
1157-
let this = &*self;
1155+
debug_assert_nounwind!(
1156+
N != 0 && self.len() % N == 0,
1157+
"slice::as_chunks_unchecked requires `N != 0` and the slice to split exactly into `N`-element chunks",
1158+
);
11581159
// SAFETY: Caller must guarantee that `N` is nonzero and exactly divides the slice length
1159-
let new_len = unsafe {
1160-
assert_unsafe_precondition!(
1161-
"slice::as_chunks_unchecked_mut requires `N != 0` and the slice to split exactly into `N`-element chunks",
1162-
[T](this: &[T], N: usize) => N != 0 && this.len() % N == 0
1163-
);
1164-
exact_div(this.len(), N)
1165-
};
1160+
let new_len = unsafe { exact_div(self.len(), N) };
11661161
// SAFETY: We cast a slice of `new_len * N` elements into
11671162
// a slice of `new_len` many `N` elements chunks.
11681163
unsafe { from_raw_parts_mut(self.as_mut_ptr().cast(), new_len) }
@@ -1694,14 +1689,13 @@ impl<T> [T] {
16941689
let len = self.len();
16951690
let ptr = self.as_ptr();
16961691

1692+
debug_assert_nounwind!(
1693+
mid <= len,
1694+
"slice::split_at_unchecked requires the index to be within the slice",
1695+
);
1696+
16971697
// SAFETY: Caller has to check that `0 <= mid <= self.len()`
1698-
unsafe {
1699-
assert_unsafe_precondition!(
1700-
"slice::split_at_unchecked requires the index to be within the slice",
1701-
(mid: usize, len: usize) => mid <= len
1702-
);
1703-
(from_raw_parts(ptr, mid), from_raw_parts(ptr.add(mid), len - mid))
1704-
}
1698+
unsafe { (from_raw_parts(ptr, mid), from_raw_parts(ptr.add(mid), len - mid)) }
17051699
}
17061700

17071701
/// Divides one mutable slice into two at an index, without doing bounds checking.
@@ -1745,17 +1739,16 @@ impl<T> [T] {
17451739
let len = self.len();
17461740
let ptr = self.as_mut_ptr();
17471741

1742+
debug_assert_nounwind!(
1743+
mid <= len,
1744+
"slice::split_at_mut_unchecked requires the index to be within the slice",
1745+
);
1746+
17481747
// SAFETY: Caller has to check that `0 <= mid <= self.len()`.
17491748
//
17501749
// `[ptr; mid]` and `[mid; len]` are not overlapping, so returning a mutable reference
17511750
// is fine.
1752-
unsafe {
1753-
assert_unsafe_precondition!(
1754-
"slice::split_at_mut_unchecked requires the index to be within the slice",
1755-
(mid: usize, len: usize) => mid <= len
1756-
);
1757-
(from_raw_parts_mut(ptr, mid), from_raw_parts_mut(ptr.add(mid), len - mid))
1758-
}
1751+
unsafe { (from_raw_parts_mut(ptr, mid), from_raw_parts_mut(ptr.add(mid), len - mid)) }
17591752
}
17601753

17611754
/// Divides one slice into an array and a remainder slice at an index.

0 commit comments

Comments
 (0)