Skip to content

Commit 8b16b02

Browse files
committed
Index HIR after creating TyCtxt
1 parent 3538cb3 commit 8b16b02

File tree

51 files changed

+180
-279
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

51 files changed

+180
-279
lines changed

src/librustc/arena.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ macro_rules! arena_types {
163163
[] where_predicate: rustc_hir::WherePredicate<$tcx>,
164164

165165
// HIR query types
166-
[few] hir_map: rustc::hir::map::Map<$tcx>,
166+
[few] indexed_hir: rustc::hir::map::IndexedHir<$tcx>,
167167
[few] hir_definitions: rustc::hir::map::definitions::Definitions,
168168
[] hir_owner: rustc::hir::HirOwner<$tcx>,
169169
[] hir_owner_items: rustc::hir::HirOwnerItems<$tcx>,

src/librustc/hir/map/collector.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -307,7 +307,7 @@ impl<'a, 'hir> Visitor<'hir> for NodeCollector<'a, 'hir> {
307307
/// deep walking so that we walk nested items in the context of
308308
/// their outer items.
309309
310-
fn nested_visit_map(&mut self) -> NestedVisitorMap<'_, Self::Map> {
310+
fn nested_visit_map(&mut self) -> NestedVisitorMap<Self::Map> {
311311
panic!("`visit_nested_xxx` must be manually implemented in this visitor");
312312
}
313313

src/librustc/hir/map/hir_id_validator.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::hir::map::EarlyMap;
1+
use crate::ty::TyCtxt;
22
/*use rustc_data_structures::fx::FxHashSet;
33
use rustc_data_structures::sync::{par_iter, Lock, ParallelIterator};
44
use rustc_hir as hir;
@@ -7,8 +7,8 @@ use rustc_hir::intravisit;
77
use rustc_hir::itemlikevisit::ItemLikeVisitor;
88
use rustc_hir::{HirId, ItemLocalId};*/
99

10-
pub fn check_crate(_: &EarlyMap<'_>, sess: &rustc_session::Session) {
11-
/*hir_map.dep_graph.assert_ignored();
10+
pub fn check_crate(_tcx: TyCtxt<'_>) {
11+
/*tcx.dep_graph.assert_ignored();
1212
1313
let errors = Lock::new(Vec::new());
1414
@@ -24,7 +24,7 @@ pub fn check_crate(_: &EarlyMap<'_>, sess: &rustc_session::Session) {
2424
2525
if !errors.is_empty() {
2626
let message = errors.iter().fold(String::new(), |s1, s2| s1 + "\n" + s2);
27-
sess.delay_span_bug(rustc_span::DUMMY_SP, &message);
27+
tcx.sess.delay_span_bug(rustc_span::DUMMY_SP, &message);
2828
}*/
2929
}
3030
/*
@@ -135,7 +135,7 @@ impl<'a, 'hir> HirIdValidator<'a, 'hir> {
135135
impl<'a, 'hir> intravisit::Visitor<'hir> for HirIdValidator<'a, 'hir> {
136136
type Map = EarlyMap<'hir>;
137137
138-
fn nested_visit_map(&mut self) -> intravisit::NestedVisitorMap<'_, Self::Map> {
138+
fn nested_visit_map(&mut self) -> intravisit::NestedVisitorMap<Self::Map> {
139139
intravisit::NestedVisitorMap::OnlyBodies(self.hir_map)
140140
}
141141

src/librustc/hir/map/mod.rs

Lines changed: 39 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,14 @@ pub use self::definitions::{
33
DefKey, DefPath, DefPathData, DefPathHash, Definitions, DisambiguatedDefPathData,
44
};
55

6-
use crate::arena::Arena;
76
use crate::hir::{HirOwner, HirOwnerItems};
8-
use crate::middle::cstore::CrateStoreDyn;
97
use crate::ty::query::Providers;
108
use crate::ty::TyCtxt;
119
use rustc_ast::ast::{self, Name, NodeId};
1210
use rustc_data_structures::fx::FxHashMap;
1311
use rustc_data_structures::svh::Svh;
1412
use rustc_hir::def::{DefKind, Res};
15-
use rustc_hir::def_id::{DefId, DefIndex, LocalDefId, LOCAL_CRATE};
13+
use rustc_hir::def_id::{CrateNum, DefId, DefIndex, LocalDefId, LOCAL_CRATE};
1614
use rustc_hir::intravisit;
1715
use rustc_hir::itemlikevisit::ItemLikeVisitor;
1816
use rustc_hir::print::Nested;
@@ -129,38 +127,20 @@ fn is_body_owner<'hir>(node: Node<'hir>, hir_id: HirId) -> bool {
129127
}
130128
}
131129

132-
/// Represents a mapping from `NodeId`s to AST elements and their parent `NodeId`s.
133-
pub struct EarlyMap<'hir> {
134-
pub krate: &'hir Crate<'hir>,
135-
130+
pub struct IndexedHir<'hir> {
136131
/// The SVH of the local crate.
137132
pub crate_hash: Svh,
138133

139134
pub(super) owner_map: FxHashMap<DefIndex, &'hir HirOwner<'hir>>,
140135
pub(super) owner_items_map: FxHashMap<DefIndex, &'hir HirOwnerItems<'hir>>,
141136

142-
pub(crate) definitions: &'hir Definitions,
143-
144137
/// The reverse mapping of `node_to_hir_id`.
145138
pub(super) hir_to_node_id: FxHashMap<HirId, NodeId>,
146139
}
147140

148-
/// Represents a mapping from `NodeId`s to AST elements and their parent `NodeId`s.
141+
#[derive(Copy, Clone)]
149142
pub struct Map<'hir> {
150143
pub(super) tcx: TyCtxt<'hir>,
151-
152-
pub(super) krate: &'hir Crate<'hir>,
153-
154-
/// The SVH of the local crate.
155-
pub crate_hash: Svh,
156-
157-
pub(super) owner_map: FxHashMap<DefIndex, &'hir HirOwner<'hir>>,
158-
pub(super) owner_items_map: FxHashMap<DefIndex, &'hir HirOwnerItems<'hir>>,
159-
160-
pub(super) definitions: &'hir Definitions,
161-
162-
/// The reverse mapping of `node_to_hir_id`.
163-
pub(super) hir_to_node_id: FxHashMap<HirId, NodeId>,
164144
}
165145

166146
/// An iterator that walks up the ancestor tree of a given `HirId`.
@@ -196,21 +176,18 @@ impl<'hir> Iterator for ParentHirIterator<'_, 'hir> {
196176
}
197177

198178
impl<'hir> Map<'hir> {
199-
/// This is used internally in the dependency tracking system.
200-
/// Use the `krate` method to ensure your dependency on the
201-
/// crate is tracked.
202-
pub fn untracked_krate(&self) -> &Crate<'hir> {
203-
&self.krate
179+
pub fn krate(&self) -> &'hir Crate<'hir> {
180+
self.tcx.hir_crate(LOCAL_CRATE)
204181
}
205182

206183
#[inline]
207-
pub fn definitions(&self) -> &Definitions {
208-
&self.definitions
184+
pub fn definitions(&self) -> &'hir Definitions {
185+
&self.tcx.definitions
209186
}
210187

211188
pub fn def_key(&self, def_id: DefId) -> DefKey {
212189
assert!(def_id.is_local());
213-
self.definitions.def_key(def_id.index)
190+
self.tcx.definitions.def_key(def_id.index)
214191
}
215192

216193
pub fn def_path_from_hir_id(&self, id: HirId) -> Option<DefPath> {
@@ -219,7 +196,7 @@ impl<'hir> Map<'hir> {
219196

220197
pub fn def_path(&self, def_id: DefId) -> DefPath {
221198
assert!(def_id.is_local());
222-
self.definitions.def_path(def_id.index)
199+
self.tcx.definitions.def_path(def_id.index)
223200
}
224201

225202
#[inline]
@@ -248,42 +225,42 @@ impl<'hir> Map<'hir> {
248225
#[inline]
249226
pub fn opt_local_def_id(&self, hir_id: HirId) -> Option<DefId> {
250227
let node_id = self.hir_to_node_id(hir_id);
251-
self.definitions.opt_local_def_id(node_id)
228+
self.tcx.definitions.opt_local_def_id(node_id)
252229
}
253230

254231
#[inline]
255232
pub fn opt_local_def_id_from_node_id(&self, node: NodeId) -> Option<DefId> {
256-
self.definitions.opt_local_def_id(node)
233+
self.tcx.definitions.opt_local_def_id(node)
257234
}
258235

259236
#[inline]
260237
pub fn as_local_node_id(&self, def_id: DefId) -> Option<NodeId> {
261-
self.definitions.as_local_node_id(def_id)
238+
self.tcx.definitions.as_local_node_id(def_id)
262239
}
263240

264241
#[inline]
265242
pub fn as_local_hir_id(&self, def_id: DefId) -> Option<HirId> {
266-
self.definitions.as_local_hir_id(def_id)
243+
self.tcx.definitions.as_local_hir_id(def_id)
267244
}
268245

269246
#[inline]
270247
pub fn hir_to_node_id(&self, hir_id: HirId) -> NodeId {
271-
self.hir_to_node_id[&hir_id]
248+
self.tcx.index_hir(LOCAL_CRATE).hir_to_node_id[&hir_id]
272249
}
273250

274251
#[inline]
275252
pub fn node_to_hir_id(&self, node_id: NodeId) -> HirId {
276-
self.definitions.node_to_hir_id(node_id)
253+
self.tcx.definitions.node_to_hir_id(node_id)
277254
}
278255

279256
#[inline]
280257
pub fn def_index_to_hir_id(&self, def_index: DefIndex) -> HirId {
281-
self.definitions.def_index_to_hir_id(def_index)
258+
self.tcx.definitions.def_index_to_hir_id(def_index)
282259
}
283260

284261
#[inline]
285262
pub fn local_def_id_to_hir_id(&self, def_id: LocalDefId) -> HirId {
286-
self.definitions.def_index_to_hir_id(def_id.to_def_id().index)
263+
self.tcx.definitions.def_index_to_hir_id(def_id.to_def_id().index)
287264
}
288265

289266
pub fn def_kind(&self, hir_id: HirId) -> Option<DefKind> {
@@ -1045,45 +1022,42 @@ impl Named for ImplItem<'_> {
10451022
}
10461023
}
10471024

1048-
pub fn map_crate<'hir>(
1049-
sess: &rustc_session::Session,
1050-
arena: &'hir Arena<'hir>,
1051-
cstore: &CrateStoreDyn,
1052-
krate: &'hir Crate<'hir>,
1053-
definitions: Definitions,
1054-
) -> EarlyMap<'hir> {
1055-
let _prof_timer = sess.prof.generic_activity("build_hir_map");
1025+
pub(super) fn index_hir<'tcx>(tcx: TyCtxt<'tcx>, cnum: CrateNum) -> &'tcx IndexedHir<'tcx> {
1026+
assert_eq!(cnum, LOCAL_CRATE);
1027+
1028+
let _prof_timer = tcx.sess.prof.generic_activity("build_hir_map");
10561029

10571030
// Build the reverse mapping of `node_to_hir_id`.
1058-
let hir_to_node_id = definitions
1031+
let hir_to_node_id = tcx
1032+
.definitions
10591033
.node_to_hir_id
10601034
.iter_enumerated()
10611035
.map(|(node_id, &hir_id)| (hir_id, node_id))
10621036
.collect();
10631037

10641038
let (owner_map, owner_items_map, crate_hash) = {
1065-
let hcx = crate::ich::StableHashingContext::new(sess, krate, &definitions, cstore);
1066-
1067-
let mut collector =
1068-
NodeCollector::root(sess, arena, krate, &definitions, &hir_to_node_id, hcx);
1069-
intravisit::walk_crate(&mut collector, krate);
1070-
1071-
let crate_disambiguator = sess.local_crate_disambiguator();
1072-
let cmdline_args = sess.opts.dep_tracking_hash();
1073-
collector.finalize_and_compute_crate_hash(crate_disambiguator, cstore, cmdline_args)
1039+
let hcx = tcx.create_stable_hashing_context();
1040+
1041+
let mut collector = NodeCollector::root(
1042+
tcx.sess,
1043+
&**tcx.arena,
1044+
tcx.untracked_crate,
1045+
&tcx.definitions,
1046+
&hir_to_node_id,
1047+
hcx,
1048+
);
1049+
intravisit::walk_crate(&mut collector, tcx.untracked_crate);
1050+
1051+
let crate_disambiguator = tcx.sess.local_crate_disambiguator();
1052+
let cmdline_args = tcx.sess.opts.dep_tracking_hash();
1053+
collector.finalize_and_compute_crate_hash(crate_disambiguator, &*tcx.cstore, cmdline_args)
10741054
};
10751055

1076-
let map = EarlyMap {
1077-
krate,
1056+
let map = tcx.arena.alloc(IndexedHir {
10781057
crate_hash,
10791058
owner_map,
10801059
owner_items_map: owner_items_map.into_iter().map(|(k, v)| (k, &*v)).collect(),
10811060
hir_to_node_id,
1082-
definitions: arena.alloc(definitions),
1083-
};
1084-
1085-
sess.time("validate_HIR_map", || {
1086-
hir_id_validator::check_crate(&map, sess);
10871061
});
10881062

10891063
map

src/librustc/hir/mod.rs

Lines changed: 8 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,14 @@ pub mod map;
88
use crate::ich::StableHashingContext;
99
use crate::ty::query::Providers;
1010
use crate::ty::TyCtxt;
11-
use rustc_data_structures::cold_path;
1211
use rustc_data_structures::fx::FxHashMap;
1312
use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
1413
use rustc_hir::def_id::{DefId, LOCAL_CRATE};
15-
use rustc_hir::print;
1614
use rustc_hir::Body;
17-
use rustc_hir::Crate;
1815
use rustc_hir::HirId;
1916
use rustc_hir::ItemLocalId;
2017
use rustc_hir::Node;
2118
use rustc_index::vec::IndexVec;
22-
use std::ops::Deref;
2319

2420
#[derive(HashStable)]
2521
pub struct HirOwner<'tcx> {
@@ -60,48 +56,10 @@ impl<'a, 'tcx> HashStable<StableHashingContext<'a>> for HirOwnerItems<'tcx> {
6056
}
6157
}
6258

63-
/// A wrapper type which allows you to access HIR.
64-
#[derive(Clone)]
65-
pub struct Hir<'tcx> {
66-
tcx: TyCtxt<'tcx>,
67-
map: &'tcx map::Map<'tcx>,
68-
}
69-
70-
impl<'tcx> Hir<'tcx> {
71-
pub fn krate(&self) -> &'tcx Crate<'tcx> {
72-
self.tcx.hir_crate(LOCAL_CRATE)
73-
}
74-
}
75-
76-
impl<'tcx> Deref for Hir<'tcx> {
77-
type Target = &'tcx map::Map<'tcx>;
78-
79-
#[inline(always)]
80-
fn deref(&self) -> &Self::Target {
81-
&self.map
82-
}
83-
}
84-
85-
impl<'hir> print::PpAnn for Hir<'hir> {
86-
fn nested(&self, state: &mut print::State<'_>, nested: print::Nested) {
87-
self.map.nested(state, nested)
88-
}
89-
}
90-
9159
impl<'tcx> TyCtxt<'tcx> {
9260
#[inline(always)]
93-
pub fn hir(self) -> Hir<'tcx> {
94-
let map = self.late_hir_map.load();
95-
let map = if unlikely!(map.is_none()) {
96-
cold_path(|| {
97-
let map = self.hir_map(LOCAL_CRATE);
98-
self.late_hir_map.store(Some(map));
99-
map
100-
})
101-
} else {
102-
map.unwrap()
103-
};
104-
Hir { tcx: self, map }
61+
pub fn hir(self) -> map::Map<'tcx> {
62+
map::Map { tcx: self }
10563
}
10664

10765
pub fn parent_module(self, id: HirId) -> DefId {
@@ -114,37 +72,16 @@ pub fn provide(providers: &mut Providers<'_>) {
11472
let hir = tcx.hir();
11573
hir.local_def_id(hir.get_module_parent_node(hir.as_local_hir_id(id).unwrap()))
11674
};
117-
providers.hir_crate = |tcx, _| tcx.hir_map(LOCAL_CRATE).untracked_krate();
118-
providers.hir_map = |tcx, id| {
119-
assert_eq!(id, LOCAL_CRATE);
120-
let early = tcx.hir_map.steal();
121-
tcx.arena.alloc(map::Map {
122-
tcx,
123-
krate: early.krate,
124-
125-
crate_hash: early.crate_hash,
126-
127-
owner_map: early.owner_map,
128-
owner_items_map: early.owner_items_map,
129-
130-
definitions: early.definitions,
131-
132-
hir_to_node_id: early.hir_to_node_id,
133-
})
134-
};
75+
providers.hir_crate = |tcx, _| tcx.untracked_crate;
76+
providers.index_hir = map::index_hir;
13577
providers.hir_module_items = |tcx, id| {
13678
assert_eq!(id.krate, LOCAL_CRATE);
13779
let hir = tcx.hir();
13880
let module = hir.as_local_hir_id(id).unwrap();
139-
&hir.untracked_krate().modules[&module]
140-
};
141-
providers.hir_owner = |tcx, id| {
142-
assert_eq!(id.krate, LOCAL_CRATE);
143-
*tcx.hir().map.owner_map.get(&id.index).unwrap()
144-
};
145-
providers.hir_owner_items = |tcx, id| {
146-
assert_eq!(id.krate, LOCAL_CRATE);
147-
*tcx.hir().map.owner_items_map.get(&id.index).unwrap()
81+
&tcx.untracked_crate.modules[&module]
14882
};
83+
providers.hir_owner = |tcx, id| *tcx.index_hir(id.krate).owner_map.get(&id.index).unwrap();
84+
providers.hir_owner_items =
85+
|tcx, id| *tcx.index_hir(id.krate).owner_items_map.get(&id.index).unwrap();
14986
map::provide(providers);
15087
}

src/librustc/query/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ rustc_queries! {
5555
desc { "get the crate HIR" }
5656
}
5757

58-
query hir_map(_: CrateNum) -> &'tcx map::Map<'tcx> {
58+
query index_hir(_: CrateNum) -> &'tcx map::IndexedHir<'tcx> {
5959
eval_always
6060
no_hash
6161
desc { "index HIR" }

0 commit comments

Comments
 (0)