Skip to content

Expand docs on std::hint::assert_unchecked - #161262

Open
lukaslueg wants to merge 1 commit into
rust-lang:mainfrom
lukaslueg:assertuncheckeddocs
Open

Expand docs on std::hint::assert_unchecked#161262
lukaslueg wants to merge 1 commit into
rust-lang:mainfrom
lukaslueg:assertuncheckeddocs

Conversation

@lukaslueg

@lukaslueg lukaslueg commented Aug 17, 2026

Copy link
Copy Markdown
Contributor

This expands the documentation for std::hint::assert_unchecked(). I tried to rework+expand some rather imprecise language ("simplify things", "UB", "quite unlike anything that would be used in the real world"), reworked the "good usage"-example to not rely on assembly to make its point, and added a "a bool may answer more than you asked for"-counter-example / too-narrow / too-broad. Most importantly for review

  • Language was introduced to the effect that UB may time-travel, without getting into the weeds of it; one consequence of UB is that the argument to assert_unchecked is not guaranteed to be evaluated to the extend the programmer might expect from a bool; parts of the paragraph were aligned with the unreachable_unchecked docs.
  • On the other hand, assert_unchecked is not just a hint, as the argument needs to be evaluated before entering the function. So the question "How unchecked is unchecked?" can be answered to the effect that the compiler is not allowed to just completely ignore evaluating cond; effects of evaluating cond are guaranteed to be observed.
  • Language was introduced to the effect that a panic is guaranteed in debug mode; I'm not sure if ub_checks::assert_unsafe_precondition() should be guaranteed, but if it is, it is good to know for the reader.

I checked that codegen for the examples actually produces what the examples demonstrate.

All tokens used in generating this PR were of purely biological nature.

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-libs Relevant to the library team, which will review and decide on the PR/issue. labels Aug 17, 2026
@rustbot

rustbot commented Aug 17, 2026

Copy link
Copy Markdown
Collaborator

r? @Mark-Simulacrum

rustbot has assigned @Mark-Simulacrum.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

Why was this reviewer chosen?

The reviewer was selected based on:

  • Owners of files modified in this PR: libs
  • libs expanded to 12 candidates
  • Random selection from JohnTitor, Mark-Simulacrum, clarfonthey, nia-e, tgross35

@rust-log-analyzer

This comment has been minimized.

@lukaslueg
lukaslueg force-pushed the assertuncheckeddocs branch from 328a011 to 90204f5 Compare August 18, 2026 19:52

@Mark-Simulacrum Mark-Simulacrum left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Language was introduced to the effect that UB may time-travel

Technically, UB probably does not today time travel - rust-lang/unsafe-code-guidelines#407... I don't know if structuring the language to suggest otherwise makes sense.

Language was introduced to the effect that a panic is guaranteed in debug mode

I think our current policy is that all ub checks are not guaranteed, so we shouldn't commit to that.

View changes since this review

@rustbot rustbot added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Aug 21, 2026
@rustbot

rustbot commented Aug 21, 2026

Copy link
Copy Markdown
Collaborator

Reminder, once the PR becomes ready for a review, use @rustbot ready.

@lukaslueg

Copy link
Copy Markdown
Contributor Author

I'll change the PR. Lets wait on rust-lang/reference#2320; once that commits, we can link to it

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. T-libs Relevant to the library team, which will review and decide on the PR/issue.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants