From db9fea508a6db55075d7851211ea5d4b0485e322 Mon Sep 17 00:00:00 2001 From: Camille GILLOT Date: Sun, 12 Sep 2021 01:11:22 +0200 Subject: [PATCH 1/5] Avoid more invocations of hir_crate query. --- compiler/rustc_hir/src/hir.rs | 51 +---------------- compiler/rustc_hir_pretty/src/lib.rs | 13 ----- .../rustc_incremental/src/assert_dep_graph.rs | 2 +- .../src/persist/dirty_clean.rs | 3 +- .../rustc_interface/src/proc_macro_decls.rs | 2 +- compiler/rustc_lint/src/builtin.rs | 10 +++- .../rustc_metadata/src/foreign_modules.rs | 2 +- compiler/rustc_metadata/src/native_libs.rs | 2 +- compiler/rustc_metadata/src/rmeta/encoder.rs | 7 +-- compiler/rustc_middle/src/hir/map/mod.rs | 55 +++++++++++++++++++ compiler/rustc_middle/src/ty/print/pretty.rs | 2 +- compiler/rustc_mir_transform/src/lib.rs | 4 +- compiler/rustc_monomorphize/src/collector.rs | 2 +- compiler/rustc_passes/src/dead.rs | 19 +++---- compiler/rustc_passes/src/diagnostic_items.rs | 2 +- compiler/rustc_passes/src/entry.rs | 6 +- compiler/rustc_passes/src/lang_items.rs | 2 +- compiler/rustc_passes/src/layout_test.rs | 2 +- compiler/rustc_passes/src/reachable.rs | 2 +- compiler/rustc_passes/src/stability.rs | 3 +- compiler/rustc_passes/src/weak_lang_items.rs | 2 +- compiler/rustc_privacy/src/lib.rs | 4 +- .../rustc_save_analysis/src/dump_visitor.rs | 10 ++-- compiler/rustc_save_analysis/src/lib.rs | 4 +- compiler/rustc_symbol_mangling/src/test.rs | 2 +- compiler/rustc_typeck/src/check/check.rs | 2 +- .../rustc_typeck/src/check/method/suggest.rs | 2 +- compiler/rustc_typeck/src/check_unused.rs | 4 +- .../src/coherence/inherent_impls.rs | 3 +- .../src/coherence/inherent_impls_overlap.rs | 3 +- compiler/rustc_typeck/src/coherence/orphan.rs | 2 +- .../rustc_typeck/src/coherence/unsafety.rs | 2 +- .../src/outlives/implicit_infer.rs | 2 +- compiler/rustc_typeck/src/outlives/test.rs | 2 +- .../rustc_typeck/src/variance/constraints.rs | 2 +- compiler/rustc_typeck/src/variance/terms.rs | 2 +- compiler/rustc_typeck/src/variance/test.rs | 2 +- src/librustdoc/clean/types.rs | 6 +- src/librustdoc/clean/utils.rs | 3 +- src/librustdoc/visit_ast.rs | 6 +- .../clippy/clippy_lints/src/missing_doc.rs | 8 +-- 41 files changed, 123 insertions(+), 141 deletions(-) diff --git a/compiler/rustc_hir/src/hir.rs b/compiler/rustc_hir/src/hir.rs index 05b652fd5af..21089b0208d 100644 --- a/compiler/rustc_hir/src/hir.rs +++ b/compiler/rustc_hir/src/hir.rs @@ -1,7 +1,7 @@ use crate::def::{CtorKind, DefKind, Res}; use crate::def_id::{DefId, CRATE_DEF_ID}; crate use crate::hir_id::{HirId, ItemLocalId}; -use crate::{itemlikevisit, LangItem}; +use crate::LangItem; use rustc_ast::util::parser::ExprPrecedence; use rustc_ast::{self as ast, CrateSugar, LlvmAsmDialect}; @@ -10,7 +10,6 @@ pub use rustc_ast::{BorrowKind, ImplPolarity, IsAuto}; pub use rustc_ast::{CaptureBy, Movability, Mutability}; use rustc_ast::{InlineAsmOptions, InlineAsmTemplatePiece}; use rustc_data_structures::fx::FxHashMap; -use rustc_data_structures::sync::{par_for_each_in, Send, Sync}; use rustc_index::vec::IndexVec; use rustc_macros::HashStable_Generic; use rustc_span::source_map::Spanned; @@ -672,7 +671,7 @@ pub struct Crate<'hir> { pub attrs: BTreeMap, } -impl Crate<'hir> { +impl<'hir> Crate<'hir> { pub fn module(&self) -> &'hir Mod<'hir> { if let Some(OwnerNode::Crate(m)) = self.owners[CRATE_DEF_ID] { m } else { panic!() } } @@ -698,52 +697,6 @@ impl Crate<'hir> { } } -impl Crate<'_> { - /// Visits all items in the crate in some deterministic (but - /// unspecified) order. If you just need to process every item, - /// but don't care about nesting, this method is the best choice. - /// - /// If you do care about nesting -- usually because your algorithm - /// follows lexical scoping rules -- then you want a different - /// approach. You should override `visit_nested_item` in your - /// visitor and then call `intravisit::walk_crate` instead. - pub fn visit_all_item_likes<'hir, V>(&'hir self, visitor: &mut V) - where - V: itemlikevisit::ItemLikeVisitor<'hir>, - { - for owner in self.owners.iter().filter_map(Option::as_ref) { - match owner { - OwnerNode::Item(item) => visitor.visit_item(item), - OwnerNode::ForeignItem(item) => visitor.visit_foreign_item(item), - OwnerNode::ImplItem(item) => visitor.visit_impl_item(item), - OwnerNode::TraitItem(item) => visitor.visit_trait_item(item), - OwnerNode::Crate(_) => {} - } - } - } - - /// A parallel version of `visit_all_item_likes`. - pub fn par_visit_all_item_likes<'hir, V>(&'hir self, visitor: &V) - where - V: itemlikevisit::ParItemLikeVisitor<'hir> + Sync + Send, - { - par_for_each_in(&self.owners.raw, |owner| match owner { - Some(OwnerNode::Item(item)) => visitor.visit_item(item), - Some(OwnerNode::ForeignItem(item)) => visitor.visit_foreign_item(item), - Some(OwnerNode::ImplItem(item)) => visitor.visit_impl_item(item), - Some(OwnerNode::TraitItem(item)) => visitor.visit_trait_item(item), - Some(OwnerNode::Crate(_)) | None => {} - }) - } - - pub fn items<'hir>(&'hir self) -> impl Iterator> + 'hir { - self.owners.iter().filter_map(|owner| match owner { - Some(OwnerNode::Item(item)) => Some(*item), - _ => None, - }) - } -} - /// A block of statements `{ .. }`, which may have a label (in this case the /// `targeted_by_break` field will be `true`) and may be `unsafe` by means of /// the `rules` being anything but `DefaultBlock`. diff --git a/compiler/rustc_hir_pretty/src/lib.rs b/compiler/rustc_hir_pretty/src/lib.rs index 67f92bc0a51..9196344cb3f 100644 --- a/compiler/rustc_hir_pretty/src/lib.rs +++ b/compiler/rustc_hir_pretty/src/lib.rs @@ -51,19 +51,6 @@ pub struct NoAnn; impl PpAnn for NoAnn {} pub const NO_ANN: &dyn PpAnn = &NoAnn; -impl PpAnn for hir::Crate<'_> { - fn nested(&self, state: &mut State<'_>, nested: Nested) { - match nested { - Nested::Item(id) => state.print_item(self.item(id)), - Nested::TraitItem(id) => state.print_trait_item(self.trait_item(id)), - Nested::ImplItem(id) => state.print_impl_item(self.impl_item(id)), - Nested::ForeignItem(id) => state.print_foreign_item(self.foreign_item(id)), - Nested::Body(id) => state.print_expr(&self.body(id).value), - Nested::BodyParamPat(id, i) => state.print_pat(&self.body(id).params[i].pat), - } - } -} - /// Identical to the `PpAnn` implementation for `hir::Crate`, /// except it avoids creating a dependency on the whole crate. impl PpAnn for &dyn rustc_hir::intravisit::Map<'_> { diff --git a/compiler/rustc_incremental/src/assert_dep_graph.rs b/compiler/rustc_incremental/src/assert_dep_graph.rs index 0a558eb0555..d42e2f7a99c 100644 --- a/compiler/rustc_incremental/src/assert_dep_graph.rs +++ b/compiler/rustc_incremental/src/assert_dep_graph.rs @@ -74,7 +74,7 @@ pub fn assert_dep_graph(tcx: TyCtxt<'_>) { let mut visitor = IfThisChanged { tcx, if_this_changed: vec![], then_this_would_need: vec![] }; visitor.process_attrs(hir::CRATE_HIR_ID); - tcx.hir().krate().visit_all_item_likes(&mut visitor.as_deep_visitor()); + tcx.hir().visit_all_item_likes(&mut visitor.as_deep_visitor()); (visitor.if_this_changed, visitor.then_this_would_need) }; diff --git a/compiler/rustc_incremental/src/persist/dirty_clean.rs b/compiler/rustc_incremental/src/persist/dirty_clean.rs index c190391d211..55286384de3 100644 --- a/compiler/rustc_incremental/src/persist/dirty_clean.rs +++ b/compiler/rustc_incremental/src/persist/dirty_clean.rs @@ -137,9 +137,8 @@ pub fn check_dirty_clean_annotations(tcx: TyCtxt<'_>) { } tcx.dep_graph.with_ignore(|| { - let krate = tcx.hir().krate(); let mut dirty_clean_visitor = DirtyCleanVisitor { tcx, checked_attrs: Default::default() }; - krate.visit_all_item_likes(&mut dirty_clean_visitor); + tcx.hir().visit_all_item_likes(&mut dirty_clean_visitor); let mut all_attrs = FindAllAttrs { tcx, found_attrs: vec![] }; tcx.hir().walk_attributes(&mut all_attrs); diff --git a/compiler/rustc_interface/src/proc_macro_decls.rs b/compiler/rustc_interface/src/proc_macro_decls.rs index 88cf6275ebb..c0316ce58d2 100644 --- a/compiler/rustc_interface/src/proc_macro_decls.rs +++ b/compiler/rustc_interface/src/proc_macro_decls.rs @@ -7,7 +7,7 @@ use rustc_span::symbol::sym; fn proc_macro_decls_static(tcx: TyCtxt<'_>, (): ()) -> Option { let mut finder = Finder { tcx, decls: None }; - tcx.hir().krate().visit_all_item_likes(&mut finder); + tcx.hir().visit_all_item_likes(&mut finder); finder.decls.map(|id| tcx.hir().local_def_id(id)) } diff --git a/compiler/rustc_lint/src/builtin.rs b/compiler/rustc_lint/src/builtin.rs index e1bcc3aa52b..a24f346fb94 100644 --- a/compiler/rustc_lint/src/builtin.rs +++ b/compiler/rustc_lint/src/builtin.rs @@ -584,8 +584,14 @@ impl<'tcx> LateLintPass<'tcx> for MissingDoc { self.doc_hidden_stack.pop().expect("empty doc_hidden_stack"); } - fn check_crate(&mut self, cx: &LateContext<'_>, krate: &hir::Crate<'_>) { - self.check_missing_docs_attrs(cx, CRATE_DEF_ID, krate.module().inner, "the", "crate"); + fn check_crate(&mut self, cx: &LateContext<'_>, _: &hir::Crate<'_>) { + self.check_missing_docs_attrs( + cx, + CRATE_DEF_ID, + cx.tcx.def_span(CRATE_DEF_ID), + "the", + "crate", + ); } fn check_item(&mut self, cx: &LateContext<'_>, it: &hir::Item<'_>) { diff --git a/compiler/rustc_metadata/src/foreign_modules.rs b/compiler/rustc_metadata/src/foreign_modules.rs index 3d3071c18f2..9eac4c9f69b 100644 --- a/compiler/rustc_metadata/src/foreign_modules.rs +++ b/compiler/rustc_metadata/src/foreign_modules.rs @@ -5,7 +5,7 @@ use rustc_middle::ty::TyCtxt; crate fn collect(tcx: TyCtxt<'_>) -> Vec { let mut collector = Collector { modules: Vec::new() }; - tcx.hir().krate().visit_all_item_likes(&mut collector); + tcx.hir().visit_all_item_likes(&mut collector); collector.modules } diff --git a/compiler/rustc_metadata/src/native_libs.rs b/compiler/rustc_metadata/src/native_libs.rs index 5ad55dbf5c8..39709e1bd07 100644 --- a/compiler/rustc_metadata/src/native_libs.rs +++ b/compiler/rustc_metadata/src/native_libs.rs @@ -14,7 +14,7 @@ use rustc_target::spec::abi::Abi; crate fn collect(tcx: TyCtxt<'_>) -> Vec { let mut collector = Collector { tcx, libs: Vec::new() }; - tcx.hir().krate().visit_all_item_likes(&mut collector); + tcx.hir().visit_all_item_likes(&mut collector); collector.process_command_line(); collector.libs } diff --git a/compiler/rustc_metadata/src/rmeta/encoder.rs b/compiler/rustc_metadata/src/rmeta/encoder.rs index 8509aa00bc0..c0f3228767e 100644 --- a/compiler/rustc_metadata/src/rmeta/encoder.rs +++ b/compiler/rustc_metadata/src/rmeta/encoder.rs @@ -440,8 +440,7 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> { } fn encode_info_for_items(&mut self) { - let krate = self.tcx.hir().krate(); - self.encode_info_for_mod(CRATE_DEF_ID, krate.module()); + self.encode_info_for_mod(CRATE_DEF_ID, self.tcx.hir().root_module()); // Proc-macro crates only export proc-macro items, which are looked // up using `proc_macro_data` @@ -449,7 +448,7 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> { return; } - krate.visit_all_item_likes(&mut self.as_deep_visitor()); + self.tcx.hir().visit_all_item_likes(&mut self.as_deep_visitor()); } fn encode_def_path_table(&mut self) { @@ -1782,7 +1781,7 @@ impl EncodeContext<'a, 'tcx> { debug!("EncodeContext::encode_impls()"); let tcx = self.tcx; let mut visitor = ImplVisitor { tcx, impls: FxHashMap::default() }; - tcx.hir().krate().visit_all_item_likes(&mut visitor); + tcx.hir().visit_all_item_likes(&mut visitor); let mut all_impls: Vec<_> = visitor.impls.into_iter().collect(); diff --git a/compiler/rustc_middle/src/hir/map/mod.rs b/compiler/rustc_middle/src/hir/map/mod.rs index 3707fadadac..dfa738652a5 100644 --- a/compiler/rustc_middle/src/hir/map/mod.rs +++ b/compiler/rustc_middle/src/hir/map/mod.rs @@ -6,6 +6,7 @@ use rustc_ast as ast; use rustc_data_structures::fingerprint::Fingerprint; use rustc_data_structures::stable_hasher::{HashStable, StableHasher}; use rustc_data_structures::svh::Svh; +use rustc_data_structures::sync::{par_for_each_in, Send, Sync}; use rustc_hir::def::{DefKind, Res}; use rustc_hir::def_id::{CrateNum, DefId, LocalDefId, CRATE_DEF_ID, LOCAL_CRATE}; use rustc_hir::definitions::{DefKey, DefPath, DefPathHash}; @@ -156,6 +157,21 @@ impl<'hir> Map<'hir> { self.tcx.hir_crate(()) } + pub fn root_module(&self) -> &'hir Mod<'hir> { + match self.tcx.hir_owner(CRATE_DEF_ID).map(|o| o.node) { + Some(OwnerNode::Crate(item)) => item, + _ => panic!(), + } + } + + crate fn items(&self) -> impl Iterator> + 'hir { + let krate = self.krate(); + krate.owners.iter().filter_map(|owner| match owner.as_ref()? { + OwnerNode::Item(item) => Some(*item), + _ => None, + }) + } + pub fn def_key(&self, def_id: LocalDefId) -> DefKey { // Accessing the DefKey is ok, since it is part of DefPathHash. self.tcx.untracked_resolutions.definitions.def_key(def_id) @@ -531,6 +547,45 @@ impl<'hir> Map<'hir> { } } + /// Visits all items in the crate in some deterministic (but + /// unspecified) order. If you just need to process every item, + /// but don't care about nesting, this method is the best choice. + /// + /// If you do care about nesting -- usually because your algorithm + /// follows lexical scoping rules -- then you want a different + /// approach. You should override `visit_nested_item` in your + /// visitor and then call `intravisit::walk_crate` instead. + pub fn visit_all_item_likes(&self, visitor: &mut V) + where + V: itemlikevisit::ItemLikeVisitor<'hir>, + { + let krate = self.krate(); + for owner in krate.owners.iter().filter_map(Option::as_ref) { + match owner { + OwnerNode::Item(item) => visitor.visit_item(item), + OwnerNode::ForeignItem(item) => visitor.visit_foreign_item(item), + OwnerNode::ImplItem(item) => visitor.visit_impl_item(item), + OwnerNode::TraitItem(item) => visitor.visit_trait_item(item), + OwnerNode::Crate(_) => {} + } + } + } + + /// A parallel version of `visit_all_item_likes`. + pub fn par_visit_all_item_likes(&self, visitor: &V) + where + V: itemlikevisit::ParItemLikeVisitor<'hir> + Sync + Send, + { + let krate = self.krate(); + par_for_each_in(&krate.owners.raw, |owner| match owner.as_ref() { + Some(OwnerNode::Item(item)) => visitor.visit_item(item), + Some(OwnerNode::ForeignItem(item)) => visitor.visit_foreign_item(item), + Some(OwnerNode::ImplItem(item)) => visitor.visit_impl_item(item), + Some(OwnerNode::TraitItem(item)) => visitor.visit_trait_item(item), + Some(OwnerNode::Crate(_)) | None => {} + }) + } + pub fn visit_item_likes_in_module(&self, module: LocalDefId, visitor: &mut V) where V: ItemLikeVisitor<'hir>, diff --git a/compiler/rustc_middle/src/ty/print/pretty.rs b/compiler/rustc_middle/src/ty/print/pretty.rs index b8bca6363db..a2211f4c3b2 100644 --- a/compiler/rustc_middle/src/ty/print/pretty.rs +++ b/compiler/rustc_middle/src/ty/print/pretty.rs @@ -2340,7 +2340,7 @@ define_print_and_forward_display! { fn for_each_def(tcx: TyCtxt<'_>, mut collect_fn: impl for<'b> FnMut(&'b Ident, Namespace, DefId)) { // Iterate all local crate items no matter where they are defined. let hir = tcx.hir(); - for item in hir.krate().items() { + for item in hir.items() { if item.ident.name.as_str().is_empty() || matches!(item.kind, ItemKind::Use(_, _)) { continue; } diff --git a/compiler/rustc_mir_transform/src/lib.rs b/compiler/rustc_mir_transform/src/lib.rs index bfd0de85438..fadb7014570 100644 --- a/compiler/rustc_mir_transform/src/lib.rs +++ b/compiler/rustc_mir_transform/src/lib.rs @@ -160,9 +160,7 @@ fn mir_keys(tcx: TyCtxt<'_>, (): ()) -> FxHashSet { NestedVisitorMap::None } } - tcx.hir() - .krate() - .visit_all_item_likes(&mut GatherCtors { tcx, set: &mut set }.as_deep_visitor()); + tcx.hir().visit_all_item_likes(&mut GatherCtors { tcx, set: &mut set }.as_deep_visitor()); set } diff --git a/compiler/rustc_monomorphize/src/collector.rs b/compiler/rustc_monomorphize/src/collector.rs index 1e39b1bd5e8..f06426308a2 100644 --- a/compiler/rustc_monomorphize/src/collector.rs +++ b/compiler/rustc_monomorphize/src/collector.rs @@ -330,7 +330,7 @@ fn collect_roots(tcx: TyCtxt<'_>, mode: MonoItemCollectionMode) -> Vec, id: hir::HirId) -> bool { // or // 2) We are not sure to be live or not // * Implementations of traits and trait methods -struct LifeSeeder<'k, 'tcx> { +struct LifeSeeder<'tcx> { worklist: Vec, - krate: &'k hir::Crate<'k>, tcx: TyCtxt<'tcx>, // see `MarkSymbolVisitor::struct_constructors` struct_constructors: FxHashMap, } -impl<'v, 'k, 'tcx> ItemLikeVisitor<'v> for LifeSeeder<'k, 'tcx> { +impl<'v, 'tcx> ItemLikeVisitor<'v> for LifeSeeder<'tcx> { fn visit_item(&mut self, item: &hir::Item<'_>) { let allow_dead_code = has_allow_dead_code_or_lang_attr(self.tcx, item.hir_id()); if allow_dead_code { @@ -545,7 +544,7 @@ impl<'v, 'k, 'tcx> ItemLikeVisitor<'v> for LifeSeeder<'k, 'tcx> { self.worklist.push(item.def_id); } for impl_item_ref in items { - let impl_item = self.krate.impl_item(impl_item_ref.id); + let impl_item = self.tcx.hir().impl_item(impl_item_ref.id); if of_trait.is_some() || has_allow_dead_code_or_lang_attr(self.tcx, impl_item.hir_id()) { @@ -589,7 +588,6 @@ impl<'v, 'k, 'tcx> ItemLikeVisitor<'v> for LifeSeeder<'k, 'tcx> { fn create_and_seed_worklist<'tcx>( tcx: TyCtxt<'tcx>, access_levels: &privacy::AccessLevels, - krate: &hir::Crate<'_>, ) -> (Vec, FxHashMap) { let worklist = access_levels .map @@ -604,9 +602,8 @@ fn create_and_seed_worklist<'tcx>( .collect::>(); // Seed implemented trait items - let mut life_seeder = - LifeSeeder { worklist, krate, tcx, struct_constructors: Default::default() }; - krate.visit_all_item_likes(&mut life_seeder); + let mut life_seeder = LifeSeeder { worklist, tcx, struct_constructors: Default::default() }; + tcx.hir().visit_all_item_likes(&mut life_seeder); (life_seeder.worklist, life_seeder.struct_constructors) } @@ -614,9 +611,8 @@ fn create_and_seed_worklist<'tcx>( fn find_live<'tcx>( tcx: TyCtxt<'tcx>, access_levels: &privacy::AccessLevels, - krate: &hir::Crate<'_>, ) -> FxHashSet { - let (worklist, struct_constructors) = create_and_seed_worklist(tcx, access_levels, krate); + let (worklist, struct_constructors) = create_and_seed_worklist(tcx, access_levels); let mut symbol_visitor = MarkSymbolVisitor { worklist, tcx, @@ -834,8 +830,7 @@ impl Visitor<'tcx> for DeadVisitor<'tcx> { pub fn check_crate(tcx: TyCtxt<'_>) { let access_levels = &tcx.privacy_access_levels(()); - let krate = tcx.hir().krate(); - let live_symbols = find_live(tcx, access_levels, krate); + let live_symbols = find_live(tcx, access_levels); let mut visitor = DeadVisitor { tcx, live_symbols }; tcx.hir().walk_toplevel_module(&mut visitor); } diff --git a/compiler/rustc_passes/src/diagnostic_items.rs b/compiler/rustc_passes/src/diagnostic_items.rs index 3f12a744be0..d35a1cc293e 100644 --- a/compiler/rustc_passes/src/diagnostic_items.rs +++ b/compiler/rustc_passes/src/diagnostic_items.rs @@ -105,7 +105,7 @@ fn diagnostic_items<'tcx>(tcx: TyCtxt<'tcx>, cnum: CrateNum) -> FxHashMap, (): ()) -> Option<(DefId, EntryFnType)> { non_main_fns: Vec::new(), }; - tcx.hir().krate().visit_all_item_likes(&mut ctxt); + tcx.hir().visit_all_item_likes(&mut ctxt); configure_main(tcx, &ctxt) } @@ -183,7 +183,7 @@ fn configure_main(tcx: TyCtxt<'_>, visitor: &EntryContext<'_, '_>) -> Option<(De } fn no_main_err(tcx: TyCtxt<'_>, visitor: &EntryContext<'_, '_>) { - let sp = tcx.hir().krate().module().inner; + let sp = tcx.def_span(CRATE_DEF_ID); if *tcx.sess.parse_sess.reached_eof.borrow() { // There's an unclosed brace that made the parser reach `Eof`, we shouldn't complain about // the missing `fn main()` then as it might have been hidden inside an unclosed block. diff --git a/compiler/rustc_passes/src/lang_items.rs b/compiler/rustc_passes/src/lang_items.rs index 77ff8dc5b4a..c14f4460a31 100644 --- a/compiler/rustc_passes/src/lang_items.rs +++ b/compiler/rustc_passes/src/lang_items.rs @@ -262,7 +262,7 @@ fn get_lang_items(tcx: TyCtxt<'_>, (): ()) -> LanguageItems { } // Collect lang items in this crate. - tcx.hir().krate().visit_all_item_likes(&mut collector); + tcx.hir().visit_all_item_likes(&mut collector); // Extract out the found lang items. let LanguageItemCollector { mut items, .. } = collector; diff --git a/compiler/rustc_passes/src/layout_test.rs b/compiler/rustc_passes/src/layout_test.rs index aa78fcfb4b3..558d8958b13 100644 --- a/compiler/rustc_passes/src/layout_test.rs +++ b/compiler/rustc_passes/src/layout_test.rs @@ -12,7 +12,7 @@ use rustc_target::abi::{HasDataLayout, TargetDataLayout}; pub fn test_layout(tcx: TyCtxt<'_>) { if tcx.features().rustc_attrs { // if the `rustc_attrs` feature is not enabled, don't bother testing layout - tcx.hir().krate().visit_all_item_likes(&mut LayoutTest { tcx }); + tcx.hir().visit_all_item_likes(&mut LayoutTest { tcx }); } } diff --git a/compiler/rustc_passes/src/reachable.rs b/compiler/rustc_passes/src/reachable.rs index 23f43233b79..bd1e9520ee9 100644 --- a/compiler/rustc_passes/src/reachable.rs +++ b/compiler/rustc_passes/src/reachable.rs @@ -427,7 +427,7 @@ fn reachable_set<'tcx>(tcx: TyCtxt<'tcx>, (): ()) -> FxHashSet { access_levels, worklist: &mut reachable_context.worklist, }; - tcx.hir().krate().visit_all_item_likes(&mut collect_private_impl_items); + tcx.hir().visit_all_item_likes(&mut collect_private_impl_items); } // Step 2: Mark all symbols that the symbols on the worklist touch. diff --git a/compiler/rustc_passes/src/stability.rs b/compiler/rustc_passes/src/stability.rs index d7c354aeb49..8c9f04bef13 100644 --- a/compiler/rustc_passes/src/stability.rs +++ b/compiler/rustc_passes/src/stability.rs @@ -906,11 +906,10 @@ pub fn check_unused_or_stable_features(tcx: TyCtxt<'_>) { let access_levels = &tcx.privacy_access_levels(()); if tcx.stability().staged_api[&LOCAL_CRATE] { - let krate = tcx.hir().krate(); let mut missing = MissingStabilityAnnotations { tcx, access_levels }; missing.check_missing_stability(CRATE_DEF_ID, tcx.hir().span(CRATE_HIR_ID)); tcx.hir().walk_toplevel_module(&mut missing); - krate.visit_all_item_likes(&mut missing.as_deep_visitor()); + tcx.hir().visit_all_item_likes(&mut missing.as_deep_visitor()); } let declared_lang_features = &tcx.features().declared_lang_features; diff --git a/compiler/rustc_passes/src/weak_lang_items.rs b/compiler/rustc_passes/src/weak_lang_items.rs index 3a662a9bab2..bb5be90cd40 100644 --- a/compiler/rustc_passes/src/weak_lang_items.rs +++ b/compiler/rustc_passes/src/weak_lang_items.rs @@ -33,7 +33,7 @@ pub fn check_crate<'tcx>(tcx: TyCtxt<'tcx>, items: &mut lang_items::LanguageItem { let mut cx = Context { tcx, items }; - tcx.hir().krate().visit_all_item_likes(&mut cx.as_deep_visitor()); + tcx.hir().visit_all_item_likes(&mut cx.as_deep_visitor()); } verify(tcx, items); } diff --git a/compiler/rustc_privacy/src/lib.rs b/compiler/rustc_privacy/src/lib.rs index e14f758ddae..719d4134ab8 100644 --- a/compiler/rustc_privacy/src/lib.rs +++ b/compiler/rustc_privacy/src/lib.rs @@ -2189,8 +2189,6 @@ fn privacy_access_levels(tcx: TyCtxt<'_>, (): ()) -> &AccessLevels { fn check_private_in_public(tcx: TyCtxt<'_>, (): ()) { let access_levels = tcx.privacy_access_levels(()); - let krate = tcx.hir().krate(); - let mut visitor = ObsoleteVisiblePrivateTypesVisitor { tcx, access_levels: &access_levels, @@ -2230,5 +2228,5 @@ fn check_private_in_public(tcx: TyCtxt<'_>, (): ()) { .filter_map(|hir_id| tcx.hir().opt_local_def_id(hir_id)) .collect(), }; - krate.visit_all_item_likes(&mut DeepVisitor::new(&mut visitor)); + tcx.hir().visit_all_item_likes(&mut DeepVisitor::new(&mut visitor)); } diff --git a/compiler/rustc_save_analysis/src/dump_visitor.rs b/compiler/rustc_save_analysis/src/dump_visitor.rs index 3e99f4e29ef..c47d8b934cf 100644 --- a/compiler/rustc_save_analysis/src/dump_visitor.rs +++ b/compiler/rustc_save_analysis/src/dump_visitor.rs @@ -18,7 +18,7 @@ use rustc_ast::walk_list; use rustc_data_structures::fx::FxHashSet; use rustc_hir as hir; use rustc_hir::def::{DefKind as HirDefKind, Res}; -use rustc_hir::def_id::{DefId, LocalDefId}; +use rustc_hir::def_id::{DefId, LocalDefId, CRATE_DEF_ID}; use rustc_hir::intravisit::{self, Visitor}; use rustc_hir_pretty::{bounds_to_string, fn_to_string, generic_params_to_string, ty_to_string}; use rustc_middle::hir::map::Map; @@ -128,7 +128,7 @@ impl<'tcx> DumpVisitor<'tcx> { self.save_ctxt.lookup_def_id(ref_id) } - pub fn dump_crate_info(&mut self, name: &str, krate: &hir::Crate<'_>) { + pub fn dump_crate_info(&mut self, name: &str) { let source_file = self.tcx.sess.local_crate_source_file.as_ref(); let crate_root = source_file.map(|source_file| { let source_file = Path::new(source_file); @@ -146,7 +146,7 @@ impl<'tcx> DumpVisitor<'tcx> { }, crate_root: crate_root.unwrap_or_else(|| "".to_owned()), external_crates: self.save_ctxt.get_external_crates(), - span: self.span_from_span(krate.module().inner), + span: self.span_from_span(self.tcx.def_span(CRATE_DEF_ID)), }; self.dumper.crate_prelude(data); @@ -1090,13 +1090,13 @@ impl<'tcx> DumpVisitor<'tcx> { } } - pub(crate) fn process_crate(&mut self, krate: &'tcx hir::Crate<'tcx>) { + pub(crate) fn process_crate(&mut self) { let id = hir::CRATE_HIR_ID; let qualname = format!("::{}", self.tcx.def_path_str(self.tcx.hir().local_def_id(id).to_def_id())); let sm = self.tcx.sess.source_map(); - let krate_mod = krate.module(); + let krate_mod = self.tcx.hir().root_module(); let filename = sm.span_to_filename(krate_mod.inner); let data_id = id_from_hir_id(id, &self.save_ctxt); let children = diff --git a/compiler/rustc_save_analysis/src/lib.rs b/compiler/rustc_save_analysis/src/lib.rs index 0f9257809fe..d69d7daa555 100644 --- a/compiler/rustc_save_analysis/src/lib.rs +++ b/compiler/rustc_save_analysis/src/lib.rs @@ -1003,9 +1003,9 @@ pub fn process_crate<'l, 'tcx, H: SaveHandler>( let mut visitor = DumpVisitor::new(save_ctxt); - visitor.dump_crate_info(cratename, tcx.hir().krate()); + visitor.dump_crate_info(cratename); visitor.dump_compilation_options(input, cratename); - visitor.process_crate(tcx.hir().krate()); + visitor.process_crate(); handler.save(&visitor.save_ctxt, &visitor.analysis()) }) diff --git a/compiler/rustc_symbol_mangling/src/test.rs b/compiler/rustc_symbol_mangling/src/test.rs index 183df96f316..f7d68b5cc70 100644 --- a/compiler/rustc_symbol_mangling/src/test.rs +++ b/compiler/rustc_symbol_mangling/src/test.rs @@ -23,7 +23,7 @@ pub fn report_symbol_names(tcx: TyCtxt<'_>) { tcx.dep_graph.with_ignore(|| { let mut visitor = SymbolNamesTest { tcx }; - tcx.hir().krate().visit_all_item_likes(&mut visitor); + tcx.hir().visit_all_item_likes(&mut visitor); }) } diff --git a/compiler/rustc_typeck/src/check/check.rs b/compiler/rustc_typeck/src/check/check.rs index c1d14413554..47da8e9572b 100644 --- a/compiler/rustc_typeck/src/check/check.rs +++ b/compiler/rustc_typeck/src/check/check.rs @@ -31,7 +31,7 @@ use std::ops::ControlFlow; pub fn check_wf_new(tcx: TyCtxt<'_>) { let visit = wfcheck::CheckTypeWellFormedVisitor::new(tcx); - tcx.hir().krate().par_visit_all_item_likes(&visit); + tcx.hir().par_visit_all_item_likes(&visit); } pub(super) fn check_abi(tcx: TyCtxt<'_>, hir_id: hir::HirId, span: Span, abi: Abi) { diff --git a/compiler/rustc_typeck/src/check/method/suggest.rs b/compiler/rustc_typeck/src/check/method/suggest.rs index 9c7853dd78d..0071fd2c494 100644 --- a/compiler/rustc_typeck/src/check/method/suggest.rs +++ b/compiler/rustc_typeck/src/check/method/suggest.rs @@ -1646,7 +1646,7 @@ fn compute_all_traits(tcx: TyCtxt<'_>, (): ()) -> &[DefId] { fn visit_foreign_item(&mut self, _foreign_item: &hir::ForeignItem<'_>) {} } - tcx.hir().krate().visit_all_item_likes(&mut Visitor { traits: &mut traits }); + tcx.hir().visit_all_item_likes(&mut Visitor { traits: &mut traits }); // Cross-crate: diff --git a/compiler/rustc_typeck/src/check_unused.rs b/compiler/rustc_typeck/src/check_unused.rs index cb127880c62..048f48b0351 100644 --- a/compiler/rustc_typeck/src/check_unused.rs +++ b/compiler/rustc_typeck/src/check_unused.rs @@ -16,7 +16,7 @@ pub fn check_crate(tcx: TyCtxt<'_>) { } let mut visitor = CheckVisitor { tcx, used_trait_imports }; - tcx.hir().krate().visit_all_item_likes(&mut visitor); + tcx.hir().visit_all_item_likes(&mut visitor); unused_crates_lint(tcx); } @@ -111,7 +111,7 @@ fn unused_crates_lint(tcx: TyCtxt<'_>) { // Collect all the extern crates (in a reliable order). let mut crates_to_lint = vec![]; - tcx.hir().krate().visit_all_item_likes(&mut CollectExternCrateVisitor { + tcx.hir().visit_all_item_likes(&mut CollectExternCrateVisitor { crates_to_lint: &mut crates_to_lint, }); diff --git a/compiler/rustc_typeck/src/coherence/inherent_impls.rs b/compiler/rustc_typeck/src/coherence/inherent_impls.rs index f3fe09ac003..0b6d0a539b5 100644 --- a/compiler/rustc_typeck/src/coherence/inherent_impls.rs +++ b/compiler/rustc_typeck/src/coherence/inherent_impls.rs @@ -17,9 +17,8 @@ use rustc_span::Span; /// On-demand query: yields a map containing all types mapped to their inherent impls. pub fn crate_inherent_impls(tcx: TyCtxt<'_>, (): ()) -> CrateInherentImpls { - let krate = tcx.hir().krate(); let mut collect = InherentCollect { tcx, impls_map: Default::default() }; - krate.visit_all_item_likes(&mut collect); + tcx.hir().visit_all_item_likes(&mut collect); collect.impls_map } diff --git a/compiler/rustc_typeck/src/coherence/inherent_impls_overlap.rs b/compiler/rustc_typeck/src/coherence/inherent_impls_overlap.rs index 1c36335be81..11ffd61cb2e 100644 --- a/compiler/rustc_typeck/src/coherence/inherent_impls_overlap.rs +++ b/compiler/rustc_typeck/src/coherence/inherent_impls_overlap.rs @@ -10,8 +10,7 @@ use smallvec::SmallVec; use std::collections::hash_map::Entry; pub fn crate_inherent_impls_overlap_check(tcx: TyCtxt<'_>, (): ()) { - let krate = tcx.hir().krate(); - krate.visit_all_item_likes(&mut InherentOverlapChecker { tcx }); + tcx.hir().visit_all_item_likes(&mut InherentOverlapChecker { tcx }); } struct InherentOverlapChecker<'tcx> { diff --git a/compiler/rustc_typeck/src/coherence/orphan.rs b/compiler/rustc_typeck/src/coherence/orphan.rs index 05932427bcf..0326d1fd74f 100644 --- a/compiler/rustc_typeck/src/coherence/orphan.rs +++ b/compiler/rustc_typeck/src/coherence/orphan.rs @@ -10,7 +10,7 @@ use rustc_trait_selection::traits; pub fn check(tcx: TyCtxt<'_>) { let mut orphan = OrphanChecker { tcx }; - tcx.hir().krate().visit_all_item_likes(&mut orphan); + tcx.hir().visit_all_item_likes(&mut orphan); } struct OrphanChecker<'tcx> { diff --git a/compiler/rustc_typeck/src/coherence/unsafety.rs b/compiler/rustc_typeck/src/coherence/unsafety.rs index 6b995b97386..e7b03fa3ac6 100644 --- a/compiler/rustc_typeck/src/coherence/unsafety.rs +++ b/compiler/rustc_typeck/src/coherence/unsafety.rs @@ -9,7 +9,7 @@ use rustc_middle::ty::TyCtxt; pub fn check(tcx: TyCtxt<'_>) { let mut unsafety = UnsafetyChecker { tcx }; - tcx.hir().krate().visit_all_item_likes(&mut unsafety); + tcx.hir().visit_all_item_likes(&mut unsafety); } struct UnsafetyChecker<'tcx> { diff --git a/compiler/rustc_typeck/src/outlives/implicit_infer.rs b/compiler/rustc_typeck/src/outlives/implicit_infer.rs index 0e96601d89f..86d712e2d79 100644 --- a/compiler/rustc_typeck/src/outlives/implicit_infer.rs +++ b/compiler/rustc_typeck/src/outlives/implicit_infer.rs @@ -37,7 +37,7 @@ pub fn infer_predicates<'tcx>( }; // Visit all the crates and infer predicates - tcx.hir().krate().visit_all_item_likes(&mut visitor); + tcx.hir().visit_all_item_likes(&mut visitor); } global_inferred_outlives diff --git a/compiler/rustc_typeck/src/outlives/test.rs b/compiler/rustc_typeck/src/outlives/test.rs index d4bef0c409a..ec4fa9cd4b5 100644 --- a/compiler/rustc_typeck/src/outlives/test.rs +++ b/compiler/rustc_typeck/src/outlives/test.rs @@ -5,7 +5,7 @@ use rustc_middle::ty::TyCtxt; use rustc_span::symbol::sym; pub fn test_inferred_outlives(tcx: TyCtxt<'_>) { - tcx.hir().krate().visit_all_item_likes(&mut OutlivesTest { tcx }); + tcx.hir().visit_all_item_likes(&mut OutlivesTest { tcx }); } struct OutlivesTest<'tcx> { diff --git a/compiler/rustc_typeck/src/variance/constraints.rs b/compiler/rustc_typeck/src/variance/constraints.rs index f5355ea042b..708c0cf4a64 100644 --- a/compiler/rustc_typeck/src/variance/constraints.rs +++ b/compiler/rustc_typeck/src/variance/constraints.rs @@ -62,7 +62,7 @@ pub fn add_constraints_from_crate<'a, 'tcx>( constraints: Vec::new(), }; - tcx.hir().krate().visit_all_item_likes(&mut constraint_cx); + tcx.hir().visit_all_item_likes(&mut constraint_cx); constraint_cx } diff --git a/compiler/rustc_typeck/src/variance/terms.rs b/compiler/rustc_typeck/src/variance/terms.rs index 5d5baf78d33..d7f9df668bf 100644 --- a/compiler/rustc_typeck/src/variance/terms.rs +++ b/compiler/rustc_typeck/src/variance/terms.rs @@ -79,7 +79,7 @@ pub fn determine_parameters_to_be_inferred<'a, 'tcx>( // // - https://rustc-dev-guide.rust-lang.org/query.html // - https://rustc-dev-guide.rust-lang.org/variance.html - tcx.hir().krate().visit_all_item_likes(&mut terms_cx); + tcx.hir().visit_all_item_likes(&mut terms_cx); terms_cx } diff --git a/compiler/rustc_typeck/src/variance/test.rs b/compiler/rustc_typeck/src/variance/test.rs index 2a0d950c87d..7be3c68e8f6 100644 --- a/compiler/rustc_typeck/src/variance/test.rs +++ b/compiler/rustc_typeck/src/variance/test.rs @@ -5,7 +5,7 @@ use rustc_middle::ty::TyCtxt; use rustc_span::symbol::sym; pub fn test_variance(tcx: TyCtxt<'_>) { - tcx.hir().krate().visit_all_item_likes(&mut VarianceTest { tcx }); + tcx.hir().visit_all_item_likes(&mut VarianceTest { tcx }); } struct VarianceTest<'tcx> { diff --git a/src/librustdoc/clean/types.rs b/src/librustdoc/clean/types.rs index 68a35e55c26..206e8f91bd2 100644 --- a/src/librustdoc/clean/types.rs +++ b/src/librustdoc/clean/types.rs @@ -230,8 +230,7 @@ impl ExternalCrate { }; if root.is_local() { tcx.hir() - .krate() - .module() + .root_module() .item_ids .iter() .filter_map(|&id| { @@ -297,8 +296,7 @@ impl ExternalCrate { if root.is_local() { tcx.hir() - .krate() - .module() + .root_module() .item_ids .iter() .filter_map(|&id| { diff --git a/src/librustdoc/clean/utils.rs b/src/librustdoc/clean/utils.rs index 33d460d587a..2a05787e40c 100644 --- a/src/librustdoc/clean/utils.rs +++ b/src/librustdoc/clean/utils.rs @@ -26,8 +26,7 @@ mod tests; crate fn krate(cx: &mut DocContext<'_>) -> Crate { use crate::visit_lib::LibEmbargoVisitor; - let krate = cx.tcx.hir().krate(); - let module = crate::visit_ast::RustdocVisitor::new(cx).visit(krate); + let module = crate::visit_ast::RustdocVisitor::new(cx).visit(); let mut externs = Vec::new(); for &cnum in cx.tcx.crates(()).iter() { diff --git a/src/librustdoc/visit_ast.rs b/src/librustdoc/visit_ast.rs index 897b9140fc8..4f5495a176d 100644 --- a/src/librustdoc/visit_ast.rs +++ b/src/librustdoc/visit_ast.rs @@ -71,12 +71,12 @@ impl<'a, 'tcx> RustdocVisitor<'a, 'tcx> { self.exact_paths.entry(did).or_insert_with(|| def_id_to_path(tcx, did)); } - crate fn visit(mut self, krate: &'tcx hir::Crate<'_>) -> Module<'tcx> { - let span = krate.module().inner; + crate fn visit(mut self) -> Module<'tcx> { + let span = self.cx.tcx.def_span(CRATE_DEF_ID); let mut top_level_module = self.visit_mod_contents( &Spanned { span, node: hir::VisibilityKind::Public }, hir::CRATE_HIR_ID, - &krate.module(), + self.cx.tcx.hir().root_module(), self.cx.tcx.crate_name(LOCAL_CRATE), ); diff --git a/src/tools/clippy/clippy_lints/src/missing_doc.rs b/src/tools/clippy/clippy_lints/src/missing_doc.rs index 940eee7a788..578fddbd772 100644 --- a/src/tools/clippy/clippy_lints/src/missing_doc.rs +++ b/src/tools/clippy/clippy_lints/src/missing_doc.rs @@ -78,9 +78,7 @@ impl MissingDoc { return; } - let has_doc = attrs - .iter() - .any(|a| a.doc_str().is_some()); + let has_doc = attrs.iter().any(|a| a.doc_str().is_some()); if !has_doc { span_lint( cx, @@ -104,9 +102,9 @@ impl<'tcx> LateLintPass<'tcx> for MissingDoc { self.doc_hidden_stack.pop().expect("empty doc_hidden_stack"); } - fn check_crate(&mut self, cx: &LateContext<'tcx>, krate: &'tcx hir::Crate<'_>) { + fn check_crate(&mut self, cx: &LateContext<'tcx>, _: &'tcx hir::Crate<'_>) { let attrs = cx.tcx.hir().attrs(hir::CRATE_HIR_ID); - self.check_missing_docs_attrs(cx, attrs, krate.module().inner, "the", "crate"); + self.check_missing_docs_attrs(cx, attrs, cx.tcx.hir().root_module().inner, "the", "crate"); } fn check_item(&mut self, cx: &LateContext<'tcx>, it: &'tcx hir::Item<'_>) { From abc57f63ad98436f8a97c710f357e1e45088effc Mon Sep 17 00:00:00 2001 From: Camille GILLOT Date: Sun, 12 Sep 2021 11:33:16 +0200 Subject: [PATCH 2/5] Move body_owners to tcx.hir(). --- compiler/rustc_driver/src/pretty.rs | 2 +- compiler/rustc_interface/src/passes.rs | 8 +++----- compiler/rustc_middle/src/hir/map/mod.rs | 11 +++++++++++ compiler/rustc_middle/src/ty/mod.rs | 14 -------------- compiler/rustc_mir_transform/src/lib.rs | 2 +- compiler/rustc_typeck/src/check/mod.rs | 4 +--- compiler/rustc_typeck/src/check_unused.rs | 2 +- 7 files changed, 18 insertions(+), 25 deletions(-) diff --git a/compiler/rustc_driver/src/pretty.rs b/compiler/rustc_driver/src/pretty.rs index a25cc000443..ff8920863b1 100644 --- a/compiler/rustc_driver/src/pretty.rs +++ b/compiler/rustc_driver/src/pretty.rs @@ -489,7 +489,7 @@ fn print_with_analysis( let mut out = String::new(); abort_on_err(rustc_typeck::check_crate(tcx), tcx.sess); debug!("pretty printing THIR tree"); - for did in tcx.body_owners() { + for did in tcx.hir().body_owners() { let _ = writeln!( out, "{:?}:\n{}\n", diff --git a/compiler/rustc_interface/src/passes.rs b/compiler/rustc_interface/src/passes.rs index 8aff093dd18..795fa19021c 100644 --- a/compiler/rustc_interface/src/passes.rs +++ b/compiler/rustc_interface/src/passes.rs @@ -886,9 +886,7 @@ fn analysis(tcx: TyCtxt<'_>, (): ()) -> Result<()> { parallel!( { sess.time("match_checking", || { - tcx.par_body_owners(|def_id| { - tcx.ensure().check_match(def_id.to_def_id()); - }); + tcx.hir().par_body_owners(|def_id| tcx.ensure().check_match(def_id.to_def_id())) }); }, { @@ -907,11 +905,11 @@ fn analysis(tcx: TyCtxt<'_>, (): ()) -> Result<()> { }); sess.time("MIR_borrow_checking", || { - tcx.par_body_owners(|def_id| tcx.ensure().mir_borrowck(def_id)); + tcx.hir().par_body_owners(|def_id| tcx.ensure().mir_borrowck(def_id)); }); sess.time("MIR_effect_checking", || { - for def_id in tcx.body_owners() { + for def_id in tcx.hir().body_owners() { tcx.ensure().thir_check_unsafety(def_id); if !tcx.sess.opts.debugging_opts.thir_unsafeck { rustc_mir_transform::check_unsafety::check_unsafety(tcx, def_id); diff --git a/compiler/rustc_middle/src/hir/map/mod.rs b/compiler/rustc_middle/src/hir/map/mod.rs index dfa738652a5..7d1c63c0645 100644 --- a/compiler/rustc_middle/src/hir/map/mod.rs +++ b/compiler/rustc_middle/src/hir/map/mod.rs @@ -491,6 +491,17 @@ impl<'hir> Map<'hir> { Some(ccx) } + /// Returns an iterator of the `DefId`s for all body-owners in this + /// crate. If you would prefer to iterate over the bodies + /// themselves, you can do `self.hir().krate().body_ids.iter()`. + pub fn body_owners(self) -> impl Iterator + 'hir { + self.krate().bodies.keys().map(move |&body_id| self.body_owner_def_id(body_id)) + } + + pub fn par_body_owners(self, f: F) { + par_for_each_in(&self.krate().bodies, |(&body_id, _)| f(self.body_owner_def_id(body_id))); + } + pub fn ty_param_owner(&self, id: HirId) -> HirId { match self.get(id) { Node::Item(&Item { kind: ItemKind::Trait(..) | ItemKind::TraitAlias(..), .. }) => id, diff --git a/compiler/rustc_middle/src/ty/mod.rs b/compiler/rustc_middle/src/ty/mod.rs index b3ae76d9871..bcd0112f226 100644 --- a/compiler/rustc_middle/src/ty/mod.rs +++ b/compiler/rustc_middle/src/ty/mod.rs @@ -29,10 +29,8 @@ use crate::ty::subst::{GenericArg, InternalSubsts, Subst, SubstsRef}; use crate::ty::util::Discr; use rustc_ast as ast; use rustc_attr as attr; -use rustc_data_structures::captures::Captures; use rustc_data_structures::fx::{FxHashMap, FxHashSet}; use rustc_data_structures::stable_hasher::{HashStable, StableHasher}; -use rustc_data_structures::sync::{self, par_iter, ParallelIterator}; use rustc_data_structures::tagged_ptr::CopyTaggedPtr; use rustc_hir as hir; use rustc_hir::def::{CtorKind, CtorOf, DefKind, Res}; @@ -1660,18 +1658,6 @@ impl<'tcx> TyCtxt<'tcx> { self.typeck(self.hir().body_owner_def_id(body)) } - /// Returns an iterator of the `DefId`s for all body-owners in this - /// crate. If you would prefer to iterate over the bodies - /// themselves, you can do `self.hir().krate().body_ids.iter()`. - pub fn body_owners(self) -> impl Iterator + Captures<'tcx> + 'tcx { - self.hir().krate().bodies.keys().map(move |&body_id| self.hir().body_owner_def_id(body_id)) - } - - pub fn par_body_owners(self, f: F) { - par_iter(&self.hir().krate().bodies) - .for_each(|(&body_id, _)| f(self.hir().body_owner_def_id(body_id))); - } - pub fn provided_trait_methods(self, id: DefId) -> impl 'tcx + Iterator { self.associated_items(id) .in_definition_order() diff --git a/compiler/rustc_mir_transform/src/lib.rs b/compiler/rustc_mir_transform/src/lib.rs index fadb7014570..b1b31e0784c 100644 --- a/compiler/rustc_mir_transform/src/lib.rs +++ b/compiler/rustc_mir_transform/src/lib.rs @@ -133,7 +133,7 @@ fn mir_keys(tcx: TyCtxt<'_>, (): ()) -> FxHashSet { let mut set = FxHashSet::default(); // All body-owners have MIR associated with them. - set.extend(tcx.body_owners()); + set.extend(tcx.hir().body_owners()); // Additionally, tuple struct/variant constructors have MIR, but // they don't have a BodyId, so we need to build them separately. diff --git a/compiler/rustc_typeck/src/check/mod.rs b/compiler/rustc_typeck/src/check/mod.rs index a10490a9a15..8a55f7ebf87 100644 --- a/compiler/rustc_typeck/src/check/mod.rs +++ b/compiler/rustc_typeck/src/check/mod.rs @@ -921,9 +921,7 @@ impl ItemLikeVisitor<'tcx> for CheckItemTypesVisitor<'tcx> { } fn typeck_item_bodies(tcx: TyCtxt<'_>, (): ()) { - tcx.par_body_owners(|body_owner_def_id| { - tcx.ensure().typeck(body_owner_def_id); - }); + tcx.hir().par_body_owners(|body_owner_def_id| tcx.ensure().typeck(body_owner_def_id)); } fn fatally_break_rust(sess: &Session) { diff --git a/compiler/rustc_typeck/src/check_unused.rs b/compiler/rustc_typeck/src/check_unused.rs index 048f48b0351..7b5d782b0cb 100644 --- a/compiler/rustc_typeck/src/check_unused.rs +++ b/compiler/rustc_typeck/src/check_unused.rs @@ -9,7 +9,7 @@ use rustc_span::{Span, Symbol}; pub fn check_crate(tcx: TyCtxt<'_>) { let mut used_trait_imports = FxHashSet::default(); - for item_def_id in tcx.body_owners() { + for item_def_id in tcx.hir().body_owners() { let imports = tcx.used_trait_imports(item_def_id); debug!("GatherVisitor: item_def_id={:?} with imports {:#?}", item_def_id, imports); used_trait_imports.extend(imports.iter()); From faa7d4221be02551d475e5b359a1317401bcfd0d Mon Sep 17 00:00:00 2001 From: Camille GILLOT Date: Sun, 12 Sep 2021 11:58:27 +0200 Subject: [PATCH 3/5] Do not pass hir::Crate to lints. --- compiler/rustc_lint/src/builtin.rs | 2 +- compiler/rustc_lint/src/late.rs | 6 ++---- compiler/rustc_lint/src/passes.rs | 4 ++-- compiler/rustc_middle/src/hir/map/mod.rs | 2 +- src/tools/clippy/clippy_lints/src/cargo_common_metadata.rs | 4 ++-- src/tools/clippy/clippy_lints/src/disallowed_method.rs | 4 ++-- src/tools/clippy/clippy_lints/src/disallowed_type.rs | 4 ++-- src/tools/clippy/clippy_lints/src/doc.rs | 2 +- src/tools/clippy/clippy_lints/src/feature_name.rs | 4 ++-- src/tools/clippy/clippy_lints/src/inherent_impl.rs | 4 ++-- src/tools/clippy/clippy_lints/src/macro_use.rs | 2 +- src/tools/clippy/clippy_lints/src/main_recursion.rs | 4 ++-- src/tools/clippy/clippy_lints/src/missing_doc.rs | 5 +++-- .../clippy_lints/src/missing_enforced_import_rename.rs | 4 ++-- .../clippy/clippy_lints/src/multiple_crate_versions.rs | 4 ++-- src/tools/clippy/clippy_lints/src/same_name_method.rs | 6 +++--- src/tools/clippy/clippy_lints/src/wildcard_dependencies.rs | 4 ++-- 17 files changed, 32 insertions(+), 33 deletions(-) diff --git a/compiler/rustc_lint/src/builtin.rs b/compiler/rustc_lint/src/builtin.rs index a24f346fb94..4513c8329ca 100644 --- a/compiler/rustc_lint/src/builtin.rs +++ b/compiler/rustc_lint/src/builtin.rs @@ -584,7 +584,7 @@ impl<'tcx> LateLintPass<'tcx> for MissingDoc { self.doc_hidden_stack.pop().expect("empty doc_hidden_stack"); } - fn check_crate(&mut self, cx: &LateContext<'_>, _: &hir::Crate<'_>) { + fn check_crate(&mut self, cx: &LateContext<'_>) { self.check_missing_docs_attrs( cx, CRATE_DEF_ID, diff --git a/compiler/rustc_lint/src/late.rs b/compiler/rustc_lint/src/late.rs index 00c3a6fa25e..773e5751f13 100644 --- a/compiler/rustc_lint/src/late.rs +++ b/compiler/rustc_lint/src/late.rs @@ -430,8 +430,6 @@ pub fn late_lint_mod<'tcx, T: LateLintPass<'tcx>>( fn late_lint_pass_crate<'tcx, T: LateLintPass<'tcx>>(tcx: TyCtxt<'tcx>, pass: T) { let access_levels = &tcx.privacy_access_levels(()); - let krate = tcx.hir().krate(); - let context = LateContext { tcx, enclosing_body: None, @@ -450,10 +448,10 @@ fn late_lint_pass_crate<'tcx, T: LateLintPass<'tcx>>(tcx: TyCtxt<'tcx>, pass: T) cx.with_lint_attrs(hir::CRATE_HIR_ID, |cx| { // since the root module isn't visited as an item (because it isn't an // item), warn for it here. - lint_callback!(cx, check_crate, krate); + lint_callback!(cx, check_crate,); tcx.hir().walk_toplevel_module(cx); tcx.hir().walk_attributes(cx); - lint_callback!(cx, check_crate_post, krate); + lint_callback!(cx, check_crate_post,); }) } diff --git a/compiler/rustc_lint/src/passes.rs b/compiler/rustc_lint/src/passes.rs index 2d047ac7a08..b1b4229b1f7 100644 --- a/compiler/rustc_lint/src/passes.rs +++ b/compiler/rustc_lint/src/passes.rs @@ -16,8 +16,8 @@ macro_rules! late_lint_methods { fn check_body(a: &$hir hir::Body<$hir>); fn check_body_post(a: &$hir hir::Body<$hir>); fn check_name(a: Span, b: Symbol); - fn check_crate(a: &$hir hir::Crate<$hir>); - fn check_crate_post(a: &$hir hir::Crate<$hir>); + fn check_crate(); + fn check_crate_post(); fn check_mod(a: &$hir hir::Mod<$hir>, b: Span, c: hir::HirId); fn check_mod_post(a: &$hir hir::Mod<$hir>, b: Span, c: hir::HirId); fn check_foreign_item(a: &$hir hir::ForeignItem<$hir>); diff --git a/compiler/rustc_middle/src/hir/map/mod.rs b/compiler/rustc_middle/src/hir/map/mod.rs index 7d1c63c0645..3989a94a47c 100644 --- a/compiler/rustc_middle/src/hir/map/mod.rs +++ b/compiler/rustc_middle/src/hir/map/mod.rs @@ -164,7 +164,7 @@ impl<'hir> Map<'hir> { } } - crate fn items(&self) -> impl Iterator> + 'hir { + pub fn items(&self) -> impl Iterator> + 'hir { let krate = self.krate(); krate.owners.iter().filter_map(|owner| match owner.as_ref()? { OwnerNode::Item(item) => Some(*item), diff --git a/src/tools/clippy/clippy_lints/src/cargo_common_metadata.rs b/src/tools/clippy/clippy_lints/src/cargo_common_metadata.rs index 162911b77d6..ff619c59b6e 100644 --- a/src/tools/clippy/clippy_lints/src/cargo_common_metadata.rs +++ b/src/tools/clippy/clippy_lints/src/cargo_common_metadata.rs @@ -1,7 +1,7 @@ //! lint on missing cargo common metadata use clippy_utils::{diagnostics::span_lint, is_lint_allowed}; -use rustc_hir::{hir_id::CRATE_HIR_ID, Crate}; +use rustc_hir::hir_id::CRATE_HIR_ID; use rustc_lint::{LateContext, LateLintPass}; use rustc_session::{declare_tool_lint, impl_lint_pass}; use rustc_span::source_map::DUMMY_SP; @@ -77,7 +77,7 @@ fn is_empty_vec(value: &[String]) -> bool { } impl LateLintPass<'_> for CargoCommonMetadata { - fn check_crate(&mut self, cx: &LateContext<'_>, _: &Crate<'_>) { + fn check_crate(&mut self, cx: &LateContext<'_>) { if is_lint_allowed(cx, CARGO_COMMON_METADATA, CRATE_HIR_ID) { return; } diff --git a/src/tools/clippy/clippy_lints/src/disallowed_method.rs b/src/tools/clippy/clippy_lints/src/disallowed_method.rs index 1167b26c8f1..22d726cdcb7 100644 --- a/src/tools/clippy/clippy_lints/src/disallowed_method.rs +++ b/src/tools/clippy/clippy_lints/src/disallowed_method.rs @@ -1,7 +1,7 @@ use clippy_utils::diagnostics::span_lint_and_then; use clippy_utils::fn_def_id; -use rustc_hir::{def::Res, def_id::DefIdMap, Crate, Expr}; +use rustc_hir::{def::Res, def_id::DefIdMap, Expr}; use rustc_lint::{LateContext, LateLintPass}; use rustc_session::{declare_tool_lint, impl_lint_pass}; @@ -70,7 +70,7 @@ impl DisallowedMethod { impl_lint_pass!(DisallowedMethod => [DISALLOWED_METHOD]); impl<'tcx> LateLintPass<'tcx> for DisallowedMethod { - fn check_crate(&mut self, cx: &LateContext<'_>, _: &Crate<'_>) { + fn check_crate(&mut self, cx: &LateContext<'_>) { for conf in &self.conf_disallowed { let (path, reason) = match conf { conf::DisallowedMethod::Simple(path) => (path, None), diff --git a/src/tools/clippy/clippy_lints/src/disallowed_type.rs b/src/tools/clippy/clippy_lints/src/disallowed_type.rs index 6c861fb33a9..87124f093a8 100644 --- a/src/tools/clippy/clippy_lints/src/disallowed_type.rs +++ b/src/tools/clippy/clippy_lints/src/disallowed_type.rs @@ -2,7 +2,7 @@ use clippy_utils::diagnostics::span_lint; use rustc_data_structures::fx::FxHashSet; use rustc_hir::{ - def::Res, def_id::DefId, Crate, Item, ItemKind, PolyTraitRef, PrimTy, TraitBoundModifier, Ty, TyKind, UseKind, + def::Res, def_id::DefId, Item, ItemKind, PolyTraitRef, PrimTy, TraitBoundModifier, Ty, TyKind, UseKind, }; use rustc_lint::{LateContext, LateLintPass}; use rustc_session::{declare_tool_lint, impl_lint_pass}; @@ -75,7 +75,7 @@ impl DisallowedType { impl_lint_pass!(DisallowedType => [DISALLOWED_TYPE]); impl<'tcx> LateLintPass<'tcx> for DisallowedType { - fn check_crate(&mut self, cx: &LateContext<'_>, _: &Crate<'_>) { + fn check_crate(&mut self, cx: &LateContext<'_>) { for path in &self.disallowed { let segs = path.iter().map(ToString::to_string).collect::>(); match clippy_utils::path_to_res(cx, &segs.iter().map(String::as_str).collect::>()) { diff --git a/src/tools/clippy/clippy_lints/src/doc.rs b/src/tools/clippy/clippy_lints/src/doc.rs index 0b61909ddd8..84f3b76a3c7 100644 --- a/src/tools/clippy/clippy_lints/src/doc.rs +++ b/src/tools/clippy/clippy_lints/src/doc.rs @@ -212,7 +212,7 @@ impl_lint_pass!(DocMarkdown => ); impl<'tcx> LateLintPass<'tcx> for DocMarkdown { - fn check_crate(&mut self, cx: &LateContext<'tcx>, _: &'tcx hir::Crate<'_>) { + fn check_crate(&mut self, cx: &LateContext<'tcx>) { let attrs = cx.tcx.hir().attrs(hir::CRATE_HIR_ID); check_attrs(cx, &self.valid_idents, attrs); } diff --git a/src/tools/clippy/clippy_lints/src/feature_name.rs b/src/tools/clippy/clippy_lints/src/feature_name.rs index eef1407a80c..f534327f7a0 100644 --- a/src/tools/clippy/clippy_lints/src/feature_name.rs +++ b/src/tools/clippy/clippy_lints/src/feature_name.rs @@ -1,6 +1,6 @@ use clippy_utils::diagnostics::span_lint_and_help; use clippy_utils::{diagnostics::span_lint, is_lint_allowed}; -use rustc_hir::{Crate, CRATE_HIR_ID}; +use rustc_hir::CRATE_HIR_ID; use rustc_lint::{LateContext, LateLintPass}; use rustc_session::{declare_lint_pass, declare_tool_lint}; use rustc_span::source_map::DUMMY_SP; @@ -110,7 +110,7 @@ fn lint(cx: &LateContext<'_>, feature: &str, substring: &str, is_prefix: bool) { } impl LateLintPass<'_> for FeatureName { - fn check_crate(&mut self, cx: &LateContext<'_>, _: &Crate<'_>) { + fn check_crate(&mut self, cx: &LateContext<'_>) { if is_lint_allowed(cx, REDUNDANT_FEATURE_NAMES, CRATE_HIR_ID) && is_lint_allowed(cx, NEGATIVE_FEATURE_NAMES, CRATE_HIR_ID) { diff --git a/src/tools/clippy/clippy_lints/src/inherent_impl.rs b/src/tools/clippy/clippy_lints/src/inherent_impl.rs index d87055c842c..0d23bec27a3 100644 --- a/src/tools/clippy/clippy_lints/src/inherent_impl.rs +++ b/src/tools/clippy/clippy_lints/src/inherent_impl.rs @@ -3,7 +3,7 @@ use clippy_utils::diagnostics::span_lint_and_note; use clippy_utils::{in_macro, is_lint_allowed}; use rustc_data_structures::fx::FxHashMap; -use rustc_hir::{def_id::LocalDefId, Crate, Item, ItemKind, Node}; +use rustc_hir::{def_id::LocalDefId, Item, ItemKind, Node}; use rustc_lint::{LateContext, LateLintPass}; use rustc_session::{declare_lint_pass, declare_tool_lint}; use rustc_span::Span; @@ -44,7 +44,7 @@ declare_clippy_lint! { declare_lint_pass!(MultipleInherentImpl => [MULTIPLE_INHERENT_IMPL]); impl<'tcx> LateLintPass<'tcx> for MultipleInherentImpl { - fn check_crate_post(&mut self, cx: &LateContext<'tcx>, _: &'tcx Crate<'_>) { + fn check_crate_post(&mut self, cx: &LateContext<'tcx>) { // Map from a type to it's first impl block. Needed to distinguish generic arguments. // e.g. `Foo` and `Foo` let mut type_map = FxHashMap::default(); diff --git a/src/tools/clippy/clippy_lints/src/macro_use.rs b/src/tools/clippy/clippy_lints/src/macro_use.rs index aff6b3853a4..c38162743a3 100644 --- a/src/tools/clippy/clippy_lints/src/macro_use.rs +++ b/src/tools/clippy/clippy_lints/src/macro_use.rs @@ -135,7 +135,7 @@ impl<'tcx> LateLintPass<'tcx> for MacroUseImports { } } #[allow(clippy::too_many_lines)] - fn check_crate_post(&mut self, cx: &LateContext<'_>, _krate: &hir::Crate<'_>) { + fn check_crate_post(&mut self, cx: &LateContext<'_>) { let mut used = FxHashMap::default(); let mut check_dup = vec![]; for (import, span) in &self.imports { diff --git a/src/tools/clippy/clippy_lints/src/main_recursion.rs b/src/tools/clippy/clippy_lints/src/main_recursion.rs index 776e4b3fe76..23b3ba2296e 100644 --- a/src/tools/clippy/clippy_lints/src/main_recursion.rs +++ b/src/tools/clippy/clippy_lints/src/main_recursion.rs @@ -2,7 +2,7 @@ use clippy_utils::diagnostics::span_lint_and_help; use clippy_utils::source::snippet; use clippy_utils::{is_entrypoint_fn, is_no_std_crate}; use if_chain::if_chain; -use rustc_hir::{Crate, Expr, ExprKind, QPath}; +use rustc_hir::{Expr, ExprKind, QPath}; use rustc_lint::{LateContext, LateLintPass}; use rustc_session::{declare_tool_lint, impl_lint_pass}; @@ -33,7 +33,7 @@ pub struct MainRecursion { impl_lint_pass!(MainRecursion => [MAIN_RECURSION]); impl LateLintPass<'_> for MainRecursion { - fn check_crate(&mut self, cx: &LateContext<'_>, _: &Crate<'_>) { + fn check_crate(&mut self, cx: &LateContext<'_>) { self.has_no_std_attr = is_no_std_crate(cx); } diff --git a/src/tools/clippy/clippy_lints/src/missing_doc.rs b/src/tools/clippy/clippy_lints/src/missing_doc.rs index 578fddbd772..564f021268c 100644 --- a/src/tools/clippy/clippy_lints/src/missing_doc.rs +++ b/src/tools/clippy/clippy_lints/src/missing_doc.rs @@ -12,6 +12,7 @@ use rustc_hir as hir; use rustc_lint::{LateContext, LateLintPass, LintContext}; use rustc_middle::ty; use rustc_session::{declare_tool_lint, impl_lint_pass}; +use rustc_span::def_id::CRATE_DEF_ID; use rustc_span::source_map::Span; use rustc_span::sym; @@ -102,9 +103,9 @@ impl<'tcx> LateLintPass<'tcx> for MissingDoc { self.doc_hidden_stack.pop().expect("empty doc_hidden_stack"); } - fn check_crate(&mut self, cx: &LateContext<'tcx>, _: &'tcx hir::Crate<'_>) { + fn check_crate(&mut self, cx: &LateContext<'tcx>) { let attrs = cx.tcx.hir().attrs(hir::CRATE_HIR_ID); - self.check_missing_docs_attrs(cx, attrs, cx.tcx.hir().root_module().inner, "the", "crate"); + self.check_missing_docs_attrs(cx, attrs, cx.tcx.def_span(CRATE_DEF_ID), "the", "crate"); } fn check_item(&mut self, cx: &LateContext<'tcx>, it: &'tcx hir::Item<'_>) { diff --git a/src/tools/clippy/clippy_lints/src/missing_enforced_import_rename.rs b/src/tools/clippy/clippy_lints/src/missing_enforced_import_rename.rs index 9d27870321c..448bfc2fdd6 100644 --- a/src/tools/clippy/clippy_lints/src/missing_enforced_import_rename.rs +++ b/src/tools/clippy/clippy_lints/src/missing_enforced_import_rename.rs @@ -2,7 +2,7 @@ use clippy_utils::{diagnostics::span_lint_and_sugg, source::snippet_opt}; use rustc_data_structures::fx::FxHashMap; use rustc_errors::Applicability; -use rustc_hir::{def::Res, def_id::DefId, Crate, Item, ItemKind, UseKind}; +use rustc_hir::{def::Res, def_id::DefId, Item, ItemKind, UseKind}; use rustc_lint::{LateContext, LateLintPass, LintContext}; use rustc_session::{declare_tool_lint, impl_lint_pass}; use rustc_span::Symbol; @@ -55,7 +55,7 @@ impl ImportRename { impl_lint_pass!(ImportRename => [MISSING_ENFORCED_IMPORT_RENAMES]); impl LateLintPass<'_> for ImportRename { - fn check_crate(&mut self, cx: &LateContext<'_>, _: &Crate<'_>) { + fn check_crate(&mut self, cx: &LateContext<'_>) { for Rename { path, rename } in &self.conf_renames { if let Res::Def(_, id) = clippy_utils::path_to_res(cx, &path.split("::").collect::>()) { self.renames.insert(id, Symbol::intern(rename)); diff --git a/src/tools/clippy/clippy_lints/src/multiple_crate_versions.rs b/src/tools/clippy/clippy_lints/src/multiple_crate_versions.rs index 1c61970fdc8..816b2f275fb 100644 --- a/src/tools/clippy/clippy_lints/src/multiple_crate_versions.rs +++ b/src/tools/clippy/clippy_lints/src/multiple_crate_versions.rs @@ -3,7 +3,7 @@ use clippy_utils::diagnostics::span_lint; use clippy_utils::is_lint_allowed; use rustc_hir::def_id::LOCAL_CRATE; -use rustc_hir::{Crate, CRATE_HIR_ID}; +use rustc_hir::CRATE_HIR_ID; use rustc_lint::{LateContext, LateLintPass}; use rustc_session::{declare_lint_pass, declare_tool_lint}; use rustc_span::source_map::DUMMY_SP; @@ -41,7 +41,7 @@ declare_clippy_lint! { declare_lint_pass!(MultipleCrateVersions => [MULTIPLE_CRATE_VERSIONS]); impl LateLintPass<'_> for MultipleCrateVersions { - fn check_crate(&mut self, cx: &LateContext<'_>, _: &Crate<'_>) { + fn check_crate(&mut self, cx: &LateContext<'_>) { if is_lint_allowed(cx, MULTIPLE_CRATE_VERSIONS, CRATE_HIR_ID) { return; } diff --git a/src/tools/clippy/clippy_lints/src/same_name_method.rs b/src/tools/clippy/clippy_lints/src/same_name_method.rs index 014898e6dab..737ff634e44 100644 --- a/src/tools/clippy/clippy_lints/src/same_name_method.rs +++ b/src/tools/clippy/clippy_lints/src/same_name_method.rs @@ -1,7 +1,7 @@ use clippy_utils::diagnostics::span_lint_and_then; use rustc_data_structures::fx::FxHashMap; use rustc_hir::def::{DefKind, Res}; -use rustc_hir::{Crate, Impl, ItemKind, Node, Path, QPath, TraitRef, TyKind}; +use rustc_hir::{Impl, ItemKind, Node, Path, QPath, TraitRef, TyKind}; use rustc_lint::{LateContext, LateLintPass}; use rustc_middle::ty::AssocKind; use rustc_session::{declare_lint_pass, declare_tool_lint}; @@ -46,10 +46,10 @@ struct ExistingName { } impl<'tcx> LateLintPass<'tcx> for SameNameMethod { - fn check_crate_post(&mut self, cx: &LateContext<'tcx>, krate: &'tcx Crate<'tcx>) { + fn check_crate_post(&mut self, cx: &LateContext<'tcx>) { let mut map = FxHashMap::::default(); - for item in krate.items() { + for item in cx.tcx.hir().items() { if let ItemKind::Impl(Impl { items, of_trait, diff --git a/src/tools/clippy/clippy_lints/src/wildcard_dependencies.rs b/src/tools/clippy/clippy_lints/src/wildcard_dependencies.rs index fd3872bacbe..d0c98b6bd79 100644 --- a/src/tools/clippy/clippy_lints/src/wildcard_dependencies.rs +++ b/src/tools/clippy/clippy_lints/src/wildcard_dependencies.rs @@ -1,5 +1,5 @@ use clippy_utils::{diagnostics::span_lint, is_lint_allowed}; -use rustc_hir::{hir_id::CRATE_HIR_ID, Crate}; +use rustc_hir::hir_id::CRATE_HIR_ID; use rustc_lint::{LateContext, LateLintPass}; use rustc_session::{declare_lint_pass, declare_tool_lint}; use rustc_span::source_map::DUMMY_SP; @@ -28,7 +28,7 @@ declare_clippy_lint! { declare_lint_pass!(WildcardDependencies => [WILDCARD_DEPENDENCIES]); impl LateLintPass<'_> for WildcardDependencies { - fn check_crate(&mut self, cx: &LateContext<'_>, _: &Crate<'_>) { + fn check_crate(&mut self, cx: &LateContext<'_>) { if is_lint_allowed(cx, WILDCARD_DEPENDENCIES, CRATE_HIR_ID) { return; } From 659b0a2fd94440c9429fff6ec9967990b219caf0 Mon Sep 17 00:00:00 2001 From: Camille GILLOT Date: Sun, 12 Sep 2021 13:19:04 +0200 Subject: [PATCH 4/5] Bless tests. --- .../run-make-fulldeps/obtain-borrowck/driver.rs | 5 +---- .../ui-fulldeps/auxiliary/lint-for-crate-rpass.rs | 6 ++++-- src/test/ui-fulldeps/auxiliary/lint-for-crate.rs | 13 ++++++------- src/test/ui/privacy/privacy2.stderr | 8 +++++++- src/test/ui/privacy/privacy3.stderr | 8 +++++++- 5 files changed, 25 insertions(+), 15 deletions(-) diff --git a/src/test/run-make-fulldeps/obtain-borrowck/driver.rs b/src/test/run-make-fulldeps/obtain-borrowck/driver.rs index 961ea518c13..a288b90d792 100644 --- a/src/test/run-make-fulldeps/obtain-borrowck/driver.rs +++ b/src/test/run-make-fulldeps/obtain-borrowck/driver.rs @@ -48,7 +48,6 @@ fn main() { pub struct CompilerCalls; impl rustc_driver::Callbacks for CompilerCalls { - // In this callback we override the mir_borrowck query. fn config(&mut self, config: &mut Config) { assert!(config.override_queries.is_none()); @@ -64,12 +63,10 @@ impl rustc_driver::Callbacks for CompilerCalls { ) -> Compilation { compiler.session().abort_if_errors(); queries.global_ctxt().unwrap().peek_mut().enter(|tcx| { - // Collect definition ids of MIR bodies. let hir = tcx.hir(); - let krate = hir.krate(); let mut visitor = HirVisitor { bodies: Vec::new() }; - krate.visit_all_item_likes(&mut visitor); + hir.visit_all_item_likes(&mut visitor); // Trigger borrow checking of all bodies. for def_id in visitor.bodies { diff --git a/src/test/ui-fulldeps/auxiliary/lint-for-crate-rpass.rs b/src/test/ui-fulldeps/auxiliary/lint-for-crate-rpass.rs index fc57c14ec32..e726f8402ef 100644 --- a/src/test/ui-fulldeps/auxiliary/lint-for-crate-rpass.rs +++ b/src/test/ui-fulldeps/auxiliary/lint-for-crate-rpass.rs @@ -13,6 +13,7 @@ extern crate rustc_ast; use rustc_ast::attr; use rustc_driver::plugin::Registry; use rustc_lint::{LateContext, LateLintPass, LintContext, LintPass}; +use rustc_span::def_id::CRATE_DEF_ID; use rustc_span::symbol::Symbol; macro_rules! fake_lint_pass { @@ -26,13 +27,14 @@ macro_rules! fake_lint_pass { } impl LateLintPass<'_> for $struct { - fn check_crate(&mut self, cx: &LateContext, krate: &rustc_hir::Crate) { + fn check_crate(&mut self, cx: &LateContext) { let attrs = cx.tcx.hir().attrs(rustc_hir::CRATE_HIR_ID); + let span = cx.tcx.def_span(CRATE_DEF_ID); $( if !cx.sess().contains_name(attrs, $attr) { cx.lint(CRATE_NOT_OKAY, |lint| { let msg = format!("crate is not marked with #![{}]", $attr); - lint.build(&msg).set_span(krate.module().inner).emit() + lint.build(&msg).set_span(span).emit() }); } )* diff --git a/src/test/ui-fulldeps/auxiliary/lint-for-crate.rs b/src/test/ui-fulldeps/auxiliary/lint-for-crate.rs index 78c6c7ed887..4bbed502980 100644 --- a/src/test/ui-fulldeps/auxiliary/lint-for-crate.rs +++ b/src/test/ui-fulldeps/auxiliary/lint-for-crate.rs @@ -8,13 +8,13 @@ extern crate rustc_hir; extern crate rustc_lint; #[macro_use] extern crate rustc_session; -extern crate rustc_span; extern crate rustc_ast; +extern crate rustc_span; use rustc_driver::plugin::Registry; -use rustc_lint::{LateContext, LateLintPass, LintArray, LintContext, LintPass}; +use rustc_lint::{LateContext, LateLintPass, LintContext}; +use rustc_span::def_id::CRATE_DEF_ID; use rustc_span::symbol::Symbol; -use rustc_ast::attr; declare_lint! { CRATE_NOT_OKAY, @@ -25,13 +25,12 @@ declare_lint! { declare_lint_pass!(Pass => [CRATE_NOT_OKAY]); impl<'tcx> LateLintPass<'tcx> for Pass { - fn check_crate(&mut self, cx: &LateContext, krate: &rustc_hir::Crate) { + fn check_crate(&mut self, cx: &LateContext) { let attrs = cx.tcx.hir().attrs(rustc_hir::CRATE_HIR_ID); + let span = cx.tcx.def_span(CRATE_DEF_ID); if !cx.sess().contains_name(attrs, Symbol::intern("crate_okay")) { cx.lint(CRATE_NOT_OKAY, |lint| { - lint.build("crate is not marked with #![crate_okay]") - .set_span(krate.module().inner) - .emit() + lint.build("crate is not marked with #![crate_okay]").set_span(span).emit() }); } } diff --git a/src/test/ui/privacy/privacy2.stderr b/src/test/ui/privacy/privacy2.stderr index c2a33ce1f59..882f314655d 100644 --- a/src/test/ui/privacy/privacy2.stderr +++ b/src/test/ui/privacy/privacy2.stderr @@ -23,7 +23,13 @@ LL | pub fn foo() {} error: requires `sized` lang_item -error: aborting due to 3 previous errors +error: requires `sized` lang_item + +error: requires `sized` lang_item + +error: requires `sized` lang_item + +error: aborting due to 6 previous errors Some errors have detailed explanations: E0432, E0603. For more information about an error, try `rustc --explain E0432`. diff --git a/src/test/ui/privacy/privacy3.stderr b/src/test/ui/privacy/privacy3.stderr index 22c1e48b07d..42ce456d962 100644 --- a/src/test/ui/privacy/privacy3.stderr +++ b/src/test/ui/privacy/privacy3.stderr @@ -6,6 +6,12 @@ LL | use bar::gpriv; error: requires `sized` lang_item -error: aborting due to 2 previous errors +error: requires `sized` lang_item + +error: requires `sized` lang_item + +error: requires `sized` lang_item + +error: aborting due to 5 previous errors For more information about this error, try `rustc --explain E0432`. From 77c300285c0528241b2b08823167598739fc0f82 Mon Sep 17 00:00:00 2001 From: Camille Gillot Date: Mon, 13 Sep 2021 20:43:43 +0200 Subject: [PATCH 5/5] Apply suggestions from code review Co-authored-by: Oli Scherer --- compiler/rustc_hir/src/hir.rs | 2 +- compiler/rustc_middle/src/hir/map/mod.rs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/compiler/rustc_hir/src/hir.rs b/compiler/rustc_hir/src/hir.rs index 21089b0208d..4ef3a699d3d 100644 --- a/compiler/rustc_hir/src/hir.rs +++ b/compiler/rustc_hir/src/hir.rs @@ -671,7 +671,7 @@ pub struct Crate<'hir> { pub attrs: BTreeMap, } -impl<'hir> Crate<'hir> { +impl Crate<'hir> { pub fn module(&self) -> &'hir Mod<'hir> { if let Some(OwnerNode::Crate(m)) = self.owners[CRATE_DEF_ID] { m } else { panic!() } } diff --git a/compiler/rustc_middle/src/hir/map/mod.rs b/compiler/rustc_middle/src/hir/map/mod.rs index 3989a94a47c..e6f56b0be93 100644 --- a/compiler/rustc_middle/src/hir/map/mod.rs +++ b/compiler/rustc_middle/src/hir/map/mod.rs @@ -160,7 +160,7 @@ impl<'hir> Map<'hir> { pub fn root_module(&self) -> &'hir Mod<'hir> { match self.tcx.hir_owner(CRATE_DEF_ID).map(|o| o.node) { Some(OwnerNode::Crate(item)) => item, - _ => panic!(), + _ => bug!(), } }