Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Clarify black_box() docs when passed a unit-returning function #133923

Closed
BD103 opened this issue Dec 5, 2024 · 3 comments · Fixed by #133942
Closed

Clarify black_box() docs when passed a unit-returning function #133923

BD103 opened this issue Dec 5, 2024 · 3 comments · Fixed by #133942
Assignees
Labels
A-docs Area: documentation for any part of the project, including the compiler, standard library, and tools T-libs Relevant to the library team, which will review and decide on the PR/issue.

Comments

@BD103
Copy link
Contributor

BD103 commented Dec 5, 2024

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.

Footnotes

  1. Mostly equivalent. black_box() does prevent optimizations from call to jmp instructions, even when the function returns unit. See Godbolt. Are there any other possible optimizations that may make this meaningful?

@BD103 BD103 added the A-docs Area: documentation for any part of the project, including the compiler, standard library, and tools label Dec 5, 2024
@rustbot rustbot added the needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. label Dec 5, 2024
@jieyouxu jieyouxu added T-libs Relevant to the library team, which will review and decide on the PR/issue. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. and removed needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. labels Dec 5, 2024
@saethlin
Copy link
Member

saethlin commented Dec 5, 2024

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.

Those mean exactly the same thing.

But I agree that the docs should be improved, and I think an example of misuse like from y21 is probably the way to make the point that it's a normal function, not a different kind of syntax that obscures the expression between the parens.

@workingjubilee workingjubilee removed the T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. label Dec 5, 2024
@workingjubilee
Copy link
Member

Confirming: this is a library documentation issue.

@BD103
Copy link
Contributor Author

BD103 commented Dec 5, 2024

@rustbot claim

matthiaskrgr added a commit to matthiaskrgr/rust that referenced this issue Dec 12, 2024
Clarify how to use `black_box()`

Closes rust-lang#133923.

r? libs
^ (I think that's the right group, this is my first time!)

This PR adds further clarification on the [`black_box()`](https://doc.rust-lang.org/stable/std/hint/fn.black_box.html) documentation. Specifically, it teaches _how_ to use it, instead of just _when_ to use it.

I tried my best to make it clear and accurate, but a lot of my information is sourced from rust-lang/rust-clippy#12707 and [manually inspecting assembly](https://godbolt.org/). Please tell me if I got anything wrong!
compiler-errors added a commit to compiler-errors/rust that referenced this issue Dec 13, 2024
Clarify how to use `black_box()`

Closes rust-lang#133923.

r? libs
^ (I think that's the right group, this is my first time!)

This PR adds further clarification on the [`black_box()`](https://doc.rust-lang.org/stable/std/hint/fn.black_box.html) documentation. Specifically, it teaches _how_ to use it, instead of just _when_ to use it.

I tried my best to make it clear and accurate, but a lot of my information is sourced from rust-lang/rust-clippy#12707 and [manually inspecting assembly](https://godbolt.org/). Please tell me if I got anything wrong!
Zalathar added a commit to Zalathar/rust that referenced this issue Dec 13, 2024
Clarify how to use `black_box()`

Closes rust-lang#133923.

r? libs
^ (I think that's the right group, this is my first time!)

This PR adds further clarification on the [`black_box()`](https://doc.rust-lang.org/stable/std/hint/fn.black_box.html) documentation. Specifically, it teaches _how_ to use it, instead of just _when_ to use it.

I tried my best to make it clear and accurate, but a lot of my information is sourced from rust-lang/rust-clippy#12707 and [manually inspecting assembly](https://godbolt.org/). Please tell me if I got anything wrong!
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this issue Dec 13, 2024
Clarify how to use `black_box()`

Closes rust-lang#133923.

r? libs
^ (I think that's the right group, this is my first time!)

This PR adds further clarification on the [`black_box()`](https://doc.rust-lang.org/stable/std/hint/fn.black_box.html) documentation. Specifically, it teaches _how_ to use it, instead of just _when_ to use it.

I tried my best to make it clear and accurate, but a lot of my information is sourced from rust-lang/rust-clippy#12707 and [manually inspecting assembly](https://godbolt.org/). Please tell me if I got anything wrong!
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this issue Dec 13, 2024
Clarify how to use `black_box()`

Closes rust-lang#133923.

r? libs
^ (I think that's the right group, this is my first time!)

This PR adds further clarification on the [`black_box()`](https://doc.rust-lang.org/stable/std/hint/fn.black_box.html) documentation. Specifically, it teaches _how_ to use it, instead of just _when_ to use it.

I tried my best to make it clear and accurate, but a lot of my information is sourced from rust-lang/rust-clippy#12707 and [manually inspecting assembly](https://godbolt.org/). Please tell me if I got anything wrong!
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this issue Dec 13, 2024
Clarify how to use `black_box()`

Closes rust-lang#133923.

r? libs
^ (I think that's the right group, this is my first time!)

This PR adds further clarification on the [`black_box()`](https://doc.rust-lang.org/stable/std/hint/fn.black_box.html) documentation. Specifically, it teaches _how_ to use it, instead of just _when_ to use it.

I tried my best to make it clear and accurate, but a lot of my information is sourced from rust-lang/rust-clippy#12707 and [manually inspecting assembly](https://godbolt.org/). Please tell me if I got anything wrong!
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this issue Dec 13, 2024
Clarify how to use `black_box()`

Closes rust-lang#133923.

r? libs
^ (I think that's the right group, this is my first time!)

This PR adds further clarification on the [`black_box()`](https://doc.rust-lang.org/stable/std/hint/fn.black_box.html) documentation. Specifically, it teaches _how_ to use it, instead of just _when_ to use it.

I tried my best to make it clear and accurate, but a lot of my information is sourced from rust-lang/rust-clippy#12707 and [manually inspecting assembly](https://godbolt.org/). Please tell me if I got anything wrong!
Zalathar added a commit to Zalathar/rust that referenced this issue Dec 14, 2024
Clarify how to use `black_box()`

Closes rust-lang#133923.

r? libs
^ (I think that's the right group, this is my first time!)

This PR adds further clarification on the [`black_box()`](https://doc.rust-lang.org/stable/std/hint/fn.black_box.html) documentation. Specifically, it teaches _how_ to use it, instead of just _when_ to use it.

I tried my best to make it clear and accurate, but a lot of my information is sourced from rust-lang/rust-clippy#12707 and [manually inspecting assembly](https://godbolt.org/). Please tell me if I got anything wrong!
@bors bors closed this as completed in 8abb823 Dec 14, 2024
rust-timer added a commit to rust-lang-ci/rust that referenced this issue Dec 14, 2024
Rollup merge of rust-lang#133942 - BD103:black-box-docs, r=saethlin

Clarify how to use `black_box()`

Closes rust-lang#133923.

r? libs
^ (I think that's the right group, this is my first time!)

This PR adds further clarification on the [`black_box()`](https://doc.rust-lang.org/stable/std/hint/fn.black_box.html) documentation. Specifically, it teaches _how_ to use it, instead of just _when_ to use it.

I tried my best to make it clear and accurate, but a lot of my information is sourced from rust-lang/rust-clippy#12707 and [manually inspecting assembly](https://godbolt.org/). Please tell me if I got anything wrong!
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-docs Area: documentation for any part of the project, including the compiler, standard library, and tools T-libs Relevant to the library team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants