-
Notifications
You must be signed in to change notification settings - Fork 321
Clarify when safety may rely on correctness #523
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
ia0
wants to merge
1
commit into
rust-lang:master
Choose a base branch
from
ia0:unsafe
base: master
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.
+10
−4
Open
Changes from all commits
Commits
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
| Original file line number | Diff line number | Diff line change | ||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -58,12 +58,13 @@ interfaces built on top of these implementations can be assumed to be safe. | |||||||||||||||
| The need for all of this separation boils down to a single fundamental property | ||||||||||||||||
| of Safe Rust, the *soundness property*: | ||||||||||||||||
|
|
||||||||||||||||
| **No matter what, Safe Rust can't cause Undefined Behavior.** | ||||||||||||||||
| **No matter what, Safe Rust clients can't cause Undefined Behavior.** | ||||||||||||||||
|
|
||||||||||||||||
| The design of the safe/unsafe split means that there is an asymmetric trust | ||||||||||||||||
| relationship between Safe and Unsafe Rust. Safe Rust inherently has to | ||||||||||||||||
| trust that any Unsafe Rust it touches has been written correctly. | ||||||||||||||||
| On the other hand, Unsafe Rust cannot trust Safe Rust without care. | ||||||||||||||||
| On the other hand, Unsafe Rust cannot trust Safe Rust without care. It can | ||||||||||||||||
| trust Safe Rust dependencies but cannot trust Safe Rust clients. | ||||||||||||||||
|
|
||||||||||||||||
| As an example, Rust has the [`PartialOrd`] and [`Ord`] traits to differentiate | ||||||||||||||||
| between types which can "just" be compared, and those that provide a "total" | ||||||||||||||||
|
|
@@ -91,8 +92,13 @@ can be weighed against the benefit. In this case there's basically zero risk; | |||||||||||||||
| if integers and slices are broken, *everyone* is broken. Also, they're maintained | ||||||||||||||||
| by the same people who maintain `BTreeMap`, so it's easy to keep tabs on them. | ||||||||||||||||
|
|
||||||||||||||||
| On the other hand, `BTreeMap`'s key type is generic. Trusting its `Ord` implementation | ||||||||||||||||
| means trusting every `Ord` implementation in the past, present, and future. | ||||||||||||||||
| This difference also holds for arbitrary implementations of one very specific | ||||||||||||||||
| dependency. Unsafe Rust in crate `foo` (which depends on crate `bar`) may rely on | ||||||||||||||||
| Safe Rust in crate `bar` to be written correctly, regardless of the actual | ||||||||||||||||
| implementation. | ||||||||||||||||
|
Comment on lines
+95
to
+98
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. How about this wording? I'd avoid using two words, dependency and crate in the same paragraph.
Suggested change
|
||||||||||||||||
|
|
||||||||||||||||
| On the other hand, `BTreeMap`'s key type is generic. Trusting its `Ord` | ||||||||||||||||
| implementation means trusting the `Ord` implementation of arbitrary clients. | ||||||||||||||||
| Here the risk is high: someone somewhere is going to make a mistake and mess up | ||||||||||||||||
| their `Ord` implementation, or even just straight up lie about providing a total | ||||||||||||||||
| ordering because "it seems to work". When that happens, `BTreeMap` needs to be | ||||||||||||||||
|
|
||||||||||||||||
Oops, something went wrong.
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.
Maybe it'd be better to describe what cannot be trusted a bit more specific.