Skip to content

Commit fa6f5ad

Browse files
committed
Gather module items after lowering.
1 parent 0212c70 commit fa6f5ad

File tree

13 files changed

+121
-69
lines changed

13 files changed

+121
-69
lines changed

compiler/rustc_ast_lowering/src/item.rs

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -50,12 +50,6 @@ impl<'a> Visitor<'a> for ItemLowerer<'a, '_, '_> {
5050
self.lctx.with_parent_item_lifetime_defs(hir_id, |this| {
5151
let this = &mut ItemLowerer { lctx: this };
5252
match item.kind {
53-
ItemKind::Mod(..) => {
54-
let def_id = this.lctx.lower_node_id(item.id).expect_owner();
55-
let old_current_module = mem::replace(&mut this.lctx.current_module, def_id);
56-
visit::walk_item(this, item);
57-
this.lctx.current_module = old_current_module;
58-
}
5953
ItemKind::Impl(box ImplKind { ref of_trait, .. }) => {
6054
this.with_trait_impl_ref(of_trait, |this| visit::walk_item(this, item));
6155
}

compiler/rustc_ast_lowering/src/lib.rs

Lines changed: 2 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -102,8 +102,6 @@ struct LoweringContext<'a, 'hir: 'a> {
102102
owners: IndexVec<LocalDefId, Option<hir::OwnerNode<'hir>>>,
103103
bodies: BTreeMap<hir::BodyId, hir::Body<'hir>>,
104104

105-
modules: BTreeMap<LocalDefId, hir::ModuleItems>,
106-
107105
generator_kind: Option<hir::GeneratorKind>,
108106

109107
attrs: BTreeMap<hir::HirId, &'hir [Attribute]>,
@@ -152,8 +150,6 @@ struct LoweringContext<'a, 'hir: 'a> {
152150
/// vector.
153151
in_scope_lifetimes: Vec<ParamName>,
154152

155-
current_module: LocalDefId,
156-
157153
current_hir_id_owner: (LocalDefId, u32),
158154
item_local_id_counters: NodeMap<u32>,
159155
node_id_to_hir_id: IndexVec<NodeId, Option<hir::HirId>>,
@@ -327,15 +323,13 @@ pub fn lower_crate<'a, 'hir>(
327323
arena,
328324
owners: IndexVec::default(),
329325
bodies: BTreeMap::new(),
330-
modules: BTreeMap::new(),
331326
attrs: BTreeMap::default(),
332327
catch_scope: None,
333328
loop_scope: None,
334329
is_in_loop_condition: false,
335330
is_in_trait_impl: false,
336331
is_in_dyn_type: false,
337332
anonymous_lifetime_mode: AnonymousLifetimeMode::PassThrough,
338-
current_module: CRATE_DEF_ID,
339333
current_hir_id_owner: (CRATE_DEF_ID, 0),
340334
item_local_id_counters: Default::default(),
341335
node_id_to_hir_id: IndexVec::new(),
@@ -508,13 +502,8 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
508502
}
509503
}
510504

511-
let krate = hir::Crate {
512-
owners: self.owners,
513-
bodies: self.bodies,
514-
modules: self.modules,
515-
trait_map,
516-
attrs: self.attrs,
517-
};
505+
let krate =
506+
hir::Crate { owners: self.owners, bodies: self.bodies, trait_map, attrs: self.attrs };
518507
self.arena.alloc(krate)
519508
}
520509

@@ -523,7 +512,6 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
523512
let item = self.arena.alloc(item);
524513
self.owners.ensure_contains_elem(id.def_id, || None);
525514
self.owners[id.def_id] = Some(hir::OwnerNode::Item(item));
526-
self.modules.entry(self.current_module).or_default().items.insert(id);
527515
id
528516
}
529517

@@ -532,7 +520,6 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
532520
let item = self.arena.alloc(item);
533521
self.owners.ensure_contains_elem(id.def_id, || None);
534522
self.owners[id.def_id] = Some(hir::OwnerNode::ForeignItem(item));
535-
self.modules.entry(self.current_module).or_default().foreign_items.insert(id);
536523
id
537524
}
538525

@@ -541,7 +528,6 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
541528
let item = self.arena.alloc(item);
542529
self.owners.ensure_contains_elem(id.def_id, || None);
543530
self.owners[id.def_id] = Some(hir::OwnerNode::ImplItem(item));
544-
self.modules.entry(self.current_module).or_default().impl_items.insert(id);
545531
id
546532
}
547533

@@ -550,7 +536,6 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
550536
let item = self.arena.alloc(item);
551537
self.owners.ensure_contains_elem(id.def_id, || None);
552538
self.owners[id.def_id] = Some(hir::OwnerNode::TraitItem(item));
553-
self.modules.entry(self.current_module).or_default().trait_items.insert(id);
554539
id
555540
}
556541

compiler/rustc_hir/src/hir.rs

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ use rustc_target::asm::InlineAsmRegOrRegClass;
2121
use rustc_target::spec::abi::Abi;
2222

2323
use smallvec::SmallVec;
24-
use std::collections::{BTreeMap, BTreeSet};
24+
use std::collections::BTreeMap;
2525
use std::fmt;
2626

2727
#[derive(Copy, Clone, Encodable, HashStable_Generic)]
@@ -653,16 +653,6 @@ pub struct WhereEqPredicate<'hir> {
653653
pub rhs_ty: &'hir Ty<'hir>,
654654
}
655655

656-
#[derive(Default, Encodable, Debug, HashStable_Generic)]
657-
pub struct ModuleItems {
658-
// Use BTreeSets here so items are in the same order as in the
659-
// list of all items in Crate
660-
pub items: BTreeSet<ItemId>,
661-
pub trait_items: BTreeSet<TraitItemId>,
662-
pub impl_items: BTreeSet<ImplItemId>,
663-
pub foreign_items: BTreeSet<ForeignItemId>,
664-
}
665-
666656
/// The top-level data structure that stores the entire contents of
667657
/// the crate currently being compiled.
668658
///
@@ -674,10 +664,6 @@ pub struct Crate<'hir> {
674664
pub owners: IndexVec<LocalDefId, Option<OwnerNode<'hir>>>,
675665
pub bodies: BTreeMap<BodyId, Body<'hir>>,
676666

677-
/// A list of modules written out in the order in which they
678-
/// appear in the crate. This includes the main crate module.
679-
pub modules: BTreeMap<LocalDefId, ModuleItems>,
680-
681667
/// Map indicating what traits are in scope for places where this
682668
/// is relevant; generated by resolve.
683669
pub trait_map: FxHashMap<LocalDefId, FxHashMap<ItemLocalId, Box<[TraitCandidate]>>>,

compiler/rustc_interface/src/passes.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use rustc_borrowck as mir_borrowck;
88
use rustc_codegen_ssa::back::link::emit_metadata;
99
use rustc_codegen_ssa::traits::CodegenBackend;
1010
use rustc_data_structures::parallel;
11-
use rustc_data_structures::sync::{par_iter, Lrc, OnceCell, ParallelIterator, WorkerLocal};
11+
use rustc_data_structures::sync::{Lrc, OnceCell, WorkerLocal};
1212
use rustc_data_structures::temp_dir::MaybeTempDir;
1313
use rustc_errors::{ErrorReported, PResult};
1414
use rustc_expand::base::ExtCtxt;
@@ -861,7 +861,7 @@ fn analysis(tcx: TyCtxt<'_>, (): ()) -> Result<()> {
861861
CStore::from_tcx(tcx).report_unused_deps(tcx);
862862
},
863863
{
864-
par_iter(&tcx.hir().krate().modules).for_each(|(&module, _)| {
864+
tcx.hir().par_for_each_module(|module| {
865865
tcx.ensure().check_mod_loops(module);
866866
tcx.ensure().check_mod_attrs(module);
867867
tcx.ensure().check_mod_naked_functions(module);
@@ -893,7 +893,7 @@ fn analysis(tcx: TyCtxt<'_>, (): ()) -> Result<()> {
893893
},
894894
{
895895
sess.time("liveness_and_intrinsic_checking", || {
896-
par_iter(&tcx.hir().krate().modules).for_each(|(&module, _)| {
896+
tcx.hir().par_for_each_module(|module| {
897897
// this must run before MIR dump, because
898898
// "not all control paths return a value" is reported here.
899899
//
@@ -963,7 +963,7 @@ fn analysis(tcx: TyCtxt<'_>, (): ()) -> Result<()> {
963963
},
964964
{
965965
sess.time("privacy_checking_modules", || {
966-
par_iter(&tcx.hir().krate().modules).for_each(|(&module, _)| {
966+
tcx.hir().par_for_each_module(|module| {
967967
tcx.ensure().check_mod_privacy(module);
968968
});
969969
});

compiler/rustc_lint/src/late.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
1717
use crate::{passes::LateLintPassObject, LateContext, LateLintPass, LintStore};
1818
use rustc_ast as ast;
19-
use rustc_data_structures::sync::{join, par_iter, ParallelIterator};
19+
use rustc_data_structures::sync::join;
2020
use rustc_hir as hir;
2121
use rustc_hir::def_id::LocalDefId;
2222
use rustc_hir::intravisit as hir_visit;
@@ -501,9 +501,7 @@ pub fn check_crate<'tcx, T: LateLintPass<'tcx>>(
501501
|| {
502502
tcx.sess.time("module_lints", || {
503503
// Run per-module lints
504-
par_iter(&tcx.hir().krate().modules).for_each(|(&module, _)| {
505-
tcx.ensure().lint_mod(module);
506-
});
504+
tcx.hir().par_for_each_module(|module| tcx.ensure().lint_mod(module));
507505
});
508506
},
509507
);

compiler/rustc_middle/src/hir/map/mod.rs

Lines changed: 88 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
use self::collector::NodeCollector;
22

3-
use crate::hir::{AttributeMap, IndexedHir, Owner};
3+
use crate::hir::{AttributeMap, IndexedHir, ModuleItems, Owner};
44
use crate::ty::TyCtxt;
55
use rustc_ast as ast;
66
use rustc_data_structures::fingerprint::Fingerprint;
77
use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
88
use rustc_data_structures::svh::Svh;
9+
use rustc_data_structures::sync::{self, par_iter};
910
use rustc_hir::def::{DefKind, Res};
1011
use rustc_hir::def_id::{CrateNum, DefId, LocalDefId, CRATE_DEF_ID, CRATE_DEF_INDEX, LOCAL_CRATE};
1112
use rustc_hir::definitions::{DefKey, DefPath, DefPathHash};
@@ -19,6 +20,7 @@ use rustc_span::source_map::Spanned;
1920
use rustc_span::symbol::{kw, sym, Ident, Symbol};
2021
use rustc_span::Span;
2122
use rustc_target::spec::abi::Abi;
23+
use std::collections::BTreeSet;
2224

2325
pub mod blocks;
2426
mod collector;
@@ -558,6 +560,31 @@ impl<'hir> Map<'hir> {
558560
}
559561
}
560562

563+
pub fn for_each_module(&self, f: impl Fn(LocalDefId)) {
564+
let mut queue = BTreeSet::default();
565+
queue.insert(CRATE_DEF_ID);
566+
567+
while let Some(id) = queue.pop_first() {
568+
f(id);
569+
let items = self.tcx.hir_module_items(id);
570+
queue.extend(items.submodules.iter().copied())
571+
}
572+
}
573+
574+
pub fn par_for_each_module(&self, f: impl Fn(LocalDefId) + sync::Sync) {
575+
use rustc_data_structures::sync::ParallelIterator;
576+
par_iter_submodules(self.tcx, CRATE_DEF_ID, &f);
577+
578+
fn par_iter_submodules<F>(tcx: TyCtxt<'_>, module: LocalDefId, f: &F)
579+
where
580+
F: Fn(LocalDefId) + sync::Sync,
581+
{
582+
(*f)(module);
583+
let items = tcx.hir_module_items(module);
584+
par_iter(&items.submodules).for_each(|&sm| par_iter_submodules(tcx, sm, f));
585+
}
586+
}
587+
561588
/// Returns an iterator for the nodes in the ancestor tree of the `current_id`
562589
/// until the crate root is reached. Prefer this over your own loop using `get_parent_node`.
563590
pub fn parent_iter(&self, current_id: HirId) -> ParentHirIterator<'_, 'hir> {
@@ -1118,3 +1145,63 @@ fn hir_id_to_string(map: &Map<'_>, id: HirId) -> String {
11181145
None => format!("unknown node{}", id_str),
11191146
}
11201147
}
1148+
1149+
pub(super) fn hir_module_items(tcx: TyCtxt<'_>, module_id: LocalDefId) -> ModuleItems {
1150+
let mut collector = ModuleCollector {
1151+
tcx,
1152+
submodules: BTreeSet::default(),
1153+
items: BTreeSet::default(),
1154+
trait_items: BTreeSet::default(),
1155+
impl_items: BTreeSet::default(),
1156+
foreign_items: BTreeSet::default(),
1157+
};
1158+
1159+
let (hir_mod, span, hir_id) = tcx.hir().get_module(module_id);
1160+
collector.visit_mod(hir_mod, span, hir_id);
1161+
1162+
let ModuleCollector { submodules, items, trait_items, impl_items, foreign_items, .. } =
1163+
collector;
1164+
return ModuleItems { submodules, items, trait_items, impl_items, foreign_items };
1165+
1166+
struct ModuleCollector<'tcx> {
1167+
tcx: TyCtxt<'tcx>,
1168+
submodules: BTreeSet<LocalDefId>,
1169+
items: BTreeSet<ItemId>,
1170+
trait_items: BTreeSet<TraitItemId>,
1171+
impl_items: BTreeSet<ImplItemId>,
1172+
foreign_items: BTreeSet<ForeignItemId>,
1173+
}
1174+
1175+
impl<'hir> Visitor<'hir> for ModuleCollector<'hir> {
1176+
type Map = Map<'hir>;
1177+
1178+
fn nested_visit_map(&mut self) -> intravisit::NestedVisitorMap<Self::Map> {
1179+
intravisit::NestedVisitorMap::All(self.tcx.hir())
1180+
}
1181+
1182+
fn visit_item(&mut self, item: &'hir Item<'hir>) {
1183+
self.items.insert(item.item_id());
1184+
if let ItemKind::Mod(..) = item.kind {
1185+
// If this declares another module, do not recurse inside it.
1186+
self.submodules.insert(item.def_id);
1187+
} else {
1188+
intravisit::walk_item(self, item)
1189+
}
1190+
}
1191+
1192+
fn visit_trait_item(&mut self, item: &'hir TraitItem<'hir>) {
1193+
self.trait_items.insert(item.trait_item_id());
1194+
intravisit::walk_trait_item(self, item)
1195+
}
1196+
1197+
fn visit_impl_item(&mut self, item: &'hir ImplItem<'hir>) {
1198+
self.impl_items.insert(item.impl_item_id());
1199+
intravisit::walk_impl_item(self, item)
1200+
}
1201+
1202+
fn visit_foreign_item(&mut self, item: &'hir ForeignItem<'hir>) {
1203+
self.foreign_items.insert(item.foreign_item_id());
1204+
intravisit::walk_foreign_item(self, item)
1205+
}
1206+
}
1207+
}

compiler/rustc_middle/src/hir/mod.rs

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ use rustc_hir::def_id::LocalDefId;
1717
use rustc_hir::*;
1818
use rustc_index::vec::{Idx, IndexVec};
1919
use rustc_span::DUMMY_SP;
20-
use std::collections::BTreeMap;
20+
use std::collections::{BTreeMap, BTreeSet};
2121

2222
/// Result of HIR indexing.
2323
#[derive(Debug)]
@@ -121,6 +121,17 @@ impl<'tcx> AttributeMap<'tcx> {
121121
}
122122
}
123123

124+
#[derive(Default, Encodable, Debug, HashStable)]
125+
pub struct ModuleItems {
126+
// Use BTreeSets here so items are in the same order as in the
127+
// list of all items in Crate
128+
submodules: BTreeSet<LocalDefId>,
129+
items: BTreeSet<ItemId>,
130+
trait_items: BTreeSet<TraitItemId>,
131+
impl_items: BTreeSet<ImplItemId>,
132+
foreign_items: BTreeSet<ForeignItemId>,
133+
}
134+
124135
impl<'tcx> TyCtxt<'tcx> {
125136
#[inline(always)]
126137
pub fn hir(self) -> map::Map<'tcx> {
@@ -140,7 +151,7 @@ pub fn provide(providers: &mut Providers) {
140151
providers.hir_crate = |tcx, ()| tcx.untracked_crate;
141152
providers.index_hir = map::index_hir;
142153
providers.crate_hash = map::crate_hash;
143-
providers.hir_module_items = |tcx, id| &tcx.untracked_crate.modules[&id];
154+
providers.hir_module_items = map::hir_module_items;
144155
providers.hir_owner = |tcx, id| {
145156
let owner = tcx.index_hir(()).map[id].as_ref()?;
146157
let node = owner.nodes[ItemLocalId::new(0)].as_ref().unwrap().node;

compiler/rustc_middle/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
#![feature(discriminant_kind)]
3434
#![feature(exhaustive_patterns)]
3535
#![feature(if_let_guard)]
36+
#![feature(map_first_last)]
3637
#![feature(never_type)]
3738
#![feature(extern_types)]
3839
#![feature(new_uninit)]

compiler/rustc_middle/src/query/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,8 @@ rustc_queries! {
5252
///
5353
/// This can be conveniently accessed by `tcx.hir().visit_item_likes_in_module`.
5454
/// Avoid calling this query directly.
55-
query hir_module_items(key: LocalDefId) -> &'tcx hir::ModuleItems {
56-
eval_always
55+
query hir_module_items(key: LocalDefId) -> rustc_middle::hir::ModuleItems {
56+
storage(ArenaCacheSelector<'tcx>)
5757
desc { |tcx| "HIR module items in `{}`", tcx.def_path_str(key.to_def_id()) }
5858
}
5959

compiler/rustc_passes/src/hir_id_validator.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use rustc_data_structures::fx::FxHashSet;
2-
use rustc_data_structures::sync::{par_iter, Lock, ParallelIterator};
2+
use rustc_data_structures::sync::Lock;
33
use rustc_hir as hir;
44
use rustc_hir::def_id::{LocalDefId, CRATE_DEF_INDEX};
55
use rustc_hir::intravisit;
@@ -18,9 +18,9 @@ pub fn check_crate(tcx: TyCtxt<'_>) {
1818
let errors = Lock::new(Vec::new());
1919
let hir_map = tcx.hir();
2020

21-
par_iter(&hir_map.krate().modules).for_each(|(&module_id, _)| {
21+
hir_map.par_for_each_module(|module_id| {
2222
hir_map
23-
.visit_item_likes_in_module(module_id, &mut OuterVisitor { hir_map, errors: &errors });
23+
.visit_item_likes_in_module(module_id, &mut OuterVisitor { hir_map, errors: &errors })
2424
});
2525

2626
let errors = errors.into_inner();

compiler/rustc_typeck/src/impl_wf_check.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,9 +58,7 @@ pub fn impl_wf_check(tcx: TyCtxt<'_>) {
5858
// We will tag this as part of the WF check -- logically, it is,
5959
// but it's one that we must perform earlier than the rest of
6060
// WfCheck.
61-
for &module in tcx.hir().krate().modules.keys() {
62-
tcx.ensure().check_mod_impl_wf(module);
63-
}
61+
tcx.hir().for_each_module(|module| tcx.ensure().check_mod_impl_wf(module))
6462
}
6563

6664
fn check_mod_impl_wf(tcx: TyCtxt<'_>, module_def_id: LocalDefId) {

0 commit comments

Comments
 (0)