Skip to content

Commit

Permalink
Constify core::intrinsics::black_box
Browse files Browse the repository at this point in the history
  • Loading branch information
lilasta committed Dec 23, 2021
1 parent 390bb34 commit 72f15ea
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 1 deletion.
3 changes: 2 additions & 1 deletion library/core/src/hint.rs
Expand Up @@ -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<T>(dummy: T) -> T {
#[rustc_const_unstable(feature = "const_black_box", issue = "none")]
pub const fn black_box<T>(dummy: T) -> T {
crate::intrinsics::black_box(dummy)
}
1 change: 1 addition & 0 deletions library/core/src/intrinsics.rs
Expand Up @@ -1940,6 +1940,7 @@ extern "rust-intrinsic" {
/// 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<T>(dummy: T) -> T;
}

Expand Down
1 change: 1 addition & 0 deletions library/core/src/lib.rs
Expand Up @@ -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)]
Expand Down
15 changes: 15 additions & 0 deletions library/core/tests/intrinsics.rs
Expand Up @@ -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));
}
}
4 changes: 4 additions & 0 deletions library/core/tests/lib.rs
Expand Up @@ -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)]
Expand All @@ -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)]
Expand All @@ -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)]
Expand Down

0 comments on commit 72f15ea

Please sign in to comment.