Rollup merge of #72636 - marmeladema:resolver-outputs-def-id, r=petrochenkov

Cleanup `Resolver::<clone|into>_outputs` methods

Follow-up cleanup work of https://github.com/rust-lang/rust/pull/72402

First commit has been split out from https://github.com/rust-lang/rust/pull/72552

r? @ecstatic-morse
This commit is contained in:
Dylan DPC 2020-05-29 02:33:16 +02:00 committed by GitHub
commit 48ea37b82e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
10 changed files with 44 additions and 79 deletions

View File

@ -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<ID> TraitCandidate<ID> {
// Trait method resolution
pub type TraitMap<ID = HirId> = NodeMap<Vec<TraitCandidate<ID>>>;
// Map from the NodeId of a glob import to a list of items which are actually
// imported.
pub type GlobMap = NodeMap<FxHashSet<Symbol>>;
#[derive(Copy, Clone, Debug, HashStable_Generic)]
pub enum Node<'hir> {
Param(&'hir Param<'hir>),

View File

@ -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<NodeId> {
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)

View File

@ -973,7 +973,9 @@ rustc_queries! {
desc { "fetching what a crate is named" }
}
query item_children(_: DefId) -> &'tcx [Export<hir::HirId>] {}
query extern_mod_stmt_cnum(_: DefId) -> Option<CrateNum> {}
query extern_mod_stmt_cnum(_: LocalDefId) -> Option<CrateNum> {
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" }
}

View File

@ -925,7 +925,7 @@ pub struct GlobalCtxt<'tcx> {
pub consts: CommonConsts<'tcx>,
/// Resolutions of `extern crate` items produced by resolver.
extern_crate_map: FxHashMap<DefId, CrateNum>,
extern_crate_map: FxHashMap<LocalDefId, CrateNum>,
/// 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<LocalDefId>,
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<LocalDefId, FxHashSet<Symbol>>,

View File

@ -120,10 +120,10 @@ mod sty;
pub struct ResolverOutputs {
pub definitions: rustc_hir::definitions::Definitions,
pub cstore: Box<CrateStoreDyn>,
pub extern_crate_map: FxHashMap<DefId, CrateNum>,
pub extern_crate_map: FxHashMap<LocalDefId, CrateNum>,
pub trait_map: FxHashMap<hir::HirId, Vec<hir::TraitCandidate<hir::HirId>>>,
pub maybe_unused_trait_imports: FxHashSet<LocalDefId>,
pub maybe_unused_extern_crates: Vec<(DefId, Span)>,
pub maybe_unused_extern_crates: Vec<(LocalDefId, Span)>,
pub export_map: ExportMap<hir::HirId>,
pub glob_map: FxHashMap<LocalDefId, FxHashSet<Symbol>>,
/// Extern prelude entries. The value is `true` if the entry was introduced

View File

@ -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();

View File

@ -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";

View File

@ -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;

View File

@ -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<NodeId>,
/// `CrateNum` resolutions of `extern crate` items.
extern_crate_map: NodeMap<CrateNum>,
extern_crate_map: FxHashMap<LocalDefId, CrateNum>,
export_map: ExportMap<NodeId>,
trait_map: TraitMap<NodeId>,
@ -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<LocalDefId, FxHashSet<Symbol>>,
used_imports: FxHashSet<(NodeId, Namespace)>,
maybe_unused_trait_imports: NodeSet,
maybe_unused_extern_crates: Vec<(NodeId, Span)>,
maybe_unused_trait_imports: FxHashSet<LocalDefId>,
maybe_unused_extern_crates: Vec<(LocalDefId, Span)>,
/// Privacy errors are delayed until the end in order to deduplicate them.
privacy_errors: Vec<PrivacyError<'a>>,
@ -924,7 +924,7 @@ pub struct Resolver<'a> {
dummy_ext_bang: Lrc<SyntaxExtension>,
dummy_ext_derive: Lrc<SyntaxExtension>,
non_macro_attrs: [Lrc<SyntaxExtension>; 2],
local_macro_def_scopes: FxHashMap<NodeId, Module<'a>>,
local_macro_def_scopes: FxHashMap<LocalDefId, Module<'a>>,
ast_transform_scopes: FxHashMap<ExpnId, Module<'a>>,
unused_macros: NodeMap<Span>,
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);
}
}

View File

@ -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<DefId, Span> = tcx
let unused_extern_crates: FxHashMap<LocalDefId, Span> = 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