Skip to content

Commit 97c1502

Browse files
committed
Convert many assert_unsafe_precondition to debug_assert_nounwind
1 parent 4ccec45 commit 97c1502

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
@@ -75,12 +75,12 @@ macro_rules! nonzero_integers {
7575
#[must_use]
7676
#[inline]
7777
pub const unsafe fn new_unchecked(n: $Int) -> Self {
78+
crate::panic::debug_assert_nounwind!(
79+
n != 0,
80+
concat!(stringify!($Ty), "::new_unchecked requires a non-zero argument")
81+
);
7882
// SAFETY: this is guaranteed to be safe by the caller.
7983
unsafe {
80-
core::intrinsics::assert_unsafe_precondition!(
81-
concat!(stringify!($Ty), "::new_unchecked requires a non-zero argument"),
82-
(n: $Int) => n != 0
83-
);
8484
Self(n)
8585
}
8686
}

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

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

8884
// SAFETY: By precondition, this must be a power of two, and
8985
// 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")]
@@ -225,31 +225,25 @@ unsafe impl<T> SliceIndex<[T]> for usize {
225225

226226
#[inline]
227227
unsafe fn get_unchecked(self, slice: *const [T]) -> *const T {
228-
let this = self;
228+
debug_assert_nounwind!(
229+
self < slice.len(),
230+
"slice::get_unchecked requires that the index is within the slice",
231+
);
229232
// SAFETY: the caller guarantees that `slice` is not dangling, so it
230233
// cannot be longer than `isize::MAX`. They also guarantee that
231234
// `self` is in bounds of `slice` so `self` cannot overflow an `isize`,
232235
// so the call to `add` is safe.
233-
unsafe {
234-
assert_unsafe_precondition!(
235-
"slice::get_unchecked requires that the index is within the slice",
236-
[T](this: usize, slice: *const [T]) => this < slice.len()
237-
);
238-
slice.as_ptr().add(self)
239-
}
236+
unsafe { slice.as_ptr().add(self) }
240237
}
241238

242239
#[inline]
243240
unsafe fn get_unchecked_mut(self, slice: *mut [T]) -> *mut T {
244-
let this = self;
241+
debug_assert_nounwind!(
242+
self < slice.len(),
243+
"slice::get_unchecked_mut requires that the index is within the slice",
244+
);
245245
// SAFETY: see comments for `get_unchecked` above.
246-
unsafe {
247-
assert_unsafe_precondition!(
248-
"slice::get_unchecked_mut requires that the index is within the slice",
249-
[T](this: usize, slice: *mut [T]) => this < slice.len()
250-
);
251-
slice.as_mut_ptr().add(self)
252-
}
246+
unsafe { slice.as_mut_ptr().add(self) }
253247
}
254248

255249
#[inline]
@@ -293,32 +287,25 @@ unsafe impl<T> SliceIndex<[T]> for ops::IndexRange {
293287

294288
#[inline]
295289
unsafe fn get_unchecked(self, slice: *const [T]) -> *const [T] {
296-
let end = self.end();
290+
debug_assert_nounwind!(
291+
self.end() <= slice.len(),
292+
"slice::get_unchecked requires that the index is within the slice"
293+
);
297294
// SAFETY: the caller guarantees that `slice` is not dangling, so it
298295
// cannot be longer than `isize::MAX`. They also guarantee that
299296
// `self` is in bounds of `slice` so `self` cannot overflow an `isize`,
300297
// so the call to `add` is safe.
301-
302-
unsafe {
303-
assert_unsafe_precondition!(
304-
"slice::get_unchecked requires that the index is within the slice",
305-
[T](end: usize, slice: *const [T]) => end <= slice.len()
306-
);
307-
ptr::slice_from_raw_parts(slice.as_ptr().add(self.start()), self.len())
308-
}
298+
unsafe { ptr::slice_from_raw_parts(slice.as_ptr().add(self.start()), self.len()) }
309299
}
310300

311301
#[inline]
312302
unsafe fn get_unchecked_mut(self, slice: *mut [T]) -> *mut [T] {
313-
let end = self.end();
303+
debug_assert_nounwind!(
304+
self.end() <= slice.len(),
305+
"slice::get_unchecked_mut requires that the index is within the slice",
306+
);
314307
// SAFETY: see comments for `get_unchecked` above.
315-
unsafe {
316-
assert_unsafe_precondition!(
317-
"slice::get_unchecked_mut requires that the index is within the slice",
318-
[T](end: usize, slice: *mut [T]) => end <= slice.len()
319-
);
320-
ptr::slice_from_raw_parts_mut(slice.as_mut_ptr().add(self.start()), self.len())
321-
}
308+
unsafe { ptr::slice_from_raw_parts_mut(slice.as_mut_ptr().add(self.start()), self.len()) }
322309
}
323310

324311
#[inline]
@@ -369,32 +356,28 @@ unsafe impl<T> SliceIndex<[T]> for ops::Range<usize> {
369356

370357
#[inline]
371358
unsafe fn get_unchecked(self, slice: *const [T]) -> *const [T] {
372-
let this = ops::Range { ..self };
359+
debug_assert_nounwind!(
360+
self.end >= self.start && self.end <= slice.len(),
361+
"slice::get_unchecked requires that the range is within the slice",
362+
);
373363
// SAFETY: the caller guarantees that `slice` is not dangling, so it
374364
// cannot be longer than `isize::MAX`. They also guarantee that
375365
// `self` is in bounds of `slice` so `self` cannot overflow an `isize`,
376366
// so the call to `add` is safe and the length calculation cannot overflow.
377367
unsafe {
378-
assert_unsafe_precondition!(
379-
"slice::get_unchecked requires that the range is within the slice",
380-
[T](this: ops::Range<usize>, slice: *const [T]) =>
381-
this.end >= this.start && this.end <= slice.len()
382-
);
383368
let new_len = unchecked_sub(self.end, self.start);
384369
ptr::slice_from_raw_parts(slice.as_ptr().add(self.start), new_len)
385370
}
386371
}
387372

388373
#[inline]
389374
unsafe fn get_unchecked_mut(self, slice: *mut [T]) -> *mut [T] {
390-
let this = ops::Range { ..self };
375+
debug_assert_nounwind!(
376+
self.end >= self.start && self.end <= slice.len(),
377+
"slice::get_unchecked_mut requires that the range is within the slice",
378+
);
391379
// SAFETY: see comments for `get_unchecked` above.
392380
unsafe {
393-
assert_unsafe_precondition!(
394-
"slice::get_unchecked_mut requires that the range is within the slice",
395-
[T](this: ops::Range<usize>, slice: *mut [T]) =>
396-
this.end >= this.start && this.end <= slice.len()
397-
);
398381
let new_len = unchecked_sub(self.end, self.start);
399382
ptr::slice_from_raw_parts_mut(slice.as_mut_ptr().add(self.start), new_len)
400383
}

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, Equal, 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};
@@ -929,14 +930,14 @@ impl<T> [T] {
929930
#[unstable(feature = "slice_swap_unchecked", issue = "88539")]
930931
#[rustc_const_unstable(feature = "const_swap", issue = "83163")]
931932
pub const unsafe fn swap_unchecked(&mut self, a: usize, b: usize) {
932-
let this = self;
933-
let ptr = this.as_mut_ptr();
933+
debug_assert_nounwind!(
934+
a < self.len() && b < self.len(),
935+
"slice::swap_unchecked requires that the indices are within the slice",
936+
);
937+
938+
let ptr = self.as_mut_ptr();
934939
// SAFETY: caller has to guarantee that `a < self.len()` and `b < self.len()`
935940
unsafe {
936-
assert_unsafe_precondition!(
937-
"slice::swap_unchecked requires that the indices are within the slice",
938-
[T](a: usize, b: usize, this: &mut [T]) => a < this.len() && b < this.len()
939-
);
940941
ptr::swap(ptr.add(a), ptr.add(b));
941942
}
942943
}
@@ -1269,15 +1270,12 @@ impl<T> [T] {
12691270
#[inline]
12701271
#[must_use]
12711272
pub const unsafe fn as_chunks_unchecked<const N: usize>(&self) -> &[[T; N]] {
1272-
let this = self;
1273+
debug_assert_nounwind!(
1274+
N != 0 && self.len() % N == 0,
1275+
"slice::as_chunks_unchecked requires `N != 0` and the slice to split exactly into `N`-element chunks",
1276+
);
12731277
// SAFETY: Caller must guarantee that `N` is nonzero and exactly divides the slice length
1274-
let new_len = unsafe {
1275-
assert_unsafe_precondition!(
1276-
"slice::as_chunks_unchecked requires `N != 0` and the slice to split exactly into `N`-element chunks",
1277-
[T](this: &[T], N: usize) => N != 0 && this.len() % N == 0
1278-
);
1279-
exact_div(self.len(), N)
1280-
};
1278+
let new_len = unsafe { exact_div(self.len(), N) };
12811279
// SAFETY: We cast a slice of `new_len * N` elements into
12821280
// a slice of `new_len` many `N` elements chunks.
12831281
unsafe { from_raw_parts(self.as_ptr().cast(), new_len) }
@@ -1426,15 +1424,12 @@ impl<T> [T] {
14261424
#[inline]
14271425
#[must_use]
14281426
pub const unsafe fn as_chunks_unchecked_mut<const N: usize>(&mut self) -> &mut [[T; N]] {
1429-
let this = &*self;
1427+
debug_assert_nounwind!(
1428+
N != 0 && self.len() % N == 0,
1429+
"slice::as_chunks_unchecked requires `N != 0` and the slice to split exactly into `N`-element chunks",
1430+
);
14301431
// SAFETY: Caller must guarantee that `N` is nonzero and exactly divides the slice length
1431-
let new_len = unsafe {
1432-
assert_unsafe_precondition!(
1433-
"slice::as_chunks_unchecked_mut requires `N != 0` and the slice to split exactly into `N`-element chunks",
1434-
[T](this: &[T], N: usize) => N != 0 && this.len() % N == 0
1435-
);
1436-
exact_div(this.len(), N)
1437-
};
1432+
let new_len = unsafe { exact_div(self.len(), N) };
14381433
// SAFETY: We cast a slice of `new_len * N` elements into
14391434
// a slice of `new_len` many `N` elements chunks.
14401435
unsafe { from_raw_parts_mut(self.as_mut_ptr().cast(), new_len) }
@@ -1967,14 +1962,13 @@ impl<T> [T] {
19671962
let len = self.len();
19681963
let ptr = self.as_ptr();
19691964

1965+
debug_assert_nounwind!(
1966+
mid <= len,
1967+
"slice::split_at_unchecked requires the index to be within the slice",
1968+
);
1969+
19701970
// SAFETY: Caller has to check that `0 <= mid <= self.len()`
1971-
unsafe {
1972-
assert_unsafe_precondition!(
1973-
"slice::split_at_unchecked requires the index to be within the slice",
1974-
(mid: usize, len: usize) => mid <= len
1975-
);
1976-
(from_raw_parts(ptr, mid), from_raw_parts(ptr.add(mid), len - mid))
1977-
}
1971+
unsafe { (from_raw_parts(ptr, mid), from_raw_parts(ptr.add(mid), len - mid)) }
19781972
}
19791973

19801974
/// Divides one mutable slice into two at an index, without doing bounds checking.
@@ -2018,17 +2012,16 @@ impl<T> [T] {
20182012
let len = self.len();
20192013
let ptr = self.as_mut_ptr();
20202014

2015+
debug_assert_nounwind!(
2016+
mid <= len,
2017+
"slice::split_at_mut_unchecked requires the index to be within the slice",
2018+
);
2019+
20212020
// SAFETY: Caller has to check that `0 <= mid <= self.len()`.
20222021
//
20232022
// `[ptr; mid]` and `[mid; len]` are not overlapping, so returning a mutable reference
20242023
// is fine.
2025-
unsafe {
2026-
assert_unsafe_precondition!(
2027-
"slice::split_at_mut_unchecked requires the index to be within the slice",
2028-
(mid: usize, len: usize) => mid <= len
2029-
);
2030-
(from_raw_parts_mut(ptr, mid), from_raw_parts_mut(ptr.add(mid), len - mid))
2031-
}
2024+
unsafe { (from_raw_parts_mut(ptr, mid), from_raw_parts_mut(ptr.add(mid), len - mid)) }
20322025
}
20332026

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

0 commit comments

Comments
 (0)