Skip to content

Commit 623dcb0

Browse files
committed
Remove the Forest type
1 parent 20ce2f6 commit 623dcb0

File tree

8 files changed

+51
-78
lines changed

8 files changed

+51
-78
lines changed

src/librustc/arena.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ macro_rules! arena_types {
127127
[] tys: rustc::ty::TyS<$tcx>,
128128

129129
// HIR types
130-
[few] hir_forest: rustc::hir::map::Forest<$tcx>,
130+
[few] hir_krate: rustc_hir::Crate<$tcx>,
131131
[] arm: rustc_hir::Arm<$tcx>,
132132
[] attribute: syntax::ast::Attribute,
133133
[] block: rustc_hir::Block<$tcx>,

src/librustc/hir/map/hir_id_validator.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ pub fn check_crate(hir_map: &Map<'_>) {
1212

1313
let errors = Lock::new(Vec::new());
1414

15-
par_iter(&hir_map.forest.krate.modules).for_each(|(module_id, _)| {
15+
par_iter(&hir_map.krate.modules).for_each(|(module_id, _)| {
1616
let local_def_id = hir_map.local_def_id(*module_id);
1717
hir_map.visit_item_likes_in_module(
1818
local_def_id,

src/librustc/hir/map/mod.rs

Lines changed: 31 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -129,25 +129,6 @@ impl<'hir> Entry<'hir> {
129129
}
130130
}
131131

132-
/// Stores a crate and any number of inlined items from other crates.
133-
pub struct Forest<'hir> {
134-
krate: Crate<'hir>,
135-
pub dep_graph: DepGraph,
136-
}
137-
138-
impl Forest<'hir> {
139-
pub fn new(krate: Crate<'hir>, dep_graph: &DepGraph) -> Forest<'hir> {
140-
Forest { krate, dep_graph: dep_graph.clone() }
141-
}
142-
143-
/// This is used internally in the dependency tracking system.
144-
/// Use the `krate` method to ensure your dependency on the
145-
/// crate is tracked.
146-
pub fn untracked_krate(&self) -> &Crate<'hir> {
147-
&self.krate
148-
}
149-
}
150-
151132
/// This type is effectively a `HashMap<HirId, Entry<'hir>>`,
152133
/// but it is implemented as 2 layers of arrays.
153134
/// - first we have `A = IndexVec<DefIndex, B>` mapping `DefIndex`s to an inner value
@@ -157,11 +138,8 @@ pub(super) type HirEntryMap<'hir> = IndexVec<DefIndex, IndexVec<ItemLocalId, Opt
157138
/// Represents a mapping from `NodeId`s to AST elements and their parent `NodeId`s.
158139
#[derive(Clone)]
159140
pub struct Map<'hir> {
160-
/// The backing storage for all the AST nodes.
161-
pub forest: &'hir Forest<'hir>,
141+
pub krate: &'hir Crate<'hir>,
162142

163-
/// Same as the dep_graph in forest, just available with one fewer
164-
/// deref. This is a gratuitous micro-optimization.
165143
pub dep_graph: DepGraph,
166144

167145
/// The SVH of the local crate.
@@ -212,6 +190,13 @@ impl<'hir> Iterator for ParentHirIterator<'_, 'hir> {
212190
}
213191

214192
impl<'hir> Map<'hir> {
193+
/// This is used internally in the dependency tracking system.
194+
/// Use the `krate` method to ensure your dependency on the
195+
/// crate is tracked.
196+
pub fn untracked_krate(&self) -> &Crate<'hir> {
197+
&self.krate
198+
}
199+
215200
#[inline]
216201
fn lookup(&self, id: HirId) -> Option<&Entry<'hir>> {
217202
let local_map = self.map.get(id.owner)?;
@@ -399,33 +384,33 @@ impl<'hir> Map<'hir> {
399384
pub fn item(&self, id: HirId) -> &'hir Item<'hir> {
400385
self.read(id);
401386

402-
// N.B., intentionally bypass `self.forest.krate()` so that we
387+
// N.B., intentionally bypass `self.krate()` so that we
403388
// do not trigger a read of the whole krate here
404-
self.forest.krate.item(id)
389+
self.krate.item(id)
405390
}
406391

407392
pub fn trait_item(&self, id: TraitItemId) -> &'hir TraitItem<'hir> {
408393
self.read(id.hir_id);
409394

410-
// N.B., intentionally bypass `self.forest.krate()` so that we
395+
// N.B., intentionally bypass `self.krate()` so that we
411396
// do not trigger a read of the whole krate here
412-
self.forest.krate.trait_item(id)
397+
self.krate.trait_item(id)
413398
}
414399

415400
pub fn impl_item(&self, id: ImplItemId) -> &'hir ImplItem<'hir> {
416401
self.read(id.hir_id);
417402

418-
// N.B., intentionally bypass `self.forest.krate()` so that we
403+
// N.B., intentionally bypass `self.krate()` so that we
419404
// do not trigger a read of the whole krate here
420-
self.forest.krate.impl_item(id)
405+
self.krate.impl_item(id)
421406
}
422407

423408
pub fn body(&self, id: BodyId) -> &'hir Body<'hir> {
424409
self.read(id.hir_id);
425410

426-
// N.B., intentionally bypass `self.forest.krate()` so that we
411+
// N.B., intentionally bypass `self.krate()` so that we
427412
// do not trigger a read of the whole krate here
428-
self.forest.krate.body(id)
413+
self.krate.body(id)
429414
}
430415

431416
pub fn fn_decl_by_hir_id(&self, hir_id: HirId) -> Option<&'hir FnDecl<'hir>> {
@@ -521,9 +506,9 @@ impl<'hir> Map<'hir> {
521506
pub fn trait_impls(&self, trait_did: DefId) -> &'hir [HirId] {
522507
self.dep_graph.read(DepNode::new_no_params(DepKind::AllLocalTraitImpls));
523508

524-
// N.B., intentionally bypass `self.forest.krate()` so that we
509+
// N.B., intentionally bypass `self.krate()` so that we
525510
// do not trigger a read of the whole krate here
526-
self.forest.krate.trait_impls.get(&trait_did).map_or(&[], |xs| &xs[..])
511+
self.krate.trait_impls.get(&trait_did).map_or(&[], |xs| &xs[..])
527512
}
528513

529514
/// Gets the attributes on the crate. This is preferable to
@@ -533,15 +518,15 @@ impl<'hir> Map<'hir> {
533518
let def_path_hash = self.definitions.def_path_hash(CRATE_DEF_INDEX);
534519

535520
self.dep_graph.read(def_path_hash.to_dep_node(DepKind::Hir));
536-
&self.forest.krate.attrs
521+
&self.krate.attrs
537522
}
538523

539524
pub fn get_module(&self, module: DefId) -> (&'hir Mod<'hir>, Span, HirId) {
540525
let hir_id = self.as_local_hir_id(module).unwrap();
541526
self.read(hir_id);
542527
match self.find_entry(hir_id).unwrap().node {
543528
Node::Item(&Item { span, kind: ItemKind::Mod(ref m), .. }) => (m, span, hir_id),
544-
Node::Crate => (&self.forest.krate.module, self.forest.krate.span, hir_id),
529+
Node::Crate => (&self.krate.module, self.krate.span, hir_id),
545530
node => panic!("not a module: {:?}", node),
546531
}
547532
}
@@ -558,7 +543,7 @@ impl<'hir> Map<'hir> {
558543
// in the expect_* calls the loops below
559544
self.read(hir_id);
560545

561-
let module = &self.forest.krate.modules[&hir_id];
546+
let module = &self.krate.modules[&hir_id];
562547

563548
for id in &module.items {
564549
visitor.visit_item(self.expect_item(*id));
@@ -975,7 +960,7 @@ impl<'hir> Map<'hir> {
975960
// Unit/tuple structs/variants take the attributes straight from
976961
// the struct/variant definition.
977962
Some(Node::Ctor(..)) => return self.attrs(self.get_parent_item(id)),
978-
Some(Node::Crate) => Some(&self.forest.krate.attrs[..]),
963+
Some(Node::Crate) => Some(&self.krate.attrs[..]),
979964
_ => None,
980965
};
981966
attrs.unwrap_or(&[])
@@ -1054,7 +1039,7 @@ impl<'hir> Map<'hir> {
10541039
Some(Node::Visibility(v)) => bug!("unexpected Visibility {:?}", v),
10551040
Some(Node::Local(local)) => local.span,
10561041
Some(Node::MacroDef(macro_def)) => macro_def.span,
1057-
Some(Node::Crate) => self.forest.krate.span,
1042+
Some(Node::Crate) => self.krate.span,
10581043
None => bug!("hir::map::Map::span: id not in map: {:?}", hir_id),
10591044
}
10601045
}
@@ -1222,7 +1207,8 @@ impl Named for ImplItem<'_> {
12221207
pub fn map_crate<'hir>(
12231208
sess: &rustc_session::Session,
12241209
cstore: &CrateStoreDyn,
1225-
forest: &'hir Forest<'hir>,
1210+
krate: &'hir Crate<'hir>,
1211+
dep_graph: DepGraph,
12261212
definitions: Definitions,
12271213
) -> Map<'hir> {
12281214
let _prof_timer = sess.prof.generic_activity("build_hir_map");
@@ -1235,31 +1221,18 @@ pub fn map_crate<'hir>(
12351221
.collect();
12361222

12371223
let (map, crate_hash) = {
1238-
let hcx = crate::ich::StableHashingContext::new(sess, &forest.krate, &definitions, cstore);
1239-
1240-
let mut collector = NodeCollector::root(
1241-
sess,
1242-
&forest.krate,
1243-
&forest.dep_graph,
1244-
&definitions,
1245-
&hir_to_node_id,
1246-
hcx,
1247-
);
1248-
intravisit::walk_crate(&mut collector, &forest.krate);
1224+
let hcx = crate::ich::StableHashingContext::new(sess, krate, &definitions, cstore);
1225+
1226+
let mut collector =
1227+
NodeCollector::root(sess, krate, &dep_graph, &definitions, &hir_to_node_id, hcx);
1228+
intravisit::walk_crate(&mut collector, krate);
12491229

12501230
let crate_disambiguator = sess.local_crate_disambiguator();
12511231
let cmdline_args = sess.opts.dep_tracking_hash();
12521232
collector.finalize_and_compute_crate_hash(crate_disambiguator, cstore, cmdline_args)
12531233
};
12541234

1255-
let map = Map {
1256-
forest,
1257-
dep_graph: forest.dep_graph.clone(),
1258-
crate_hash,
1259-
map,
1260-
hir_to_node_id,
1261-
definitions,
1262-
};
1235+
let map = Map { krate, dep_graph, crate_hash, map, hir_to_node_id, definitions };
12631236

12641237
sess.time("validate_HIR_map", || {
12651238
hir_id_validator::check_crate(&map);

src/librustc/hir/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,6 @@ impl<'tcx> TyCtxt<'tcx> {
4949
}
5050

5151
pub fn provide(providers: &mut Providers<'_>) {
52-
providers.hir_crate = |tcx, _| tcx.hir_map.forest.untracked_krate();
52+
providers.hir_crate = |tcx, _| tcx.hir_map.untracked_krate();
5353
map::provide(providers);
5454
}

src/librustc/ty/context.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1323,7 +1323,7 @@ impl<'tcx> TyCtxt<'tcx> {
13231323

13241324
#[inline(always)]
13251325
pub fn create_stable_hashing_context(self) -> StableHashingContext<'tcx> {
1326-
let krate = self.gcx.hir_map.forest.untracked_krate();
1326+
let krate = self.gcx.hir_map.untracked_krate();
13271327

13281328
StableHashingContext::new(self.sess, krate, self.hir().definitions(), &*self.cstore)
13291329
}

src/librustc_interface/passes.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ use rustc_data_structures::{box_region_allow_access, declare_box_region_type, pa
2525
use rustc_errors::PResult;
2626
use rustc_expand::base::ExtCtxt;
2727
use rustc_hir::def_id::{CrateNum, LOCAL_CRATE};
28+
use rustc_hir::Crate;
2829
use rustc_lint::LintStore;
2930
use rustc_mir as mir;
3031
use rustc_mir_build as mir_build;
@@ -422,7 +423,7 @@ pub fn lower_to_hir<'res, 'tcx>(
422423
dep_graph: &'res DepGraph,
423424
krate: &'res ast::Crate,
424425
arena: &'tcx Arena<'tcx>,
425-
) -> Result<map::Forest<'tcx>> {
426+
) -> Crate<'tcx> {
426427
// Lower AST to HIR.
427428
let hir_crate = rustc_ast_lowering::lower_crate(
428429
sess,
@@ -437,8 +438,6 @@ pub fn lower_to_hir<'res, 'tcx>(
437438
hir_stats::print_hir_stats(&hir_crate);
438439
}
439440

440-
let hir_forest = map::Forest::new(hir_crate, &dep_graph);
441-
442441
sess.time("early_lint_checks", || {
443442
rustc_lint::check_ast_crate(
444443
sess,
@@ -455,7 +454,7 @@ pub fn lower_to_hir<'res, 'tcx>(
455454
rustc_span::hygiene::clear_syntax_context_map();
456455
}
457456

458-
Ok(hir_forest)
457+
hir_crate
459458
}
460459

461460
// Returns all the paths that correspond to generated files.
@@ -705,7 +704,8 @@ impl<'tcx> QueryContext<'tcx> {
705704
pub fn create_global_ctxt<'tcx>(
706705
compiler: &'tcx Compiler,
707706
lint_store: Lrc<LintStore>,
708-
hir_forest: &'tcx map::Forest<'tcx>,
707+
krate: &'tcx Crate<'tcx>,
708+
dep_graph: DepGraph,
709709
mut resolver_outputs: ResolverOutputs,
710710
outputs: OutputFilenames,
711711
crate_name: &str,
@@ -716,7 +716,7 @@ pub fn create_global_ctxt<'tcx>(
716716
let defs = mem::take(&mut resolver_outputs.definitions);
717717

718718
// Construct the HIR map.
719-
let hir_map = map::map_crate(sess, &*resolver_outputs.cstore, &hir_forest, defs);
719+
let hir_map = map::map_crate(sess, &*resolver_outputs.cstore, krate, dep_graph, defs);
720720

721721
let query_result_on_disk_cache = rustc_incremental::load_query_result_cache(sess);
722722

src/librustc_interface/queries.rs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ use crate::passes::{self, BoxedResolver, QueryContext};
33

44
use rustc::arena::Arena;
55
use rustc::dep_graph::DepGraph;
6-
use rustc::hir::map;
76
use rustc::session::config::{OutputFilenames, OutputType};
87
use rustc::session::Session;
98
use rustc::ty::steal::Steal;
@@ -12,6 +11,7 @@ use rustc::util::common::ErrorReported;
1211
use rustc_codegen_utils::codegen_backend::CodegenBackend;
1312
use rustc_data_structures::sync::{Lrc, Once, WorkerLocal};
1413
use rustc_hir::def_id::LOCAL_CRATE;
14+
use rustc_hir::Crate;
1515
use rustc_incremental::DepGraphFuture;
1616
use rustc_lint::LintStore;
1717
use std::any::Any;
@@ -74,7 +74,7 @@ pub struct Queries<'tcx> {
7474
register_plugins: Query<(ast::Crate, Lrc<LintStore>)>,
7575
expansion: Query<(ast::Crate, Steal<Rc<RefCell<BoxedResolver>>>, Lrc<LintStore>)>,
7676
dep_graph: Query<DepGraph>,
77-
lower_to_hir: Query<(&'tcx map::Forest<'tcx>, Steal<ResolverOutputs>)>,
77+
lower_to_hir: Query<(&'tcx Crate<'tcx>, Steal<ResolverOutputs>)>,
7878
prepare_outputs: Query<OutputFilenames>,
7979
global_ctxt: Query<QueryContext<'tcx>>,
8080
ongoing_codegen: Query<Box<dyn Any>>,
@@ -207,24 +207,22 @@ impl<'tcx> Queries<'tcx> {
207207
})
208208
}
209209

210-
pub fn lower_to_hir(
211-
&'tcx self,
212-
) -> Result<&Query<(&'tcx map::Forest<'tcx>, Steal<ResolverOutputs>)>> {
210+
pub fn lower_to_hir(&'tcx self) -> Result<&Query<(&'tcx Crate<'tcx>, Steal<ResolverOutputs>)>> {
213211
self.lower_to_hir.compute(|| {
214212
let expansion_result = self.expansion()?;
215213
let peeked = expansion_result.peek();
216214
let krate = &peeked.0;
217215
let resolver = peeked.1.steal();
218216
let lint_store = &peeked.2;
219217
let hir = resolver.borrow_mut().access(|resolver| {
220-
passes::lower_to_hir(
218+
Ok(passes::lower_to_hir(
221219
self.session(),
222220
lint_store,
223221
resolver,
224222
&*self.dep_graph()?.peek(),
225223
&krate,
226224
&self.arena,
227-
)
225+
))
228226
})?;
229227
let hir = self.arena.alloc(hir);
230228
Ok((hir, Steal::new(BoxedResolver::to_resolver_outputs(resolver))))
@@ -253,12 +251,14 @@ impl<'tcx> Queries<'tcx> {
253251
let outputs = self.prepare_outputs()?.peek().clone();
254252
let lint_store = self.expansion()?.peek().2.clone();
255253
let hir = self.lower_to_hir()?.peek();
256-
let (ref hir_forest, ref resolver_outputs) = &*hir;
254+
let dep_graph = self.dep_graph()?.peek().clone();
255+
let (ref krate, ref resolver_outputs) = &*hir;
257256
let _timer = self.session().timer("create_global_ctxt");
258257
Ok(passes::create_global_ctxt(
259258
self.compiler,
260259
lint_store,
261-
hir_forest,
260+
krate,
261+
dep_graph,
262262
resolver_outputs.steal(),
263263
outputs,
264264
&crate_name,

src/librustdoc/test.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ pub fn run(options: Options) -> i32 {
8787
compiler.enter(|queries| {
8888
let lower_to_hir = queries.lower_to_hir()?;
8989

90-
let mut opts = scrape_test_config(lower_to_hir.peek().0.untracked_krate());
90+
let mut opts = scrape_test_config(lower_to_hir.peek().0);
9191
opts.display_warnings |= options.display_warnings;
9292
let enable_per_target_ignores = options.enable_per_target_ignores;
9393
let mut collector = Collector::new(

0 commit comments

Comments
 (0)