Skip to content

Commit caaf365

Browse files
committed
rustc: Remove HirId from queries
This'll allow us to reconstruct query parameters purely from the `DepNode` they're associated with. Some queries could move straight to `HirId` but others that don't always have a correspondance between `HirId` and `DefId` moved to two-level maps where the query operates over a `DefIndex`, returning a map, which is then keyed off `ItemLocalId`. Closes #44414
1 parent ddd123e commit caaf365

File tree

16 files changed

+142
-121
lines changed

16 files changed

+142
-121
lines changed

src/librustc/dep_graph/dep_node.rs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@
6060
//! user of the `DepNode` API of having to know how to compute the expected
6161
//! fingerprint for a given set of node parameters.
6262
63-
use hir::def_id::{CrateNum, DefId};
63+
use hir::def_id::{CrateNum, DefId, DefIndex};
6464
use hir::map::DefPathHash;
6565
use hir::{HirId, ItemLocalId};
6666

@@ -528,8 +528,8 @@ define_dep_nodes!( <'tcx>
528528
[] ExternCrate(DefId),
529529
[] LintLevels,
530530
[] Specializes { impl1: DefId, impl2: DefId },
531-
[] InScopeTraits(HirId),
532-
[] ModuleExports(HirId),
531+
[] InScopeTraits(DefIndex),
532+
[] ModuleExports(DefId),
533533
[] IsSanitizerRuntime(CrateNum),
534534
[] IsProfilerRuntime(CrateNum),
535535
[] GetPanicStrategy(CrateNum),
@@ -551,15 +551,15 @@ define_dep_nodes!( <'tcx>
551551
[] NativeLibraryKind(DefId),
552552
[] LinkArgs,
553553

554-
[] NamedRegion(HirId),
555-
[] IsLateBound(HirId),
556-
[] ObjectLifetimeDefaults(HirId),
554+
[] NamedRegion(DefIndex),
555+
[] IsLateBound(DefIndex),
556+
[] ObjectLifetimeDefaults(DefIndex),
557557

558558
[] Visibility(DefId),
559559
[] DepKind(CrateNum),
560560
[] CrateName(CrateNum),
561561
[] ItemChildren(DefId),
562-
[] ExternModStmtCnum(HirId),
562+
[] ExternModStmtCnum(DefId),
563563
[] GetLangItems,
564564
[] DefinedLangItems(CrateNum),
565565
[] MissingLangItems(CrateNum),
@@ -570,8 +570,8 @@ define_dep_nodes!( <'tcx>
570570
[] UsedCrateSource(CrateNum),
571571
[] PostorderCnums,
572572

573-
[] Freevars(HirId),
574-
[] MaybeUnusedTraitImport(HirId),
573+
[] Freevars(DefId),
574+
[] MaybeUnusedTraitImport(DefId),
575575
[] MaybeUnusedExternCrates,
576576
[] StabilityIndex,
577577
[] AllCrateNums,

src/librustc/hir/def.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
// except according to those terms.
1010

1111
use hir::def_id::DefId;
12-
use util::nodemap::NodeMap;
12+
use util::nodemap::{NodeMap, DefIdMap};
1313
use syntax::ast;
1414
use syntax::ext::base::MacroKind;
1515
use syntax_pos::Span;
@@ -114,7 +114,7 @@ pub type DefMap = NodeMap<PathResolution>;
114114

115115
/// This is the replacement export map. It maps a module to all of the exports
116116
/// within.
117-
pub type ExportMap = NodeMap<Vec<Export>>;
117+
pub type ExportMap = DefIdMap<Vec<Export>>;
118118

119119
#[derive(Copy, Clone, Debug, RustcEncodable, RustcDecodable)]
120120
pub struct Export {

src/librustc/ich/hcx.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -199,20 +199,19 @@ impl<'a, 'gcx, 'tcx> HashStable<StableHashingContext<'a, 'gcx, 'tcx>> for ast::N
199199
fn hash_stable<W: StableHasherResult>(&self,
200200
hcx: &mut StableHashingContext<'a, 'gcx, 'tcx>,
201201
hasher: &mut StableHasher<W>) {
202+
let hir_id = hcx.tcx.hir.node_to_hir_id(*self);
202203
match hcx.node_id_hashing_mode {
203204
NodeIdHashingMode::Ignore => {
204205
// Most NodeIds in the HIR can be ignored, but if there is a
205206
// corresponding entry in the `trait_map` we need to hash that.
206207
// Make sure we don't ignore too much by checking that there is
207208
// no entry in a debug_assert!().
208-
let hir_id = hcx.tcx.hir.node_to_hir_id(*self);
209209
debug_assert!(hcx.tcx.in_scope_traits(hir_id).is_none());
210210
}
211211
NodeIdHashingMode::HashDefPath => {
212-
hcx.tcx.hir.definitions().node_to_hir_id(*self).hash_stable(hcx, hasher);
212+
hir_id.hash_stable(hcx, hasher);
213213
}
214214
NodeIdHashingMode::HashTraitsInScope => {
215-
let hir_id = hcx.tcx.hir.node_to_hir_id(*self);
216215
if let Some(traits) = hcx.tcx.in_scope_traits(hir_id) {
217216
// The ordering of the candidates is not fixed. So we hash
218217
// the def-ids and then sort them and hash the collection.

src/librustc/middle/stability.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -612,8 +612,8 @@ impl<'a, 'tcx> Visitor<'tcx> for Checker<'a, 'tcx> {
612612
// compiler-generated `extern crate` items have a dummy span.
613613
if item.span == DUMMY_SP { return }
614614

615-
let hir_id = self.tcx.hir.node_to_hir_id(item.id);
616-
let cnum = match self.tcx.extern_mod_stmt_cnum(hir_id) {
615+
let def_id = self.tcx.hir.local_def_id(item.id);
616+
let cnum = match self.tcx.extern_mod_stmt_cnum(def_id) {
617617
Some(cnum) => cnum,
618618
None => return,
619619
};

src/librustc/ty/context.rs

Lines changed: 78 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@ use dep_graph::DepGraph;
1414
use errors::DiagnosticBuilder;
1515
use session::Session;
1616
use middle;
17-
use hir::{TraitCandidate, HirId};
17+
use hir::{TraitCandidate, HirId, ItemLocalId};
1818
use hir::def::{Def, Export};
19-
use hir::def_id::{CrateNum, DefId, LOCAL_CRATE};
19+
use hir::def_id::{CrateNum, DefId, DefIndex, LOCAL_CRATE};
2020
use hir::map as hir_map;
2121
use hir::map::DefPathHash;
2222
use lint::{self, Lint};
@@ -816,10 +816,10 @@ pub struct GlobalCtxt<'tcx> {
816816

817817
/// Map indicating what traits are in scope for places where this
818818
/// is relevant; generated by resolve.
819-
trait_map: FxHashMap<HirId, Rc<Vec<TraitCandidate>>>,
819+
trait_map: FxHashMap<DefIndex, Rc<FxHashMap<ItemLocalId, Rc<Vec<TraitCandidate>>>>>,
820820

821821
/// Export map produced by name resolution.
822-
export_map: FxHashMap<HirId, Rc<Vec<Export>>>,
822+
export_map: FxHashMap<DefId, Rc<Vec<Export>>>,
823823

824824
named_region_map: NamedRegionMap,
825825

@@ -836,11 +836,11 @@ pub struct GlobalCtxt<'tcx> {
836836
// Records the free variables refrenced by every closure
837837
// expression. Do not track deps for this, just recompute it from
838838
// scratch every time.
839-
freevars: FxHashMap<HirId, Rc<Vec<hir::Freevar>>>,
839+
freevars: FxHashMap<DefId, Rc<Vec<hir::Freevar>>>,
840840

841-
maybe_unused_trait_imports: FxHashSet<HirId>,
841+
maybe_unused_trait_imports: FxHashSet<DefId>,
842842

843-
maybe_unused_extern_crates: Vec<(HirId, Span)>,
843+
maybe_unused_extern_crates: Vec<(DefId, Span)>,
844844

845845
// Internal cache for metadata decoding. No need to track deps on this.
846846
pub rcache: RefCell<FxHashMap<ty::CReaderCacheKey, Ty<'tcx>>>,
@@ -1031,6 +1031,35 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
10311031
None
10321032
};
10331033

1034+
let mut trait_map = FxHashMap();
1035+
for (k, v) in resolutions.trait_map {
1036+
let hir_id = hir.node_to_hir_id(k);
1037+
let map = trait_map.entry(hir_id.owner)
1038+
.or_insert_with(|| Rc::new(FxHashMap()));
1039+
Rc::get_mut(map).unwrap().insert(hir_id.local_id, Rc::new(v));
1040+
}
1041+
let mut defs = FxHashMap();
1042+
for (k, v) in named_region_map.defs {
1043+
let hir_id = hir.node_to_hir_id(k);
1044+
let map = defs.entry(hir_id.owner)
1045+
.or_insert_with(|| Rc::new(FxHashMap()));
1046+
Rc::get_mut(map).unwrap().insert(hir_id.local_id, v);
1047+
}
1048+
let mut late_bound = FxHashMap();
1049+
for k in named_region_map.late_bound {
1050+
let hir_id = hir.node_to_hir_id(k);
1051+
let map = late_bound.entry(hir_id.owner)
1052+
.or_insert_with(|| Rc::new(FxHashSet()));
1053+
Rc::get_mut(map).unwrap().insert(hir_id.local_id);
1054+
}
1055+
let mut object_lifetime_defaults = FxHashMap();
1056+
for (k, v) in named_region_map.object_lifetime_defaults {
1057+
let hir_id = hir.node_to_hir_id(k);
1058+
let map = object_lifetime_defaults.entry(hir_id.owner)
1059+
.or_insert_with(|| Rc::new(FxHashMap()));
1060+
Rc::get_mut(map).unwrap().insert(hir_id.local_id, Rc::new(v));
1061+
}
1062+
10341063
tls::enter_global(GlobalCtxt {
10351064
sess: s,
10361065
trans_trait_caches: traits::trans::TransTraitCaches::new(dep_graph.clone()),
@@ -1039,40 +1068,26 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
10391068
dep_graph: dep_graph.clone(),
10401069
types: common_types,
10411070
named_region_map: NamedRegionMap {
1042-
defs:
1043-
named_region_map.defs
1044-
.into_iter()
1045-
.map(|(k, v)| (hir.node_to_hir_id(k), v))
1046-
.collect(),
1047-
late_bound:
1048-
named_region_map.late_bound
1049-
.into_iter()
1050-
.map(|k| hir.node_to_hir_id(k))
1051-
.collect(),
1052-
object_lifetime_defaults:
1053-
named_region_map.object_lifetime_defaults
1054-
.into_iter()
1055-
.map(|(k, v)| (hir.node_to_hir_id(k), Rc::new(v)))
1056-
.collect(),
1071+
defs,
1072+
late_bound,
1073+
object_lifetime_defaults,
10571074
},
1058-
trait_map: resolutions.trait_map.into_iter().map(|(k, v)| {
1059-
(hir.node_to_hir_id(k), Rc::new(v))
1060-
}).collect(),
1075+
trait_map,
10611076
export_map: resolutions.export_map.into_iter().map(|(k, v)| {
1062-
(hir.node_to_hir_id(k), Rc::new(v))
1077+
(k, Rc::new(v))
10631078
}).collect(),
10641079
freevars: resolutions.freevars.into_iter().map(|(k, v)| {
1065-
(hir.node_to_hir_id(k), Rc::new(v))
1080+
(hir.local_def_id(k), Rc::new(v))
10661081
}).collect(),
10671082
maybe_unused_trait_imports:
10681083
resolutions.maybe_unused_trait_imports
10691084
.into_iter()
1070-
.map(|id| hir.node_to_hir_id(id))
1085+
.map(|id| hir.local_def_id(id))
10711086
.collect(),
10721087
maybe_unused_extern_crates:
10731088
resolutions.maybe_unused_extern_crates
10741089
.into_iter()
1075-
.map(|(id, sp)| (hir.node_to_hir_id(id), sp))
1090+
.map(|(id, sp)| (hir.local_def_id(id), sp))
10761091
.collect(),
10771092
hir,
10781093
def_path_hash_to_def_id,
@@ -1966,6 +1981,29 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
19661981
let (level, src) = self.lint_level_at_node(lint, id);
19671982
lint::struct_lint_level(self.sess, lint, level, src, None, msg)
19681983
}
1984+
1985+
pub fn in_scope_traits(self, id: HirId) -> Option<Rc<Vec<TraitCandidate>>> {
1986+
self.in_scope_traits_map(id.owner)
1987+
.and_then(|map| map.get(&id.local_id).cloned())
1988+
}
1989+
1990+
pub fn named_region(self, id: HirId) -> Option<resolve_lifetime::Region> {
1991+
self.named_region_map(id.owner)
1992+
.and_then(|map| map.get(&id.local_id).cloned())
1993+
}
1994+
1995+
pub fn is_late_bound(self, id: HirId) -> bool {
1996+
self.is_late_bound_map(id.owner)
1997+
.map(|set| set.contains(&id.local_id))
1998+
.unwrap_or(false)
1999+
}
2000+
2001+
pub fn object_lifetime_defaults(self, id: HirId)
2002+
-> Option<Rc<Vec<ObjectLifetimeDefault>>>
2003+
{
2004+
self.object_lifetime_defaults_map(id.owner)
2005+
.and_then(|map| map.get(&id.local_id).cloned())
2006+
}
19692007
}
19702008

19712009
pub trait InternAs<T: ?Sized, R> {
@@ -2013,20 +2051,24 @@ impl<T, R, E> InternIteratorElement<T, R> for Result<T, E> {
20132051
}
20142052

20152053
struct NamedRegionMap {
2016-
defs: FxHashMap<HirId, resolve_lifetime::Region>,
2017-
late_bound: FxHashSet<HirId>,
2018-
object_lifetime_defaults: FxHashMap<HirId, Rc<Vec<ObjectLifetimeDefault>>>,
2054+
defs: FxHashMap<DefIndex, Rc<FxHashMap<ItemLocalId, resolve_lifetime::Region>>>,
2055+
late_bound: FxHashMap<DefIndex, Rc<FxHashSet<ItemLocalId>>>,
2056+
object_lifetime_defaults:
2057+
FxHashMap<
2058+
DefIndex,
2059+
Rc<FxHashMap<ItemLocalId, Rc<Vec<ObjectLifetimeDefault>>>>,
2060+
>,
20192061
}
20202062

20212063
pub fn provide(providers: &mut ty::maps::Providers) {
20222064
// FIXME(#44234) - almost all of these queries have no sub-queries and
20232065
// therefore no actual inputs, they're just reading tables calculated in
20242066
// resolve! Does this work? Unsure! That's what the issue is about
2025-
providers.in_scope_traits = |tcx, id| tcx.gcx.trait_map.get(&id).cloned();
2067+
providers.in_scope_traits_map = |tcx, id| tcx.gcx.trait_map.get(&id).cloned();
20262068
providers.module_exports = |tcx, id| tcx.gcx.export_map.get(&id).cloned();
2027-
providers.named_region = |tcx, id| tcx.gcx.named_region_map.defs.get(&id).cloned();
2028-
providers.is_late_bound = |tcx, id| tcx.gcx.named_region_map.late_bound.contains(&id);
2029-
providers.object_lifetime_defaults = |tcx, id| {
2069+
providers.named_region_map = |tcx, id| tcx.gcx.named_region_map.defs.get(&id).cloned();
2070+
providers.is_late_bound_map = |tcx, id| tcx.gcx.named_region_map.late_bound.get(&id).cloned();
2071+
providers.object_lifetime_defaults_map = |tcx, id| {
20302072
tcx.gcx.named_region_map.object_lifetime_defaults.get(&id).cloned()
20312073
};
20322074
providers.crate_name = |tcx, id| {

0 commit comments

Comments
 (0)