Skip to content

Commit fc239e8

Browse files
committed
Make likely and unlikely const
They are gated by internal feature gate const_likely
1 parent 224bc05 commit fc239e8

File tree

4 files changed

+10
-0
lines changed

4 files changed

+10
-0
lines changed

src/libcore/intrinsics.rs

+2
Original file line numberDiff line numberDiff line change
@@ -952,6 +952,7 @@ extern "rust-intrinsic" {
952952
/// Any use other than with `if` statements will probably not have an effect.
953953
///
954954
/// This intrinsic does not have a stable counterpart.
955+
#[rustc_const_unstable(feature = "const_likely", issue = "none")]
955956
pub fn likely(b: bool) -> bool;
956957

957958
/// Hints to the compiler that branch condition is likely to be false.
@@ -960,6 +961,7 @@ extern "rust-intrinsic" {
960961
/// Any use other than with `if` statements will probably not have an effect.
961962
///
962963
/// This intrinsic does not have a stable counterpart.
964+
#[rustc_const_unstable(feature = "const_likely", issue = "none")]
963965
pub fn unlikely(b: bool) -> bool;
964966

965967
/// Executes a breakpoint trap, for inspection by a debugger.

src/libcore/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@
9292
#![feature(const_slice_from_raw_parts)]
9393
#![feature(const_slice_ptr_len)]
9494
#![feature(const_type_name)]
95+
#![feature(const_likely)]
9596
#![feature(custom_inner_attributes)]
9697
#![feature(decl_macro)]
9798
#![feature(doc_cfg)]

src/librustc_mir/interpret/intrinsics.rs

+5
Original file line numberDiff line numberDiff line change
@@ -399,6 +399,11 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
399399
);
400400
self.copy_op(self.operand_index(args[0], index)?, dest)?;
401401
}
402+
sym::likely | sym::unlikely => {
403+
// These just return their argument
404+
let a = self.read_immediate(args[0])?;
405+
self.write_immediate(*a, dest)?;
406+
}
402407
// FIXME(#73156): Handle source code coverage in const eval
403408
sym::count_code_region => (),
404409
_ => return Ok(false),

src/librustc_span/symbol.rs

+2
Original file line numberDiff line numberDiff line change
@@ -427,6 +427,7 @@ symbols! {
427427
lhs,
428428
lib,
429429
lifetime,
430+
likely,
430431
line,
431432
link,
432433
linkage,
@@ -813,6 +814,7 @@ symbols! {
813814
underscore_lifetimes,
814815
uniform_paths,
815816
universal_impl_trait,
817+
unlikely,
816818
unmarked_api,
817819
unreachable_code,
818820
unrestricted_attribute_tokens,

0 commit comments

Comments
 (0)