diff options
| author | bors <bors@rust-lang.org> | 2019-11-22 22:03:11 +0000 |
|---|---|---|
| committer | bors <bors@rust-lang.org> | 2019-11-22 22:03:11 +0000 |
| commit | a449535bbc7912c4adc1bbf2ab2738d0442f212c (patch) | |
| tree | 40cae34afe14332b5c35ee5d819261fbaffcaee7 /src/libcore | |
| parent | 5fa0af2327944bd806b2fa382d4e983149ae7e4a (diff) | |
| parent | 56512b9d427012321384b91dcde5dc8daa83cf89 (diff) | |
| download | rust-a449535bbc7912c4adc1bbf2ab2738d0442f212c.tar.gz rust-a449535bbc7912c4adc1bbf2ab2738d0442f212c.zip | |
Auto merge of #66640 - Centril:rollup-862009l, r=Centril
Rollup of 8 pull requests Successful merges: - #66183 (*Syntactically* permit visibilities on trait items & enum variants) - #66566 (Document pitfall with `impl PartialEq<B> for A`) - #66575 (Remove pretty printing of specific nodes in AST) - #66587 (Handle statics in MIR as const pointers) - #66619 (follow the convention in this file to use third-person singular verbs) - #66633 (Error code's long explanation cleanup) - #66637 (fix reoccuring typo: dereferencable -> dereferenceable) - #66639 (resolve: more declarative `fresh_binding`) Failed merges: r? @ghost
Diffstat (limited to 'src/libcore')
| -rw-r--r-- | src/libcore/cmp.rs | 32 | ||||
| -rw-r--r-- | src/libcore/iter/traits/iterator.rs | 4 |
2 files changed, 20 insertions, 16 deletions
diff --git a/src/libcore/cmp.rs b/src/libcore/cmp.rs index 1e2e595f1dd..eb5121dd0e0 100644 --- a/src/libcore/cmp.rs +++ b/src/libcore/cmp.rs @@ -135,10 +135,15 @@ use self::Ordering::*; /// By changing `impl PartialEq for Book` to `impl PartialEq<BookFormat> for Book`, /// we allow `BookFormat`s to be compared with `Book`s. /// -/// You can also combine these implementations to let the `==` operator work with -/// two different types: -/// -/// ``` +/// A comparison like the one above, which ignores some fields of the struct, +/// can be dangerous. It can easily lead to an unintended violation of the +/// requirements for a partial equivalence relation. For example, if we kept +/// the above implementation of `PartialEq<Book>` for `BookFormat` and added an +/// implementation of `PartialEq<Book>` for `Book` (either via a `#[derive]` or +/// via the manual implementation from the first example) then the result would +/// violate transitivity: +/// +/// ```should_panic /// #[derive(PartialEq)] /// enum BookFormat { /// Paperback, @@ -146,6 +151,7 @@ use self::Ordering::*; /// Ebook, /// } /// +/// #[derive(PartialEq)] /// struct Book { /// isbn: i32, /// format: BookFormat, @@ -163,18 +169,16 @@ use self::Ordering::*; /// } /// } /// -/// impl PartialEq for Book { -/// fn eq(&self, other: &Book) -> bool { -/// self.isbn == other.isbn -/// } -/// } +/// fn main() { +/// let b1 = Book { isbn: 1, format: BookFormat::Paperback }; +/// let b2 = Book { isbn: 2, format: BookFormat::Paperback }; /// -/// let b1 = Book { isbn: 3, format: BookFormat::Paperback }; -/// let b2 = Book { isbn: 3, format: BookFormat::Ebook }; +/// assert!(b1 == BookFormat::Paperback); +/// assert!(BookFormat::Paperback == b2); /// -/// assert!(b1 == BookFormat::Paperback); -/// assert!(BookFormat::Ebook != b1); -/// assert!(b1 == b2); +/// // The following should hold by transitivity but doesn't. +/// assert!(b1 == b2); // <-- PANICS +/// } /// ``` /// /// # Examples diff --git a/src/libcore/iter/traits/iterator.rs b/src/libcore/iter/traits/iterator.rs index b7a35568e3f..61e8b07511a 100644 --- a/src/libcore/iter/traits/iterator.rs +++ b/src/libcore/iter/traits/iterator.rs @@ -1255,7 +1255,7 @@ pub trait Iterator { Fuse::new(self) } - /// Do something with each element of an iterator, passing the value on. + /// Does something with each element of an iterator, passing the value on. /// /// When using iterators, you'll often chain several of them together. /// While working on such code, you might want to check out what's @@ -1548,7 +1548,7 @@ pub trait Iterator { (left, right) } - /// Reorder the elements of this iterator *in-place* according to the given predicate, + /// Reorders the elements of this iterator *in-place* according to the given predicate, /// such that all those that return `true` precede all those that return `false`. /// Returns the number of `true` elements found. /// |
