-
Notifications
You must be signed in to change notification settings - Fork 16
[Guideline] Add do not divide by 0 #132
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
Open
vapdrs
wants to merge
7
commits into
rustfoundation:main
Choose a base branch
from
vapdrs:feature/do-not-divide-by-0
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+51
−0
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
538c49c
[Guideline] Add do not divide by 0
vapdrs a976035
Remove example code from compliant example
vapdrs 899d826
Add compliant example with a suggestion
vapdrs 982d7e9
Lowercasing of guideline metadata
vapdrs 681be7e
Add Rust std link transformation
vapdrs 7fd5a7b
Clarify scope of expressions this applies to
vapdrs 9dabe78
Merge remote-tracking branch 'upstream/main' into feature/do-not-divi…
vapdrs File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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.
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.
Asking a bit of a practical question to folks. Does the combination of:
mandatory
undecidable
put too great of a burden on reviewers / auditors or is this "just the way it is and should be" given the nature of writing safety-critical code?
Tagging @AlexCeleste and @rcseacord for their thoughts as well.
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.
Just a bit of rationale for why I chose that combination. I used "mandatory" as a stand in for "very important" as a reason it goes above and beyond "required".
I would not want to mandate that no safety-critical programmer could never used unchecked arithmetic, the decidable subset version of this rule, but I do want to mandate that they do not take an action (divide by zero) that would lead to a panic.
Uh oh!
There was an error while loading. Please reload this page.
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.
Well, "do not divide by zero" is undecidable in the sense that statically knowing if the
b
ina / b
is0
is undecidable.However, the guideline is not impossible to comply with since the user themselves can check (
if b != 0
etc) but also because there are APIs that we can use for this, like theNonZero<T>
struct and thechecked_div
andchecked_rem
functions.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.
That is true, there are more clever ways to enforce this in Rust.
I would be fine mandating that any division must use checked functions, or have a
NonZero<T>
divisor (which elides any performance penalty from checking). This would then kick the can down the road, where as now your problem is don't useNonZero<T>::new_unchecked
and cause UB.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.
Definitely, I agree. Perhaps this is an instance of where we need to make use of the Exceptions section to list the sorts of suggestions you made. What do you two think?
TIL about
NonZero<T>
. Super cool.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.
The rule already shouldn't apply to this, it is specified as applying to:
which is the "built-in" rules of the ArithmeticExpression. Using
NonZero<T>
goes throughcore::ops::Div::div
which is already outside the scope of this guideline. I will clarify that to make it more explicit though.Uh oh!
There was an error while loading. Please reload this page.
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.
Indeed. But then,
new_unchecked
is anunsafe
function. So we've effectively eliminated all safe ways to create a division by zero, which I think is a good starting point.If nothing else, using
unsafe
should feel like a red flag for something as simple as integer division.