Skip to content

Commit ac83eee

Browse files
committed
assert_unsafe_precondition!: assume() the expression
This may help the compiler to get some facts about the code and thus optimize it better. It may however have an effect on compile times
1 parent 6a20f7d commit ac83eee

File tree

1 file changed

+12
-7
lines changed

1 file changed

+12
-7
lines changed

library/core/src/intrinsics.rs

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2274,22 +2274,27 @@ extern "rust-intrinsic" {
22742274
#[allow_internal_unstable(const_eval_select)] // permit this to be called in stably-const fn
22752275
macro_rules! assert_unsafe_precondition {
22762276
($name:expr, $([$($tt:tt)*])?($($i:ident:$ty:ty),*$(,)?) => $e:expr) => {
2277-
if cfg!(debug_assertions) {
2277+
{
22782278
// allow non_snake_case to allow capturing const generics
22792279
#[allow(non_snake_case)]
22802280
#[inline(always)]
22812281
fn runtime$(<$($tt)*>)?($($i:$ty),*) {
2282-
if !$e {
2283-
// don't unwind to reduce impact on code size
2284-
::core::panicking::panic_nounwind(
2285-
concat!("unsafe precondition(s) violated: ", $name)
2286-
);
2282+
if cfg!(debug_assertions) {
2283+
if !$e {
2284+
// don't unwind to reduce impact on code size
2285+
$crate::panicking::panic_nounwind(
2286+
concat!("unsafe precondition(s) violated: ", $name)
2287+
);
2288+
}
2289+
} else {
2290+
// SAFETY: the caller must ensure this
2291+
unsafe { $crate::intrinsics::assume($e) };
22872292
}
22882293
}
22892294
#[allow(non_snake_case)]
22902295
const fn comptime$(<$($tt)*>)?($(_:$ty),*) {}
22912296

2292-
::core::intrinsics::const_eval_select(($($i,)*), comptime, runtime);
2297+
$crate::intrinsics::const_eval_select(($($i,)*), comptime, runtime);
22932298
}
22942299
};
22952300
}

0 commit comments

Comments
 (0)