diff --git a/library/core/src/hint.rs b/library/core/src/hint.rs index f49aefea81b..003391e52be 100644 --- a/library/core/src/hint.rs +++ b/library/core/src/hint.rs @@ -159,6 +159,7 @@ pub fn spin_loop() { /// [`std::convert::identity`]: crate::convert::identity #[inline] #[unstable(feature = "bench_black_box", issue = "64102")] -pub fn black_box(dummy: T) -> T { +#[rustc_const_unstable(feature = "const_black_box", issue = "none")] +pub const fn black_box(dummy: T) -> T { crate::intrinsics::black_box(dummy) } diff --git a/library/core/src/intrinsics.rs b/library/core/src/intrinsics.rs index 46370f76e7c..4ecc3b0c7f8 100644 --- a/library/core/src/intrinsics.rs +++ b/library/core/src/intrinsics.rs @@ -1940,6 +1940,7 @@ pub unsafe fn drop_in_place(to_drop: *mut T) { /// See documentation of [`std::hint::black_box`] for details. /// /// [`std::hint::black_box`]: crate::hint::black_box + #[rustc_const_unstable(feature = "const_black_box", issue = "none")] pub fn black_box(dummy: T) -> T; } diff --git a/library/core/src/lib.rs b/library/core/src/lib.rs index 32a5fd9c481..4bd94e3ce39 100644 --- a/library/core/src/lib.rs +++ b/library/core/src/lib.rs @@ -103,6 +103,7 @@ #![feature(const_arguments_as_str)] #![feature(const_array_into_iter_constructors)] #![feature(const_bigint_helper_methods)] +#![feature(const_black_box)] #![feature(const_caller_location)] #![feature(const_cell_into_inner)] #![feature(const_char_convert)] diff --git a/library/core/tests/intrinsics.rs b/library/core/tests/intrinsics.rs index 84cef53b358..7a2e4e29065 100644 --- a/library/core/tests/intrinsics.rs +++ b/library/core/tests/intrinsics.rs @@ -65,3 +65,18 @@ const fn test_write_bytes_in_const_contexts() { assert!(TEST2[1] == 16843009); assert!(TEST2[2] == 3); } + +#[test] +fn test_hints_in_const_contexts() { + use core::intrinsics::{likely, unlikely}; + + // In const contexts, they just return their argument. + const { + assert!(true == likely(true)); + assert!(false == likely(false)); + assert!(true == unlikely(true)); + assert!(false == unlikely(false)); + assert!(42u32 == core::intrinsics::black_box(42u32)); + assert!(42u32 == core::hint::black_box(42u32)); + } +} diff --git a/library/core/tests/lib.rs b/library/core/tests/lib.rs index 21562acf3d7..ec700346ac9 100644 --- a/library/core/tests/lib.rs +++ b/library/core/tests/lib.rs @@ -2,12 +2,14 @@ #![feature(array_chunks)] #![feature(array_methods)] #![feature(array_windows)] +#![feature(bench_black_box)] #![feature(bool_to_option)] #![feature(box_syntax)] #![feature(cell_update)] #![feature(cfg_panic)] #![feature(cfg_target_has_atomic)] #![feature(const_assume)] +#![feature(const_black_box)] #![feature(const_bool_to_option)] #![feature(const_cell_into_inner)] #![feature(const_convert)] @@ -18,6 +20,7 @@ #![feature(const_ptr_write)] #![feature(const_ptr_offset)] #![feature(const_trait_impl)] +#![feature(const_likely)] #![feature(core_intrinsics)] #![feature(core_private_bignum)] #![feature(core_private_diy_float)] @@ -35,6 +38,7 @@ #![feature(array_from_fn)] #![feature(hashmap_internals)] #![feature(try_find)] +#![feature(inline_const)] #![feature(is_sorted)] #![feature(pattern)] #![feature(sort_internals)]