diff --git a/library/core/src/intrinsics/mod.rs b/library/core/src/intrinsics/mod.rs index 2179b451c375..d46d3ed9d513 100644 --- a/library/core/src/intrinsics/mod.rs +++ b/library/core/src/intrinsics/mod.rs @@ -2402,8 +2402,7 @@ pub const fn const_eval_select( /// The `@capture` block declares which surrounding variables / expressions can be /// used inside the `if const`. /// Note that the two arms of this `if` really each become their own function, which is why the -/// macro supports setting attributes for those functions. The runtime function is always -/// marked as `#[inline]`. +/// macro supports setting attributes for those functions. Both functions are marked as `#[inline]`. /// /// See [`const_eval_select()`] for the rules and requirements around that intrinsic. pub(crate) macro const_eval_select { @@ -2413,35 +2412,14 @@ pub const fn const_eval_select( $(#[$compiletime_attr:meta])* $compiletime:block else $(#[$runtime_attr:meta])* $runtime:block - ) => { - // Use the `noinline` arm, after adding explicit `inline` attributes - $crate::intrinsics::const_eval_select!( - @capture$([$($binders)*])? { $($arg : $ty = $val),* } $(-> $ret)? : - #[noinline] - if const - #[inline] // prevent codegen on this function - $(#[$compiletime_attr])* - $compiletime - else - #[inline] // avoid the overhead of an extra fn call - $(#[$runtime_attr])* - $runtime - ) - }, - // With a leading #[noinline], we don't add inline attributes - ( - @capture$([$($binders:tt)*])? { $($arg:ident : $ty:ty = $val:expr),* $(,)? } $( -> $ret:ty )? : - #[noinline] - if const - $(#[$compiletime_attr:meta])* $compiletime:block - else - $(#[$runtime_attr:meta])* $runtime:block ) => {{ + #[inline] $(#[$runtime_attr])* fn runtime$(<$($binders)*>)?($($arg: $ty),*) $( -> $ret )? { $runtime } + #[inline] $(#[$compiletime_attr])* const fn compiletime$(<$($binders)*>)?($($arg: $ty),*) $( -> $ret )? { // Don't warn if one of the arguments is unused. diff --git a/library/core/src/panic.rs b/library/core/src/panic.rs index 7a42b3d0fc78..81520c3ecd1b 100644 --- a/library/core/src/panic.rs +++ b/library/core/src/panic.rs @@ -166,10 +166,9 @@ fn as_str(&mut self) -> Option<&str> { const fn do_panic($($arg: $ty),*) -> ! { $crate::intrinsics::const_eval_select!( @capture { $($arg: $ty = $arg),* } -> !: - #[noinline] - if const #[track_caller] #[inline] { // Inline this, to prevent codegen + if const #[track_caller] { $crate::panic!($const_msg) - } else #[track_caller] { // Do not inline this, it makes perf worse + } else #[track_caller] { $crate::panic!($runtime_msg) } ) @@ -195,7 +194,7 @@ const fn do_panic($($arg: $ty),*) -> ! { #[doc(hidden)] pub macro const_assert { ($condition: expr, $const_msg:literal, $runtime_msg:literal, $($arg:tt)*) => {{ - if !$crate::intrinsics::likely($condition) { + if !($condition) { $crate::panic::const_panic!($const_msg, $runtime_msg, $($arg)*) } }} diff --git a/tests/codegen-llvm/intrinsics/likely_assert.rs b/tests/codegen-llvm/intrinsics/likely_assert.rs index 87ffb4ee3fb6..59a40c750eab 100644 --- a/tests/codegen-llvm/intrinsics/likely_assert.rs +++ b/tests/codegen-llvm/intrinsics/likely_assert.rs @@ -1,17 +1,31 @@ //@ compile-flags: -Copt-level=3 +#![feature(panic_internals, const_eval_select, rustc_allow_const_fn_unstable, core_intrinsics)] #![crate_type = "lib"] +// check that assert! and const_assert! emit branch weights + #[no_mangle] pub fn test_assert(x: bool) { assert!(x); } -// check that assert! emits branch weights - // CHECK-LABEL: @test_assert( // CHECK: br i1 %x, label %bb2, label %bb1, !prof ![[NUM:[0-9]+]] // CHECK: bb1: // CHECK: panic // CHECK: bb2: // CHECK: ret void + +#[no_mangle] +pub fn test_const_assert(x: bool) { + core::panic::const_assert!(x, "", "",); +} + +// CHECK-LABEL: @test_const_assert( +// CHECK: br i1 %x, label %bb2, label %bb1, !prof ![[NUM:[0-9]+]] +// CHECK: bb1: +// CHECK: panic +// CHECK: bb2: +// CHECK: ret void + // CHECK: ![[NUM]] = !{!"branch_weights", {{(!"expected", )?}}i32 2000, i32 1}