Skip to content

Commit f8231df

Browse files
authored
Rollup merge of rust-lang#73090 - marmeladema:resolver-outputs-local-def-id, r=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 rust-lang#50928 r? @petrochenkov
2 parents 3df9fc0 + e759222 commit f8231df

File tree

7 files changed

+27
-37
lines changed

7 files changed

+27
-37
lines changed

src/librustc_metadata/rmeta/encoder.rs

+11-2
Original file line numberDiff line numberDiff line change
@@ -693,16 +693,25 @@ impl EncodeContext<'tcx> {
693693
vis: &hir::Visibility<'_>,
694694
) {
695695
let tcx = self.tcx;
696-
let def_id = tcx.hir().local_def_id(id).to_def_id();
696+
let def_id = tcx.hir().local_def_id(id);
697697
debug!("EncodeContext::encode_info_for_mod({:?})", def_id);
698698

699699
let data = ModData {
700700
reexports: match tcx.module_exports(def_id) {
701-
Some(exports) => self.lazy(exports),
701+
Some(exports) => {
702+
let hir_map = self.tcx.hir();
703+
self.lazy(
704+
exports
705+
.iter()
706+
.map(|export| export.map_id(|id| hir_map.as_local_hir_id(id))),
707+
)
708+
}
702709
_ => Lazy::empty(),
703710
},
704711
};
705712

713+
let def_id = def_id.to_def_id();
714+
706715
record!(self.tables.kind[def_id] <- EntryKind::Mod(self.lazy(data)));
707716
record!(self.tables.visibility[def_id] <- ty::Visibility::from_hir(vis, id, self.tcx));
708717
record!(self.tables.span[def_id] <- self.tcx.def_span(def_id));

src/librustc_middle/hir/exports.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
use crate::ty;
22

3+
use rustc_data_structures::fx::FxHashMap;
34
use rustc_hir::def::Res;
4-
use rustc_hir::def_id::DefIdMap;
5+
use rustc_hir::def_id::LocalDefId;
56
use rustc_macros::HashStable;
67
use rustc_span::symbol::Ident;
78
use rustc_span::Span;
@@ -10,7 +11,7 @@ use std::fmt::Debug;
1011

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

1516
#[derive(Copy, Clone, Debug, RustcEncodable, RustcDecodable, HashStable)]
1617
pub struct Export<Id> {

src/librustc_middle/query/mod.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -865,8 +865,8 @@ rustc_queries! {
865865
}
866866

867867
Other {
868-
query module_exports(def_id: DefId) -> Option<&'tcx [Export<hir::HirId>]> {
869-
desc { |tcx| "looking up items exported by `{}`", tcx.def_path_str(def_id) }
868+
query module_exports(def_id: LocalDefId) -> Option<&'tcx [Export<LocalDefId>]> {
869+
desc { |tcx| "looking up items exported by `{}`", tcx.def_path_str(def_id.to_def_id()) }
870870
eval_always
871871
}
872872
}

src/librustc_middle/ty/context.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
33
use crate::arena::Arena;
44
use crate::dep_graph::{self, DepConstructor, DepGraph};
5-
use crate::hir::exports::Export;
5+
use crate::hir::exports::ExportMap;
66
use crate::ich::{NodeIdHashingMode, StableHashingContext};
77
use crate::infer::canonical::{Canonical, CanonicalVarInfo, CanonicalVarInfos};
88
use crate::lint::{struct_lint_level, LintDiagnosticBuilder, LintSource};
@@ -919,7 +919,7 @@ pub struct GlobalCtxt<'tcx> {
919919
trait_map: FxHashMap<LocalDefId, FxHashMap<ItemLocalId, StableVec<TraitCandidate>>>,
920920

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

924924
pub(crate) untracked_crate: &'tcx hir::Crate<'tcx>,
925925
pub(crate) definitions: &'tcx Definitions,

src/librustc_middle/ty/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ pub struct ResolverOutputs {
124124
pub trait_map: FxHashMap<hir::HirId, Vec<hir::TraitCandidate<hir::HirId>>>,
125125
pub maybe_unused_trait_imports: FxHashSet<LocalDefId>,
126126
pub maybe_unused_extern_crates: Vec<(LocalDefId, Span)>,
127-
pub export_map: ExportMap<hir::HirId>,
127+
pub export_map: ExportMap<LocalDefId>,
128128
pub glob_map: FxHashMap<LocalDefId, FxHashSet<Symbol>>,
129129
/// Extern prelude entries. The value is `true` if the entry was introduced
130130
/// via `extern crate` item and not `--extern` option or compiler built-in.

src/librustc_resolve/imports.rs

+5-3
Original file line numberDiff line numberDiff line change
@@ -1393,8 +1393,8 @@ impl<'a, 'b> ImportResolver<'a, 'b> {
13931393
let is_good_import =
13941394
binding.is_import() && !binding.is_ambiguity() && !ident.span.from_expansion();
13951395
if is_good_import || binding.is_macro_def() {
1396-
let res = binding.res();
1397-
if res != Res::Err {
1396+
let res = binding.res().map_id(|id| this.definitions.local_def_id(id));
1397+
if res != def::Res::Err {
13981398
reexports.push(Export { ident, res, span: binding.span, vis: binding.vis });
13991399
}
14001400
}
@@ -1467,7 +1467,9 @@ impl<'a, 'b> ImportResolver<'a, 'b> {
14671467

14681468
if !reexports.is_empty() {
14691469
if let Some(def_id) = module.def_id() {
1470-
self.r.export_map.insert(def_id, reexports);
1470+
// Call to `expect_local` should be fine because current
1471+
// code is only called for local modules.
1472+
self.r.export_map.insert(def_id.expect_local(), reexports);
14711473
}
14721474
}
14731475
}

src/librustc_resolve/lib.rs

+3-25
Original file line numberDiff line numberDiff line change
@@ -878,7 +878,7 @@ pub struct Resolver<'a> {
878878

879879
/// `CrateNum` resolutions of `extern crate` items.
880880
extern_crate_map: FxHashMap<LocalDefId, CrateNum>,
881-
export_map: ExportMap<NodeId>,
881+
export_map: ExportMap<LocalDefId>,
882882
trait_map: TraitMap<NodeId>,
883883

884884
/// A map from nodes to anonymous modules.
@@ -1281,18 +1281,7 @@ impl<'a> Resolver<'a> {
12811281
pub fn into_outputs(self) -> ResolverOutputs {
12821282
let definitions = self.definitions;
12831283
let extern_crate_map = self.extern_crate_map;
1284-
let export_map = self
1285-
.export_map
1286-
.into_iter()
1287-
.map(|(k, v)| {
1288-
(
1289-
k,
1290-
v.into_iter()
1291-
.map(|e| e.map_id(|id| definitions.node_id_to_hir_id(id)))
1292-
.collect(),
1293-
)
1294-
})
1295-
.collect();
1284+
let export_map = self.export_map;
12961285
let trait_map = self
12971286
.trait_map
12981287
.into_iter()
@@ -1330,18 +1319,7 @@ impl<'a> Resolver<'a> {
13301319
definitions: self.definitions.clone(),
13311320
cstore: Box::new(self.cstore().clone()),
13321321
extern_crate_map: self.extern_crate_map.clone(),
1333-
export_map: self
1334-
.export_map
1335-
.iter()
1336-
.map(|(&k, v)| {
1337-
(
1338-
k,
1339-
v.iter()
1340-
.map(|e| e.map_id(|id| self.definitions.node_id_to_hir_id(id)))
1341-
.collect(),
1342-
)
1343-
})
1344-
.collect(),
1322+
export_map: self.export_map.clone(),
13451323
trait_map: self
13461324
.trait_map
13471325
.iter()

0 commit comments

Comments
 (0)