Skip to content

Commit a4b3dbe

Browse files
committed
Improve the documentation for std::hint::black_box.
1 parent 4bc1ce7 commit a4b3dbe

File tree

1 file changed

+12
-4
lines changed

1 file changed

+12
-4
lines changed

src/libcore/hint.rs

+12-4
Original file line numberDiff line numberDiff line change
@@ -104,11 +104,19 @@ pub fn spin_loop() {
104104
}
105105
}
106106

107-
/// A function that is opaque to the optimizer, to allow benchmarks to
108-
/// pretend to use outputs to assist in avoiding dead-code
109-
/// elimination.
107+
/// An identity function that *__hints__* to the compiler to be maximally pessimistic about what
108+
/// `black_box` could do.
110109
///
111-
/// This function is a no-op, and does not even read from `dummy`.
110+
/// [`std::convert::identity`]: https://doc.rust-lang.org/core/convert/fn.identity.html
111+
///
112+
/// Unlike [`std::convert::identity`], a Rust compiler is encouraged to assume that `black_box` can
113+
/// use `x` in any possible valid way that Rust code is allowed to without introducing undefined
114+
/// behavior in the calling code. This property makes `black_box` useful for writing code in which
115+
/// certain optimizations are not desired, such as benchmarks.
116+
///
117+
/// Note however, that `black_box` is only (and can only be) provided on a "best-effort" basis. The
118+
/// extent to which it can block optimisations may vary depending upon the platform and code-gen
119+
/// backend used. Programs cannot rely on `black_box` for *correctness* in any way.
112120
#[inline]
113121
#[unstable(feature = "test", issue = "27812")]
114122
#[allow(unreachable_code)] // this makes #[cfg] a bit easier below.

0 commit comments

Comments
 (0)