-
Notifications
You must be signed in to change notification settings - Fork 13.3k
Expand core::hint::unreachable_unchecked() docs #96154
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
Conversation
Hey! It looks like you've submitted a new PR for the library teams! If this PR contains changes to any Examples of
|
r? @scottmcm (rust-highfive has picked a reviewer for you, use r? to override) |
library/core/src/hint.rs
Outdated
/// divisors.retain(|divisor| *divisor != 0) | ||
/// } | ||
/// | ||
/// fn do_computation(i: u32, divisors: &[u32]) -> u32 { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This function should be marked unsafe
, to make it clear that the invariants established in prepare_inputs
must be upheld.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Agreed, and have a comment on it like
/// fn do_computation(i: u32, divisors: &[u32]) -> u32 { | |
/// /// # Safety | |
/// /// All the elements of `divisors` must be non-zero. | |
/// fn do_computation(i: u32, divisors: &[u32]) -> u32 { |
As well as // Safety: ...
comment on the unsafe
block for the call.
Thank you for cleaning up the docs and providing an example! I think this example is not ideal, since it could be replaced by using enum Mascot {
Ferris,
Corro {
unsafe_permit: String,
},
}
impl Mascot {
unsafe fn get_unsafe_permit_unchecked(&self) -> &str {
match self {
Mascot::Corro { unsafe_permit } => {
unsafe_permit
}
Mascot::Ferris => {
std::hint::unreachable_unchecked()
}
}
}
} I think using such an example would better illustrate the unique capabilities of this function, since it cannot be implemented in other ways. (feel free to use my example or choose another one) |
@Nilstrieb I know about |
library/core/src/hint.rs
Outdated
/// therefore will eliminate all branches that reach to a call to | ||
/// Reaching this function is *Undefined Behavior* (UB). In particular, as the | ||
/// compiler assumes that all forms of Undefined Behavior can never happen, it | ||
/// will eliminate all branches which themselves reach a call to |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: like you said "possibly enabling" above, I think this should also update accordingly. And I maybe "reach" could be tweaked too, since the branch arguably doesn't directly "reach" a call, but reaches a block containing the call?
/// will eliminate all branches which themselves reach a call to | |
/// may eliminate all branches which themselves lead to a call to |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You are referring to "possibly" -> "it will possibly eliminate"? I suggest keeping using strong language here because the programmer should always assume that the compiler does eliminate those branches, even if it technically doesn't, yet does other funky stuff which causes time travel and nose daemons.
Maybe more clear: "it will eliminate all branches in the surrounding code which the compiler can determine will invariably lead to a call to unreachable_unchecked!
."
library/core/src/hint.rs
Outdated
/// | ||
/// ``` | ||
/// | ||
/// While using `unreachable_unchecked()` is perfectly safe in the following |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fix: We try to be really careful about "safe" in these docs, since it implies "not unsafe". I think this should be
/// While using `unreachable_unchecked()` is perfectly safe in the following | |
/// While using `unreachable_unchecked()` is perfectly sound in the following |
Addressed previous comments. Maybe I should add a comment to the tune that the behavior shown in the example is provided by |
/// that is, if the site which is calling `unreachable_unchecked()` is actually | ||
/// reachable at runtime - the compiler may have generated nonsensical machine | ||
/// instructions for this situation, including in seemingly unrelated code, | ||
/// causing difficult-to-debug problems. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Arguably "difficult to debug" is the least of the problems with UB, but that was already here, so this PR doesn't really need to fix it.
Thanks! I agree this is better than what was there before, so it's good to go in: @bors r+ rollup=always For future reference, to update the attributes on a PR once you've made changes, you can use |
📌 Commit cd1746b has been approved by |
…askrgr Rollup of 5 pull requests Successful merges: - rust-lang#96154 (Expand core::hint::unreachable_unchecked() docs) - rust-lang#96615 (Add a regression test for rust-lang#54779) - rust-lang#96982 (fix clippy expect_fun_call) - rust-lang#97003 (Remove some unnecessary `rustc_allow_const_fn_unstable` attributes.) - rust-lang#97011 (Add regression test for rust-lang#28935) Failed merges: r? `@ghost` `@rustbot` modify labels: rollup
Rework the docs for
unreachable_unchecked
, encouraging deliberate use, and providing a better example for action at a distance.Fixes #95865