From ac83eeea2bf7799717e4289dec974810130529ff Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Beno=C3=AEt=20du=20Garreau?= Date: Wed, 28 Dec 2022 17:43:25 +0100 Subject: [PATCH] `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 --- library/core/src/intrinsics.rs | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/library/core/src/intrinsics.rs b/library/core/src/intrinsics.rs index 6eeaef5ab0129..dc268e685365a 100644 --- a/library/core/src/intrinsics.rs +++ b/library/core/src/intrinsics.rs @@ -2274,22 +2274,27 @@ extern "rust-intrinsic" { #[allow_internal_unstable(const_eval_select)] // permit this to be called in stably-const fn macro_rules! assert_unsafe_precondition { ($name:expr, $([$($tt:tt)*])?($($i:ident:$ty:ty),*$(,)?) => $e:expr) => { - if cfg!(debug_assertions) { + { // allow non_snake_case to allow capturing const generics #[allow(non_snake_case)] #[inline(always)] fn runtime$(<$($tt)*>)?($($i:$ty),*) { - if !$e { - // don't unwind to reduce impact on code size - ::core::panicking::panic_nounwind( - concat!("unsafe precondition(s) violated: ", $name) - ); + if cfg!(debug_assertions) { + if !$e { + // don't unwind to reduce impact on code size + $crate::panicking::panic_nounwind( + concat!("unsafe precondition(s) violated: ", $name) + ); + } + } else { + // SAFETY: the caller must ensure this + unsafe { $crate::intrinsics::assume($e) }; } } #[allow(non_snake_case)] const fn comptime$(<$($tt)*>)?($(_:$ty),*) {} - ::core::intrinsics::const_eval_select(($($i,)*), comptime, runtime); + $crate::intrinsics::const_eval_select(($($i,)*), comptime, runtime); } }; }