From f7019a4e2f80577d38ec35fcebd64d5970b15f78 Mon Sep 17 00:00:00 2001 From: Jonathan Turner Date: Thu, 21 Jul 2016 12:38:15 -0700 Subject: [PATCH 1/7] Remove unused methods from MultiSpan --- src/libsyntax_pos/lib.rs | 19 ++++--------------- 1 file changed, 4 insertions(+), 15 deletions(-) diff --git a/src/libsyntax_pos/lib.rs b/src/libsyntax_pos/lib.rs index 7dfe19452a2a9..c96be8fec2b02 100644 --- a/src/libsyntax_pos/lib.rs +++ b/src/libsyntax_pos/lib.rs @@ -193,20 +193,6 @@ impl MultiSpan { } } - pub fn from_span(primary_span: Span) -> MultiSpan { - MultiSpan { - primary_spans: vec![primary_span], - span_labels: vec![] - } - } - - pub fn from_spans(vec: Vec) -> MultiSpan { - MultiSpan { - primary_spans: vec, - span_labels: vec![] - } - } - pub fn push_span_label(&mut self, span: Span, label: String) { self.span_labels.push((span, label)); } @@ -254,7 +240,10 @@ impl MultiSpan { impl From for MultiSpan { fn from(span: Span) -> MultiSpan { - MultiSpan::from_span(span) + MultiSpan { + primary_spans: vec![span], + span_labels: vec![] + } } } From e8ac07941c952d352683c7311b343ae72baf9fde Mon Sep 17 00:00:00 2001 From: Oliver Schneider Date: Fri, 22 Jul 2016 09:34:44 +0200 Subject: [PATCH 2/7] improve const eval error reporting on "" and b"" casts --- src/librustc_const_eval/eval.rs | 18 ++++++++++++++++-- src/test/run-pass/const-byte-str-cast.rs | 5 ++++- 2 files changed, 20 insertions(+), 3 deletions(-) diff --git a/src/librustc_const_eval/eval.rs b/src/librustc_const_eval/eval.rs index a3c707e82a0ff..4643686786be6 100644 --- a/src/librustc_const_eval/eval.rs +++ b/src/librustc_const_eval/eval.rs @@ -1105,11 +1105,25 @@ fn cast_const<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, val: ConstVal, ty: ty::Ty) Float(f) => cast_const_float(tcx, f, ty), Char(c) => cast_const_int(tcx, Infer(c as u64), ty), Function(_) => Err(UnimplementedConstVal("casting fn pointers")), - ByteStr(_) => match ty.sty { + ByteStr(b) => match ty.sty { ty::TyRawPtr(_) => { Err(ErrKind::UnimplementedConstVal("casting a bytestr to a raw ptr")) }, - ty::TyRef(..) => Err(ErrKind::UnimplementedConstVal("casting a bytestr to slice")), + ty::TyRef(_, ty::TypeAndMut { ref ty, mutbl: hir::MutImmutable }) => match ty.sty { + ty::TyArray(ty, n) if ty == tcx.types.u8 && n == b.len() => Ok(ByteStr(b)), + ty::TySlice(_) => { + Err(ErrKind::UnimplementedConstVal("casting a bytestr to slice")) + }, + _ => Err(CannotCast), + }, + _ => Err(CannotCast), + }, + Str(s) => match ty.sty { + ty::TyRawPtr(_) => Err(ErrKind::UnimplementedConstVal("casting a str to a raw ptr")), + ty::TyRef(_, ty::TypeAndMut { ref ty, mutbl: hir::MutImmutable }) => match ty.sty { + ty::TyStr => Ok(Str(s)), + _ => Err(CannotCast), + }, _ => Err(CannotCast), }, _ => Err(CannotCast), diff --git a/src/test/run-pass/const-byte-str-cast.rs b/src/test/run-pass/const-byte-str-cast.rs index 2f265b9112b98..7297c71a6d668 100644 --- a/src/test/run-pass/const-byte-str-cast.rs +++ b/src/test/run-pass/const-byte-str-cast.rs @@ -1,4 +1,4 @@ -// Copyright 2013 The Rust Project Developers. See the COPYRIGHT +// 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. // @@ -12,4 +12,7 @@ pub fn main() { let _ = b"x" as &[u8]; + let _ = b"y" as &[u8; 1]; + let _ = b"z" as *const u8; + let _ = "รค" as *const str; } From 3c8fae369f54649fe90dc85f284e897974aeb1ca Mon Sep 17 00:00:00 2001 From: ggomez Date: Fri, 22 Jul 2016 14:57:52 +0200 Subject: [PATCH 3/7] Add Random state doc --- src/libstd/collections/hash/map.rs | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/src/libstd/collections/hash/map.rs b/src/libstd/collections/hash/map.rs index 60d7e01d98814..d4b09807e749b 100644 --- a/src/libstd/collections/hash/map.rs +++ b/src/libstd/collections/hash/map.rs @@ -1666,6 +1666,17 @@ impl<'a, K, V, S> Extend<(&'a K, &'a V)> for HashMap /// A particular instance `RandomState` will create the same instances of /// `Hasher`, but the hashers created by two different `RandomState` /// instances are unlikely to produce the same result for the same values. +/// +/// # Examples +/// +/// ``` +/// use std::collections::HashMap; +/// use std::collections::hash_map::RandomState; +/// +/// let s = RandomState::new(); +/// let mut map = HashMap::with_hasher(s); +/// map.insert(1, 2); +/// ``` #[derive(Clone)] #[stable(feature = "hashmap_build_hasher", since = "1.7.0")] pub struct RandomState { @@ -1675,6 +1686,14 @@ pub struct RandomState { impl RandomState { /// Constructs a new `RandomState` that is initialized with random keys. + /// + /// # Examples + /// + /// ``` + /// use std::collections::hash_map::RandomState; + /// + /// let s = RandomState::new(); + /// ``` #[inline] #[allow(deprecated)] // rand #[stable(feature = "hashmap_build_hasher", since = "1.7.0")] From 890706070dee22035032f936a51a122670d1cd40 Mon Sep 17 00:00:00 2001 From: ggomez Date: Fri, 22 Jul 2016 16:38:16 +0200 Subject: [PATCH 4/7] Add BuildHasher example --- src/libcore/hash/mod.rs | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/src/libcore/hash/mod.rs b/src/libcore/hash/mod.rs index 9e3f7a4a84a81..27fdbd383017f 100644 --- a/src/libcore/hash/mod.rs +++ b/src/libcore/hash/mod.rs @@ -234,6 +234,16 @@ pub trait BuildHasher { type Hasher: Hasher; /// Creates a new hasher. + /// + /// # Examples + /// + /// ``` + /// use std::collections::hash_map::RandomState; + /// use std::hash::BuildHasher; + /// + /// let s = RandomState::new(); + /// let new_s = s.build_hasher(); + /// ``` #[stable(since = "1.7.0", feature = "build_hasher")] fn build_hasher(&self) -> Self::Hasher; } From f2f8bbc49f240c9494f876c57389ad58fe7d6bb0 Mon Sep 17 00:00:00 2001 From: Camille Roussel Date: Fri, 22 Jul 2016 10:48:19 -0400 Subject: [PATCH 5/7] Fixed to spelling errors in char.rs Fixed two small spelling mistakes (interator -> iterator) in the documentation for encode_utf8 and encode_utf16 --- src/librustc_unicode/char.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/librustc_unicode/char.rs b/src/librustc_unicode/char.rs index 7445ff94eb502..1ea0f8d70a8eb 100644 --- a/src/librustc_unicode/char.rs +++ b/src/librustc_unicode/char.rs @@ -392,7 +392,7 @@ impl char { C::len_utf16(self) } - /// Returns an interator over the bytes of this character as UTF-8. + /// Returns an iterator over the bytes of this character as UTF-8. /// /// The returned iterator also has an `as_slice()` method to view the /// encoded bytes as a byte slice. @@ -415,7 +415,7 @@ impl char { C::encode_utf8(self) } - /// Returns an interator over the `u16` entries of this character as UTF-16. + /// Returns an iterator over the `u16` entries of this character as UTF-16. /// /// The returned iterator also has an `as_slice()` method to view the /// encoded form as a slice. From 1e0043eb6c445fb96981b6d46dae4c93af4fbda3 Mon Sep 17 00:00:00 2001 From: Corey Farwell Date: Fri, 22 Jul 2016 21:43:59 -0400 Subject: [PATCH 6/7] Fix incorrect 'memory leak' example for `Vec::set_len`. Example was written in https://github.com/rust-lang/rust/pull/34911 Issue was brought up in this comment: https://github.com/rust-lang/rust/commit/a005b2cd2ac679da7393e537aa05e2b7d32d36d5#commitcomment-18346958 --- src/libcollections/vec.rs | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/libcollections/vec.rs b/src/libcollections/vec.rs index 2d77b38879b8e..967baccd2740a 100644 --- a/src/libcollections/vec.rs +++ b/src/libcollections/vec.rs @@ -593,11 +593,12 @@ impl Vec { /// ``` /// /// In this example, there is a memory leak since the memory locations - /// owned by the vector were not freed prior to the `set_len` call: + /// owned by the inner vectors were not freed prior to the `set_len` call: /// /// ``` - /// let mut vec = vec!['r', 'u', 's', 't']; - /// + /// let mut vec = vec![vec![1, 0, 0], + /// vec![0, 1, 0], + /// vec![0, 0, 1]]; /// unsafe { /// vec.set_len(0); /// } From c77f8ce7c3284441a00faed6782d08eb5a78296c Mon Sep 17 00:00:00 2001 From: Corey Farwell Date: Fri, 22 Jul 2016 20:23:25 -0400 Subject: [PATCH 7/7] Doc example improvements for `slice::windows`. * Modify existing example to not rely on printing to see results * Add an example demonstrating when slice is shorter than `size` --- src/libcollections/slice.rs | 21 ++++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/src/libcollections/slice.rs b/src/libcollections/slice.rs index 2c54dc13c8d0b..1f8eea56fc69c 100644 --- a/src/libcollections/slice.rs +++ b/src/libcollections/slice.rs @@ -544,14 +544,21 @@ impl [T] { /// /// # Example /// - /// Print the adjacent pairs of a slice (i.e. `[1,2]`, `[2,3]`, - /// `[3,4]`): + /// ``` + /// let slice = ['r', 'u', 's', 't']; + /// let mut iter = slice.windows(2); + /// assert_eq!(iter.next().unwrap(), &['r', 'u']); + /// assert_eq!(iter.next().unwrap(), &['u', 's']); + /// assert_eq!(iter.next().unwrap(), &['s', 't']); + /// assert!(iter.next().is_none()); + /// ``` /// - /// ```rust - /// let v = &[1, 2, 3, 4]; - /// for win in v.windows(2) { - /// println!("{:?}", win); - /// } + /// If the slice is shorter than `size`: + /// + /// ``` + /// let slice = ['f', 'o', 'o']; + /// let mut iter = slice.windows(4); + /// assert!(iter.next().is_none()); /// ``` #[stable(feature = "rust1", since = "1.0.0")] #[inline]