Constify core::intrinsics::black_box

This commit is contained in:
woppopo 2021-12-23 20:07:41 +09:00
parent 390bb3406d
commit 72f15ea22a
5 changed files with 23 additions and 1 deletions

View File

@ -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)
}

View File

@ -1940,6 +1940,7 @@ pub unsafe fn drop_in_place<T: ?Sized>(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<T>(dummy: T) -> T;
}

View File

@ -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)]

View File

@ -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));
}
}

View File

@ -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)]