about summary refs log tree commit diff
path: root/library/core/src/num
diff options
context:
space:
mode:
authorBen Kimock <kimockb@gmail.com>2022-01-08 23:55:09 -0500
committerBen Kimock <kimockb@gmail.com>2022-03-29 11:05:24 -0400
commit6e6d0cbf838fef856abd5b5c63d1f156c4ebfe72 (patch)
treeda08d09e5c771f37b3072c25a8df157a2e956729 /library/core/src/num
parentba14a836c7038da21f5e102aacc7e6d5964f79a6 (diff)
downloadrust-6e6d0cbf838fef856abd5b5c63d1f156c4ebfe72.tar.gz
rust-6e6d0cbf838fef856abd5b5c63d1f156c4ebfe72.zip
Add debug assertions to some unsafe functions
These debug assertions are all implemented only at runtime using
`const_eval_select`, and in the error path they execute
`intrinsics::abort` instead of being a normal debug assertion to
minimize the impact of these assertions on code size, when enabled.

Of all these changes, the bounds checks for unchecked indexing are
expected to be most impactful (case in point, they found a problem in
rustc).
Diffstat (limited to 'library/core/src/num')
-rw-r--r--library/core/src/num/nonzero.rs6
1 files changed, 5 insertions, 1 deletions
diff --git a/library/core/src/num/nonzero.rs b/library/core/src/num/nonzero.rs
index 1ebd1c58f2b..b42b6a939c4 100644
--- a/library/core/src/num/nonzero.rs
+++ b/library/core/src/num/nonzero.rs
@@ -52,9 +52,13 @@ macro_rules! nonzero_integers {
                 #[$const_new_unchecked_stability]
                 #[must_use]
                 #[inline]
+                #[rustc_allow_const_fn_unstable(const_fn_fn_ptr_basics)] // required by assert_unsafe_precondition
                 pub const unsafe fn new_unchecked(n: $Int) -> Self {
                     // SAFETY: this is guaranteed to be safe by the caller.
-                    unsafe { Self(n) }
+                    unsafe {
+                        core::intrinsics::assert_unsafe_precondition!(n != 0);
+                        Self(n)
+                    }
                 }
 
                 /// Creates a non-zero if the given value is not zero.