From 81eea9e4312253afb655c051d0bf0661744ab56e Mon Sep 17 00:00:00 2001 From: Alex Crichton Date: Fri, 21 Jul 2017 09:41:20 -0700 Subject: [PATCH 01/17] Thread through the original error when opening archives This updates the management of opening archives to thread through the original piece of error information from LLVM over to the end consumer, trans. --- src/librustc_llvm/archive_ro.rs | 6 +++--- src/librustc_trans/back/archive.rs | 7 +++---- src/librustc_trans/metadata.rs | 16 ++++++++-------- 3 files changed, 14 insertions(+), 15 deletions(-) diff --git a/src/librustc_llvm/archive_ro.rs b/src/librustc_llvm/archive_ro.rs index b3f5f8e536052..0b24e55541b07 100644 --- a/src/librustc_llvm/archive_ro.rs +++ b/src/librustc_llvm/archive_ro.rs @@ -39,14 +39,14 @@ impl ArchiveRO { /// /// If this archive is used with a mutable method, then an error will be /// raised. - pub fn open(dst: &Path) -> Option { + pub fn open(dst: &Path) -> Result { return unsafe { let s = path2cstr(dst); let ar = ::LLVMRustOpenArchive(s.as_ptr()); if ar.is_null() { - None + Err(::last_error().unwrap_or("failed to open archive".to_string())) } else { - Some(ArchiveRO { ptr: ar }) + Ok(ArchiveRO { ptr: ar }) } }; diff --git a/src/librustc_trans/back/archive.rs b/src/librustc_trans/back/archive.rs index 902065c8688d0..6ec40bd689c23 100644 --- a/src/librustc_trans/back/archive.rs +++ b/src/librustc_trans/back/archive.rs @@ -126,7 +126,7 @@ impl<'a> ArchiveBuilder<'a> { Some(ref src) => src, None => return None, }; - self.src_archive = Some(ArchiveRO::open(src)); + self.src_archive = Some(ArchiveRO::open(src).ok()); self.src_archive.as_ref().unwrap().as_ref() } @@ -186,9 +186,8 @@ impl<'a> ArchiveBuilder<'a> { where F: FnMut(&str) -> bool + 'static { let archive = match ArchiveRO::open(archive) { - Some(ar) => ar, - None => return Err(io::Error::new(io::ErrorKind::Other, - "failed to open archive")), + Ok(ar) => ar, + Err(e) => return Err(io::Error::new(io::ErrorKind::Other, e)), }; self.additions.push(Addition::Archive { archive: archive, diff --git a/src/librustc_trans/metadata.rs b/src/librustc_trans/metadata.rs index 2c0148dfbb371..883808c59091a 100644 --- a/src/librustc_trans/metadata.rs +++ b/src/librustc_trans/metadata.rs @@ -31,10 +31,10 @@ impl MetadataLoader for LlvmMetadataLoader { // just keeping the archive along while the metadata is in use. let archive = ArchiveRO::open(filename) .map(|ar| OwningRef::new(box ar)) - .ok_or_else(|| { - debug!("llvm didn't like `{}`", filename.display()); - format!("failed to read rlib metadata: '{}'", filename.display()) - })?; + .map_err(|e| { + debug!("llvm didn't like `{}`: {}", filename.display(), e); + format!("failed to read rlib metadata in '{}': {}", filename.display(), e) + })?; let buf: OwningRef<_, [u8]> = archive .try_map(|ar| { ar.iter() @@ -42,10 +42,10 @@ impl MetadataLoader for LlvmMetadataLoader { .find(|sect| sect.name() == Some(METADATA_FILENAME)) .map(|s| s.data()) .ok_or_else(|| { - debug!("didn't find '{}' in the archive", METADATA_FILENAME); - format!("failed to read rlib metadata: '{}'", - filename.display()) - }) + debug!("didn't find '{}' in the archive", METADATA_FILENAME); + format!("failed to read rlib metadata: '{}'", + filename.display()) + }) })?; Ok(buf.erase_owner()) } From 236b7487d525359440815a425cba97fa36903afc Mon Sep 17 00:00:00 2001 From: Tymoteusz Jankowski Date: Sun, 23 Jul 2017 14:18:34 +0200 Subject: [PATCH 02/17] Add simple docs example for struct Cell --- src/libcore/cell.rs | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/src/libcore/cell.rs b/src/libcore/cell.rs index 35744f3f16b39..7e12d5466c2bf 100644 --- a/src/libcore/cell.rs +++ b/src/libcore/cell.rs @@ -187,6 +187,29 @@ use ops::{Deref, DerefMut, CoerceUnsized}; use ptr; /// A mutable memory location. +/// +/// ``` +/// use std::cell::Cell; +/// +/// struct SomeStruct { +/// regular_field: u8, +/// special_field: Cell, +/// } +/// +/// let my_struct = SomeStruct { +/// regular_field: 0, +/// special_field: Cell::new(1), +/// }; +/// +/// let new_value = 100; +/// +/// // ERROR, because my_struct is immutable +/// // immutable.regular_field = new_value; +/// +/// // WORKS, special_field is mutable because it is Cell +/// immutable.special_field.set(new_value); +/// assert_eq!(immutable.special_field.get(), new_value); +/// ``` /// /// See the [module-level documentation](index.html) for more. #[stable(feature = "rust1", since = "1.0.0")] From bb65d3256841e1a7d267a3177a9147fd83857727 Mon Sep 17 00:00:00 2001 From: Tymoteusz Jankowski Date: Mon, 24 Jul 2017 16:23:26 +0200 Subject: [PATCH 03/17] add prose --- src/libcore/cell.rs | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/libcore/cell.rs b/src/libcore/cell.rs index 7e12d5466c2bf..acff77004ee40 100644 --- a/src/libcore/cell.rs +++ b/src/libcore/cell.rs @@ -188,6 +188,11 @@ use ptr; /// A mutable memory location. /// +/// # Example +/// +/// Here you can see how using `Cell` allows to use muttable field inside +/// immutable struct (which is also called "interior mutability"). +/// /// ``` /// use std::cell::Cell; /// @@ -206,7 +211,7 @@ use ptr; /// // ERROR, because my_struct is immutable /// // immutable.regular_field = new_value; /// -/// // WORKS, special_field is mutable because it is Cell +/// // WORKS, although `my_struct` is immutable, field `special_field` is mutable because it is Cell /// immutable.special_field.set(new_value); /// assert_eq!(immutable.special_field.get(), new_value); /// ``` From 3c535952bc7df52b8b9becae26511fb6ccdab7b1 Mon Sep 17 00:00:00 2001 From: Tymoteusz Jankowski Date: Mon, 24 Jul 2017 18:01:50 +0200 Subject: [PATCH 04/17] review fixes --- src/libcore/cell.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/libcore/cell.rs b/src/libcore/cell.rs index acff77004ee40..804d95f12c42d 100644 --- a/src/libcore/cell.rs +++ b/src/libcore/cell.rs @@ -188,10 +188,10 @@ use ptr; /// A mutable memory location. /// -/// # Example +/// # Examples /// -/// Here you can see how using `Cell` allows to use muttable field inside -/// immutable struct (which is also called "interior mutability"). +/// Here you can see how using `Cell` allows to use mutable field inside +/// immutable struct (which is also called 'interior mutability'). /// /// ``` /// use std::cell::Cell; From 82860753463314e6a1b94f1f97d4d9c4effc0742 Mon Sep 17 00:00:00 2001 From: Tymoteusz Jankowski Date: Mon, 24 Jul 2017 18:07:51 +0200 Subject: [PATCH 05/17] ci fix? --- src/libcore/cell.rs | 5 ----- 1 file changed, 5 deletions(-) diff --git a/src/libcore/cell.rs b/src/libcore/cell.rs index 804d95f12c42d..1610e89a82d73 100644 --- a/src/libcore/cell.rs +++ b/src/libcore/cell.rs @@ -187,12 +187,9 @@ use ops::{Deref, DerefMut, CoerceUnsized}; use ptr; /// A mutable memory location. -/// /// # Examples -/// /// Here you can see how using `Cell` allows to use mutable field inside /// immutable struct (which is also called 'interior mutability'). -/// /// ``` /// use std::cell::Cell; /// @@ -207,10 +204,8 @@ use ptr; /// }; /// /// let new_value = 100; -/// /// // ERROR, because my_struct is immutable /// // immutable.regular_field = new_value; -/// /// // WORKS, although `my_struct` is immutable, field `special_field` is mutable because it is Cell /// immutable.special_field.set(new_value); /// assert_eq!(immutable.special_field.get(), new_value); From beb072a8938db93e694435e852510b79a0909fd3 Mon Sep 17 00:00:00 2001 From: Tymoteusz Jankowski Date: Mon, 24 Jul 2017 21:45:21 +0200 Subject: [PATCH 06/17] empty lines --- src/libcore/cell.rs | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/libcore/cell.rs b/src/libcore/cell.rs index 1610e89a82d73..1e7c8dfce5b3b 100644 --- a/src/libcore/cell.rs +++ b/src/libcore/cell.rs @@ -187,9 +187,12 @@ use ops::{Deref, DerefMut, CoerceUnsized}; use ptr; /// A mutable memory location. +/// /// # Examples +/// /// Here you can see how using `Cell` allows to use mutable field inside /// immutable struct (which is also called 'interior mutability'). +/// /// ``` /// use std::cell::Cell; /// @@ -204,8 +207,10 @@ use ptr; /// }; /// /// let new_value = 100; +/// /// // ERROR, because my_struct is immutable /// // immutable.regular_field = new_value; +/// /// // WORKS, although `my_struct` is immutable, field `special_field` is mutable because it is Cell /// immutable.special_field.set(new_value); /// assert_eq!(immutable.special_field.get(), new_value); From d429a4eac81aea6070655cdfb5604187d94355a2 Mon Sep 17 00:00:00 2001 From: Tymoteusz Jankowski Date: Mon, 24 Jul 2017 23:43:34 +0200 Subject: [PATCH 07/17] s/immutable/my_struct --- src/libcore/cell.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/libcore/cell.rs b/src/libcore/cell.rs index 1e7c8dfce5b3b..21b5557db99f2 100644 --- a/src/libcore/cell.rs +++ b/src/libcore/cell.rs @@ -209,11 +209,11 @@ use ptr; /// let new_value = 100; /// /// // ERROR, because my_struct is immutable -/// // immutable.regular_field = new_value; +/// // my_struct.regular_field = new_value; /// /// // WORKS, although `my_struct` is immutable, field `special_field` is mutable because it is Cell -/// immutable.special_field.set(new_value); -/// assert_eq!(immutable.special_field.get(), new_value); +/// my_struct.special_field.set(new_value); +/// assert_eq!(my_struct.special_field.get(), new_value); /// ``` /// /// See the [module-level documentation](index.html) for more. From dd371a2069e84bf58702cb4c760681ee9c8aa874 Mon Sep 17 00:00:00 2001 From: Alex Crichton Date: Mon, 31 Jul 2017 18:39:25 -0700 Subject: [PATCH 08/17] rustc: Inline bitwise modification operators These need to be inlined across crates to avoid showing up as one-instruction functions in profiles! In the benchmark from #43578 this decreased the translation item collection step from 30s to 23s, and looks like it also allowed vectorization elsewhere of the operations! --- src/librustc_data_structures/bitslice.rs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/librustc_data_structures/bitslice.rs b/src/librustc_data_structures/bitslice.rs index ba53578e57918..f74af6ee1632e 100644 --- a/src/librustc_data_structures/bitslice.rs +++ b/src/librustc_data_structures/bitslice.rs @@ -134,9 +134,11 @@ pub trait BitwiseOperator { pub struct Union; impl BitwiseOperator for Union { + #[inline] fn join(&self, a: usize, b: usize) -> usize { a | b } } pub struct Subtract; impl BitwiseOperator for Subtract { + #[inline] fn join(&self, a: usize, b: usize) -> usize { a & !b } } From bdb53e55b0d30ad4f5438eff74d0b705f8675d98 Mon Sep 17 00:00:00 2001 From: Danek Duvall Date: Tue, 1 Aug 2017 12:38:36 -0700 Subject: [PATCH 09/17] Fix the Solaris pthread_t raw type in std to match what's in libc The old type causes failures when building cargo 0.20.0 after changeset 8304e06b5 in the libc repo. --- src/libstd/os/solaris/raw.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/libstd/os/solaris/raw.rs b/src/libstd/os/solaris/raw.rs index b84fdba9ca25c..5a813c5c76bca 100644 --- a/src/libstd/os/solaris/raw.rs +++ b/src/libstd/os/solaris/raw.rs @@ -32,7 +32,7 @@ use os::unix::raw::{uid_t, gid_t}; #[stable(feature = "raw_ext", since = "1.1.0")] pub type time_t = i64; #[stable(feature = "pthread_t", since = "1.8.0")] -pub type pthread_t = usize; +pub type pthread_t = u32; #[repr(C)] #[derive(Clone)] From 1b831cf54e441aa0c5d20a344f4c9f75d01d2538 Mon Sep 17 00:00:00 2001 From: Inokentiy Babushkin Date: Tue, 1 Aug 2017 22:27:30 +0200 Subject: [PATCH 10/17] Derive `Hash` on `AssociatedKind`. This is a trivial change useful in downstream code poking in rustc's innards. --- src/librustc/ty/mod.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/librustc/ty/mod.rs b/src/librustc/ty/mod.rs index e0b0aca1261bf..f245b1503dab8 100644 --- a/src/librustc/ty/mod.rs +++ b/src/librustc/ty/mod.rs @@ -174,7 +174,7 @@ pub struct AssociatedItem { pub method_has_self_argument: bool, } -#[derive(Copy, Clone, PartialEq, Eq, Debug, RustcEncodable, RustcDecodable)] +#[derive(Copy, Clone, PartialEq, Eq, Debug, Hash, RustcEncodable, RustcDecodable)] pub enum AssociatedKind { Const, Method, From 881062776afe4575f3c9d6534f688a70cf34a7db Mon Sep 17 00:00:00 2001 From: Corey Farwell Date: Tue, 1 Aug 2017 07:41:06 -0400 Subject: [PATCH 11/17] Add doc example for HashSet::hasher. --- src/libstd/collections/hash/set.rs | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/src/libstd/collections/hash/set.rs b/src/libstd/collections/hash/set.rs index d80df5f18b610..040595bbb0426 100644 --- a/src/libstd/collections/hash/set.rs +++ b/src/libstd/collections/hash/set.rs @@ -215,6 +215,17 @@ impl HashSet /// Returns a reference to the set's [`BuildHasher`]. /// /// [`BuildHasher`]: ../../std/hash/trait.BuildHasher.html + /// + /// # Examples + /// + /// ``` + /// use std::collections::HashSet; + /// use std::collections::hash_map::RandomState; + /// + /// let hasher = RandomState::new(); + /// let set: HashSet = HashSet::with_hasher(hasher); + /// let hasher: &RandomState = set.hasher(); + /// ``` #[stable(feature = "hashmap_public_hasher", since = "1.9.0")] pub fn hasher(&self) -> &S { self.map.hasher() From 9e192602860c897974e81c0c93b9c7293ce0fd3e Mon Sep 17 00:00:00 2001 From: Corey Farwell Date: Tue, 1 Aug 2017 07:42:59 -0400 Subject: [PATCH 12/17] Show that the capacity changed in HashSet::reserve doc example. --- src/libstd/collections/hash/set.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/src/libstd/collections/hash/set.rs b/src/libstd/collections/hash/set.rs index 040595bbb0426..0e65a30f3b2a3 100644 --- a/src/libstd/collections/hash/set.rs +++ b/src/libstd/collections/hash/set.rs @@ -260,6 +260,7 @@ impl HashSet /// use std::collections::HashSet; /// let mut set: HashSet = HashSet::new(); /// set.reserve(10); + /// assert!(set.capacity() >= 10); /// ``` #[stable(feature = "rust1", since = "1.0.0")] pub fn reserve(&mut self, additional: usize) { From 070eb3c66775789fbfe1607d3d6ef643c9afe3db Mon Sep 17 00:00:00 2001 From: Corey Farwell Date: Tue, 1 Aug 2017 07:44:43 -0400 Subject: [PATCH 13/17] Indicate HashSet is code-like in docs. --- src/libstd/collections/hash/set.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/libstd/collections/hash/set.rs b/src/libstd/collections/hash/set.rs index 0e65a30f3b2a3..ff19a0a1f149d 100644 --- a/src/libstd/collections/hash/set.rs +++ b/src/libstd/collections/hash/set.rs @@ -123,7 +123,7 @@ pub struct HashSet { } impl HashSet { - /// Creates an empty HashSet. + /// Creates an empty `HashSet`. /// /// # Examples /// From 9e2b0c6390526e46f7bd217e13c281f7895bd1d9 Mon Sep 17 00:00:00 2001 From: Corey Farwell Date: Tue, 1 Aug 2017 07:46:41 -0400 Subject: [PATCH 14/17] Remove unnecessary 'mut' bindings. --- src/libstd/collections/hash/set.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/libstd/collections/hash/set.rs b/src/libstd/collections/hash/set.rs index ff19a0a1f149d..f8a3166571403 100644 --- a/src/libstd/collections/hash/set.rs +++ b/src/libstd/collections/hash/set.rs @@ -129,7 +129,7 @@ impl HashSet { /// /// ``` /// use std::collections::HashSet; - /// let mut set: HashSet = HashSet::new(); + /// let set: HashSet = HashSet::new(); /// ``` #[inline] #[stable(feature = "rust1", since = "1.0.0")] @@ -146,7 +146,7 @@ impl HashSet { /// /// ``` /// use std::collections::HashSet; - /// let mut set: HashSet = HashSet::with_capacity(10); + /// let set: HashSet = HashSet::with_capacity(10); /// ``` #[inline] #[stable(feature = "rust1", since = "1.0.0")] From 1599fad5b485cbfbed698df3590665f064999948 Mon Sep 17 00:00:00 2001 From: Corey Farwell Date: Tue, 1 Aug 2017 07:47:17 -0400 Subject: [PATCH 15/17] Show the capacity in HashSet::with_capacity doc example. --- src/libstd/collections/hash/set.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/src/libstd/collections/hash/set.rs b/src/libstd/collections/hash/set.rs index f8a3166571403..a02674ed109a6 100644 --- a/src/libstd/collections/hash/set.rs +++ b/src/libstd/collections/hash/set.rs @@ -147,6 +147,7 @@ impl HashSet { /// ``` /// use std::collections::HashSet; /// let set: HashSet = HashSet::with_capacity(10); + /// assert!(set.capacity() >= 10); /// ``` #[inline] #[stable(feature = "rust1", since = "1.0.0")] From 34c1bfb0e142587bbbede848b69b2d498b8ede34 Mon Sep 17 00:00:00 2001 From: Corey Farwell Date: Tue, 1 Aug 2017 07:55:44 -0400 Subject: [PATCH 16/17] Remove unnecessary clones in doc examples. --- src/libstd/collections/hash/set.rs | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/src/libstd/collections/hash/set.rs b/src/libstd/collections/hash/set.rs index a02674ed109a6..3c39db3fbabb3 100644 --- a/src/libstd/collections/hash/set.rs +++ b/src/libstd/collections/hash/set.rs @@ -325,13 +325,13 @@ impl HashSet /// println!("{}", x); // Print 1 /// } /// - /// let diff: HashSet<_> = a.difference(&b).cloned().collect(); - /// assert_eq!(diff, [1].iter().cloned().collect()); + /// let diff: HashSet<_> = a.difference(&b).collect(); + /// assert_eq!(diff, [1].iter().collect()); /// /// // Note that difference is not symmetric, /// // and `b - a` means something else: - /// let diff: HashSet<_> = b.difference(&a).cloned().collect(); - /// assert_eq!(diff, [4].iter().cloned().collect()); + /// let diff: HashSet<_> = b.difference(&a).collect(); + /// assert_eq!(diff, [4].iter().collect()); /// ``` #[stable(feature = "rust1", since = "1.0.0")] pub fn difference<'a>(&'a self, other: &'a HashSet) -> Difference<'a, T, S> { @@ -356,11 +356,11 @@ impl HashSet /// println!("{}", x); /// } /// - /// let diff1: HashSet<_> = a.symmetric_difference(&b).cloned().collect(); - /// let diff2: HashSet<_> = b.symmetric_difference(&a).cloned().collect(); + /// let diff1: HashSet<_> = a.symmetric_difference(&b).collect(); + /// let diff2: HashSet<_> = b.symmetric_difference(&a).collect(); /// /// assert_eq!(diff1, diff2); - /// assert_eq!(diff1, [1, 4].iter().cloned().collect()); + /// assert_eq!(diff1, [1, 4].iter().collect()); /// ``` #[stable(feature = "rust1", since = "1.0.0")] pub fn symmetric_difference<'a>(&'a self, @@ -384,8 +384,8 @@ impl HashSet /// println!("{}", x); /// } /// - /// let intersection: HashSet<_> = a.intersection(&b).cloned().collect(); - /// assert_eq!(intersection, [2, 3].iter().cloned().collect()); + /// let intersection: HashSet<_> = a.intersection(&b).collect(); + /// assert_eq!(intersection, [2, 3].iter().collect()); /// ``` #[stable(feature = "rust1", since = "1.0.0")] pub fn intersection<'a>(&'a self, other: &'a HashSet) -> Intersection<'a, T, S> { @@ -410,8 +410,8 @@ impl HashSet /// println!("{}", x); /// } /// - /// let union: HashSet<_> = a.union(&b).cloned().collect(); - /// assert_eq!(union, [1, 2, 3, 4].iter().cloned().collect()); + /// let union: HashSet<_> = a.union(&b).collect(); + /// assert_eq!(union, [1, 2, 3, 4].iter().collect()); /// ``` #[stable(feature = "rust1", since = "1.0.0")] pub fn union<'a>(&'a self, other: &'a HashSet) -> Union<'a, T, S> { From d9df2963ad40b67aecde95cbfe98599a45351352 Mon Sep 17 00:00:00 2001 From: Corey Farwell Date: Tue, 1 Aug 2017 08:12:01 -0400 Subject: [PATCH 17/17] Add doc example for HashSet::drain. --- src/libstd/collections/hash/set.rs | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/src/libstd/collections/hash/set.rs b/src/libstd/collections/hash/set.rs index 3c39db3fbabb3..80a223c7d74ea 100644 --- a/src/libstd/collections/hash/set.rs +++ b/src/libstd/collections/hash/set.rs @@ -453,6 +453,22 @@ impl HashSet } /// Clears the set, returning all elements in an iterator. + /// + /// # Examples + /// + /// ``` + /// use std::collections::HashSet; + /// + /// let mut set: HashSet<_> = [1, 2, 3].iter().cloned().collect(); + /// assert!(!set.is_empty()); + /// + /// // print 1, 2, 3 in an arbitrary order + /// for i in set.drain() { + /// println!("{}", i); + /// } + /// + /// assert!(set.is_empty()); + /// ``` #[inline] #[stable(feature = "drain", since = "1.6.0")] pub fn drain(&mut self) -> Drain {