Skip to content

Commit 64d9d42

Browse files
committed
resolver: Move local_modules from ResolverArenas to Resolver
Thus avoiding a `RefCell`.
1 parent 0b278a5 commit 64d9d42

File tree

3 files changed

+26
-25
lines changed

3 files changed

+26
-25
lines changed

compiler/rustc_resolve/src/check_unused.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -507,7 +507,7 @@ impl Resolver<'_, '_> {
507507

508508
let unused_imports = visitor.unused_imports;
509509
let mut check_redundant_imports = FxIndexSet::default();
510-
for module in self.arenas.local_modules().iter() {
510+
for module in &self.local_modules {
511511
for (_key, resolution) in self.resolutions(*module).borrow().iter() {
512512
if let Some(binding) = resolution.borrow().best_binding()
513513
&& let NameBindingKind::Import { import, .. } = binding.kind

compiler/rustc_resolve/src/imports.rs

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use rustc_data_structures::intern::Interned;
88
use rustc_errors::codes::*;
99
use rustc_errors::{Applicability, MultiSpan, pluralize, struct_span_code_err};
1010
use rustc_hir::def::{self, DefKind, PartialRes};
11-
use rustc_hir::def_id::DefId;
11+
use rustc_hir::def_id::{DefId, LocalDefIdMap};
1212
use rustc_middle::metadata::{ModChild, Reexport};
1313
use rustc_middle::span_bug;
1414
use rustc_middle::ty::Visibility;
@@ -572,9 +572,11 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
572572
}
573573

574574
pub(crate) fn finalize_imports(&mut self) {
575-
for module in self.arenas.local_modules().iter() {
576-
self.finalize_resolutions_in(*module);
575+
let mut module_children = Default::default();
576+
for module in &self.local_modules {
577+
self.finalize_resolutions_in(*module, &mut module_children);
577578
}
579+
self.module_children = module_children;
578580

579581
let mut seen_spans = FxHashSet::default();
580582
let mut errors = vec![];
@@ -651,7 +653,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
651653
}
652654

653655
pub(crate) fn lint_reexports(&mut self, exported_ambiguities: FxHashSet<NameBinding<'ra>>) {
654-
for module in self.arenas.local_modules().iter() {
656+
for module in &self.local_modules {
655657
for (key, resolution) in self.resolutions(*module).borrow().iter() {
656658
let resolution = resolution.borrow();
657659
let Some(binding) = resolution.best_binding() else { continue };
@@ -1548,7 +1550,11 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
15481550

15491551
// Miscellaneous post-processing, including recording re-exports,
15501552
// reporting conflicts, and reporting unresolved imports.
1551-
fn finalize_resolutions_in(&mut self, module: Module<'ra>) {
1553+
fn finalize_resolutions_in(
1554+
&self,
1555+
module: Module<'ra>,
1556+
module_children: &mut LocalDefIdMap<Vec<ModChild>>,
1557+
) {
15521558
// Since import resolution is finished, globs will not define any more names.
15531559
*module.globs.borrow_mut(self) = Vec::new();
15541560

@@ -1573,7 +1579,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
15731579

15741580
if !children.is_empty() {
15751581
// Should be fine because this code is only called for local modules.
1576-
self.module_children.insert(def_id.expect_local(), children);
1582+
module_children.insert(def_id.expect_local(), children);
15771583
}
15781584
}
15791585
}

compiler/rustc_resolve/src/lib.rs

Lines changed: 13 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1145,6 +1145,8 @@ pub struct Resolver<'ra, 'tcx> {
11451145
/// some AST passes can generate identifiers that only resolve to local or
11461146
/// lang items.
11471147
empty_module: Module<'ra>,
1148+
/// All local modules, including blocks.
1149+
local_modules: Vec<Module<'ra>>,
11481150
/// Eagerly populated map of all local non-block modules.
11491151
local_module_map: FxIndexMap<LocalDefId, Module<'ra>>,
11501152
/// Lazily populated cache of modules loaded from external crates.
@@ -1298,7 +1300,6 @@ pub struct Resolver<'ra, 'tcx> {
12981300
#[derive(Default)]
12991301
pub struct ResolverArenas<'ra> {
13001302
modules: TypedArena<ModuleData<'ra>>,
1301-
local_modules: RefCell<Vec<Module<'ra>>>,
13021303
imports: TypedArena<ImportData<'ra>>,
13031304
name_resolutions: TypedArena<RefCell<NameResolution<'ra>>>,
13041305
ast_paths: TypedArena<ast::Path>,
@@ -1341,28 +1342,20 @@ impl<'ra> ResolverArenas<'ra> {
13411342
span: Span,
13421343
no_implicit_prelude: bool,
13431344
) -> Module<'ra> {
1344-
let (def_id, self_binding) = match kind {
1345-
ModuleKind::Def(def_kind, def_id, _) => (
1346-
Some(def_id),
1347-
Some(self.new_pub_res_binding(Res::Def(def_kind, def_id), span, LocalExpnId::ROOT)),
1348-
),
1349-
ModuleKind::Block => (None, None),
1345+
let self_binding = match kind {
1346+
ModuleKind::Def(def_kind, def_id, _) => {
1347+
Some(self.new_pub_res_binding(Res::Def(def_kind, def_id), span, LocalExpnId::ROOT))
1348+
}
1349+
ModuleKind::Block => None,
13501350
};
1351-
let module = Module(Interned::new_unchecked(self.modules.alloc(ModuleData::new(
1351+
Module(Interned::new_unchecked(self.modules.alloc(ModuleData::new(
13521352
parent,
13531353
kind,
13541354
expn_id,
13551355
span,
13561356
no_implicit_prelude,
13571357
self_binding,
1358-
))));
1359-
if def_id.is_none_or(|def_id| def_id.is_local()) {
1360-
self.local_modules.borrow_mut().push(module);
1361-
}
1362-
module
1363-
}
1364-
fn local_modules(&'ra self) -> std::cell::Ref<'ra, Vec<Module<'ra>>> {
1365-
self.local_modules.borrow()
1358+
))))
13661359
}
13671360
fn alloc_name_binding(&'ra self, name_binding: NameBindingData<'ra>) -> NameBinding<'ra> {
13681361
Interned::new_unchecked(self.dropless.alloc(name_binding))
@@ -1505,15 +1498,15 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
15051498
arenas: &'ra ResolverArenas<'ra>,
15061499
) -> Resolver<'ra, 'tcx> {
15071500
let root_def_id = CRATE_DEF_ID.to_def_id();
1508-
let mut local_module_map = FxIndexMap::default();
15091501
let graph_root = arenas.new_module(
15101502
None,
15111503
ModuleKind::Def(DefKind::Mod, root_def_id, None),
15121504
ExpnId::root(),
15131505
crate_span,
15141506
attr::contains_name(attrs, sym::no_implicit_prelude),
15151507
);
1516-
local_module_map.insert(CRATE_DEF_ID, graph_root);
1508+
let local_modules = vec![graph_root];
1509+
let local_module_map = FxIndexMap::from_iter([(CRATE_DEF_ID, graph_root)]);
15171510
let empty_module = arenas.new_module(
15181511
None,
15191512
ModuleKind::Def(DefKind::Mod, root_def_id, None),
@@ -1591,6 +1584,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
15911584
module_children: Default::default(),
15921585
trait_map: NodeMap::default(),
15931586
empty_module,
1587+
local_modules,
15941588
local_module_map,
15951589
extern_module_map: Default::default(),
15961590
block_map: Default::default(),
@@ -1694,6 +1688,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
16941688
no_implicit_prelude: bool,
16951689
) -> Module<'ra> {
16961690
let module = self.arenas.new_module(parent, kind, expn_id, span, no_implicit_prelude);
1691+
self.local_modules.push(module);
16971692
if let Some(def_id) = module.opt_def_id() {
16981693
self.local_module_map.insert(def_id.expect_local(), module);
16991694
}

0 commit comments

Comments
 (0)