Description
Location
The API documentation for black_box()
.
Summary
Please see rust-lang/rust-clippy#12707 for more context!
I believe the API docs for black_box()
are unclear when passed a unit-returning function. For example:
let x: () = black_box(thread::sleep(...));
The black_box()
call makes the returned value treated as volatile, which is pointless because it is a zero-sized type. The above code is equivalent1 to just:
let x: () = thread::sleep(...);
This is unclear in the documentation for black_box()
, though. The docs make it seem like the actual call to thread::sleep(...)
will be barred from optimizations, which isn't true. As pointed out by y21, constant-folding and other optimizations are still present:
// This...
let y = black_box(42 * 42);
// ...gets compiled to this:
let y = black_box(1764);
I propose that the docs be updated to mention that black_box()
makes the value appear volatile to the compiler, and does not affect the argument passed to it.