unsafe-deep-dive: replace may_overflow with a real safety precondition - #3244
Open
rmyndharis wants to merge 1 commit into
Open
unsafe-deep-dive: replace may_overflow with a real safety precondition#3244rmyndharis wants to merge 1 commit into
rmyndharis wants to merge 1 commit into
Conversation
The "Example: may_overflow" slide taught that signed integer overflow is undefined behavior in release mode and that the function therefore needs the `unsafe` keyword. Both claims are false: integer overflow is well defined in Rust (debug builds panic, release builds wrap), and `a + i32::MAX` does not require `unsafe` at all. Replace the example with an `unsafe fn element_at` built on `slice::get_unchecked`, which has a genuine safety precondition: the caller must keep the index in bounds. This preserves the slide's purpose (a concrete example of responsibility shifting to the programmer) and leads directly into the "Safety Preconditions" segment. The speaker notes state plainly that integer overflow is defined behavior and never requires `unsafe`. Closes google#3122.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
The "Example: may_overflow" slide taught that signed integer overflow is
undefined behavior in release mode and that the function therefore needs
the
unsafekeyword. Both claims are false: integer overflow is welldefined in Rust (debug builds panic, release builds wrap), and
a + i32::MAXdoes not requireunsafeat all.Replace the example with an
unsafe fn element_atbuilt onslice::get_unchecked, which has a genuine safety precondition: thecaller must keep the index in bounds. This preserves the slide's purpose
(a concrete example of responsibility shifting to the programmer) and
leads directly into the "Safety Preconditions" segment. The speaker notes
state plainly that integer overflow is defined behavior and never requires
unsafe.Closes #3122.