From 1e493fd979c826c44b2fa5d4b74302d405fbd17d Mon Sep 17 00:00:00 2001 From: "Carol (Nichols || Goulding)" Date: Fri, 20 May 2016 15:50:34 -0400 Subject: [PATCH 01/31] Add explanations about what derived trait implementations do --- src/libcore/clone.rs | 3 ++- src/libcore/cmp.rs | 9 +++++++-- src/libcore/fmt/mod.rs | 6 +++++- src/libcore/hash/mod.rs | 4 +++- src/libcore/marker.rs | 3 ++- 5 files changed, 19 insertions(+), 6 deletions(-) diff --git a/src/libcore/clone.rs b/src/libcore/clone.rs index e8ea993c694..cfb29cf479d 100644 --- a/src/libcore/clone.rs +++ b/src/libcore/clone.rs @@ -48,7 +48,8 @@ use marker::Sized; /// A common trait for cloning an object. /// -/// This trait can be used with `#[derive]`. +/// This trait can be used with `#[derive]` if all fields are `Clone`. The `derive`d +/// implementation of `clone()` calls `clone()` on each field. /// /// Types that are `Copy` should have a trivial implementation of `Clone`. More formally: /// if `T: Copy`, `x: T`, and `y: &T`, then `let x = y.clone();` is equivalent to `let x = *y;`. diff --git a/src/libcore/cmp.rs b/src/libcore/cmp.rs index d3481ba3f05..cd0bbcd3356 100644 --- a/src/libcore/cmp.rs +++ b/src/libcore/cmp.rs @@ -58,7 +58,10 @@ use option::Option::{self, Some}; /// the rule that `eq` is a strict inverse of `ne`; that is, `!(a == b)` if and /// only if `a != b`. /// -/// This trait can be used with `#[derive]`. +/// This trait can be used with `#[derive]`. When `derive`d on structs, two +/// instances are equal if all fields are equal, and non equal if any fields +/// are not equal. When `derive`d on enums, each variant is equal to itself +/// and not equal to the other variants. /// /// # Examples /// @@ -96,7 +99,9 @@ pub trait PartialEq { /// This property cannot be checked by the compiler, and therefore `Eq` implies /// `PartialEq`, and has no extra methods. /// -/// This trait can be used with `#[derive]`. +/// This trait can be used with `#[derive]`. When `derive`d, because `Eq` has +/// no extra methods, it is only informing the compiler that this is an +/// equivalence relation rather than a partial equivalence relation. #[stable(feature = "rust1", since = "1.0.0")] pub trait Eq: PartialEq { // FIXME #13101: this method is used solely by #[deriving] to diff --git a/src/libcore/fmt/mod.rs b/src/libcore/fmt/mod.rs index dde4d03dad8..6579e5dab54 100644 --- a/src/libcore/fmt/mod.rs +++ b/src/libcore/fmt/mod.rs @@ -318,7 +318,11 @@ impl<'a> Display for Arguments<'a> { /// /// [module]: ../../std/fmt/index.html /// -/// This trait can be used with `#[derive]`. +/// This trait can be used with `#[derive]` if all fields implement `Debug`. When +/// `derive`d for structs, it will use the name of the `struct`, then `{`, then a +/// comma-separated list of each field's name and `Debug` value, then `}`. For +/// `enum`s, it will use the name of the variant and, if applicable, `(`, then the +/// `Debug` values of the fields, then `)`. /// /// # Examples /// diff --git a/src/libcore/hash/mod.rs b/src/libcore/hash/mod.rs index 4d0fed98334..7f0d7517a57 100644 --- a/src/libcore/hash/mod.rs +++ b/src/libcore/hash/mod.rs @@ -97,7 +97,9 @@ mod sip; /// In other words, if two keys are equal, their hashes should also be equal. /// `HashMap` and `HashSet` both rely on this behavior. /// -/// This trait can be used with `#[derive]`. +/// This trait can be used with `#[derive]` if all fields implement `Hash`. +/// When `derive`d, the resulting hash will be the combination of the values +/// from calling `.hash()` on each field. #[stable(feature = "rust1", since = "1.0.0")] pub trait Hash { /// Feeds this value into the state given, updating the hasher as necessary. diff --git a/src/libcore/marker.rs b/src/libcore/marker.rs index 1ed2a219fac..e519071a56e 100644 --- a/src/libcore/marker.rs +++ b/src/libcore/marker.rs @@ -173,7 +173,8 @@ pub trait Unsize { /// /// # Derivable /// -/// This trait can be used with `#[derive]`. +/// This trait can be used with `#[derive]` if all of its components implement `Copy` and the type +/// implements `Clone`. The implementation will copy the bytes of each field using `memcpy`. #[stable(feature = "rust1", since = "1.0.0")] #[lang = "copy"] pub trait Copy : Clone { From bbfb6e762b65bdb34fbafef540e5c32408f213d0 Mon Sep 17 00:00:00 2001 From: "Carol (Nichols || Goulding)" Date: Sat, 21 May 2016 10:39:31 -0400 Subject: [PATCH 02/31] `derive` explanation for PartialOrd should match that for Ord I think these just got out of sync, but both use a lexicographic ordering. Relevant commits in the history of these explanations: * 8b81f76 on 2015-06-30 * e22770b on 2016-02-09 --- src/libcore/cmp.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/libcore/cmp.rs b/src/libcore/cmp.rs index cd0bbcd3356..75cd56b79f4 100644 --- a/src/libcore/cmp.rs +++ b/src/libcore/cmp.rs @@ -254,8 +254,8 @@ impl PartialOrd for Ordering { /// total order. For example, for floating point numbers, `NaN < 0 == false` and `NaN >= 0 == /// false` (cf. IEEE 754-2008 section 5.11). /// -/// This trait can be used with `#[derive]`. When `derive`d, it will produce an ordering -/// based on the top-to-bottom declaration order of the struct's members. +/// This trait can be used with `#[derive]`. When `derive`d, it will produce a lexicographic +/// ordering based on the top-to-bottom declaration order of the struct's members. /// /// # Examples /// From 2f4405333243c3bb0b3a210201c98a69fe7a1c4a Mon Sep 17 00:00:00 2001 From: "Carol (Nichols || Goulding)" Date: Sat, 21 May 2016 12:01:01 -0400 Subject: [PATCH 03/31] Make `Derivable` header be an h2 instead of an h1 This matches the other subsections. --- src/libcore/marker.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/libcore/marker.rs b/src/libcore/marker.rs index e519071a56e..150a091c4bd 100644 --- a/src/libcore/marker.rs +++ b/src/libcore/marker.rs @@ -171,7 +171,7 @@ pub trait Unsize { /// then it might be prudent to not implement `Copy`. This is because removing `Copy` is a breaking /// change: that second example would fail to compile if we made `Foo` non-`Copy`. /// -/// # Derivable +/// ## Derivable /// /// This trait can be used with `#[derive]` if all of its components implement `Copy` and the type /// implements `Clone`. The implementation will copy the bytes of each field using `memcpy`. From fc467b31c2db57762ffed20a1305bb8830178071 Mon Sep 17 00:00:00 2001 From: "Carol (Nichols || Goulding)" Date: Sat, 21 May 2016 11:54:29 -0400 Subject: [PATCH 04/31] Reorder `Copy` doc sections The new order puts all the "when" questions together and puts the "how" question with the "derivable" section. So you have to scroll past (and hopefully read) the can/cannot/should caveats and guidelines to get to the information about how to actually go about doing it once you've determined that you can and should, with derivable information first so that you can just use the derived implementation if that applies. Previous order: * General explanation * When can my type be `Copy`? * How can I implement `Copy`? * When can my type _not_ be `Copy`? * When should my type be `Copy`? * Derivable New order: * General explanation * When can my type be `Copy`? * When can my type _not_ be `Copy`? * When should my type be `Copy`? * Derivable * How can I implement `Copy`? --- src/libcore/marker.rs | 40 ++++++++++++++++++++-------------------- 1 file changed, 20 insertions(+), 20 deletions(-) diff --git a/src/libcore/marker.rs b/src/libcore/marker.rs index 150a091c4bd..c18d230be31 100644 --- a/src/libcore/marker.rs +++ b/src/libcore/marker.rs @@ -136,26 +136,6 @@ pub trait Unsize { /// the trait `Copy` may not be implemented for this type; field `points` does not implement `Copy` /// ``` /// -/// ## How can I implement `Copy`? -/// -/// There are two ways to implement `Copy` on your type: -/// -/// ``` -/// #[derive(Copy, Clone)] -/// struct MyStruct; -/// ``` -/// -/// and -/// -/// ``` -/// struct MyStruct; -/// impl Copy for MyStruct {} -/// impl Clone for MyStruct { fn clone(&self) -> MyStruct { *self } } -/// ``` -/// -/// There is a small difference between the two: the `derive` strategy will also place a `Copy` -/// bound on type parameters, which isn't always desired. -/// /// ## When can my type _not_ be `Copy`? /// /// Some types can't be copied safely. For example, copying `&mut T` would create an aliased @@ -175,6 +155,26 @@ pub trait Unsize { /// /// This trait can be used with `#[derive]` if all of its components implement `Copy` and the type /// implements `Clone`. The implementation will copy the bytes of each field using `memcpy`. +/// +/// ## How can I implement `Copy`? +/// +/// There are two ways to implement `Copy` on your type: +/// +/// ``` +/// #[derive(Copy, Clone)] +/// struct MyStruct; +/// ``` +/// +/// and +/// +/// ``` +/// struct MyStruct; +/// impl Copy for MyStruct {} +/// impl Clone for MyStruct { fn clone(&self) -> MyStruct { *self } } +/// ``` +/// +/// There is a small difference between the two: the `derive` strategy will also place a `Copy` +/// bound on type parameters, which isn't always desired. #[stable(feature = "rust1", since = "1.0.0")] #[lang = "copy"] pub trait Copy : Clone { From e831c72a299a9f3320f30b4ff22d100092d56640 Mon Sep 17 00:00:00 2001 From: "Carol (Nichols || Goulding)" Date: Sat, 21 May 2016 12:55:13 -0400 Subject: [PATCH 05/31] Add an explicit "How can I implement `PartialEq`"? doc section Including an example of a custom implementation. I put this expanded section after the `Derivable` section to encourage use of that first. --- src/libcore/cmp.rs | 35 +++++++++++++++++++++++++++++++---- 1 file changed, 31 insertions(+), 4 deletions(-) diff --git a/src/libcore/cmp.rs b/src/libcore/cmp.rs index 75cd56b79f4..f99358b09f4 100644 --- a/src/libcore/cmp.rs +++ b/src/libcore/cmp.rs @@ -53,16 +53,43 @@ use option::Option::{self, Some}; /// symmetrically and transitively: if `T: PartialEq` and `U: PartialEq` /// then `U: PartialEq` and `T: PartialEq`. /// -/// PartialEq only requires the `eq` method to be implemented; `ne` is defined -/// in terms of it by default. Any manual implementation of `ne` *must* respect -/// the rule that `eq` is a strict inverse of `ne`; that is, `!(a == b)` if and -/// only if `a != b`. +/// ## Derivable /// /// This trait can be used with `#[derive]`. When `derive`d on structs, two /// instances are equal if all fields are equal, and non equal if any fields /// are not equal. When `derive`d on enums, each variant is equal to itself /// and not equal to the other variants. /// +/// ## How can I implement `PartialEq`? +/// +/// PartialEq only requires the `eq` method to be implemented; `ne` is defined +/// in terms of it by default. Any manual implementation of `ne` *must* respect +/// the rule that `eq` is a strict inverse of `ne`; that is, `!(a == b)` if and +/// only if `a != b`. +/// +/// An example implementation for a domain in which two books are considered +/// the same book if their ISBN matches, even if the formats differ: +/// +/// ``` +/// enum BookFormat { Paperback, Hardback, Ebook } +/// struct Book { +/// isbn: i32, +/// format: BookFormat, +/// } +/// impl PartialEq for Book { +/// fn eq(&self, other: &Self) -> bool { +/// self.isbn == other.isbn +/// } +/// } +/// +/// let b1 = Book { isbn: 3, format: BookFormat::Paperback }; +/// let b2 = Book { isbn: 3, format: BookFormat::Ebook }; +/// let b3 = Book { isbn: 10, format: BookFormat::Paperback }; +/// +/// assert!(b1 == b2); +/// assert!(b1 != b3); +/// ``` +/// /// # Examples /// /// ``` From 54d2ef0e8ef8b03b2a0548512905484d3f094a99 Mon Sep 17 00:00:00 2001 From: "Carol (Nichols || Goulding)" Date: Sat, 21 May 2016 13:05:15 -0400 Subject: [PATCH 06/31] Add an explicit "How can I implement `Eq`" doc section Building on the example in PartialEq. --- src/libcore/cmp.rs | 25 ++++++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/src/libcore/cmp.rs b/src/libcore/cmp.rs index f99358b09f4..d536d3ada3f 100644 --- a/src/libcore/cmp.rs +++ b/src/libcore/cmp.rs @@ -126,9 +126,32 @@ pub trait PartialEq { /// This property cannot be checked by the compiler, and therefore `Eq` implies /// `PartialEq`, and has no extra methods. /// +/// ## Derivable +/// /// This trait can be used with `#[derive]`. When `derive`d, because `Eq` has /// no extra methods, it is only informing the compiler that this is an -/// equivalence relation rather than a partial equivalence relation. +/// equivalence relation rather than a partial equivalence relation. Note that +/// the `derive` strategy requires all fields are `PartialEq`, which isn't +/// always desired. +/// +/// ## How can I implement `Eq`? +/// +/// If you cannot use the `derive` strategy, specify that your type implements +/// `Eq`, which has no methods: +/// +/// ``` +/// enum BookFormat { Paperback, Hardback, Ebook } +/// struct Book { +/// isbn: i32, +/// format: BookFormat, +/// } +/// impl PartialEq for Book { +/// fn eq(&self, other: &Self) -> bool { +/// self.isbn == other.isbn +/// } +/// } +/// impl Eq for Book {} +/// ``` #[stable(feature = "rust1", since = "1.0.0")] pub trait Eq: PartialEq { // FIXME #13101: this method is used solely by #[deriving] to From c41227fefc449b08f3535758bd75b4b19866e0f7 Mon Sep 17 00:00:00 2001 From: "Carol (Nichols || Goulding)" Date: Sun, 22 May 2016 18:06:13 -0400 Subject: [PATCH 07/31] Add more detail to `Clone`'s documentation Used as resources: - https://users.rust-lang.org/t/whats-the-difference-between-trait-copy-and-clone/2609/2?u=carols10cents --- src/libcore/clone.rs | 26 +++++++++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) diff --git a/src/libcore/clone.rs b/src/libcore/clone.rs index cfb29cf479d..629808cb124 100644 --- a/src/libcore/clone.rs +++ b/src/libcore/clone.rs @@ -46,15 +46,39 @@ use marker::Sized; -/// A common trait for cloning an object. +/// A common trait for cloning an object. Differs from `Copy` in that you can +/// define `Clone` to run arbitrary code, while you are not allowed to override +/// the implementation of `Copy` that only does a `memcpy`. +/// +/// Since `Clone` is more general than `Copy`, you can automatically make anything +/// `Copy` be `Clone` as well. +/// +/// ## Derivable /// /// This trait can be used with `#[derive]` if all fields are `Clone`. The `derive`d /// implementation of `clone()` calls `clone()` on each field. /// +/// ## How can I implement `Clone`? +/// /// Types that are `Copy` should have a trivial implementation of `Clone`. More formally: /// if `T: Copy`, `x: T`, and `y: &T`, then `let x = y.clone();` is equivalent to `let x = *y;`. /// Manual implementations should be careful to uphold this invariant; however, unsafe code /// must not rely on it to ensure memory safety. +/// +/// An example is an array holding more than 32 of a type that is `Clone`; the standard library +/// only implements `Clone` up until arrays of size 32. In this case, the implementation of +/// `Clone` cannot be `derive`d, but can be implemented as: +/// +/// ``` +/// #[derive(Copy)] +/// struct Stats { +/// frequencies: [i32; 100], +/// } +/// +/// impl Clone for Stats { +/// fn clone(&self) -> Self { *self } +/// } +/// ``` #[stable(feature = "rust1", since = "1.0.0")] pub trait Clone : Sized { /// Returns a copy of the value. From 61bb9b2d0709647e2f8532f07c6aa0304cfb464e Mon Sep 17 00:00:00 2001 From: "Carol (Nichols || Goulding)" Date: Sun, 22 May 2016 18:29:13 -0400 Subject: [PATCH 08/31] Add more information about implementing `Hash` A bit of duplication from the module documentation, but simplified to be closer to being trivially copy-paste-able. --- src/libcore/hash/mod.rs | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/src/libcore/hash/mod.rs b/src/libcore/hash/mod.rs index 7f0d7517a57..844a24e80d1 100644 --- a/src/libcore/hash/mod.rs +++ b/src/libcore/hash/mod.rs @@ -97,9 +97,33 @@ mod sip; /// In other words, if two keys are equal, their hashes should also be equal. /// `HashMap` and `HashSet` both rely on this behavior. /// +/// ## Derivable +/// /// This trait can be used with `#[derive]` if all fields implement `Hash`. /// When `derive`d, the resulting hash will be the combination of the values /// from calling `.hash()` on each field. +/// +/// ## How can I implement `Hash`? +/// +/// If you need more control over how a value is hashed, you need to implement +/// the trait `Hash`: +/// +/// ``` +/// use std::hash::{Hash, Hasher}; +/// +/// struct Person { +/// id: u32, +/// name: String, +/// phone: u64, +/// } +/// +/// impl Hash for Person { +/// fn hash(&self, state: &mut H) { +/// self.id.hash(state); +/// self.phone.hash(state); +/// } +/// } +/// ``` #[stable(feature = "rust1", since = "1.0.0")] pub trait Hash { /// Feeds this value into the state given, updating the hasher as necessary. From 9efa44565610054a790c64cda8cdb086e5fbc21c Mon Sep 17 00:00:00 2001 From: "Carol (Nichols || Goulding)" Date: Sun, 22 May 2016 19:02:38 -0400 Subject: [PATCH 09/31] Add an explicit "How can I implement `Ord`" doc section References: - http://stackoverflow.com/q/29884402/51683 - http://stackoverflow.com/q/28387711/51683 --- src/libcore/cmp.rs | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/src/libcore/cmp.rs b/src/libcore/cmp.rs index d536d3ada3f..15e128c57da 100644 --- a/src/libcore/cmp.rs +++ b/src/libcore/cmp.rs @@ -245,8 +245,48 @@ impl Ordering { /// - total and antisymmetric: exactly one of `a < b`, `a == b` or `a > b` is true; and /// - transitive, `a < b` and `b < c` implies `a < c`. The same must hold for both `==` and `>`. /// +/// ## Derivable +/// /// This trait can be used with `#[derive]`. When `derive`d, it will produce a lexicographic /// ordering based on the top-to-bottom declaration order of the struct's members. +/// +/// ## How can I implement `Ord`? +/// +/// `Ord` requires that the type also be `PartialOrd` and `Eq` (which requires `PartialEq`). +/// +/// Then you must define an implementation for `cmp`. You may find it useful to use +/// `cmp` on your type's fields. +/// +/// Here's an example where you want to sort people by height only, disregarding `id` +/// and `name`: +/// +/// ``` +/// use std::cmp::Ordering; +/// #[derive(Eq)] +/// struct Person { +/// id: u32, +/// name: String, +/// height: u32, +/// } +/// +/// impl Ord for Person { +/// fn cmp(&self, other: &Self) -> Ordering { +/// self.height.cmp(&other.height) +/// } +/// } +/// +/// impl PartialOrd for Person { +/// fn partial_cmp(&self, other: &Self) -> Option { +/// Some(self.cmp(other)) +/// } +/// } +/// +/// impl PartialEq for Person { +/// fn eq(&self, other: &Self) -> bool { +/// self.height == other.height +/// } +/// } +/// ``` #[stable(feature = "rust1", since = "1.0.0")] pub trait Ord: Eq + PartialOrd { /// This method returns an `Ordering` between `self` and `other`. From 8b00a086e78faa7ca0e0be539f0587df41b31fb7 Mon Sep 17 00:00:00 2001 From: "Carol (Nichols || Goulding)" Date: Sun, 22 May 2016 19:14:38 -0400 Subject: [PATCH 10/31] Add an explicit "How can I implement `PartialOrd`" doc section Similar to the `Ord` examples but calling out that it can be defined using `cmp` from `Ord` or using `partial_cmp` in a situation that demands that. --- src/libcore/cmp.rs | 46 ++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 44 insertions(+), 2 deletions(-) diff --git a/src/libcore/cmp.rs b/src/libcore/cmp.rs index 15e128c57da..5b8620f4b8e 100644 --- a/src/libcore/cmp.rs +++ b/src/libcore/cmp.rs @@ -337,6 +337,13 @@ impl PartialOrd for Ordering { /// transitively: if `T: PartialOrd` and `U: PartialOrd` then `U: PartialOrd` and `T: /// PartialOrd`. /// +/// ## Derivable +/// +/// This trait can be used with `#[derive]`. When `derive`d, it will produce a lexicographic +/// ordering based on the top-to-bottom declaration order of the struct's members. +/// +/// ## How can I implement `Ord`? +/// /// PartialOrd only requires implementation of the `partial_cmp` method, with the others generated /// from default implementations. /// @@ -344,8 +351,43 @@ impl PartialOrd for Ordering { /// total order. For example, for floating point numbers, `NaN < 0 == false` and `NaN >= 0 == /// false` (cf. IEEE 754-2008 section 5.11). /// -/// This trait can be used with `#[derive]`. When `derive`d, it will produce a lexicographic -/// ordering based on the top-to-bottom declaration order of the struct's members. +/// `PartialOrd` requires your type to be `PartialEq`. +/// +/// If your type is `Ord`, you can implement `partial_cmp` by using `cmp`: +/// +/// ``` +/// impl PartialOrd for Person { +/// fn partial_cmp(&self, other: &Self) -> Option { +/// Some(self.cmp(other)) +/// } +/// } +/// ``` +/// +/// You may also find it useful to use `partial_cmp` on your type`s fields. Here +/// is an example of `Person` types who have a floating-point `height` field that +/// is the only field to be used for sorting: +/// +/// ``` +/// use std::cmp::Ordering; +/// +/// struct Person { +/// id: u32, +/// name: String, +/// height: f64, +/// } +/// +/// impl PartialOrd for Person { +/// fn partial_cmp(&self, other: &Self) -> Option { +/// self.height.partial_cmp(&other.height) +/// } +/// } +/// +/// impl PartialEq for Person { +/// fn eq(&self, other: &Self) -> bool { +/// self.height == other.height +/// } +/// } +/// ``` /// /// # Examples /// From bd50effe0f3c030c47cd444888426a82c589f9fa Mon Sep 17 00:00:00 2001 From: "Carol (Nichols || Goulding)" Date: Sun, 22 May 2016 19:24:58 -0400 Subject: [PATCH 11/31] Make the Default docs more like the other traits Add explicit "Derivable" and "How can I implement `Default`" sections. Copied relevant sections from the module-level documentation, but also linked to there-- it has a more comprehensive narrative with examples that show implementation AND use. Decided to just put implementation example in the trait documentation. --- src/libcore/default.rs | 29 ++++++++++++++++++++++++++--- 1 file changed, 26 insertions(+), 3 deletions(-) diff --git a/src/libcore/default.rs b/src/libcore/default.rs index 12c4a5ca200..74101c66c97 100644 --- a/src/libcore/default.rs +++ b/src/libcore/default.rs @@ -84,10 +84,33 @@ use marker::Sized; -/// A trait for giving a type a useful default value. +/// A trait for giving a type a useful default value. For more information, see +/// [the module-level documentation][module]. /// -/// A struct can derive default implementations of `Default` for basic types using -/// `#[derive(Default)]`. +/// [module]: ../../std/default/index.html +/// +/// ## Derivable +/// +/// This trait can be used with `#[derive]` if all of the type's fields implement +/// `Default`. When `derive`d, it will use the default value for each field's type. +/// +/// ## How can I implement `Default`? +/// +/// Provide an implementation for the `default()` method that returns the value of +/// your type that should be the default: +/// +/// ``` +/// # #![allow(dead_code)] +/// enum Kind { +/// A, +/// B, +/// C, +/// } +/// +/// impl Default for Kind { +/// fn default() -> Kind { Kind::A } +/// } +/// ``` /// /// # Examples /// From b4e123d3e0fd9459733f9ebb0802877a995abef4 Mon Sep 17 00:00:00 2001 From: "Carol (Nichols || Goulding)" Date: Mon, 23 May 2016 12:52:38 -0400 Subject: [PATCH 12/31] Shorten, yet clarify, initial summary sentences --- src/libcore/clone.rs | 4 +++- src/libcore/default.rs | 4 +++- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/src/libcore/clone.rs b/src/libcore/clone.rs index 629808cb124..3a5ba2387ac 100644 --- a/src/libcore/clone.rs +++ b/src/libcore/clone.rs @@ -46,7 +46,9 @@ use marker::Sized; -/// A common trait for cloning an object. Differs from `Copy` in that you can +/// A common trait for the ability to explicitly duplicate an object. +/// +/// Differs from `Copy` in that you can /// define `Clone` to run arbitrary code, while you are not allowed to override /// the implementation of `Copy` that only does a `memcpy`. /// diff --git a/src/libcore/default.rs b/src/libcore/default.rs index 74101c66c97..689ace97e23 100644 --- a/src/libcore/default.rs +++ b/src/libcore/default.rs @@ -84,7 +84,9 @@ use marker::Sized; -/// A trait for giving a type a useful default value. For more information, see +/// A trait for giving a type a useful default value. +/// +/// For more information, see /// [the module-level documentation][module]. /// /// [module]: ../../std/default/index.html From d2ee6e04ab7f2095351b48acff7c997dc4ac779a Mon Sep 17 00:00:00 2001 From: "Carol (Nichols || Goulding)" Date: Mon, 23 May 2016 12:58:42 -0400 Subject: [PATCH 13/31] Emphasize semantic differences of Copy/Clone rather than impl --- src/libcore/clone.rs | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/libcore/clone.rs b/src/libcore/clone.rs index 3a5ba2387ac..82097c6b5e7 100644 --- a/src/libcore/clone.rs +++ b/src/libcore/clone.rs @@ -48,9 +48,10 @@ use marker::Sized; /// A common trait for the ability to explicitly duplicate an object. /// -/// Differs from `Copy` in that you can -/// define `Clone` to run arbitrary code, while you are not allowed to override -/// the implementation of `Copy` that only does a `memcpy`. +/// Differs from `Copy` in that `Copy` is implicit and extremely inexpensive, while +/// `Clone` is always explicit and may or may not be expensive. In order to enforce +/// these characteristics, Rust does not allow you to reimplement `Copy`, but you +/// may reimplement `Clone` and run arbitrary code. /// /// Since `Clone` is more general than `Copy`, you can automatically make anything /// `Copy` be `Clone` as well. From c22c52449ad29021debd61cad6cadbc5a325433e Mon Sep 17 00:00:00 2001 From: "Carol (Nichols || Goulding)" Date: Mon, 23 May 2016 13:00:01 -0400 Subject: [PATCH 14/31] "more than 32" => "more than 32 elements" --- src/libcore/clone.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/libcore/clone.rs b/src/libcore/clone.rs index 82097c6b5e7..489aa66885f 100644 --- a/src/libcore/clone.rs +++ b/src/libcore/clone.rs @@ -68,8 +68,8 @@ use marker::Sized; /// Manual implementations should be careful to uphold this invariant; however, unsafe code /// must not rely on it to ensure memory safety. /// -/// An example is an array holding more than 32 of a type that is `Clone`; the standard library -/// only implements `Clone` up until arrays of size 32. In this case, the implementation of +/// An example is an array holding more than 32 elements of a type that is `Clone`; the standard +/// library only implements `Clone` up until arrays of size 32. In this case, the implementation of /// `Clone` cannot be `derive`d, but can be implemented as: /// /// ``` From 497cbb6748ea9c995da26a5cc8ae947bd0224c63 Mon Sep 17 00:00:00 2001 From: "Carol (Nichols || Goulding)" Date: Mon, 23 May 2016 13:02:16 -0400 Subject: [PATCH 15/31] "non equal" => "not equal"; consistent with the surrounding text --- src/libcore/cmp.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/libcore/cmp.rs b/src/libcore/cmp.rs index 5b8620f4b8e..f26a6dad880 100644 --- a/src/libcore/cmp.rs +++ b/src/libcore/cmp.rs @@ -56,7 +56,7 @@ use option::Option::{self, Some}; /// ## Derivable /// /// This trait can be used with `#[derive]`. When `derive`d on structs, two -/// instances are equal if all fields are equal, and non equal if any fields +/// instances are equal if all fields are equal, and not equal if any fields /// are not equal. When `derive`d on enums, each variant is equal to itself /// and not equal to the other variants. /// From 914999260427d8717bcf9fecf9407a8d5c751dec Mon Sep 17 00:00:00 2001 From: "Carol (Nichols || Goulding)" Date: Mon, 23 May 2016 13:04:05 -0400 Subject: [PATCH 16/31] Add some newlines in some code examples --- src/libcore/cmp.rs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/libcore/cmp.rs b/src/libcore/cmp.rs index f26a6dad880..be56ab1a41b 100644 --- a/src/libcore/cmp.rs +++ b/src/libcore/cmp.rs @@ -76,6 +76,7 @@ use option::Option::{self, Some}; /// isbn: i32, /// format: BookFormat, /// } +/// /// impl PartialEq for Book { /// fn eq(&self, other: &Self) -> bool { /// self.isbn == other.isbn @@ -262,6 +263,7 @@ impl Ordering { /// /// ``` /// use std::cmp::Ordering; +/// /// #[derive(Eq)] /// struct Person { /// id: u32, From daa9dcaac4cb99adbed8a6911dbe4f570b7deb75 Mon Sep 17 00:00:00 2001 From: "Carol (Nichols || Goulding)" Date: Mon, 23 May 2016 13:06:23 -0400 Subject: [PATCH 17/31] Use `()` when referring to functions --- src/libcore/cmp.rs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/libcore/cmp.rs b/src/libcore/cmp.rs index be56ab1a41b..f23128b3364 100644 --- a/src/libcore/cmp.rs +++ b/src/libcore/cmp.rs @@ -255,8 +255,8 @@ impl Ordering { /// /// `Ord` requires that the type also be `PartialOrd` and `Eq` (which requires `PartialEq`). /// -/// Then you must define an implementation for `cmp`. You may find it useful to use -/// `cmp` on your type's fields. +/// Then you must define an implementation for `cmp()`. You may find it useful to use +/// `cmp()` on your type's fields. /// /// Here's an example where you want to sort people by height only, disregarding `id` /// and `name`: @@ -355,7 +355,7 @@ impl PartialOrd for Ordering { /// /// `PartialOrd` requires your type to be `PartialEq`. /// -/// If your type is `Ord`, you can implement `partial_cmp` by using `cmp`: +/// If your type is `Ord`, you can implement `partial_cmp()` by using `cmp()`: /// /// ``` /// impl PartialOrd for Person { @@ -365,7 +365,7 @@ impl PartialOrd for Ordering { /// } /// ``` /// -/// You may also find it useful to use `partial_cmp` on your type`s fields. Here +/// You may also find it useful to use `partial_cmp()` on your type`s fields. Here /// is an example of `Person` types who have a floating-point `height` field that /// is the only field to be used for sorting: /// From d81a999b54f5c2df14a8057ab14ed7161093e353 Mon Sep 17 00:00:00 2001 From: "Carol (Nichols || Goulding)" Date: Mon, 23 May 2016 13:13:09 -0400 Subject: [PATCH 18/31] Prefer `ClassName` over `Self` in example trait implementations --- src/libcore/clone.rs | 2 +- src/libcore/cmp.rs | 16 ++++++++-------- 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/src/libcore/clone.rs b/src/libcore/clone.rs index 489aa66885f..e8cd36f3cd7 100644 --- a/src/libcore/clone.rs +++ b/src/libcore/clone.rs @@ -79,7 +79,7 @@ use marker::Sized; /// } /// /// impl Clone for Stats { -/// fn clone(&self) -> Self { *self } +/// fn clone(&self) -> Stats { *self } /// } /// ``` #[stable(feature = "rust1", since = "1.0.0")] diff --git a/src/libcore/cmp.rs b/src/libcore/cmp.rs index f23128b3364..5b289fd1e00 100644 --- a/src/libcore/cmp.rs +++ b/src/libcore/cmp.rs @@ -78,7 +78,7 @@ use option::Option::{self, Some}; /// } /// /// impl PartialEq for Book { -/// fn eq(&self, other: &Self) -> bool { +/// fn eq(&self, other: &Book) -> bool { /// self.isbn == other.isbn /// } /// } @@ -147,7 +147,7 @@ pub trait PartialEq { /// format: BookFormat, /// } /// impl PartialEq for Book { -/// fn eq(&self, other: &Self) -> bool { +/// fn eq(&self, other: &Book) -> bool { /// self.isbn == other.isbn /// } /// } @@ -272,19 +272,19 @@ impl Ordering { /// } /// /// impl Ord for Person { -/// fn cmp(&self, other: &Self) -> Ordering { +/// fn cmp(&self, other: &Person) -> Ordering { /// self.height.cmp(&other.height) /// } /// } /// /// impl PartialOrd for Person { -/// fn partial_cmp(&self, other: &Self) -> Option { +/// fn partial_cmp(&self, other: &Person) -> Option { /// Some(self.cmp(other)) /// } /// } /// /// impl PartialEq for Person { -/// fn eq(&self, other: &Self) -> bool { +/// fn eq(&self, other: &Person) -> bool { /// self.height == other.height /// } /// } @@ -359,7 +359,7 @@ impl PartialOrd for Ordering { /// /// ``` /// impl PartialOrd for Person { -/// fn partial_cmp(&self, other: &Self) -> Option { +/// fn partial_cmp(&self, other: &Person) -> Option { /// Some(self.cmp(other)) /// } /// } @@ -379,13 +379,13 @@ impl PartialOrd for Ordering { /// } /// /// impl PartialOrd for Person { -/// fn partial_cmp(&self, other: &Self) -> Option { +/// fn partial_cmp(&self, other: &Person) -> Option { /// self.height.partial_cmp(&other.height) /// } /// } /// /// impl PartialEq for Person { -/// fn eq(&self, other: &Self) -> bool { +/// fn eq(&self, other: &Person) -> bool { /// self.height == other.height /// } /// } From 1b322983965f5520ba32f32ace054f0ad268973e Mon Sep 17 00:00:00 2001 From: "Carol (Nichols || Goulding)" Date: Mon, 23 May 2016 13:47:28 -0400 Subject: [PATCH 19/31] Move all `Default` docs from module to trait I had already copied the implementation example in a previous commit; this copies the explanation and usage examples to the general trait description. --- src/libcore/default.rs | 115 +++++++++++++++-------------------------- 1 file changed, 42 insertions(+), 73 deletions(-) diff --git a/src/libcore/default.rs b/src/libcore/default.rs index 689ace97e23..485ddae07fb 100644 --- a/src/libcore/default.rs +++ b/src/libcore/default.rs @@ -9,76 +9,6 @@ // except according to those terms. //! The `Default` trait for types which may have meaningful default values. -//! -//! Sometimes, you want to fall back to some kind of default value, and -//! don't particularly care what it is. This comes up often with `struct`s -//! that define a set of options: -//! -//! ``` -//! # #[allow(dead_code)] -//! struct SomeOptions { -//! foo: i32, -//! bar: f32, -//! } -//! ``` -//! -//! How can we define some default values? You can use `Default`: -//! -//! ``` -//! # #[allow(dead_code)] -//! #[derive(Default)] -//! struct SomeOptions { -//! foo: i32, -//! bar: f32, -//! } -//! -//! -//! fn main() { -//! let options: SomeOptions = Default::default(); -//! } -//! ``` -//! -//! Now, you get all of the default values. Rust implements `Default` for various primitives types. -//! If you have your own type, you need to implement `Default` yourself: -//! -//! ``` -//! # #![allow(dead_code)] -//! enum Kind { -//! A, -//! B, -//! C, -//! } -//! -//! impl Default for Kind { -//! fn default() -> Kind { Kind::A } -//! } -//! -//! #[derive(Default)] -//! struct SomeOptions { -//! foo: i32, -//! bar: f32, -//! baz: Kind, -//! } -//! -//! -//! fn main() { -//! let options: SomeOptions = Default::default(); -//! } -//! ``` -//! -//! If you want to override a particular option, but still retain the other defaults: -//! -//! ``` -//! # #[allow(dead_code)] -//! # #[derive(Default)] -//! # struct SomeOptions { -//! # foo: i32, -//! # bar: f32, -//! # } -//! fn main() { -//! let options = SomeOptions { foo: 42, ..Default::default() }; -//! } -//! ``` #![stable(feature = "rust1", since = "1.0.0")] @@ -86,10 +16,49 @@ use marker::Sized; /// A trait for giving a type a useful default value. /// -/// For more information, see -/// [the module-level documentation][module]. +/// Sometimes, you want to fall back to some kind of default value, and +/// don't particularly care what it is. This comes up often with `struct`s +/// that define a set of options: /// -/// [module]: ../../std/default/index.html +/// ``` +/// # #[allow(dead_code)] +/// struct SomeOptions { +/// foo: i32, +/// bar: f32, +/// } +/// ``` +/// +/// How can we define some default values? You can use `Default`: +/// +/// ``` +/// # #[allow(dead_code)] +/// #[derive(Default)] +/// struct SomeOptions { +/// foo: i32, +/// bar: f32, +/// } +/// +/// +/// fn main() { +/// let options: SomeOptions = Default::default(); +/// } +/// ``` +/// +/// Now, you get all of the default values. Rust implements `Default` for various primitives types. +/// +/// If you want to override a particular option, but still retain the other defaults: +/// +/// ``` +/// # #[allow(dead_code)] +/// # #[derive(Default)] +/// # struct SomeOptions { +/// # foo: i32, +/// # bar: f32, +/// # } +/// fn main() { +/// let options = SomeOptions { foo: 42, ..Default::default() }; +/// } +/// ``` /// /// ## Derivable /// From 1a7d3e13444bb195692e37f9ecfc000d5da9ef57 Mon Sep 17 00:00:00 2001 From: "Carol (Nichols || Goulding)" Date: Mon, 23 May 2016 14:07:58 -0400 Subject: [PATCH 20/31] Complete `PartialOrd`'s example so it passes make check-docs --- src/libcore/cmp.rs | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/src/libcore/cmp.rs b/src/libcore/cmp.rs index 5b289fd1e00..8764766b2ef 100644 --- a/src/libcore/cmp.rs +++ b/src/libcore/cmp.rs @@ -358,11 +358,32 @@ impl PartialOrd for Ordering { /// If your type is `Ord`, you can implement `partial_cmp()` by using `cmp()`: /// /// ``` +/// use std::cmp::Ordering; +/// +/// #[derive(Eq)] +/// struct Person { +/// id: u32, +/// name: String, +/// height: u32, +/// } +/// /// impl PartialOrd for Person { /// fn partial_cmp(&self, other: &Person) -> Option { /// Some(self.cmp(other)) /// } /// } +/// +/// impl Ord for Person { +/// fn cmp(&self, other: &Person) -> Ordering { +/// self.height.cmp(&other.height) +/// } +/// } +/// +/// impl PartialEq for Person { +/// fn eq(&self, other: &Person) -> bool { +/// self.height == other.height +/// } +/// } /// ``` /// /// You may also find it useful to use `partial_cmp()` on your type`s fields. Here From 1e809f57a45a187a2a8c49d4d93ec64c089873ca Mon Sep 17 00:00:00 2001 From: "Carol (Nichols || Goulding)" Date: Mon, 23 May 2016 14:09:43 -0400 Subject: [PATCH 21/31] "the trait `Hash`" => "the `Hash` trait" --- src/libcore/hash/mod.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/libcore/hash/mod.rs b/src/libcore/hash/mod.rs index 844a24e80d1..051eb974895 100644 --- a/src/libcore/hash/mod.rs +++ b/src/libcore/hash/mod.rs @@ -38,7 +38,7 @@ //! ``` //! //! If you need more control over how a value is hashed, you need to implement -//! the trait `Hash`: +//! the `Hash` trait: //! //! ```rust //! use std::hash::{Hash, Hasher, SipHasher}; @@ -106,7 +106,7 @@ mod sip; /// ## How can I implement `Hash`? /// /// If you need more control over how a value is hashed, you need to implement -/// the trait `Hash`: +/// the `Hash` trait: /// /// ``` /// use std::hash::{Hash, Hasher}; From 22d626f555655bdd95ffcb0915a123292783087d Mon Sep 17 00:00:00 2001 From: Guillaume Gomez Date: Tue, 24 May 2016 00:33:16 +0200 Subject: [PATCH 22/31] Fix invalid background color in stability elements --- src/librustdoc/html/static/styles/main.css | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/librustdoc/html/static/styles/main.css b/src/librustdoc/html/static/styles/main.css index 59b2ff7e3d6..aa08cee13ef 100644 --- a/src/librustdoc/html/static/styles/main.css +++ b/src/librustdoc/html/static/styles/main.css @@ -30,6 +30,10 @@ h2, h3:not(.impl):not(.method):not(.type):not(.tymethod), h4:not(.method):not(.t background-color: white; } +div.stability > em > code { + background-color: initial; +} + .docblock code { background-color: #F5F5F5; } From ba8d46eac80fef496790486f1cb08dec61a2555d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?M=C3=A1ty=C3=A1s=20Mustoha?= Date: Fri, 20 May 2016 10:56:39 +0200 Subject: [PATCH 23/31] Fix asm-misplaced-option on ARM/AArch64 --- src/test/compile-fail/asm-misplaced-option.rs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/test/compile-fail/asm-misplaced-option.rs b/src/test/compile-fail/asm-misplaced-option.rs index 43a0ad6b5f6..1020a5ba8a4 100644 --- a/src/test/compile-fail/asm-misplaced-option.rs +++ b/src/test/compile-fail/asm-misplaced-option.rs @@ -9,6 +9,8 @@ // except according to those terms. // ignore-android +// ignore-arm +// ignore-aarch64 #![feature(asm, rustc_attrs)] From f5a398d0566cb585266778c81e2924c27e017080 Mon Sep 17 00:00:00 2001 From: Liigo Zhuang Date: Wed, 25 May 2016 10:09:20 +0800 Subject: [PATCH 24/31] Point out the clone operation in summary line docs of `Vec::extend_from_slice` --- src/libcollections/vec.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/libcollections/vec.rs b/src/libcollections/vec.rs index 58d4a4ed4eb..bd1bf6e9cc3 100644 --- a/src/libcollections/vec.rs +++ b/src/libcollections/vec.rs @@ -966,7 +966,7 @@ impl Vec { } } - /// Appends all elements in a slice to the `Vec`. + /// Clones and appends all elements in a slice to the `Vec`. /// /// Iterates over the slice `other`, clones each element, and then appends /// it to this `Vec`. The `other` vector is traversed in-order. From dfa0d2c46e4448fe117d26a9c7fff2b68a2f304f Mon Sep 17 00:00:00 2001 From: Guillaume Gomez Date: Wed, 25 May 2016 13:40:35 +0200 Subject: [PATCH 25/31] Improve E0084 error explanation --- src/librustc_typeck/diagnostics.rs | 24 ++++++++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) diff --git a/src/librustc_typeck/diagnostics.rs b/src/librustc_typeck/diagnostics.rs index 40b9b0e01ab..b94c9c4ad19 100644 --- a/src/librustc_typeck/diagnostics.rs +++ b/src/librustc_typeck/diagnostics.rs @@ -1193,12 +1193,32 @@ discriminant values so that they fit within the existing type. "##, E0084: r##" +An unsupported representation was attempted on a zero-variant enum. + +Erroneous code example: + +```compile_fail +#[repr(i32)] +enum NightWatch {} // error: unsupported representation for zero-variant enum +``` + It is impossible to define an integer type to be used to represent zero-variant enum values because there are no zero-variant enum values. There is no way to -construct an instance of the following type using only safe code: +construct an instance of the following type using only safe code. So you have +two solutions. Either you add variants in your enum: ``` -enum Empty {} +#[repr(i32)] +enum NightWatch { + JohnSnow, + Commander, +} +``` + +or you remove the integer represention of your enum: + +``` +enum NightWatch {} ``` "##, From 6d669b4f458a87cc5a1e4f26463edc805d7f9ead Mon Sep 17 00:00:00 2001 From: ggomez Date: Wed, 25 May 2016 13:58:07 +0200 Subject: [PATCH 26/31] Add new error code tests --- src/test/compile-fail/E0084.rs | 15 +++++++++++++++ src/test/compile-fail/E0087.rs | 15 +++++++++++++++ src/test/compile-fail/E0088.rs | 15 +++++++++++++++ src/test/compile-fail/E0089.rs | 15 +++++++++++++++ src/test/compile-fail/E0091.rs | 15 +++++++++++++++ src/test/compile-fail/E0092.rs | 17 +++++++++++++++++ src/test/compile-fail/E0093.rs | 17 +++++++++++++++++ src/test/compile-fail/E0094.rs | 17 +++++++++++++++++ src/test/compile-fail/E0101.rs | 13 +++++++++++++ src/test/compile-fail/E0102.rs | 13 +++++++++++++ src/test/compile-fail/E0106.rs | 21 +++++++++++++++++++++ src/test/compile-fail/E0107.rs | 25 +++++++++++++++++++++++++ src/test/compile-fail/E0109.rs | 14 ++++++++++++++ src/test/compile-fail/E0110.rs | 14 ++++++++++++++ src/test/compile-fail/E0116.rs | 14 ++++++++++++++ 15 files changed, 240 insertions(+) create mode 100644 src/test/compile-fail/E0084.rs create mode 100644 src/test/compile-fail/E0087.rs create mode 100644 src/test/compile-fail/E0088.rs create mode 100644 src/test/compile-fail/E0089.rs create mode 100644 src/test/compile-fail/E0091.rs create mode 100644 src/test/compile-fail/E0092.rs create mode 100644 src/test/compile-fail/E0093.rs create mode 100644 src/test/compile-fail/E0094.rs create mode 100644 src/test/compile-fail/E0101.rs create mode 100644 src/test/compile-fail/E0102.rs create mode 100644 src/test/compile-fail/E0106.rs create mode 100644 src/test/compile-fail/E0107.rs create mode 100644 src/test/compile-fail/E0109.rs create mode 100644 src/test/compile-fail/E0110.rs create mode 100644 src/test/compile-fail/E0116.rs diff --git a/src/test/compile-fail/E0084.rs b/src/test/compile-fail/E0084.rs new file mode 100644 index 00000000000..c579101325f --- /dev/null +++ b/src/test/compile-fail/E0084.rs @@ -0,0 +1,15 @@ +// Copyright 2016 The Rust Project Developers. See the COPYRIGHT +// file at the top-level directory of this distribution and at +// http://rust-lang.org/COPYRIGHT. +// +// Licensed under the Apache License, Version 2.0 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +#[repr(i32)] +enum Foo {} //~ ERROR E0084 + +fn main() { +} diff --git a/src/test/compile-fail/E0087.rs b/src/test/compile-fail/E0087.rs new file mode 100644 index 00000000000..ec559fc8389 --- /dev/null +++ b/src/test/compile-fail/E0087.rs @@ -0,0 +1,15 @@ +// Copyright 2016 The Rust Project Developers. See the COPYRIGHT +// file at the top-level directory of this distribution and at +// http://rust-lang.org/COPYRIGHT. +// +// Licensed under the Apache License, Version 2.0 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +fn foo() {} + +fn main() { + foo::(); //~ ERROR E0087 +} diff --git a/src/test/compile-fail/E0088.rs b/src/test/compile-fail/E0088.rs new file mode 100644 index 00000000000..0b235aa240c --- /dev/null +++ b/src/test/compile-fail/E0088.rs @@ -0,0 +1,15 @@ +// Copyright 2016 The Rust Project Developers. See the COPYRIGHT +// file at the top-level directory of this distribution and at +// http://rust-lang.org/COPYRIGHT. +// +// Licensed under the Apache License, Version 2.0 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +fn f() {} + +fn main() { + f::<'static>(); //~ ERROR E0088 +} diff --git a/src/test/compile-fail/E0089.rs b/src/test/compile-fail/E0089.rs new file mode 100644 index 00000000000..3b52f76bf09 --- /dev/null +++ b/src/test/compile-fail/E0089.rs @@ -0,0 +1,15 @@ +// Copyright 2016 The Rust Project Developers. See the COPYRIGHT +// file at the top-level directory of this distribution and at +// http://rust-lang.org/COPYRIGHT. +// +// Licensed under the Apache License, Version 2.0 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +fn foo() {} + +fn main() { + foo::(); //~ ERROR E0089 +} diff --git a/src/test/compile-fail/E0091.rs b/src/test/compile-fail/E0091.rs new file mode 100644 index 00000000000..da988dbf819 --- /dev/null +++ b/src/test/compile-fail/E0091.rs @@ -0,0 +1,15 @@ +// Copyright 2016 The Rust Project Developers. See the COPYRIGHT +// file at the top-level directory of this distribution and at +// http://rust-lang.org/COPYRIGHT. +// +// Licensed under the Apache License, Version 2.0 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +type Foo = u32; //~ ERROR E0091 +type Foo2 = Box; //~ ERROR E0091 + +fn main() { +} diff --git a/src/test/compile-fail/E0092.rs b/src/test/compile-fail/E0092.rs new file mode 100644 index 00000000000..b08164ac06d --- /dev/null +++ b/src/test/compile-fail/E0092.rs @@ -0,0 +1,17 @@ +// Copyright 2016 The Rust Project Developers. See the COPYRIGHT +// file at the top-level directory of this distribution and at +// http://rust-lang.org/COPYRIGHT. +// +// Licensed under the Apache License, Version 2.0 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +#![feature(intrinsics)] +extern "rust-intrinsic" { + fn atomic_foo(); //~ ERROR E0092 +} + +fn main() { +} diff --git a/src/test/compile-fail/E0093.rs b/src/test/compile-fail/E0093.rs new file mode 100644 index 00000000000..9b23f6d984e --- /dev/null +++ b/src/test/compile-fail/E0093.rs @@ -0,0 +1,17 @@ +// Copyright 2016 The Rust Project Developers. See the COPYRIGHT +// file at the top-level directory of this distribution and at +// http://rust-lang.org/COPYRIGHT. +// +// Licensed under the Apache License, Version 2.0 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +#![feature(intrinsics)] +extern "rust-intrinsic" { + fn foo(); //~ ERROR E0093 +} + +fn main() { +} diff --git a/src/test/compile-fail/E0094.rs b/src/test/compile-fail/E0094.rs new file mode 100644 index 00000000000..3a31874b244 --- /dev/null +++ b/src/test/compile-fail/E0094.rs @@ -0,0 +1,17 @@ +// Copyright 2016 The Rust Project Developers. See the COPYRIGHT +// file at the top-level directory of this distribution and at +// http://rust-lang.org/COPYRIGHT. +// +// Licensed under the Apache License, Version 2.0 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +#![feature(intrinsics)] +extern "rust-intrinsic" { + fn size_of() -> usize; //~ ERROR E0094 +} + +fn main() { +} diff --git a/src/test/compile-fail/E0101.rs b/src/test/compile-fail/E0101.rs new file mode 100644 index 00000000000..7651626d44f --- /dev/null +++ b/src/test/compile-fail/E0101.rs @@ -0,0 +1,13 @@ +// Copyright 2016 The Rust Project Developers. See the COPYRIGHT +// file at the top-level directory of this distribution and at +// http://rust-lang.org/COPYRIGHT. +// +// Licensed under the Apache License, Version 2.0 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +fn main() { + let x = |_| {}; //~ ERROR E0101 +} diff --git a/src/test/compile-fail/E0102.rs b/src/test/compile-fail/E0102.rs new file mode 100644 index 00000000000..c4ddbab3e86 --- /dev/null +++ b/src/test/compile-fail/E0102.rs @@ -0,0 +1,13 @@ +// Copyright 2016 The Rust Project Developers. See the COPYRIGHT +// file at the top-level directory of this distribution and at +// http://rust-lang.org/COPYRIGHT. +// +// Licensed under the Apache License, Version 2.0 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +fn main() { + let x = []; //~ ERROR E0102 +} diff --git a/src/test/compile-fail/E0106.rs b/src/test/compile-fail/E0106.rs new file mode 100644 index 00000000000..f1cd530863d --- /dev/null +++ b/src/test/compile-fail/E0106.rs @@ -0,0 +1,21 @@ +// Copyright 2016 The Rust Project Developers. See the COPYRIGHT +// file at the top-level directory of this distribution and at +// http://rust-lang.org/COPYRIGHT. +// +// Licensed under the Apache License, Version 2.0 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +struct Foo { + x: &bool, //~ ERROR E0106 +} +enum Bar { + A(u8), + B(&bool), //~ ERROR E0106 +} +type MyStr = &str; //~ ERROR E0106 + +fn main() { +} diff --git a/src/test/compile-fail/E0107.rs b/src/test/compile-fail/E0107.rs new file mode 100644 index 00000000000..d27b70865bb --- /dev/null +++ b/src/test/compile-fail/E0107.rs @@ -0,0 +1,25 @@ +// Copyright 2016 The Rust Project Developers. See the COPYRIGHT +// file at the top-level directory of this distribution and at +// http://rust-lang.org/COPYRIGHT. +// +// Licensed under the Apache License, Version 2.0 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +struct Foo<'a>(&'a str); + +enum Bar { + A, + B, + C, +} + +struct Baz<'a> { + foo: Foo, //~ ERROR E0107 + bar: Bar<'a>, //~ ERROR E0107 +} + +fn main() { +} diff --git a/src/test/compile-fail/E0109.rs b/src/test/compile-fail/E0109.rs new file mode 100644 index 00000000000..9fc47842250 --- /dev/null +++ b/src/test/compile-fail/E0109.rs @@ -0,0 +1,14 @@ +// Copyright 2016 The Rust Project Developers. See the COPYRIGHT +// file at the top-level directory of this distribution and at +// http://rust-lang.org/COPYRIGHT. +// +// Licensed under the Apache License, Version 2.0 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +type X = u32; //~ ERROR E0109 + +fn main() { +} diff --git a/src/test/compile-fail/E0110.rs b/src/test/compile-fail/E0110.rs new file mode 100644 index 00000000000..fd169f4acc5 --- /dev/null +++ b/src/test/compile-fail/E0110.rs @@ -0,0 +1,14 @@ +// Copyright 2016 The Rust Project Developers. See the COPYRIGHT +// file at the top-level directory of this distribution and at +// http://rust-lang.org/COPYRIGHT. +// +// Licensed under the Apache License, Version 2.0 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +type X = u32<'static>; //~ ERROR E0110 + +fn main() { +} diff --git a/src/test/compile-fail/E0116.rs b/src/test/compile-fail/E0116.rs new file mode 100644 index 00000000000..4020aa9475a --- /dev/null +++ b/src/test/compile-fail/E0116.rs @@ -0,0 +1,14 @@ +// Copyright 2016 The Rust Project Developers. See the COPYRIGHT +// file at the top-level directory of this distribution and at +// http://rust-lang.org/COPYRIGHT. +// +// Licensed under the Apache License, Version 2.0 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +impl Vec {} //~ ERROR E0116 + +fn main() { +} From 2c937204e9172121da5439f08c2b631de1e135e2 Mon Sep 17 00:00:00 2001 From: Carlo Teubner Date: Wed, 18 May 2016 22:42:15 +0100 Subject: [PATCH 27/31] parser.rs: fix typos in comments --- src/libsyntax/parse/parser.rs | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/libsyntax/parse/parser.rs b/src/libsyntax/parse/parser.rs index fc62cee92fd..3a28632f07a 100644 --- a/src/libsyntax/parse/parser.rs +++ b/src/libsyntax/parse/parser.rs @@ -2807,14 +2807,14 @@ impl<'a> Parser<'a> { let span = Span { hi: close_span.hi, ..pre_span }; match self.token { - // Correct delmiter. + // Correct delimiter. token::CloseDelim(d) if d == delim => { self.open_braces.pop().unwrap(); // Parse the close delimiter. self.bump(); } - // Incorect delimiter. + // Incorrect delimiter. token::CloseDelim(other) => { let token_str = self.this_token_to_string(); let mut err = self.diagnostic().struct_span_err(self.span, @@ -2829,9 +2829,9 @@ impl<'a> Parser<'a> { self.open_braces.pop().unwrap(); - // If the incorrect delimter matches an earlier opening + // If the incorrect delimiter matches an earlier opening // delimiter, then don't consume it (it can be used to - // close the earlier one)Otherwise, consume it. + // close the earlier one). Otherwise, consume it. // E.g., we try to recover from: // fn foo() { // bar(baz( @@ -2859,7 +2859,7 @@ impl<'a> Parser<'a> { // invariants: the current token is not a left-delimiter, // not an EOF, and not the desired right-delimiter (if // it were, parse_seq_to_before_end would have prevented - // reaching this point. + // reaching this point). maybe_whole!(deref self, NtTT); match self.token { token::CloseDelim(_) => { From 5b82c5f369fa9b7ab8ace58b3112e44ff5a1d388 Mon Sep 17 00:00:00 2001 From: Jeffrey Seyfried Date: Wed, 25 May 2016 20:05:47 +0000 Subject: [PATCH 28/31] Fix ICE on failure to parse token tree --- src/libsyntax/parse/parser.rs | 7 +++++-- src/test/parse-fail/issue-33569.rs | 18 ++++++++++++++++++ 2 files changed, 23 insertions(+), 2 deletions(-) create mode 100644 src/test/parse-fail/issue-33569.rs diff --git a/src/libsyntax/parse/parser.rs b/src/libsyntax/parse/parser.rs index 3a28632f07a..9f4a773973e 100644 --- a/src/libsyntax/parse/parser.rs +++ b/src/libsyntax/parse/parser.rs @@ -2702,7 +2702,10 @@ impl<'a> Parser<'a> { return Ok(TokenTree::Token(sp, SpecialVarNt(SpecialMacroVar::CrateMacroVar))); } else { sp = mk_sp(sp.lo, self.span.hi); - self.parse_ident()? + self.parse_ident().unwrap_or_else(|mut e| { + e.emit(); + keywords::Invalid.ident() + }) } } token::SubstNt(name) => { @@ -2845,7 +2848,7 @@ impl<'a> Parser<'a> { // and an error emitted then. Thus we don't pop from // self.open_braces here. }, - _ => unreachable!(), + _ => {} } Ok(TokenTree::Delimited(span, Rc::new(Delimited { diff --git a/src/test/parse-fail/issue-33569.rs b/src/test/parse-fail/issue-33569.rs new file mode 100644 index 00000000000..130278d778a --- /dev/null +++ b/src/test/parse-fail/issue-33569.rs @@ -0,0 +1,18 @@ +// Copyright 2016 The Rust Project Developers. See the COPYRIGHT +// file at the top-level directory of this distribution and at +// http://rust-lang.org/COPYRIGHT. +// +// Licensed under the Apache License, Version 2.0 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +// compile-flags: -Z no-analysis + +macro_rules! foo { + { $+ } => { //~ ERROR expected identifier, found `+` + $(x)(y) //~ ERROR expected `*` or `+` + //~^ ERROR no rules expected the token `y` + } +} From eface32e41d9c6607a4a06ce7efc3698ac737bf3 Mon Sep 17 00:00:00 2001 From: Sebastian Thiel Date: Thu, 26 May 2016 10:08:45 +0200 Subject: [PATCH 29/31] Add `make tips` as useful make target By accident, I found the `make tips` target, which helped me to gain more insight on how to work with the system more quickly. --- CONTRIBUTING.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 9d61feef81a..495d7e46baa 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -122,6 +122,8 @@ To see a full list of options, run `./configure --help`. Some common make targets are: +- `make tips` - show useful targets, variables and other tips for working with + the build system. - `make rustc-stage1` - build up to (and including) the first stage. For most cases we don't need to build the stage2 compiler, so we can save time by not building it. The stage1 compiler is a fully functioning compiler and From 7ba001603072284e084628c5e282ecf99aa2f281 Mon Sep 17 00:00:00 2001 From: Alexander Polyakov Date: Thu, 26 May 2016 22:38:33 +0300 Subject: [PATCH 30/31] Make Ipv4Addr cmp() faster --- src/libstd/net/ip.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/libstd/net/ip.rs b/src/libstd/net/ip.rs index 45b85d600a6..ba485f819f8 100644 --- a/src/libstd/net/ip.rs +++ b/src/libstd/net/ip.rs @@ -251,7 +251,7 @@ impl PartialOrd for Ipv4Addr { #[stable(feature = "rust1", since = "1.0.0")] impl Ord for Ipv4Addr { fn cmp(&self, other: &Ipv4Addr) -> Ordering { - self.octets().cmp(&other.octets()) + ntoh(self.inner.s_addr).cmp(&ntoh(other.inner.s_addr)) } } From 38bbb60c8c5e0655f46bf3ad68985e533d1af776 Mon Sep 17 00:00:00 2001 From: Srinivas Reddy Thatiparthy Date: Fri, 27 May 2016 08:06:17 +0530 Subject: [PATCH 31/31] rustfmt on liblog --- src/liblog/lib.rs | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/src/liblog/lib.rs b/src/liblog/lib.rs index a71f6efe54e..517cd016e8a 100644 --- a/src/liblog/lib.rs +++ b/src/liblog/lib.rs @@ -179,7 +179,7 @@ use std::io::prelude::*; use std::mem; use std::env; use std::slice; -use std::sync::{Once, Mutex, ONCE_INIT}; +use std::sync::{Mutex, ONCE_INIT, Once}; use directive::LOG_LEVEL_NAMES; @@ -290,9 +290,7 @@ pub fn log(level: u32, loc: &'static LogLocation, args: fmt::Arguments) { // frob the slot while we're doing the logging. This will destroy any logger // set during logging. let logger = LOCAL_LOGGER.with(|s| s.borrow_mut().take()); - let mut logger = logger.unwrap_or_else(|| { - Box::new(DefaultLogger { handle: io::stderr() }) - }); + let mut logger = logger.unwrap_or_else(|| Box::new(DefaultLogger { handle: io::stderr() })); logger.log(&LogRecord { level: LogLevel(level), args: args,