From 8f9c24f6793a9dd0604dd3ae4b5f4ef829abd93d Mon Sep 17 00:00:00 2001 From: Alex Griffiths Date: Wed, 19 Aug 2020 19:57:27 +1000 Subject: [PATCH 01/13] Extrated drain into it's own module. --- library/alloc/src/vec.rs | 133 ++------------------------------ library/alloc/src/vec/drain.rs | 134 +++++++++++++++++++++++++++++++++ 2 files changed, 139 insertions(+), 128 deletions(-) create mode 100644 library/alloc/src/vec/drain.rs diff --git a/library/alloc/src/vec.rs b/library/alloc/src/vec.rs index 80f7ff4893e21..b6ad50992948f 100644 --- a/library/alloc/src/vec.rs +++ b/library/alloc/src/vec.rs @@ -76,6 +76,11 @@ use crate::boxed::Box; use crate::collections::TryReserveError; use crate::raw_vec::RawVec; +#[stable(feature="drain", since="1.6.0")] +mod drain; +#[stable(feature="drain", since="1.6.0")] +pub use drain::Drain; + /// A contiguous growable array type, written `Vec` but pronounced 'vector'. /// /// # Examples @@ -2800,134 +2805,6 @@ unsafe impl<#[may_dangle] T> Drop for IntoIter { } } -/// A draining iterator for `Vec`. -/// -/// This `struct` is created by [`Vec::drain`]. -#[stable(feature = "drain", since = "1.6.0")] -pub struct Drain<'a, T: 'a> { - /// Index of tail to preserve - tail_start: usize, - /// Length of tail - tail_len: usize, - /// Current remaining range to remove - iter: slice::Iter<'a, T>, - vec: NonNull>, -} - -#[stable(feature = "collection_debug", since = "1.17.0")] -impl fmt::Debug for Drain<'_, T> { - fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - f.debug_tuple("Drain").field(&self.iter.as_slice()).finish() - } -} - -impl<'a, T> Drain<'a, T> { - /// Returns the remaining items of this iterator as a slice. - /// - /// # Examples - /// - /// ``` - /// let mut vec = vec!['a', 'b', 'c']; - /// let mut drain = vec.drain(..); - /// assert_eq!(drain.as_slice(), &['a', 'b', 'c']); - /// let _ = drain.next().unwrap(); - /// assert_eq!(drain.as_slice(), &['b', 'c']); - /// ``` - #[stable(feature = "vec_drain_as_slice", since = "1.46.0")] - pub fn as_slice(&self) -> &[T] { - self.iter.as_slice() - } -} - -#[stable(feature = "vec_drain_as_slice", since = "1.46.0")] -impl<'a, T> AsRef<[T]> for Drain<'a, T> { - fn as_ref(&self) -> &[T] { - self.as_slice() - } -} - -#[stable(feature = "drain", since = "1.6.0")] -unsafe impl Sync for Drain<'_, T> {} -#[stable(feature = "drain", since = "1.6.0")] -unsafe impl Send for Drain<'_, T> {} - -#[stable(feature = "drain", since = "1.6.0")] -impl Iterator for Drain<'_, T> { - type Item = T; - - #[inline] - fn next(&mut self) -> Option { - self.iter.next().map(|elt| unsafe { ptr::read(elt as *const _) }) - } - - fn size_hint(&self) -> (usize, Option) { - self.iter.size_hint() - } -} - -#[stable(feature = "drain", since = "1.6.0")] -impl DoubleEndedIterator for Drain<'_, T> { - #[inline] - fn next_back(&mut self) -> Option { - self.iter.next_back().map(|elt| unsafe { ptr::read(elt as *const _) }) - } -} - -#[stable(feature = "drain", since = "1.6.0")] -impl Drop for Drain<'_, T> { - fn drop(&mut self) { - /// Continues dropping the remaining elements in the `Drain`, then moves back the - /// un-`Drain`ed elements to restore the original `Vec`. - struct DropGuard<'r, 'a, T>(&'r mut Drain<'a, T>); - - impl<'r, 'a, T> Drop for DropGuard<'r, 'a, T> { - fn drop(&mut self) { - // Continue the same loop we have below. If the loop already finished, this does - // nothing. - self.0.for_each(drop); - - if self.0.tail_len > 0 { - unsafe { - let source_vec = self.0.vec.as_mut(); - // memmove back untouched tail, update to new length - let start = source_vec.len(); - let tail = self.0.tail_start; - if tail != start { - let src = source_vec.as_ptr().add(tail); - let dst = source_vec.as_mut_ptr().add(start); - ptr::copy(src, dst, self.0.tail_len); - } - source_vec.set_len(start + self.0.tail_len); - } - } - } - } - - // exhaust self first - while let Some(item) = self.next() { - let guard = DropGuard(self); - drop(item); - mem::forget(guard); - } - - // Drop a `DropGuard` to move back the non-drained tail of `self`. - DropGuard(self); - } -} - -#[stable(feature = "drain", since = "1.6.0")] -impl ExactSizeIterator for Drain<'_, T> { - fn is_empty(&self) -> bool { - self.iter.is_empty() - } -} - -#[unstable(feature = "trusted_len", issue = "37572")] -unsafe impl TrustedLen for Drain<'_, T> {} - -#[stable(feature = "fused", since = "1.26.0")] -impl FusedIterator for Drain<'_, T> {} - /// A splicing iterator for `Vec`. /// /// This struct is created by [`Vec::splice()`]. diff --git a/library/alloc/src/vec/drain.rs b/library/alloc/src/vec/drain.rs new file mode 100644 index 0000000000000..5deb337c24c36 --- /dev/null +++ b/library/alloc/src/vec/drain.rs @@ -0,0 +1,134 @@ +use core::slice; +use core::fmt; +use core::iter::{FusedIterator, TrustedLen}; +use core::mem::{self}; +use core::ptr::{self, NonNull}; + +use crate::vec::Vec; +/// A draining iterator for `Vec`. +/// +/// This `struct` is created by [`Vec::drain`]. +#[stable(feature = "drain", since = "1.6.0")] +pub struct Drain<'a, T: 'a> { + /// Index of tail to preserve + pub(super) tail_start: usize, + /// Length of tail + pub(super) tail_len: usize, + /// Current remaining range to remove + pub(super) iter: slice::Iter<'a, T>, + pub(super) vec: NonNull>, +} + +#[stable(feature = "collection_debug", since = "1.17.0")] +impl fmt::Debug for Drain<'_, T> { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + f.debug_tuple("Drain").field(&self.iter.as_slice()).finish() + } +} + +impl<'a, T> Drain<'a, T> { + /// Returns the remaining items of this iterator as a slice. + /// + /// # Examples + /// + /// ``` + /// let mut vec = vec!['a', 'b', 'c']; + /// let mut drain = vec.drain(..); + /// assert_eq!(drain.as_slice(), &['a', 'b', 'c']); + /// let _ = drain.next().unwrap(); + /// assert_eq!(drain.as_slice(), &['b', 'c']); + /// ``` + #[stable(feature = "vec_drain_as_slice", since = "1.46.0")] + pub fn as_slice(&self) -> &[T] { + self.iter.as_slice() + } +} + +#[stable(feature = "vec_drain_as_slice", since = "1.46.0")] +impl<'a, T> AsRef<[T]> for Drain<'a, T> { + fn as_ref(&self) -> &[T] { + self.as_slice() + } +} + +#[stable(feature = "drain", since = "1.6.0")] +unsafe impl Sync for Drain<'_, T> {} +#[stable(feature = "drain", since = "1.6.0")] +unsafe impl Send for Drain<'_, T> {} + +#[stable(feature = "drain", since = "1.6.0")] +impl Iterator for Drain<'_, T> { + type Item = T; + + #[inline] + fn next(&mut self) -> Option { + self.iter.next().map(|elt| unsafe { ptr::read(elt as *const _) }) + } + + fn size_hint(&self) -> (usize, Option) { + self.iter.size_hint() + } +} + +#[stable(feature = "drain", since = "1.6.0")] +impl DoubleEndedIterator for Drain<'_, T> { + #[inline] + fn next_back(&mut self) -> Option { + self.iter.next_back().map(|elt| unsafe { ptr::read(elt as *const _) }) + } +} + +#[stable(feature = "drain", since = "1.6.0")] +impl Drop for Drain<'_, T> { + fn drop(&mut self) { + /// Continues dropping the remaining elements in the `Drain`, then moves back the + /// un-`Drain`ed elements to restore the original `Vec`. + struct DropGuard<'r, 'a, T>(&'r mut Drain<'a, T>); + + impl<'r, 'a, T> Drop for DropGuard<'r, 'a, T> { + fn drop(&mut self) { + // Continue the same loop we have below. If the loop already finished, this does + // nothing. + self.0.for_each(drop); + + if self.0.tail_len > 0 { + unsafe { + let source_vec = self.0.vec.as_mut(); + // memmove back untouched tail, update to new length + let start = source_vec.len(); + let tail = self.0.tail_start; + if tail != start { + let src = source_vec.as_ptr().add(tail); + let dst = source_vec.as_mut_ptr().add(start); + ptr::copy(src, dst, self.0.tail_len); + } + source_vec.set_len(start + self.0.tail_len); + } + } + } + } + + // exhaust self first + while let Some(item) = self.next() { + let guard = DropGuard(self); + drop(item); + mem::forget(guard); + } + + // Drop a `DropGuard` to move back the non-drained tail of `self`. + DropGuard(self); + } +} + +#[stable(feature = "drain", since = "1.6.0")] +impl ExactSizeIterator for Drain<'_, T> { + fn is_empty(&self) -> bool { + self.iter.is_empty() + } +} + +#[unstable(feature = "trusted_len", issue = "37572")] +unsafe impl TrustedLen for Drain<'_, T> {} + +#[stable(feature = "fused", since = "1.26.0")] +impl FusedIterator for Drain<'_, T> {} \ No newline at end of file From b2c02bea7eaef9774709967be7e72eb0b19f4d54 Mon Sep 17 00:00:00 2001 From: Alex Griffiths Date: Wed, 19 Aug 2020 19:59:43 +1000 Subject: [PATCH 02/13] Remove tidy file length comment --- library/alloc/src/vec.rs | 1 - 1 file changed, 1 deletion(-) diff --git a/library/alloc/src/vec.rs b/library/alloc/src/vec.rs index b6ad50992948f..af025da59d6d4 100644 --- a/library/alloc/src/vec.rs +++ b/library/alloc/src/vec.rs @@ -1,4 +1,3 @@ -// ignore-tidy-filelength //! A contiguous growable array type with heap-allocated contents, written //! `Vec`. //! From c8b09e86681bdfce42776b6327b2a50e80152e0d Mon Sep 17 00:00:00 2001 From: Alex Griffiths Date: Wed, 19 Aug 2020 20:42:02 +1000 Subject: [PATCH 03/13] Pull Utf8Error and CharIndices structs into their own modules --- library/core/src/str/indices.rs | 76 +++++++++++++++++ library/core/src/str/mod.rs | 140 +++----------------------------- library/core/src/str/utf8.rs | 55 +++++++++++++ 3 files changed, 144 insertions(+), 127 deletions(-) create mode 100644 library/core/src/str/indices.rs create mode 100644 library/core/src/str/utf8.rs diff --git a/library/core/src/str/indices.rs b/library/core/src/str/indices.rs new file mode 100644 index 0000000000000..2f3efccef455c --- /dev/null +++ b/library/core/src/str/indices.rs @@ -0,0 +1,76 @@ +use crate::iter::FusedIterator; +use crate::str::Chars; + +/// An iterator over the [`char`]s of a string slice, and their positions. +/// +/// This struct is created by the [`char_indices`] method on [`str`]. +/// See its documentation for more. +/// +/// [`char_indices`]: str::char_indices +#[derive(Clone, Debug)] +#[stable(feature = "rust1", since = "1.0.0")] +pub struct CharIndices<'a> { + pub(super) front_offset: usize, + pub(super) iter: Chars<'a>, +} + +#[stable(feature = "rust1", since = "1.0.0")] +impl<'a> Iterator for CharIndices<'a> { + type Item = (usize, char); + + #[inline] + fn next(&mut self) -> Option<(usize, char)> { + let pre_len = self.iter.iter.len(); + match self.iter.next() { + None => None, + Some(ch) => { + let index = self.front_offset; + let len = self.iter.iter.len(); + self.front_offset += pre_len - len; + Some((index, ch)) + } + } + } + + #[inline] + fn count(self) -> usize { + self.iter.count() + } + + #[inline] + fn size_hint(&self) -> (usize, Option) { + self.iter.size_hint() + } + + #[inline] + fn last(mut self) -> Option<(usize, char)> { + // No need to go through the entire string. + self.next_back() + } +} + +#[stable(feature = "rust1", since = "1.0.0")] +impl<'a> DoubleEndedIterator for CharIndices<'a> { + #[inline] + fn next_back(&mut self) -> Option<(usize, char)> { + self.iter.next_back().map(|ch| { + let index = self.front_offset + self.iter.iter.len(); + (index, ch) + }) + } +} + +#[stable(feature = "fused", since = "1.26.0")] +impl FusedIterator for CharIndices<'_> {} + +impl<'a> CharIndices<'a> { + /// Views the underlying data as a subslice of the original data. + /// + /// This has the same lifetime as the original slice, and so the + /// iterator can continue to be used while this exists. + #[stable(feature = "iter_to_slice", since = "1.4.0")] + #[inline] + pub fn as_str(&self) -> &'a str { + self.iter.as_str() + } +} \ No newline at end of file diff --git a/library/core/src/str/mod.rs b/library/core/src/str/mod.rs index 934f581f3faeb..7134f9d9983e7 100644 --- a/library/core/src/str/mod.rs +++ b/library/core/src/str/mod.rs @@ -20,6 +20,19 @@ use crate::ops::Try; use crate::option; use crate::slice::{self, SliceIndex, Split as SliceSplit}; +#[allow(missing_docs)] +#[stable(feature = "rust1", since = "1.0.0")] +pub mod utf8; + +#[allow(missing_docs)] +#[stable(feature = "rust1", since = "1.0.0")] +pub mod indices; + +#[stable(feature = "rust1", since = "1.0.0")] +pub use indices::CharIndices; +#[stable(feature = "rust1", since = "1.0.0")] +pub use utf8::Utf8Error; + pub mod pattern; #[unstable(feature = "str_internals", issue = "none")] @@ -199,61 +212,6 @@ Section: Creating a string /// } /// } /// ``` -#[derive(Copy, Eq, PartialEq, Clone, Debug)] -#[stable(feature = "rust1", since = "1.0.0")] -pub struct Utf8Error { - valid_up_to: usize, - error_len: Option, -} - -impl Utf8Error { - /// Returns the index in the given string up to which valid UTF-8 was - /// verified. - /// - /// It is the maximum index such that `from_utf8(&input[..index])` - /// would return `Ok(_)`. - /// - /// # Examples - /// - /// Basic usage: - /// - /// ``` - /// use std::str; - /// - /// // some invalid bytes, in a vector - /// let sparkle_heart = vec![0, 159, 146, 150]; - /// - /// // std::str::from_utf8 returns a Utf8Error - /// let error = str::from_utf8(&sparkle_heart).unwrap_err(); - /// - /// // the second byte is invalid here - /// assert_eq!(1, error.valid_up_to()); - /// ``` - #[stable(feature = "utf8_error", since = "1.5.0")] - pub fn valid_up_to(&self) -> usize { - self.valid_up_to - } - - /// Provides more information about the failure: - /// - /// * `None`: the end of the input was reached unexpectedly. - /// `self.valid_up_to()` is 1 to 3 bytes from the end of the input. - /// If a byte stream (such as a file or a network socket) is being decoded incrementally, - /// this could be a valid `char` whose UTF-8 byte sequence is spanning multiple chunks. - /// - /// * `Some(len)`: an unexpected byte was encountered. - /// The length provided is that of the invalid byte sequence - /// that starts at the index given by `valid_up_to()`. - /// Decoding should resume after that sequence - /// (after inserting a [`U+FFFD REPLACEMENT CHARACTER`][U+FFFD]) in case of - /// lossy decoding. - /// - /// [U+FFFD]: ../../std/char/constant.REPLACEMENT_CHARACTER.html - #[stable(feature = "utf8_error_error_len", since = "1.20.0")] - pub fn error_len(&self) -> Option { - self.error_len.map(|len| len as usize) - } -} /// Converts a slice of bytes to a string slice. /// @@ -667,79 +625,7 @@ impl<'a> Chars<'a> { } } -/// An iterator over the [`char`]s of a string slice, and their positions. -/// -/// This struct is created by the [`char_indices`] method on [`str`]. -/// See its documentation for more. -/// -/// [`char_indices`]: str::char_indices -#[derive(Clone, Debug)] -#[stable(feature = "rust1", since = "1.0.0")] -pub struct CharIndices<'a> { - front_offset: usize, - iter: Chars<'a>, -} - -#[stable(feature = "rust1", since = "1.0.0")] -impl<'a> Iterator for CharIndices<'a> { - type Item = (usize, char); - - #[inline] - fn next(&mut self) -> Option<(usize, char)> { - let pre_len = self.iter.iter.len(); - match self.iter.next() { - None => None, - Some(ch) => { - let index = self.front_offset; - let len = self.iter.iter.len(); - self.front_offset += pre_len - len; - Some((index, ch)) - } - } - } - - #[inline] - fn count(self) -> usize { - self.iter.count() - } - - #[inline] - fn size_hint(&self) -> (usize, Option) { - self.iter.size_hint() - } - - #[inline] - fn last(mut self) -> Option<(usize, char)> { - // No need to go through the entire string. - self.next_back() - } -} - -#[stable(feature = "rust1", since = "1.0.0")] -impl<'a> DoubleEndedIterator for CharIndices<'a> { - #[inline] - fn next_back(&mut self) -> Option<(usize, char)> { - self.iter.next_back().map(|ch| { - let index = self.front_offset + self.iter.iter.len(); - (index, ch) - }) - } -} - -#[stable(feature = "fused", since = "1.26.0")] -impl FusedIterator for CharIndices<'_> {} -impl<'a> CharIndices<'a> { - /// Views the underlying data as a subslice of the original data. - /// - /// This has the same lifetime as the original slice, and so the - /// iterator can continue to be used while this exists. - #[stable(feature = "iter_to_slice", since = "1.4.0")] - #[inline] - pub fn as_str(&self) -> &'a str { - self.iter.as_str() - } -} /// An iterator over the bytes of a string slice. /// diff --git a/library/core/src/str/utf8.rs b/library/core/src/str/utf8.rs new file mode 100644 index 0000000000000..62ec2c0bf4471 --- /dev/null +++ b/library/core/src/str/utf8.rs @@ -0,0 +1,55 @@ +#[derive(Copy, Eq, PartialEq, Clone, Debug)] +#[stable(feature = "rust1", since = "1.0.0")] +pub struct Utf8Error { + pub(super) valid_up_to: usize, + pub(super) error_len: Option, +} + +impl Utf8Error { + /// Returns the index in the given string up to which valid UTF-8 was + /// verified. + /// + /// It is the maximum index such that `from_utf8(&input[..index])` + /// would return `Ok(_)`. + /// + /// # Examples + /// + /// Basic usage: + /// + /// ``` + /// use std::str; + /// + /// // some invalid bytes, in a vector + /// let sparkle_heart = vec![0, 159, 146, 150]; + /// + /// // std::str::from_utf8 returns a Utf8Error + /// let error = str::from_utf8(&sparkle_heart).unwrap_err(); + /// + /// // the second byte is invalid here + /// assert_eq!(1, error.valid_up_to()); + /// ``` + #[stable(feature = "utf8_error", since = "1.5.0")] + pub fn valid_up_to(&self) -> usize { + self.valid_up_to + } + + /// Provides more information about the failure: + /// + /// * `None`: the end of the input was reached unexpectedly. + /// `self.valid_up_to()` is 1 to 3 bytes from the end of the input. + /// If a byte stream (such as a file or a network socket) is being decoded incrementally, + /// this could be a valid `char` whose UTF-8 byte sequence is spanning multiple chunks. + /// + /// * `Some(len)`: an unexpected byte was encountered. + /// The length provided is that of the invalid byte sequence + /// that starts at the index given by `valid_up_to()`. + /// Decoding should resume after that sequence + /// (after inserting a [`U+FFFD REPLACEMENT CHARACTER`][U+FFFD]) in case of + /// lossy decoding. + /// + /// [U+FFFD]: ../../std/char/constant.REPLACEMENT_CHARACTER.html + #[stable(feature = "utf8_error_error_len", since = "1.20.0")] + pub fn error_len(&self) -> Option { + self.error_len.map(|len| len as usize) + } +} \ No newline at end of file From 4a87f4b4c992d12257881cc444f825da53faf4de Mon Sep 17 00:00:00 2001 From: Alex Griffiths Date: Wed, 19 Aug 2020 21:16:19 +1000 Subject: [PATCH 04/13] Pulled BindingError into it's own module. Fixed references to it. --- src/librustc_resolve/binding_error.rs | 30 ++++++++++++++++++++++++++ src/librustc_resolve/diagnostics.rs | 3 ++- src/librustc_resolve/late.rs | 3 ++- src/librustc_resolve/lib.rs | 31 ++++----------------------- 4 files changed, 38 insertions(+), 29 deletions(-) create mode 100644 src/librustc_resolve/binding_error.rs diff --git a/src/librustc_resolve/binding_error.rs b/src/librustc_resolve/binding_error.rs new file mode 100644 index 0000000000000..ef8dd30cf7d75 --- /dev/null +++ b/src/librustc_resolve/binding_error.rs @@ -0,0 +1,30 @@ +use std::collections::BTreeSet; +use std::cmp; +use rustc_span::Span; +use rustc_span::symbol::Symbol; + +#[derive(Eq)] +pub struct BindingError { + pub(super) name: Symbol, + pub(super) origin: BTreeSet, + pub(super) target: BTreeSet, + pub(super) could_be_path: bool, +} + +impl PartialOrd for BindingError { + fn partial_cmp(&self, other: &BindingError) -> Option { + Some(self.cmp(other)) + } +} + +impl PartialEq for BindingError { + fn eq(&self, other: &BindingError) -> bool { + self.name == other.name + } +} + +impl Ord for BindingError { + fn cmp(&self, other: &BindingError) -> cmp::Ordering { + self.name.cmp(&other.name) + } +} \ No newline at end of file diff --git a/src/librustc_resolve/diagnostics.rs b/src/librustc_resolve/diagnostics.rs index 48e1068b8daad..1de9dbb3c6088 100644 --- a/src/librustc_resolve/diagnostics.rs +++ b/src/librustc_resolve/diagnostics.rs @@ -23,8 +23,9 @@ use crate::imports::{Import, ImportKind, ImportResolver}; use crate::path_names_to_string; use crate::{AmbiguityError, AmbiguityErrorMisc, AmbiguityKind}; use crate::{ - BindingError, CrateLint, HasGenericParams, MacroRulesScope, Module, ModuleOrUniformRoot, + CrateLint, HasGenericParams, MacroRulesScope, Module, ModuleOrUniformRoot, }; +use crate::binding_error::BindingError; use crate::{NameBinding, NameBindingKind, PrivacyError, VisResolutionError}; use crate::{ParentScope, PathResult, ResolutionError, Resolver, Scope, ScopeSet, Segment}; diff --git a/src/librustc_resolve/late.rs b/src/librustc_resolve/late.rs index 0dbb6269d2eb3..39e47f4e56620 100644 --- a/src/librustc_resolve/late.rs +++ b/src/librustc_resolve/late.rs @@ -7,7 +7,8 @@ use RibKind::*; -use crate::{path_names_to_string, BindingError, CrateLint, LexicalScopeBinding}; +use crate::{path_names_to_string, CrateLint, LexicalScopeBinding}; +use crate::binding_error::BindingError; use crate::{Module, ModuleOrUniformRoot, NameBindingKind, ParentScope, PathResult}; use crate::{ResolutionError, Resolver, Segment, UseError}; diff --git a/src/librustc_resolve/lib.rs b/src/librustc_resolve/lib.rs index 36b7a430f78d1..3619934c9a6fa 100644 --- a/src/librustc_resolve/lib.rs +++ b/src/librustc_resolve/lib.rs @@ -56,7 +56,7 @@ use rustc_span::{Span, DUMMY_SP}; use std::cell::{Cell, RefCell}; use std::collections::BTreeSet; -use std::{cmp, fmt, iter, ptr}; +use std::{fmt, iter, ptr}; use tracing::debug; use diagnostics::{extend_span_to_previous_binding, find_span_of_binding_until_next_binding}; @@ -74,6 +74,9 @@ mod diagnostics; mod imports; mod late; mod macros; +mod binding_error; + +use binding_error::BindingError; enum Weak { Yes, @@ -149,32 +152,6 @@ impl<'a> ParentScope<'a> { } } -#[derive(Eq)] -struct BindingError { - name: Symbol, - origin: BTreeSet, - target: BTreeSet, - could_be_path: bool, -} - -impl PartialOrd for BindingError { - fn partial_cmp(&self, other: &BindingError) -> Option { - Some(self.cmp(other)) - } -} - -impl PartialEq for BindingError { - fn eq(&self, other: &BindingError) -> bool { - self.name == other.name - } -} - -impl Ord for BindingError { - fn cmp(&self, other: &BindingError) -> cmp::Ordering { - self.name.cmp(&other.name) - } -} - enum ResolutionError<'a> { /// Error E0401: can't use type or const parameters from outer function. GenericParamsFromOuterFunction(Res, HasGenericParams), From 64bca9dbb62750891928ed36d88333852e459604 Mon Sep 17 00:00:00 2001 From: Alex Griffiths Date: Wed, 19 Aug 2020 22:15:33 +1000 Subject: [PATCH 05/13] Moved Utf8Error doc comments into the Utf8Error module. --- library/core/src/str/mod.rs | 42 ------------------------------------ library/core/src/str/utf8.rs | 42 +++++++++++++++++++++++++++++++++++- 2 files changed, 41 insertions(+), 43 deletions(-) diff --git a/library/core/src/str/mod.rs b/library/core/src/str/mod.rs index 7134f9d9983e7..a99dedfa1cc2b 100644 --- a/library/core/src/str/mod.rs +++ b/library/core/src/str/mod.rs @@ -173,46 +173,6 @@ impl fmt::Display for ParseBoolError { Section: Creating a string */ -/// Errors which can occur when attempting to interpret a sequence of [`u8`] -/// as a string. -/// -/// As such, the `from_utf8` family of functions and methods for both [`String`]s -/// and [`&str`]s make use of this error, for example. -/// -/// [`String`]: ../../std/string/struct.String.html#method.from_utf8 -/// [`&str`]: from_utf8 -/// -/// # Examples -/// -/// This error type’s methods can be used to create functionality -/// similar to `String::from_utf8_lossy` without allocating heap memory: -/// -/// ``` -/// fn from_utf8_lossy(mut input: &[u8], mut push: F) where F: FnMut(&str) { -/// loop { -/// match std::str::from_utf8(input) { -/// Ok(valid) => { -/// push(valid); -/// break -/// } -/// Err(error) => { -/// let (valid, after_valid) = input.split_at(error.valid_up_to()); -/// unsafe { -/// push(std::str::from_utf8_unchecked(valid)) -/// } -/// push("\u{FFFD}"); -/// -/// if let Some(invalid_sequence_length) = error.error_len() { -/// input = &after_valid[invalid_sequence_length..] -/// } else { -/// break -/// } -/// } -/// } -/// } -/// } -/// ``` - /// Converts a slice of bytes to a string slice. /// /// A string slice ([`&str`]) is made of bytes ([`u8`]), and a byte slice @@ -625,8 +585,6 @@ impl<'a> Chars<'a> { } } - - /// An iterator over the bytes of a string slice. /// /// This struct is created by the [`bytes`] method on [`str`]. diff --git a/library/core/src/str/utf8.rs b/library/core/src/str/utf8.rs index 62ec2c0bf4471..3742d254742ef 100644 --- a/library/core/src/str/utf8.rs +++ b/library/core/src/str/utf8.rs @@ -1,3 +1,43 @@ +/// Errors which can occur when attempting to interpret a sequence of [`u8`] +/// as a string. +/// +/// As such, the `from_utf8` family of functions and methods for both [`String`]s +/// and [`&str`]s make use of this error, for example. +/// +/// [`String`]: ../../std/string/struct.String.html#method.from_utf8 +/// [`&str`]: from_utf8 +/// +/// # Examples +/// +/// This error type’s methods can be used to create functionality +/// similar to `String::from_utf8_lossy` without allocating heap memory: +/// +/// ``` +/// fn from_utf8_lossy(mut input: &[u8], mut push: F) where F: FnMut(&str) { +/// loop { +/// match std::str::from_utf8(input) { +/// Ok(valid) => { +/// push(valid); +/// break +/// } +/// Err(error) => { +/// let (valid, after_valid) = input.split_at(error.valid_up_to()); +/// unsafe { +/// push(std::str::from_utf8_unchecked(valid)) +/// } +/// push("\u{FFFD}"); +/// +/// if let Some(invalid_sequence_length) = error.error_len() { +/// input = &after_valid[invalid_sequence_length..] +/// } else { +/// break +/// } +/// } +/// } +/// } +/// } +/// ``` + #[derive(Copy, Eq, PartialEq, Clone, Debug)] #[stable(feature = "rust1", since = "1.0.0")] pub struct Utf8Error { @@ -52,4 +92,4 @@ impl Utf8Error { pub fn error_len(&self) -> Option { self.error_len.map(|len| len as usize) } -} \ No newline at end of file +} From 04d3d9380303f6dfb8766466fd7270aaaabc46cb Mon Sep 17 00:00:00 2001 From: Alex Griffiths Date: Wed, 19 Aug 2020 22:17:35 +1000 Subject: [PATCH 06/13] Added filelength comment to suppress warning because file is still > 3000 lines. x.py fmt changes also included. --- library/alloc/src/vec.rs | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/library/alloc/src/vec.rs b/library/alloc/src/vec.rs index af025da59d6d4..021654dc4b732 100644 --- a/library/alloc/src/vec.rs +++ b/library/alloc/src/vec.rs @@ -1,3 +1,5 @@ +// ignore-tidy-filelength + //! A contiguous growable array type with heap-allocated contents, written //! `Vec`. //! @@ -75,9 +77,9 @@ use crate::boxed::Box; use crate::collections::TryReserveError; use crate::raw_vec::RawVec; -#[stable(feature="drain", since="1.6.0")] +#[stable(feature = "drain", since = "1.6.0")] mod drain; -#[stable(feature="drain", since="1.6.0")] +#[stable(feature = "drain", since = "1.6.0")] pub use drain::Drain; /// A contiguous growable array type, written `Vec` but pronounced 'vector'. From cd2770ec79ec1cd75d8a21069ab70cec09eefc9c Mon Sep 17 00:00:00 2001 From: Alex Griffiths Date: Wed, 19 Aug 2020 22:18:46 +1000 Subject: [PATCH 07/13] Added trailing new lines as well as ran x.py fmt --- library/alloc/src/vec/drain.rs | 5 +++-- library/core/src/str/indices.rs | 2 +- src/librustc_resolve/binding_error.rs | 8 ++++---- src/librustc_resolve/diagnostics.rs | 6 ++---- src/librustc_resolve/late.rs | 2 +- src/librustc_resolve/lib.rs | 2 +- 6 files changed, 12 insertions(+), 13 deletions(-) diff --git a/library/alloc/src/vec/drain.rs b/library/alloc/src/vec/drain.rs index 5deb337c24c36..51144d2e0ea57 100644 --- a/library/alloc/src/vec/drain.rs +++ b/library/alloc/src/vec/drain.rs @@ -1,10 +1,11 @@ -use core::slice; use core::fmt; use core::iter::{FusedIterator, TrustedLen}; use core::mem::{self}; use core::ptr::{self, NonNull}; +use core::slice; use crate::vec::Vec; + /// A draining iterator for `Vec`. /// /// This `struct` is created by [`Vec::drain`]. @@ -131,4 +132,4 @@ impl ExactSizeIterator for Drain<'_, T> { unsafe impl TrustedLen for Drain<'_, T> {} #[stable(feature = "fused", since = "1.26.0")] -impl FusedIterator for Drain<'_, T> {} \ No newline at end of file +impl FusedIterator for Drain<'_, T> {} diff --git a/library/core/src/str/indices.rs b/library/core/src/str/indices.rs index 2f3efccef455c..8916585a2ff4d 100644 --- a/library/core/src/str/indices.rs +++ b/library/core/src/str/indices.rs @@ -73,4 +73,4 @@ impl<'a> CharIndices<'a> { pub fn as_str(&self) -> &'a str { self.iter.as_str() } -} \ No newline at end of file +} diff --git a/src/librustc_resolve/binding_error.rs b/src/librustc_resolve/binding_error.rs index ef8dd30cf7d75..78135002d9969 100644 --- a/src/librustc_resolve/binding_error.rs +++ b/src/librustc_resolve/binding_error.rs @@ -1,7 +1,7 @@ -use std::collections::BTreeSet; -use std::cmp; -use rustc_span::Span; use rustc_span::symbol::Symbol; +use rustc_span::Span; +use std::cmp; +use std::collections::BTreeSet; #[derive(Eq)] pub struct BindingError { @@ -27,4 +27,4 @@ impl Ord for BindingError { fn cmp(&self, other: &BindingError) -> cmp::Ordering { self.name.cmp(&other.name) } -} \ No newline at end of file +} diff --git a/src/librustc_resolve/diagnostics.rs b/src/librustc_resolve/diagnostics.rs index 1de9dbb3c6088..48aad9b3eda94 100644 --- a/src/librustc_resolve/diagnostics.rs +++ b/src/librustc_resolve/diagnostics.rs @@ -19,13 +19,11 @@ use rustc_span::symbol::{kw, sym, Ident, Symbol}; use rustc_span::{BytePos, MultiSpan, Span}; use tracing::debug; +use crate::binding_error::BindingError; use crate::imports::{Import, ImportKind, ImportResolver}; use crate::path_names_to_string; use crate::{AmbiguityError, AmbiguityErrorMisc, AmbiguityKind}; -use crate::{ - CrateLint, HasGenericParams, MacroRulesScope, Module, ModuleOrUniformRoot, -}; -use crate::binding_error::BindingError; +use crate::{CrateLint, HasGenericParams, MacroRulesScope, Module, ModuleOrUniformRoot}; use crate::{NameBinding, NameBindingKind, PrivacyError, VisResolutionError}; use crate::{ParentScope, PathResult, ResolutionError, Resolver, Scope, ScopeSet, Segment}; diff --git a/src/librustc_resolve/late.rs b/src/librustc_resolve/late.rs index 39e47f4e56620..8c7577724e126 100644 --- a/src/librustc_resolve/late.rs +++ b/src/librustc_resolve/late.rs @@ -7,8 +7,8 @@ use RibKind::*; -use crate::{path_names_to_string, CrateLint, LexicalScopeBinding}; use crate::binding_error::BindingError; +use crate::{path_names_to_string, CrateLint, LexicalScopeBinding}; use crate::{Module, ModuleOrUniformRoot, NameBindingKind, ParentScope, PathResult}; use crate::{ResolutionError, Resolver, Segment, UseError}; diff --git a/src/librustc_resolve/lib.rs b/src/librustc_resolve/lib.rs index 3619934c9a6fa..218225e268a96 100644 --- a/src/librustc_resolve/lib.rs +++ b/src/librustc_resolve/lib.rs @@ -67,6 +67,7 @@ use macros::{MacroRulesBinding, MacroRulesScope}; type Res = def::Res; +mod binding_error; mod build_reduced_graph; mod check_unused; mod def_collector; @@ -74,7 +75,6 @@ mod diagnostics; mod imports; mod late; mod macros; -mod binding_error; use binding_error::BindingError; From acc26d5451a76f8218e9c83dbb4f55f9a2941ba1 Mon Sep 17 00:00:00 2001 From: Alex Griffiths Date: Wed, 19 Aug 2020 22:33:29 +1000 Subject: [PATCH 08/13] Remove needless line --- library/core/src/str/utf8.rs | 1 - 1 file changed, 1 deletion(-) diff --git a/library/core/src/str/utf8.rs b/library/core/src/str/utf8.rs index 3742d254742ef..d493f92d4bd33 100644 --- a/library/core/src/str/utf8.rs +++ b/library/core/src/str/utf8.rs @@ -37,7 +37,6 @@ /// } /// } /// ``` - #[derive(Copy, Eq, PartialEq, Clone, Debug)] #[stable(feature = "rust1", since = "1.0.0")] pub struct Utf8Error { From f564cb6a355a764bed6610dd1d5508d288137ef5 Mon Sep 17 00:00:00 2001 From: Alex Griffiths Date: Wed, 19 Aug 2020 22:48:24 +1000 Subject: [PATCH 09/13] Removing changes in librustc_resolve as per @petrochenkov's comment --- src/librustc_resolve/binding_error.rs | 30 -------------------------- src/librustc_resolve/diagnostics.rs | 5 +++-- src/librustc_resolve/late.rs | 3 +-- src/librustc_resolve/lib.rs | 31 +++++++++++++++++++++++---- 4 files changed, 31 insertions(+), 38 deletions(-) delete mode 100644 src/librustc_resolve/binding_error.rs diff --git a/src/librustc_resolve/binding_error.rs b/src/librustc_resolve/binding_error.rs deleted file mode 100644 index 78135002d9969..0000000000000 --- a/src/librustc_resolve/binding_error.rs +++ /dev/null @@ -1,30 +0,0 @@ -use rustc_span::symbol::Symbol; -use rustc_span::Span; -use std::cmp; -use std::collections::BTreeSet; - -#[derive(Eq)] -pub struct BindingError { - pub(super) name: Symbol, - pub(super) origin: BTreeSet, - pub(super) target: BTreeSet, - pub(super) could_be_path: bool, -} - -impl PartialOrd for BindingError { - fn partial_cmp(&self, other: &BindingError) -> Option { - Some(self.cmp(other)) - } -} - -impl PartialEq for BindingError { - fn eq(&self, other: &BindingError) -> bool { - self.name == other.name - } -} - -impl Ord for BindingError { - fn cmp(&self, other: &BindingError) -> cmp::Ordering { - self.name.cmp(&other.name) - } -} diff --git a/src/librustc_resolve/diagnostics.rs b/src/librustc_resolve/diagnostics.rs index 48aad9b3eda94..48e1068b8daad 100644 --- a/src/librustc_resolve/diagnostics.rs +++ b/src/librustc_resolve/diagnostics.rs @@ -19,11 +19,12 @@ use rustc_span::symbol::{kw, sym, Ident, Symbol}; use rustc_span::{BytePos, MultiSpan, Span}; use tracing::debug; -use crate::binding_error::BindingError; use crate::imports::{Import, ImportKind, ImportResolver}; use crate::path_names_to_string; use crate::{AmbiguityError, AmbiguityErrorMisc, AmbiguityKind}; -use crate::{CrateLint, HasGenericParams, MacroRulesScope, Module, ModuleOrUniformRoot}; +use crate::{ + BindingError, CrateLint, HasGenericParams, MacroRulesScope, Module, ModuleOrUniformRoot, +}; use crate::{NameBinding, NameBindingKind, PrivacyError, VisResolutionError}; use crate::{ParentScope, PathResult, ResolutionError, Resolver, Scope, ScopeSet, Segment}; diff --git a/src/librustc_resolve/late.rs b/src/librustc_resolve/late.rs index 8c7577724e126..0dbb6269d2eb3 100644 --- a/src/librustc_resolve/late.rs +++ b/src/librustc_resolve/late.rs @@ -7,8 +7,7 @@ use RibKind::*; -use crate::binding_error::BindingError; -use crate::{path_names_to_string, CrateLint, LexicalScopeBinding}; +use crate::{path_names_to_string, BindingError, CrateLint, LexicalScopeBinding}; use crate::{Module, ModuleOrUniformRoot, NameBindingKind, ParentScope, PathResult}; use crate::{ResolutionError, Resolver, Segment, UseError}; diff --git a/src/librustc_resolve/lib.rs b/src/librustc_resolve/lib.rs index 218225e268a96..36b7a430f78d1 100644 --- a/src/librustc_resolve/lib.rs +++ b/src/librustc_resolve/lib.rs @@ -56,7 +56,7 @@ use rustc_span::{Span, DUMMY_SP}; use std::cell::{Cell, RefCell}; use std::collections::BTreeSet; -use std::{fmt, iter, ptr}; +use std::{cmp, fmt, iter, ptr}; use tracing::debug; use diagnostics::{extend_span_to_previous_binding, find_span_of_binding_until_next_binding}; @@ -67,7 +67,6 @@ use macros::{MacroRulesBinding, MacroRulesScope}; type Res = def::Res; -mod binding_error; mod build_reduced_graph; mod check_unused; mod def_collector; @@ -76,8 +75,6 @@ mod imports; mod late; mod macros; -use binding_error::BindingError; - enum Weak { Yes, No, @@ -152,6 +149,32 @@ impl<'a> ParentScope<'a> { } } +#[derive(Eq)] +struct BindingError { + name: Symbol, + origin: BTreeSet, + target: BTreeSet, + could_be_path: bool, +} + +impl PartialOrd for BindingError { + fn partial_cmp(&self, other: &BindingError) -> Option { + Some(self.cmp(other)) + } +} + +impl PartialEq for BindingError { + fn eq(&self, other: &BindingError) -> bool { + self.name == other.name + } +} + +impl Ord for BindingError { + fn cmp(&self, other: &BindingError) -> cmp::Ordering { + self.name.cmp(&other.name) + } +} + enum ResolutionError<'a> { /// Error E0401: can't use type or const parameters from outer function. GenericParamsFromOuterFunction(Res, HasGenericParams), From f7285b042547f32a86879469ae0745a7174b5afd Mon Sep 17 00:00:00 2001 From: Alex Griffiths Date: Wed, 19 Aug 2020 22:56:41 +1000 Subject: [PATCH 10/13] Added brief module docs for Utf8Errors and CharIndicies. Allowing the removal of 'allow(missing_docs)' flag --- library/core/src/str/indices.rs | 1 + library/core/src/str/mod.rs | 4 ---- library/core/src/str/utf8.rs | 2 ++ 3 files changed, 3 insertions(+), 4 deletions(-) diff --git a/library/core/src/str/indices.rs b/library/core/src/str/indices.rs index 8916585a2ff4d..d43309fe60f8f 100644 --- a/library/core/src/str/indices.rs +++ b/library/core/src/str/indices.rs @@ -1,3 +1,4 @@ +///! A module for working with iterators and string slices. use crate::iter::FusedIterator; use crate::str::Chars; diff --git a/library/core/src/str/mod.rs b/library/core/src/str/mod.rs index a99dedfa1cc2b..94a29ae8accc7 100644 --- a/library/core/src/str/mod.rs +++ b/library/core/src/str/mod.rs @@ -20,17 +20,13 @@ use crate::ops::Try; use crate::option; use crate::slice::{self, SliceIndex, Split as SliceSplit}; -#[allow(missing_docs)] #[stable(feature = "rust1", since = "1.0.0")] pub mod utf8; -#[allow(missing_docs)] #[stable(feature = "rust1", since = "1.0.0")] pub mod indices; -#[stable(feature = "rust1", since = "1.0.0")] pub use indices::CharIndices; -#[stable(feature = "rust1", since = "1.0.0")] pub use utf8::Utf8Error; pub mod pattern; diff --git a/library/core/src/str/utf8.rs b/library/core/src/str/utf8.rs index d493f92d4bd33..a4cfac7233ef1 100644 --- a/library/core/src/str/utf8.rs +++ b/library/core/src/str/utf8.rs @@ -1,3 +1,5 @@ +///! A module for working with Utf8 errors + /// Errors which can occur when attempting to interpret a sequence of [`u8`] /// as a string. /// From 1d3aef4c4c85318130b30d0785e0521e400f56f0 Mon Sep 17 00:00:00 2001 From: Alex Griffiths Date: Wed, 19 Aug 2020 23:29:39 +1000 Subject: [PATCH 11/13] Fixed incorrect module docs --- library/core/src/str/indices.rs | 2 +- library/core/src/str/utf8.rs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/library/core/src/str/indices.rs b/library/core/src/str/indices.rs index d43309fe60f8f..2e32f5b909625 100644 --- a/library/core/src/str/indices.rs +++ b/library/core/src/str/indices.rs @@ -1,4 +1,4 @@ -///! A module for working with iterators and string slices. +//! A module for working with iterators and string slices. use crate::iter::FusedIterator; use crate::str::Chars; diff --git a/library/core/src/str/utf8.rs b/library/core/src/str/utf8.rs index a4cfac7233ef1..8f8278f98e27d 100644 --- a/library/core/src/str/utf8.rs +++ b/library/core/src/str/utf8.rs @@ -1,4 +1,4 @@ -///! A module for working with Utf8 errors +//! A module for working with Utf8 errors /// Errors which can occur when attempting to interpret a sequence of [`u8`] /// as a string. From 7cc391de0525b21404acdcc46cf3a0a791e627db Mon Sep 17 00:00:00 2001 From: Alex Griffiths Date: Wed, 19 Aug 2020 23:30:20 +1000 Subject: [PATCH 12/13] Replaced stable annotation that was accidentally removed. --- library/core/src/str/mod.rs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/library/core/src/str/mod.rs b/library/core/src/str/mod.rs index 94a29ae8accc7..a63f802d486c4 100644 --- a/library/core/src/str/mod.rs +++ b/library/core/src/str/mod.rs @@ -26,7 +26,9 @@ pub mod utf8; #[stable(feature = "rust1", since = "1.0.0")] pub mod indices; +#[stable(feature = "rust1", since = "1.0.0")] pub use indices::CharIndices; +#[stable(feature = "rust1", since = "1.0.0")] pub use utf8::Utf8Error; pub mod pattern; From 981dac3ad5256522dcbc564dac8d2b92675cadbc Mon Sep 17 00:00:00 2001 From: Xadren Date: Fri, 21 Aug 2020 08:51:26 +1000 Subject: [PATCH 13/13] Fix failing from_utf8 intra-doc-link --- library/core/src/str/utf8.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/library/core/src/str/utf8.rs b/library/core/src/str/utf8.rs index 8f8278f98e27d..94f38d76356d4 100644 --- a/library/core/src/str/utf8.rs +++ b/library/core/src/str/utf8.rs @@ -7,7 +7,7 @@ /// and [`&str`]s make use of this error, for example. /// /// [`String`]: ../../std/string/struct.String.html#method.from_utf8 -/// [`&str`]: from_utf8 +/// [`&str`]: crate::str::from_utf8 /// /// # Examples ///