From 81f8ee458b10762e3c4b3389d373a5a26c1d8fb7 Mon Sep 17 00:00:00 2001 From: marmeladema Date: Sun, 24 May 2020 12:18:22 +0100 Subject: [PATCH] Store `LocalDefId` directly in `rustc_resolve::Resolver` where possible This commit also include the following changes: * Remove unused `hir::Map::as_local_node_id` method * Remove outdated comment about `hir::Map::local_def_id` method * Remove confusing `GlobMap` type alias * Use `LocalDefId` instead of `DefId` in `extern_crate_map` * Use `LocalDefId` instead of `DefId` in `maybe_unused_extern_crates` * Modify `extern_mod_stmt_cnum` query to accept a `LocalDefId` instead of a `DefId` --- src/librustc_hir/hir.rs | 5 -- src/librustc_middle/hir/map/mod.rs | 6 -- src/librustc_middle/query/mod.rs | 6 +- src/librustc_middle/ty/context.rs | 4 +- src/librustc_middle/ty/mod.rs | 4 +- src/librustc_resolve/build_reduced_graph.rs | 13 +++-- src/librustc_resolve/check_unused.rs | 8 ++- src/librustc_resolve/late.rs | 3 +- src/librustc_resolve/lib.rs | 63 ++++++--------------- src/librustc_typeck/check_unused.rs | 11 ++-- 10 files changed, 44 insertions(+), 79 deletions(-) diff --git a/src/librustc_hir/hir.rs b/src/librustc_hir/hir.rs index ef398ab25d3fb..35cff668581dd 100644 --- a/src/librustc_hir/hir.rs +++ b/src/librustc_hir/hir.rs @@ -10,7 +10,6 @@ pub use rustc_ast::ast::{CaptureBy, Movability, Mutability}; use rustc_ast::ast::{InlineAsmOptions, InlineAsmTemplatePiece}; use rustc_ast::node_id::NodeMap; use rustc_ast::util::parser::ExprPrecedence; -use rustc_data_structures::fx::FxHashSet; use rustc_data_structures::sync::{par_for_each_in, Send, Sync}; use rustc_macros::HashStable_Generic; use rustc_span::source_map::{SourceMap, Spanned}; @@ -2664,10 +2663,6 @@ impl TraitCandidate { // Trait method resolution pub type TraitMap = NodeMap>>; -// Map from the NodeId of a glob import to a list of items which are actually -// imported. -pub type GlobMap = NodeMap>; - #[derive(Copy, Clone, Debug, HashStable_Generic)] pub enum Node<'hir> { Param(&'hir Param<'hir>), diff --git a/src/librustc_middle/hir/map/mod.rs b/src/librustc_middle/hir/map/mod.rs index b823516d64f3b..1e27f1549112a 100644 --- a/src/librustc_middle/hir/map/mod.rs +++ b/src/librustc_middle/hir/map/mod.rs @@ -169,7 +169,6 @@ impl<'hir> Map<'hir> { }) } - // FIXME(eddyb) this function can and should return `LocalDefId`. #[inline] pub fn local_def_id(&self, hir_id: HirId) -> LocalDefId { self.opt_local_def_id(hir_id).unwrap_or_else(|| { @@ -192,11 +191,6 @@ impl<'hir> Map<'hir> { self.tcx.definitions.opt_local_def_id(node) } - #[inline] - pub fn as_local_node_id(&self, def_id: DefId) -> Option { - self.tcx.definitions.as_local_node_id(def_id) - } - #[inline] pub fn as_local_hir_id(&self, def_id: LocalDefId) -> HirId { self.tcx.definitions.as_local_hir_id(def_id) diff --git a/src/librustc_middle/query/mod.rs b/src/librustc_middle/query/mod.rs index 1083563c647b6..85451bf6538e4 100644 --- a/src/librustc_middle/query/mod.rs +++ b/src/librustc_middle/query/mod.rs @@ -973,7 +973,9 @@ rustc_queries! { desc { "fetching what a crate is named" } } query item_children(_: DefId) -> &'tcx [Export] {} - query extern_mod_stmt_cnum(_: DefId) -> Option {} + query extern_mod_stmt_cnum(_: LocalDefId) -> Option { + desc { "fetching extern module statement" } + } query get_lib_features(_: CrateNum) -> LibFeatures { storage(ArenaCacheSelector<'tcx>) @@ -1040,7 +1042,7 @@ rustc_queries! { desc { |tcx| "maybe_unused_trait_import for `{}`", tcx.def_path_str(def_id.to_def_id()) } } query maybe_unused_extern_crates(_: CrateNum) - -> &'tcx [(DefId, Span)] { + -> &'tcx [(LocalDefId, Span)] { eval_always desc { "looking up all possibly unused extern crates" } } diff --git a/src/librustc_middle/ty/context.rs b/src/librustc_middle/ty/context.rs index 5b53ab1778e3f..7a20014484190 100644 --- a/src/librustc_middle/ty/context.rs +++ b/src/librustc_middle/ty/context.rs @@ -925,7 +925,7 @@ pub struct GlobalCtxt<'tcx> { pub consts: CommonConsts<'tcx>, /// Resolutions of `extern crate` items produced by resolver. - extern_crate_map: FxHashMap, + extern_crate_map: FxHashMap, /// Map indicating what traits are in scope for places where this /// is relevant; generated by resolve. @@ -944,7 +944,7 @@ pub struct GlobalCtxt<'tcx> { pub queries: query::Queries<'tcx>, maybe_unused_trait_imports: FxHashSet, - maybe_unused_extern_crates: Vec<(DefId, Span)>, + maybe_unused_extern_crates: Vec<(LocalDefId, Span)>, /// A map of glob use to a set of names it actually imports. Currently only /// used in save-analysis. glob_map: FxHashMap>, diff --git a/src/librustc_middle/ty/mod.rs b/src/librustc_middle/ty/mod.rs index aad3c6889c3ce..85b5759e9936d 100644 --- a/src/librustc_middle/ty/mod.rs +++ b/src/librustc_middle/ty/mod.rs @@ -120,10 +120,10 @@ mod sty; pub struct ResolverOutputs { pub definitions: rustc_hir::definitions::Definitions, pub cstore: Box, - pub extern_crate_map: FxHashMap, + pub extern_crate_map: FxHashMap, pub trait_map: FxHashMap>>, pub maybe_unused_trait_imports: FxHashSet, - pub maybe_unused_extern_crates: Vec<(DefId, Span)>, + pub maybe_unused_extern_crates: Vec<(LocalDefId, Span)>, pub export_map: ExportMap, pub glob_map: FxHashMap>, /// Extern prelude entries. The value is `true` if the entry was introduced diff --git a/src/librustc_resolve/build_reduced_graph.rs b/src/librustc_resolve/build_reduced_graph.rs index c32b823fe73b2..92ea119d9a458 100644 --- a/src/librustc_resolve/build_reduced_graph.rs +++ b/src/librustc_resolve/build_reduced_graph.rs @@ -130,7 +130,7 @@ impl<'a> Resolver<'a> { Some(def_id) => def_id, None => return self.ast_transform_scopes.get(&expn_id).unwrap_or(&self.graph_root), }; - if let Some(id) = self.definitions.as_local_node_id(def_id) { + if let Some(id) = def_id.as_local() { self.local_macro_def_scopes[&id] } else { let module_def_id = ty::DefIdTree::parent(&*self, def_id).unwrap(); @@ -640,9 +640,10 @@ impl<'a, 'b> BuildReducedGraphVisitor<'a, 'b> { } else if orig_name == Some(kw::SelfLower) { self.r.graph_root } else { + let def_id = self.r.definitions.local_def_id(item.id); let crate_id = self.r.crate_loader.process_extern_crate(item, &self.r.definitions); - self.r.extern_crate_map.insert(item.id, crate_id); + self.r.extern_crate_map.insert(def_id, crate_id); self.r.get_module(DefId { krate: crate_id, index: CRATE_DEF_INDEX }) }; @@ -1173,10 +1174,10 @@ impl<'a, 'b> BuildReducedGraphVisitor<'a, 'b> { _ => unreachable!(), }; - let def_id = self.r.definitions.local_def_id(item.id).to_def_id(); - let res = Res::Def(DefKind::Macro(ext.macro_kind()), def_id); - self.r.macro_map.insert(def_id, ext); - self.r.local_macro_def_scopes.insert(item.id, parent_scope.module); + let def_id = self.r.definitions.local_def_id(item.id); + let res = Res::Def(DefKind::Macro(ext.macro_kind()), def_id.to_def_id()); + self.r.macro_map.insert(def_id.to_def_id(), ext); + self.r.local_macro_def_scopes.insert(def_id, parent_scope.module); if macro_rules { let ident = ident.normalize_to_macros_2_0(); diff --git a/src/librustc_resolve/check_unused.rs b/src/librustc_resolve/check_unused.rs index dd286723412dd..cc0e97aeb1430 100644 --- a/src/librustc_resolve/check_unused.rs +++ b/src/librustc_resolve/check_unused.rs @@ -64,8 +64,9 @@ impl<'a, 'b> UnusedImportCheckVisitor<'a, 'b> { fn check_import(&mut self, id: ast::NodeId) { let mut used = false; self.r.per_ns(|this, ns| used |= this.used_imports.contains(&(id, ns))); + let def_id = self.r.definitions.local_def_id(id); if !used { - if self.r.maybe_unused_trait_imports.contains(&id) { + if self.r.maybe_unused_trait_imports.contains(&def_id) { // Check later. return; } @@ -73,7 +74,7 @@ impl<'a, 'b> UnusedImportCheckVisitor<'a, 'b> { } else { // This trait import is definitely used, in a way other than // method resolution. - self.r.maybe_unused_trait_imports.remove(&id); + self.r.maybe_unused_trait_imports.remove(&def_id); if let Some(i) = self.unused_imports.get_mut(&self.base_id) { i.unused.remove(&id); } @@ -245,7 +246,8 @@ impl Resolver<'_> { } } ImportKind::ExternCrate { .. } => { - self.maybe_unused_extern_crates.push((import.id, import.span)); + let def_id = self.definitions.local_def_id(import.id); + self.maybe_unused_extern_crates.push((def_id, import.span)); } ImportKind::MacroUse => { let msg = "unused `#[macro_use]` import"; diff --git a/src/librustc_resolve/late.rs b/src/librustc_resolve/late.rs index 477e3be5cc2f8..f04813cf3bc7f 100644 --- a/src/librustc_resolve/late.rs +++ b/src/librustc_resolve/late.rs @@ -2209,7 +2209,8 @@ impl<'a, 'b, 'ast> LateResolutionVisitor<'a, 'b, 'ast> { ) -> SmallVec<[NodeId; 1]> { let mut import_ids = smallvec![]; while let NameBindingKind::Import { import, binding, .. } = kind { - self.r.maybe_unused_trait_imports.insert(import.id); + let id = self.r.definitions.local_def_id(import.id); + self.r.maybe_unused_trait_imports.insert(id); self.r.add_to_glob_map(&import, trait_name); import_ids.push(import.id); kind = &binding.kind; diff --git a/src/librustc_resolve/lib.rs b/src/librustc_resolve/lib.rs index 63a4cdfbf2928..015f1b6315c19 100644 --- a/src/librustc_resolve/lib.rs +++ b/src/librustc_resolve/lib.rs @@ -37,7 +37,7 @@ use rustc_hir::def::{self, CtorOf, DefKind, NonMacroAttrKind, PartialRes}; use rustc_hir::def_id::{CrateNum, DefId, DefIdMap, LocalDefId, CRATE_DEF_INDEX}; use rustc_hir::definitions::{DefKey, Definitions}; use rustc_hir::PrimTy::{self, Bool, Char, Float, Int, Str, Uint}; -use rustc_hir::{GlobMap, TraitMap}; +use rustc_hir::TraitMap; use rustc_metadata::creader::{CStore, CrateLoader}; use rustc_middle::hir::exports::ExportMap; use rustc_middle::middle::cstore::{CrateStore, MetadataLoaderDyn}; @@ -866,7 +866,7 @@ pub struct Resolver<'a> { label_res_map: NodeMap, /// `CrateNum` resolutions of `extern crate` items. - extern_crate_map: NodeMap, + extern_crate_map: FxHashMap, export_map: ExportMap, trait_map: TraitMap, @@ -895,11 +895,11 @@ pub struct Resolver<'a> { underscore_disambiguator: u32, /// Maps glob imports to the names of items actually imported. - glob_map: GlobMap, + glob_map: FxHashMap>, used_imports: FxHashSet<(NodeId, Namespace)>, - maybe_unused_trait_imports: NodeSet, - maybe_unused_extern_crates: Vec<(NodeId, Span)>, + maybe_unused_trait_imports: FxHashSet, + maybe_unused_extern_crates: Vec<(LocalDefId, Span)>, /// Privacy errors are delayed until the end in order to deduplicate them. privacy_errors: Vec>, @@ -924,7 +924,7 @@ pub struct Resolver<'a> { dummy_ext_bang: Lrc, dummy_ext_derive: Lrc, non_macro_attrs: [Lrc; 2], - local_macro_def_scopes: FxHashMap>, + local_macro_def_scopes: FxHashMap>, ast_transform_scopes: FxHashMap>, unused_macros: NodeMap, proc_macro_stubs: NodeSet, @@ -1269,11 +1269,7 @@ impl<'a> Resolver<'a> { pub fn into_outputs(self) -> ResolverOutputs { let definitions = self.definitions; - let extern_crate_map = self - .extern_crate_map - .into_iter() - .map(|(k, v)| (definitions.local_def_id(k).to_def_id(), v)) - .collect(); + let extern_crate_map = self.extern_crate_map; let export_map = self .export_map .into_iter() @@ -1298,21 +1294,9 @@ impl<'a> Resolver<'a> { ) }) .collect(); - let maybe_unused_trait_imports = self - .maybe_unused_trait_imports - .into_iter() - .map(|id| definitions.local_def_id(id)) - .collect(); - let maybe_unused_extern_crates = self - .maybe_unused_extern_crates - .into_iter() - .map(|(id, sp)| (definitions.local_def_id(id).to_def_id(), sp)) - .collect(); - let glob_map = self - .glob_map - .into_iter() - .map(|(id, names)| (definitions.local_def_id(id), names)) - .collect(); + let maybe_unused_trait_imports = self.maybe_unused_trait_imports; + let maybe_unused_extern_crates = self.maybe_unused_extern_crates; + let glob_map = self.glob_map; ResolverOutputs { definitions: definitions, cstore: Box::new(self.crate_loader.into_cstore()), @@ -1334,11 +1318,7 @@ impl<'a> Resolver<'a> { ResolverOutputs { definitions: self.definitions.clone(), cstore: Box::new(self.cstore().clone()), - extern_crate_map: self - .extern_crate_map - .iter() - .map(|(&k, &v)| (self.definitions.local_def_id(k).to_def_id(), v)) - .collect(), + extern_crate_map: self.extern_crate_map.clone(), export_map: self .export_map .iter() @@ -1366,21 +1346,9 @@ impl<'a> Resolver<'a> { ) }) .collect(), - glob_map: self - .glob_map - .iter() - .map(|(&id, names)| (self.definitions.local_def_id(id), names.clone())) - .collect(), - maybe_unused_trait_imports: self - .maybe_unused_trait_imports - .iter() - .map(|&id| self.definitions.local_def_id(id)) - .collect(), - maybe_unused_extern_crates: self - .maybe_unused_extern_crates - .iter() - .map(|&(id, sp)| (self.definitions.local_def_id(id).to_def_id(), sp)) - .collect(), + glob_map: self.glob_map.clone(), + maybe_unused_trait_imports: self.maybe_unused_trait_imports.clone(), + maybe_unused_extern_crates: self.maybe_unused_extern_crates.clone(), extern_prelude: self .extern_prelude .iter() @@ -1522,7 +1490,8 @@ impl<'a> Resolver<'a> { #[inline] fn add_to_glob_map(&mut self, import: &Import<'_>, ident: Ident) { if import.is_glob() { - self.glob_map.entry(import.id).or_default().insert(ident.name); + let def_id = self.definitions.local_def_id(import.id); + self.glob_map.entry(def_id).or_default().insert(ident.name); } } diff --git a/src/librustc_typeck/check_unused.rs b/src/librustc_typeck/check_unused.rs index dfe86aecbf727..eaaff70472bfb 100644 --- a/src/librustc_typeck/check_unused.rs +++ b/src/librustc_typeck/check_unused.rs @@ -1,7 +1,7 @@ use rustc_data_structures::fx::FxHashMap; use rustc_errors::Applicability; use rustc_hir as hir; -use rustc_hir::def_id::{DefId, DefIdSet, LOCAL_CRATE}; +use rustc_hir::def_id::{DefId, DefIdSet, LocalDefId, LOCAL_CRATE}; use rustc_hir::itemlikevisit::ItemLikeVisitor; use rustc_middle::ty::TyCtxt; use rustc_session::lint; @@ -70,7 +70,7 @@ fn unused_crates_lint(tcx: TyCtxt<'_>) { // Collect first the crates that are completely unused. These we // can always suggest removing (no matter which edition we are // in). - let unused_extern_crates: FxHashMap = tcx + let unused_extern_crates: FxHashMap = tcx .maybe_unused_extern_crates(LOCAL_CRATE) .iter() .filter(|&&(def_id, _)| { @@ -88,7 +88,7 @@ fn unused_crates_lint(tcx: TyCtxt<'_>) { // Note that if we carry through to the `extern_mod_stmt_cnum` query // below it'll cause a panic because `def_id` is actually bogus at this // point in time otherwise. - if tcx.hir().find(tcx.hir().as_local_hir_id(def_id.expect_local())).is_none() { + if tcx.hir().find(tcx.hir().as_local_hir_id(def_id)).is_none() { return false; } true @@ -112,13 +112,14 @@ fn unused_crates_lint(tcx: TyCtxt<'_>) { }); for extern_crate in &crates_to_lint { - let id = tcx.hir().as_local_hir_id(extern_crate.def_id.expect_local()); + let def_id = extern_crate.def_id.expect_local(); + let id = tcx.hir().as_local_hir_id(def_id); let item = tcx.hir().expect_item(id); // If the crate is fully unused, we suggest removing it altogether. // We do this in any edition. if extern_crate.warn_if_unused { - if let Some(&span) = unused_extern_crates.get(&extern_crate.def_id) { + if let Some(&span) = unused_extern_crates.get(&def_id) { tcx.struct_span_lint_hir(lint, id, span, |lint| { // Removal suggestion span needs to include attributes (Issue #54400) let span_with_attrs = tcx