Skip to content

Commit

Permalink
Rollup merge of #73090 - marmeladema:resolver-outputs-local-def-id, r…
Browse files Browse the repository at this point in the history
…=petrochenkov

Use `LocalDefId` directly in `Resolver::export_map`

This is to avoid the final conversion from `NodeId` to `HirId`
during call to `(clone|into)_outputs`

This brings down the post-lowering uses of `NodeId` down to 2 calls to convert the `trait_map`.

cc #50928

r? @petrochenkov
  • Loading branch information
Dylan-DPC authored Jun 8, 2020
2 parents 033f689 + e759222 commit 845b869
Show file tree
Hide file tree
Showing 7 changed files with 27 additions and 37 deletions.
13 changes: 11 additions & 2 deletions src/librustc_metadata/rmeta/encoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -693,16 +693,25 @@ impl EncodeContext<'tcx> {
vis: &hir::Visibility<'_>,
) {
let tcx = self.tcx;
let def_id = tcx.hir().local_def_id(id).to_def_id();
let def_id = tcx.hir().local_def_id(id);
debug!("EncodeContext::encode_info_for_mod({:?})", def_id);

let data = ModData {
reexports: match tcx.module_exports(def_id) {
Some(exports) => self.lazy(exports),
Some(exports) => {
let hir_map = self.tcx.hir();
self.lazy(
exports
.iter()
.map(|export| export.map_id(|id| hir_map.as_local_hir_id(id))),
)
}
_ => Lazy::empty(),
},
};

let def_id = def_id.to_def_id();

record!(self.tables.kind[def_id] <- EntryKind::Mod(self.lazy(data)));
record!(self.tables.visibility[def_id] <- ty::Visibility::from_hir(vis, id, self.tcx));
record!(self.tables.span[def_id] <- self.tcx.def_span(def_id));
Expand Down
5 changes: 3 additions & 2 deletions src/librustc_middle/hir/exports.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
use crate::ty;

use rustc_data_structures::fx::FxHashMap;
use rustc_hir::def::Res;
use rustc_hir::def_id::DefIdMap;
use rustc_hir::def_id::LocalDefId;
use rustc_macros::HashStable;
use rustc_span::symbol::Ident;
use rustc_span::Span;
Expand All @@ -10,7 +11,7 @@ use std::fmt::Debug;

/// This is the replacement export map. It maps a module to all of the exports
/// within.
pub type ExportMap<Id> = DefIdMap<Vec<Export<Id>>>;
pub type ExportMap<Id> = FxHashMap<LocalDefId, Vec<Export<Id>>>;

#[derive(Copy, Clone, Debug, RustcEncodable, RustcDecodable, HashStable)]
pub struct Export<Id> {
Expand Down
4 changes: 2 additions & 2 deletions src/librustc_middle/query/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -865,8 +865,8 @@ rustc_queries! {
}

Other {
query module_exports(def_id: DefId) -> Option<&'tcx [Export<hir::HirId>]> {
desc { |tcx| "looking up items exported by `{}`", tcx.def_path_str(def_id) }
query module_exports(def_id: LocalDefId) -> Option<&'tcx [Export<LocalDefId>]> {
desc { |tcx| "looking up items exported by `{}`", tcx.def_path_str(def_id.to_def_id()) }
eval_always
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/librustc_middle/ty/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

use crate::arena::Arena;
use crate::dep_graph::{self, DepConstructor, DepGraph};
use crate::hir::exports::Export;
use crate::hir::exports::ExportMap;
use crate::ich::{NodeIdHashingMode, StableHashingContext};
use crate::infer::canonical::{Canonical, CanonicalVarInfo, CanonicalVarInfos};
use crate::lint::{struct_lint_level, LintDiagnosticBuilder, LintSource};
Expand Down Expand Up @@ -919,7 +919,7 @@ pub struct GlobalCtxt<'tcx> {
trait_map: FxHashMap<LocalDefId, FxHashMap<ItemLocalId, StableVec<TraitCandidate>>>,

/// Export map produced by name resolution.
export_map: FxHashMap<DefId, Vec<Export<hir::HirId>>>,
export_map: ExportMap<LocalDefId>,

pub(crate) untracked_crate: &'tcx hir::Crate<'tcx>,
pub(crate) definitions: &'tcx Definitions,
Expand Down
2 changes: 1 addition & 1 deletion src/librustc_middle/ty/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ pub struct ResolverOutputs {
pub trait_map: FxHashMap<hir::HirId, Vec<hir::TraitCandidate<hir::HirId>>>,
pub maybe_unused_trait_imports: FxHashSet<LocalDefId>,
pub maybe_unused_extern_crates: Vec<(LocalDefId, Span)>,
pub export_map: ExportMap<hir::HirId>,
pub export_map: ExportMap<LocalDefId>,
pub glob_map: FxHashMap<LocalDefId, FxHashSet<Symbol>>,
/// Extern prelude entries. The value is `true` if the entry was introduced
/// via `extern crate` item and not `--extern` option or compiler built-in.
Expand Down
8 changes: 5 additions & 3 deletions src/librustc_resolve/imports.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1393,8 +1393,8 @@ impl<'a, 'b> ImportResolver<'a, 'b> {
let is_good_import =
binding.is_import() && !binding.is_ambiguity() && !ident.span.from_expansion();
if is_good_import || binding.is_macro_def() {
let res = binding.res();
if res != Res::Err {
let res = binding.res().map_id(|id| this.definitions.local_def_id(id));
if res != def::Res::Err {
reexports.push(Export { ident, res, span: binding.span, vis: binding.vis });
}
}
Expand Down Expand Up @@ -1467,7 +1467,9 @@ impl<'a, 'b> ImportResolver<'a, 'b> {

if !reexports.is_empty() {
if let Some(def_id) = module.def_id() {
self.r.export_map.insert(def_id, reexports);
// Call to `expect_local` should be fine because current
// code is only called for local modules.
self.r.export_map.insert(def_id.expect_local(), reexports);
}
}
}
Expand Down
28 changes: 3 additions & 25 deletions src/librustc_resolve/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -878,7 +878,7 @@ pub struct Resolver<'a> {

/// `CrateNum` resolutions of `extern crate` items.
extern_crate_map: FxHashMap<LocalDefId, CrateNum>,
export_map: ExportMap<NodeId>,
export_map: ExportMap<LocalDefId>,
trait_map: TraitMap<NodeId>,

/// A map from nodes to anonymous modules.
Expand Down Expand Up @@ -1281,18 +1281,7 @@ impl<'a> Resolver<'a> {
pub fn into_outputs(self) -> ResolverOutputs {
let definitions = self.definitions;
let extern_crate_map = self.extern_crate_map;
let export_map = self
.export_map
.into_iter()
.map(|(k, v)| {
(
k,
v.into_iter()
.map(|e| e.map_id(|id| definitions.node_id_to_hir_id(id)))
.collect(),
)
})
.collect();
let export_map = self.export_map;
let trait_map = self
.trait_map
.into_iter()
Expand Down Expand Up @@ -1330,18 +1319,7 @@ impl<'a> Resolver<'a> {
definitions: self.definitions.clone(),
cstore: Box::new(self.cstore().clone()),
extern_crate_map: self.extern_crate_map.clone(),
export_map: self
.export_map
.iter()
.map(|(&k, v)| {
(
k,
v.iter()
.map(|e| e.map_id(|id| self.definitions.node_id_to_hir_id(id)))
.collect(),
)
})
.collect(),
export_map: self.export_map.clone(),
trait_map: self
.trait_map
.iter()
Expand Down

0 comments on commit 845b869

Please sign in to comment.