From 7afe6f52e4448d7523877f652e567542f02b974e Mon Sep 17 00:00:00 2001 From: The8472 Date: Wed, 3 Nov 2021 20:47:00 +0100 Subject: [PATCH 1/5] Make RawVec private to alloc RawVec was previously exposed for compiler-internal use (libarena specifically) in 1acbb0a9350560d951359cc359361b87992a6f2b Since it is unstable, doc-hidden and has no associated tracking issue it was never meant for public use. And since it is no longer used outside alloc itself it can be made private again. Also remove some functions that are dead due to lack of internal users. --- library/alloc/src/lib.rs | 3 +- library/alloc/src/raw_vec.rs | 61 +++--------------------------------- 2 files changed, 6 insertions(+), 58 deletions(-) diff --git a/library/alloc/src/lib.rs b/library/alloc/src/lib.rs index 34d2cfc8e2c5b..2614c221042e5 100644 --- a/library/alloc/src/lib.rs +++ b/library/alloc/src/lib.rs @@ -173,6 +173,8 @@ extern crate test; #[macro_use] mod macros; +mod raw_vec; + // Heaps provided for low-level allocation strategies pub mod alloc; @@ -191,7 +193,6 @@ mod boxed { pub mod borrow; pub mod collections; pub mod fmt; -pub mod raw_vec; pub mod rc; pub mod slice; pub mod str; diff --git a/library/alloc/src/raw_vec.rs b/library/alloc/src/raw_vec.rs index 75dbd4678bb47..4ab38c802a159 100644 --- a/library/alloc/src/raw_vec.rs +++ b/library/alloc/src/raw_vec.rs @@ -1,5 +1,4 @@ -#![unstable(feature = "raw_vec_internals", reason = "implementation detail", issue = "none")] -#![doc(hidden)] +#![unstable(feature = "raw_vec_internals", reason = "unstable const warnings", issue = "none")] use core::alloc::LayoutError; use core::cmp; @@ -50,7 +49,7 @@ enum AllocInit { /// `usize::MAX`. This means that you need to be careful when round-tripping this type with a /// `Box<[T]>`, since `capacity()` won't yield the length. #[allow(missing_debug_implementations)] -pub struct RawVec { +pub(crate) struct RawVec { ptr: Unique, cap: usize, alloc: A, @@ -87,7 +86,7 @@ impl RawVec { /// # Aborts /// /// Aborts on OOM. - #[cfg(not(no_global_oom_handling))] + #[cfg(not(any(no_global_oom_handling, test)))] #[must_use] #[inline] pub fn with_capacity(capacity: usize) -> Self { @@ -95,25 +94,12 @@ impl RawVec { } /// Like `with_capacity`, but guarantees the buffer is zeroed. - #[cfg(not(no_global_oom_handling))] + #[cfg(not(any(no_global_oom_handling, test)))] #[must_use] #[inline] pub fn with_capacity_zeroed(capacity: usize) -> Self { Self::with_capacity_zeroed_in(capacity, Global) } - - /// Reconstitutes a `RawVec` from a pointer and capacity. - /// - /// # Safety - /// - /// The `ptr` must be allocated (on the system heap), and with the given `capacity`. - /// The `capacity` cannot exceed `isize::MAX` for sized types. (only a concern on 32-bit - /// systems). ZST vectors may have a capacity up to `usize::MAX`. - /// If the `ptr` and `capacity` come from a `RawVec`, then this is guaranteed. - #[inline] - pub unsafe fn from_raw_parts(ptr: *mut T, capacity: usize) -> Self { - unsafe { Self::from_raw_parts_in(ptr, capacity, Global) } - } } impl RawVec { @@ -154,14 +140,6 @@ impl RawVec { Self::allocate_in(capacity, AllocInit::Zeroed, alloc) } - /// Converts a `Box<[T]>` into a `RawVec`. - pub fn from_box(slice: Box<[T], A>) -> Self { - unsafe { - let (slice, alloc) = Box::into_raw_with_allocator(slice); - RawVec::from_raw_parts_in(slice.as_mut_ptr(), slice.len(), alloc) - } - } - /// Converts the entire buffer into `Box<[MaybeUninit]>` with the specified `len`. /// /// Note that this will correctly reconstitute any `cap` changes @@ -290,37 +268,6 @@ impl RawVec { /// # Aborts /// /// Aborts on OOM. - /// - /// # Examples - /// - /// ``` - /// # #![feature(raw_vec_internals)] - /// # extern crate alloc; - /// # use std::ptr; - /// # use alloc::raw_vec::RawVec; - /// struct MyVec { - /// buf: RawVec, - /// len: usize, - /// } - /// - /// impl MyVec { - /// pub fn push_all(&mut self, elems: &[T]) { - /// self.buf.reserve(self.len, elems.len()); - /// // reserve would have aborted or panicked if the len exceeded - /// // `isize::MAX` so this is safe to do unchecked now. - /// for x in elems { - /// unsafe { - /// ptr::write(self.buf.ptr().add(self.len), x.clone()); - /// } - /// self.len += 1; - /// } - /// } - /// } - /// # fn main() { - /// # let mut vector = MyVec { buf: RawVec::new(), len: 0 }; - /// # vector.push_all(&[1, 3, 5, 7, 9]); - /// # } - /// ``` #[cfg(not(no_global_oom_handling))] #[inline] pub fn reserve(&mut self, len: usize, additional: usize) { From fc9624c6738c57f32e6811ed93b3085415e61ccd Mon Sep 17 00:00:00 2001 From: Jimmy Envall Date: Wed, 10 Nov 2021 18:49:30 +0100 Subject: [PATCH 2/5] Fix trait object error code --- compiler/rustc_typeck/src/check/fn_ctxt/_impl.rs | 2 +- src/test/ui/editions/dyn-trait-sugg-2021.stderr | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/compiler/rustc_typeck/src/check/fn_ctxt/_impl.rs b/compiler/rustc_typeck/src/check/fn_ctxt/_impl.rs index 93b2a59525972..aae59eee99142 100644 --- a/compiler/rustc_typeck/src/check/fn_ctxt/_impl.rs +++ b/compiler/rustc_typeck/src/check/fn_ctxt/_impl.rs @@ -952,7 +952,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { let mut err = rustc_errors::struct_span_err!( self.sess(), self_ty.span, - E0783, + E0782, "{}", msg, ); diff --git a/src/test/ui/editions/dyn-trait-sugg-2021.stderr b/src/test/ui/editions/dyn-trait-sugg-2021.stderr index f6e9fa96a15b5..a7119b073abfa 100644 --- a/src/test/ui/editions/dyn-trait-sugg-2021.stderr +++ b/src/test/ui/editions/dyn-trait-sugg-2021.stderr @@ -1,4 +1,4 @@ -error[E0783]: trait objects without an explicit `dyn` are deprecated +error[E0782]: trait objects without an explicit `dyn` are deprecated --> $DIR/dyn-trait-sugg-2021.rs:10:5 | LL | Foo::hi(123); @@ -6,4 +6,4 @@ LL | Foo::hi(123); error: aborting due to previous error -For more information about this error, try `rustc --explain E0783`. +For more information about this error, try `rustc --explain E0782`. From 9f6ca7482ca885bca14ba6870ed39a783ab939b5 Mon Sep 17 00:00:00 2001 From: Marcel Hellwig Date: Wed, 10 Nov 2021 12:00:46 +0100 Subject: [PATCH 3/5] Shorten Span of unused macro lints The span has been recuded to the actual ident, instead of linting the *whole* macro. --- .../rustc_resolve/src/build_reduced_graph.rs | 14 +++----- compiler/rustc_resolve/src/lib.rs | 2 +- compiler/rustc_resolve/src/macros.rs | 9 +++-- src/test/ui/lint/unused/issue-70041.stderr | 11 +++--- .../ui/lint/unused/unused-macro-rules.stderr | 35 +++++++------------ src/test/ui/lint/unused/unused-macro.stderr | 30 +++++++--------- src/test/ui/proc-macro/issue-39889.rs | 2 +- 7 files changed, 41 insertions(+), 62 deletions(-) diff --git a/compiler/rustc_resolve/src/build_reduced_graph.rs b/compiler/rustc_resolve/src/build_reduced_graph.rs index 4173e0fbf5668..3cf9d324a38da 100644 --- a/compiler/rustc_resolve/src/build_reduced_graph.rs +++ b/compiler/rustc_resolve/src/build_reduced_graph.rs @@ -1194,15 +1194,9 @@ impl<'a, 'b> BuildReducedGraphVisitor<'a, 'b> { // Mark the given macro as unused unless its name starts with `_`. // Macro uses will remove items from this set, and the remaining // items will be reported as `unused_macros`. - fn insert_unused_macro( - &mut self, - ident: Ident, - def_id: LocalDefId, - node_id: NodeId, - span: Span, - ) { + fn insert_unused_macro(&mut self, ident: Ident, def_id: LocalDefId, node_id: NodeId) { if !ident.as_str().starts_with('_') { - self.r.unused_macros.insert(def_id, (node_id, span)); + self.r.unused_macros.insert(def_id, (node_id, ident)); } } @@ -1246,7 +1240,7 @@ impl<'a, 'b> BuildReducedGraphVisitor<'a, 'b> { self.r.define(module, ident, MacroNS, (res, vis, span, expansion, IsMacroExport)); } else { self.r.check_reserved_macro_name(ident, res); - self.insert_unused_macro(ident, def_id, item.id, span); + self.insert_unused_macro(ident, def_id, item.id); } self.r.visibilities.insert(def_id, vis); self.r.arenas.alloc_macro_rules_scope(MacroRulesScope::Binding( @@ -1267,7 +1261,7 @@ impl<'a, 'b> BuildReducedGraphVisitor<'a, 'b> { _ => self.resolve_visibility(&item.vis), }; if vis != ty::Visibility::Public { - self.insert_unused_macro(ident, def_id, item.id, span); + self.insert_unused_macro(ident, def_id, item.id); } self.r.define(module, ident, MacroNS, (res, vis, span, expansion)); self.r.visibilities.insert(def_id, vis); diff --git a/compiler/rustc_resolve/src/lib.rs b/compiler/rustc_resolve/src/lib.rs index 5f3620b247e26..f5bea83bdcf65 100644 --- a/compiler/rustc_resolve/src/lib.rs +++ b/compiler/rustc_resolve/src/lib.rs @@ -988,7 +988,7 @@ pub struct Resolver<'a> { non_macro_attr: Lrc, local_macro_def_scopes: FxHashMap>, ast_transform_scopes: FxHashMap>, - unused_macros: FxHashMap, + unused_macros: FxHashMap, proc_macro_stubs: FxHashSet, /// Traces collected during macro resolution and validated when it's complete. single_segment_macro_resolutions: diff --git a/compiler/rustc_resolve/src/macros.rs b/compiler/rustc_resolve/src/macros.rs index 4f6e23d8f84f0..31fd9b989e1c8 100644 --- a/compiler/rustc_resolve/src/macros.rs +++ b/compiler/rustc_resolve/src/macros.rs @@ -315,8 +315,13 @@ impl<'a> ResolverExpand for Resolver<'a> { } fn check_unused_macros(&mut self) { - for (_, &(node_id, span)) in self.unused_macros.iter() { - self.lint_buffer.buffer_lint(UNUSED_MACROS, node_id, span, "unused macro definition"); + for (_, &(node_id, ident)) in self.unused_macros.iter() { + self.lint_buffer.buffer_lint( + UNUSED_MACROS, + node_id, + ident.span, + &format!("unused macro definition: `{}`", ident.as_str()), + ); } } diff --git a/src/test/ui/lint/unused/issue-70041.stderr b/src/test/ui/lint/unused/issue-70041.stderr index ecd618eae8b07..b2e6d1aeb3f50 100644 --- a/src/test/ui/lint/unused/issue-70041.stderr +++ b/src/test/ui/lint/unused/issue-70041.stderr @@ -1,11 +1,8 @@ -warning: unused macro definition - --> $DIR/issue-70041.rs:4:1 +warning: unused macro definition: `regex` + --> $DIR/issue-70041.rs:4:14 | -LL | / macro_rules! regex { -LL | | -LL | | () => {}; -LL | | } - | |_^ +LL | macro_rules! regex { + | ^^^^^ | = note: `#[warn(unused_macros)]` on by default diff --git a/src/test/ui/lint/unused/unused-macro-rules.stderr b/src/test/ui/lint/unused/unused-macro-rules.stderr index 6812a1d8f631a..59db35b411183 100644 --- a/src/test/ui/lint/unused/unused-macro-rules.stderr +++ b/src/test/ui/lint/unused/unused-macro-rules.stderr @@ -1,10 +1,8 @@ -error: unused macro definition - --> $DIR/unused-macro-rules.rs:4:1 +error: unused macro definition: `unused` + --> $DIR/unused-macro-rules.rs:4:14 | -LL | / macro_rules! unused { -LL | | () => {}; -LL | | } - | |_^ +LL | macro_rules! unused { + | ^^^^^^ | note: the lint level is defined here --> $DIR/unused-macro-rules.rs:1:9 @@ -12,26 +10,17 @@ note: the lint level is defined here LL | #![deny(unused_macros)] | ^^^^^^^^^^^^^ -error: unused macro definition - --> $DIR/unused-macro-rules.rs:11:9 +error: unused macro definition: `m` + --> $DIR/unused-macro-rules.rs:11:22 | -LL | / macro_rules! m { -LL | | () => {}; -LL | | } - | |_________^ -... -LL | create_macro!(); - | --------------- in this macro invocation - | - = note: this error originates in the macro `create_macro` (in Nightly builds, run with -Z macro-backtrace for more info) +LL | macro_rules! m { + | ^ -error: unused macro definition - --> $DIR/unused-macro-rules.rs:24:5 +error: unused macro definition: `unused` + --> $DIR/unused-macro-rules.rs:24:18 | -LL | / macro_rules! unused { -LL | | () => {}; -LL | | } - | |_____^ +LL | macro_rules! unused { + | ^^^^^^ | note: the lint level is defined here --> $DIR/unused-macro-rules.rs:23:12 diff --git a/src/test/ui/lint/unused/unused-macro.stderr b/src/test/ui/lint/unused/unused-macro.stderr index f5eb76179bf4b..1a73279ed6dbd 100644 --- a/src/test/ui/lint/unused/unused-macro.stderr +++ b/src/test/ui/lint/unused/unused-macro.stderr @@ -1,10 +1,8 @@ -error: unused macro definition - --> $DIR/unused-macro.rs:5:1 +error: unused macro definition: `unused` + --> $DIR/unused-macro.rs:5:7 | -LL | / macro unused { -LL | | () => {} -LL | | } - | |_^ +LL | macro unused { + | ^^^^^^ | note: the lint level is defined here --> $DIR/unused-macro.rs:2:9 @@ -12,13 +10,11 @@ note: the lint level is defined here LL | #![deny(unused_macros)] | ^^^^^^^^^^^^^ -error: unused macro definition - --> $DIR/unused-macro.rs:15:5 +error: unused macro definition: `unused` + --> $DIR/unused-macro.rs:15:11 | -LL | / macro unused { -LL | | () => {} -LL | | } - | |_____^ +LL | macro unused { + | ^^^^^^ | note: the lint level is defined here --> $DIR/unused-macro.rs:14:12 @@ -26,13 +22,11 @@ note: the lint level is defined here LL | #[deny(unused_macros)] | ^^^^^^^^^^^^^ -error: unused macro definition - --> $DIR/unused-macro.rs:21:5 +error: unused macro definition: `unused` + --> $DIR/unused-macro.rs:21:22 | -LL | / pub(crate) macro unused { -LL | | () => {} -LL | | } - | |_____^ +LL | pub(crate) macro unused { + | ^^^^^^ error: aborting due to 3 previous errors diff --git a/src/test/ui/proc-macro/issue-39889.rs b/src/test/ui/proc-macro/issue-39889.rs index ada125a215a5d..69bfb4f3cbfbc 100644 --- a/src/test/ui/proc-macro/issue-39889.rs +++ b/src/test/ui/proc-macro/issue-39889.rs @@ -1,6 +1,6 @@ // run-pass -#![allow(dead_code)] +#![allow(dead_code, unused_macros)] // aux-build:issue-39889.rs extern crate issue_39889; From b0fd642de61f23e0913c8476840e5921d7802309 Mon Sep 17 00:00:00 2001 From: Michael Howell Date: Thu, 11 Nov 2021 13:56:32 -0700 Subject: [PATCH 4/5] Use `Vec::extend`, instead of calling `Vec::push` in a loop --- compiler/rustc_mir_build/src/thir/pattern/usefulness.rs | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/compiler/rustc_mir_build/src/thir/pattern/usefulness.rs b/compiler/rustc_mir_build/src/thir/pattern/usefulness.rs index d959d2f7f6f3f..b253108f3dc1e 100644 --- a/compiler/rustc_mir_build/src/thir/pattern/usefulness.rs +++ b/compiler/rustc_mir_build/src/thir/pattern/usefulness.rs @@ -443,9 +443,7 @@ impl<'p, 'tcx> Matrix<'p, 'tcx> { /// expands it. fn push(&mut self, row: PatStack<'p, 'tcx>) { if !row.is_empty() && row.head().is_or_pat() { - for row in row.expand_or_pat() { - self.patterns.push(row); - } + self.patterns.extend(row.expand_or_pat()); } else { self.patterns.push(row); } From a82692d6134b228c9d09396ce5e7e58d680e6d4f Mon Sep 17 00:00:00 2001 From: Michael Howell Date: Thu, 11 Nov 2021 13:57:00 -0700 Subject: [PATCH 5/5] Use `Iterator::collect` instead of calling `Vec::push` in a loop --- src/librustdoc/html/render/cache.rs | 49 +++++++++++++++-------------- 1 file changed, 26 insertions(+), 23 deletions(-) diff --git a/src/librustdoc/html/render/cache.rs b/src/librustdoc/html/render/cache.rs index 79421c128bcf8..896c254360865 100644 --- a/src/librustdoc/html/render/cache.rs +++ b/src/librustdoc/html/render/cache.rs @@ -26,7 +26,6 @@ crate enum ExternalLocation { /// Builds the search index from the collected metadata crate fn build_index<'tcx>(krate: &clean::Crate, cache: &mut Cache, tcx: TyCtxt<'tcx>) -> String { let mut defid_to_pathid = FxHashMap::default(); - let mut crate_items = Vec::with_capacity(cache.search_index.len()); let mut crate_paths = vec![]; // Attach all orphan items to the type's definition if the type @@ -77,34 +76,38 @@ crate fn build_index<'tcx>(krate: &clean::Crate, cache: &mut Cache, tcx: TyCtxt< // Reduce `DefId` in paths into smaller sequential numbers, // and prune the paths that do not appear in the index. - let mut lastpath = String::new(); + let mut lastpath = ""; let mut lastpathid = 0usize; - for item in search_index { - item.parent_idx = item.parent.and_then(|defid| match defid_to_pathid.entry(defid) { - Entry::Occupied(entry) => Some(*entry.get()), - Entry::Vacant(entry) => { - let pathid = lastpathid; - entry.insert(pathid); - lastpathid += 1; + let crate_items: Vec<&IndexItem> = search_index + .iter_mut() + .map(|item| { + item.parent_idx = item.parent.and_then(|defid| match defid_to_pathid.entry(defid) { + Entry::Occupied(entry) => Some(*entry.get()), + Entry::Vacant(entry) => { + let pathid = lastpathid; + entry.insert(pathid); + lastpathid += 1; - if let Some(&(ref fqp, short)) = paths.get(&defid) { - crate_paths.push((short, fqp.last().unwrap().clone())); - Some(pathid) - } else { - None + if let Some(&(ref fqp, short)) = paths.get(&defid) { + crate_paths.push((short, fqp.last().unwrap().clone())); + Some(pathid) + } else { + None + } } + }); + + // Omit the parent path if it is same to that of the prior item. + if lastpath == &item.path { + item.path.clear(); + } else { + lastpath = &item.path; } - }); - // Omit the parent path if it is same to that of the prior item. - if lastpath == item.path { - item.path.clear(); - } else { - lastpath = item.path.clone(); - } - crate_items.push(&*item); - } + &*item + }) + .collect(); struct CrateData<'a> { doc: String,