-
Notifications
You must be signed in to change notification settings - Fork 13.6k
document assumptions about Clone
and Eq
traits
#144330
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
gewitternacht
wants to merge
4
commits into
rust-lang:master
Choose a base branch
from
gewitternacht:document-clone-eq
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.
+29
−0
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
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 |
---|---|---|
|
@@ -139,6 +139,30 @@ mod uninit; | |
/// // Note: With the manual implementations the above line will compile. | ||
/// ``` | ||
/// | ||
/// ## `Clone` and `PartialEq`/`Eq` | ||
/// `Clone` is intended for the duplication of objects. Consequently, when implementing | ||
/// both `Clone` and [`PartialEq`], the following property is expected to hold: | ||
/// ```text | ||
/// x == x -> x.clone() == x | ||
/// ``` | ||
/// In other words, if an object compares equal to itself, | ||
/// its clone must also compare equal to the original. | ||
/// | ||
/// For types that also implement [`Eq`] – for which `x == x` always holds – | ||
/// this implies that `x.clone() == x` must always be true. | ||
/// Standard library collections such as | ||
/// [`HashMap`], [`HashSet`], [`BTreeMap`], [`BTreeSet`] and [`BinaryHeap`] | ||
/// rely on their keys respecting this property for correct behavior. | ||
/// | ||
/// This property is automatically satisfied when deriving both `Clone` and [`PartialEq`] | ||
/// using `#[derive(Clone, PartialEq)]` or when additionally deriving [`Eq`] | ||
/// using `#[derive(Clone, PartialEq, Eq)]`. | ||
Comment on lines
+157
to
+159
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. Strictly speaking this isn't correct: it relies on the inner types also properly implementing the traits. This should be reworded to say that deriving is fine because it relies on this being properly implemented by the underlying types. |
||
/// | ||
/// Violating this property is a logic error. The behavior resulting from a logic error is not | ||
/// specified, but users of the trait must ensure that such logic errors do *not* result in | ||
/// undefined behavior. This means that `unsafe` code **must not** rely on this property | ||
/// being satisfied. | ||
/// | ||
/// ## Additional implementors | ||
/// | ||
/// In addition to the [implementors listed below][impls], | ||
|
@@ -152,6 +176,11 @@ mod uninit; | |
/// (even if the referent doesn't), | ||
/// while variables captured by mutable reference never implement `Clone`. | ||
/// | ||
/// [`HashMap`]: ../../std/collections/struct.HashMap.html | ||
/// [`HashSet`]: ../../std/collections/struct.HashSet.html | ||
/// [`BTreeMap`]: ../../std/collections/struct.BTreeMap.html | ||
/// [`BTreeSet`]: ../../std/collections/struct.BTreeSet.html | ||
/// [`BinaryHeap`]: ../../std/collections/struct.BinaryHeap.html | ||
/// [impls]: #implementors | ||
#[stable(feature = "rust1", since = "1.0.0")] | ||
#[lang = "clone"] | ||
|
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.
It's unclear whether this should apply to
PartialEq
.However this definitely needs to be extended to include
Hash
andOrd
sinceHashMap
andBTreeMap
rely on this. It could also have vague wording that this may extend to other traits as well.