Skip to content

Commit 10e0481

Browse files
committed
New name for hint::black_box: pretend_used
The current name is a legacy from before RFC 2360. The RFC calls the method `bench_black_box`, though acknowledges that this name is not be ideal either. The main objection to the name during and since the RFC is that it is not truly a black box to the compiler. Instead, the hint only encourages the compiler to consider the passed-in value used. This in the hope that the compiler will materialize that value somewhere, such as in memory or in a register, and not eliminate the input as dead code. This PR proposes to rename the method to `pretend_used`. This clearly indicates the precise semantics the hint conveys to the compiler, without suggesting that it disables further compiler optimizations. The name also reads straightforwardly in code: `hint::pretend_used(x)` hints to the compiler that it should pretend that `x` is used in some arbitrary way. `pretend_used` also rectifies the secondary naming concern the RFC raised with the names `black_box` and `bench_black_box`; the potential confusion with "boxed" values. If this change lands, it completes the naming portion of rust-lang#64102.
1 parent 2c28244 commit 10e0481

File tree

5 files changed

+19
-17
lines changed

5 files changed

+19
-17
lines changed

library/core/src/hint.rs

+12-10
Original file line numberDiff line numberDiff line change
@@ -96,23 +96,25 @@ pub fn spin_loop() {
9696
}
9797
}
9898

99-
/// An identity function that *__hints__* to the compiler to be maximally pessimistic about what
100-
/// `black_box` could do.
99+
/// An identity function that suggests to the compiler that the given value is used in some
100+
/// arbitrary, unspecified way.
101101
///
102102
/// [`std::convert::identity`]: https://doc.rust-lang.org/core/convert/fn.identity.html
103103
///
104-
/// Unlike [`std::convert::identity`], a Rust compiler is encouraged to assume that `black_box` can
105-
/// use `x` in any possible valid way that Rust code is allowed to without introducing undefined
106-
/// behavior in the calling code. This property makes `black_box` useful for writing code in which
107-
/// certain optimizations are not desired, such as benchmarks.
104+
/// Unlike [`std::convert::identity`], a Rust compiler is encouraged to assume that `pretend_used`
105+
/// may use `x` in any possible valid way that Rust code is allowed to without introducing
106+
/// undefined behavior in the calling code. This property makes `pretend_used` useful for writing
107+
/// code in which certain optimizations are not desired, such as benchmarks. For example, if an
108+
/// expression whose value is never used is passed through `pretend_used`, the compiler is
109+
/// encouraged to compute the value of that expression rather than eliminate it as dead code.
108110
///
109-
/// Note however, that `black_box` is only (and can only be) provided on a "best-effort" basis. The
110-
/// extent to which it can block optimisations may vary depending upon the platform and code-gen
111-
/// backend used. Programs cannot rely on `black_box` for *correctness* in any way.
111+
/// Note however, that `pretend_used` is only (and can only be) provided on a "best-effort" basis.
112+
/// The extent to which it can block optimisations may vary depending upon the platform and
113+
/// code-gen backend used. Programs cannot rely on `pretend_used` for *correctness* in any way.
112114
#[inline]
113115
#[unstable(feature = "test", issue = "50297")]
114116
#[allow(unreachable_code)] // this makes #[cfg] a bit easier below.
115-
pub fn black_box<T>(dummy: T) -> T {
117+
pub fn pretend_used<T>(dummy: T) -> T {
116118
// We need to "use" the argument in some way LLVM can't introspect, and on
117119
// targets that support it we can typically leverage inline assembly to do
118120
// this. LLVM's interpretation of inline assembly is that it's, well, a black

library/test/src/bench.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
//! Benchmarking module.
2-
pub use std::hint::black_box;
2+
pub use std::hint::pretend_used as black_box;
33

44
use super::{
55
event::CompletedTest, helpers::sink::Sink, options::BenchMode, test_result::TestResult,

src/test/ui/sanitize/address.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,12 @@
99

1010
#![feature(test)]
1111

12-
use std::hint::black_box;
12+
use std::hint::pretend_used;
1313

1414
fn main() {
1515
let xs = [0, 1, 2, 3];
1616
// Avoid optimizing everything out.
17-
let xs = black_box(xs.as_ptr());
17+
let xs = pretend_used(xs.as_ptr());
1818
let code = unsafe { *xs.offset(4) };
1919
std::process::exit(code);
2020
}

src/test/ui/sanitize/leak.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,14 @@
88

99
#![feature(test)]
1010

11-
use std::hint::black_box;
11+
use std::hint::pretend_used;
1212
use std::mem;
1313

1414
fn main() {
1515
for _ in 0..10 {
1616
let xs = vec![1, 2, 3];
1717
// Prevent compiler from removing the memory allocation.
18-
let xs = black_box(xs);
18+
let xs = pretend_used(xs);
1919
mem::forget(xs);
2020
}
2121
}

src/test/ui/sanitize/memory.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,15 @@
1515
#![feature(start)]
1616
#![feature(test)]
1717

18-
use std::hint::black_box;
18+
use std::hint::pretend_used;
1919
use std::mem::MaybeUninit;
2020

2121
#[inline(never)]
2222
#[no_mangle]
2323
fn random() -> [isize; 32] {
2424
let r = unsafe { MaybeUninit::uninit().assume_init() };
2525
// Avoid optimizing everything out.
26-
black_box(r)
26+
pretend_used(r)
2727
}
2828

2929
#[inline(never)]

0 commit comments

Comments
 (0)