Skip to content

Commit 64d7519

Browse files
refactor usage of ImportResolutionOutputs
1 parent 94bb516 commit 64d7519

File tree

1 file changed

+21
-43
lines changed

1 file changed

+21
-43
lines changed

compiler/rustc_resolve/src/imports.rs

Lines changed: 21 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -44,51 +44,28 @@ type Res = def::Res<NodeId>;
4444

4545
pub(crate) struct ImportResolver<'r, 'ra, 'tcx> {
4646
r: CmResolver<'r, 'ra, 'tcx>, // always immutable
47-
batch: Vec<Import<'ra>>, // a.k.a. indeterminate_imports, also treated as output
48-
49-
// outputs
50-
prelude: Option<Module<'ra>>,
51-
determined_imports: Vec<Import<'ra>>,
52-
module_glob_importers: FxIndexMap<Module<'ra>, Vec<Import<'ra>>>,
53-
glob_import_bindings: Vec<(Module<'ra>, BindingKey, NameBinding<'ra>, bool)>,
54-
glob_res_outputs: Vec<(NodeId, PartialRes)>,
55-
import_bindings: PerNS<Vec<(Module<'ra>, Import<'ra>, PendingBinding<'ra>)>>,
47+
outputs: ImportResolutionOutputs<'ra>,
5648
}
5749

50+
#[derive(Default)]
5851
struct ImportResolutionOutputs<'ra> {
5952
prelude: Option<Module<'ra>>,
6053
indeterminate_imports: Vec<Import<'ra>>,
6154
determined_imports: Vec<Import<'ra>>,
6255
module_glob_importers: FxIndexMap<Module<'ra>, Vec<Import<'ra>>>,
63-
glob_import_bindings: Vec<(Module<'ra>, BindingKey, NameBinding<'ra>, bool)>,
64-
glob_res_outputs: Vec<(NodeId, PartialRes)>,
65-
import_bindings: PerNS<Vec<(Module<'ra>, Import<'ra>, PendingBinding<'ra>)>>,
56+
glob_import_bindings:
57+
Vec<(Module<'ra>, BindingKey, NameBinding<'ra>, bool /* warn_ambiguity */)>,
58+
glob_path_res: Vec<(NodeId, PartialRes)>,
59+
single_import_bindings: PerNS<Vec<(Module<'ra>, Import<'ra>, PendingBinding<'ra>)>>,
6660
}
6761

6862
impl<'r, 'ra, 'tcx> ImportResolver<'r, 'ra, 'tcx> {
69-
pub(crate) fn new(cmr: CmResolver<'r, 'ra, 'tcx>, batch: Vec<Import<'ra>>) -> Self {
70-
ImportResolver {
71-
r: cmr,
72-
batch,
73-
prelude: None,
74-
determined_imports: Default::default(),
75-
module_glob_importers: Default::default(),
76-
glob_import_bindings: Default::default(),
77-
glob_res_outputs: Default::default(),
78-
import_bindings: Default::default(),
79-
}
63+
pub(crate) fn new(cmr: CmResolver<'r, 'ra, 'tcx>) -> Self {
64+
ImportResolver { r: cmr, outputs: Default::default() }
8065
}
8166

8267
fn into_outputs(self) -> ImportResolutionOutputs<'ra> {
83-
ImportResolutionOutputs {
84-
prelude: self.prelude,
85-
indeterminate_imports: self.batch,
86-
determined_imports: self.determined_imports,
87-
module_glob_importers: self.module_glob_importers,
88-
glob_import_bindings: self.glob_import_bindings,
89-
glob_res_outputs: self.glob_res_outputs,
90-
import_bindings: self.import_bindings,
91-
}
68+
self.outputs
9269
}
9370
}
9471

@@ -111,11 +88,11 @@ impl<'ra> ImportResolutionOutputs<'ra> {
11188
let _ = r.try_define_local(module, key.ident.0, key.ns, binding, warn_ambiguity);
11289
}
11390

114-
for (id, res) in self.glob_res_outputs {
91+
for (id, res) in self.glob_path_res {
11592
r.record_partial_res(id, res);
11693
}
11794

118-
for (ns, import_bindings) in self.import_bindings.into_iter_with() {
95+
for (ns, import_bindings) in self.single_import_bindings.into_iter_with() {
11996
for (parent, import, pending_binding) in import_bindings {
12097
let ImportKind::Single { target, ref bindings, .. } = import.kind else {
12198
unreachable!();
@@ -669,7 +646,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
669646
prev_indeterminate_count = indeterminate_count;
670647
let batch = mem::take(&mut self.indeterminate_imports);
671648
self.assert_speculative = true;
672-
let (outputs, count) = ImportResolver::new(self.cm(), batch).resolve_batch();
649+
let (outputs, count) = ImportResolver::new(self.cm()).resolve_batch(batch);
673650
self.assert_speculative = false;
674651
indeterminate_count = count;
675652
outputs.commit(self);
@@ -678,14 +655,15 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
678655

679656
fn resolve_batch<'r>(
680657
mut self: ImportResolver<'r, 'ra, 'tcx>,
658+
batch: Vec<Import<'ra>>,
681659
) -> (ImportResolutionOutputs<'ra>, usize) {
682660
let mut indeterminate_count = 0;
683-
for import in mem::take(&mut self.batch) {
661+
for import in batch {
684662
let import_indeterminate_count = self.resolve_import(import);
685663
indeterminate_count += import_indeterminate_count;
686664
match import_indeterminate_count {
687-
0 => self.determined_imports.push(import),
688-
_ => self.batch.push(import),
665+
0 => self.outputs.determined_imports.push(import),
666+
_ => self.outputs.indeterminate_imports.push(import),
689667
}
690668
}
691669
(self.into_outputs(), indeterminate_count)
@@ -1040,7 +1018,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
10401018
return;
10411019
}
10421020
};
1043-
self.import_bindings[ns].push((parent, import, pending_binding));
1021+
self.outputs.single_import_bindings[ns].push((parent, import, pending_binding));
10441022
}
10451023
});
10461024

@@ -1607,12 +1585,12 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
16071585
if module == import.parent_scope.module {
16081586
return;
16091587
} else if is_prelude {
1610-
self.prelude = Some(module);
1588+
self.outputs.prelude = Some(module);
16111589
return;
16121590
}
16131591

16141592
// Add to module's glob_importers
1615-
self.module_glob_importers.entry(module).or_default().push(import);
1593+
self.outputs.module_glob_importers.entry(module).or_default().push(import);
16161594

16171595
// Ensure that `resolutions` isn't borrowed during `try_define`,
16181596
// since it might get updated via a glob cycle.
@@ -1636,7 +1614,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
16361614
.resolution(import.parent_scope.module, key)
16371615
.and_then(|r| r.binding())
16381616
.is_some_and(|binding| binding.warn_ambiguity_recursive());
1639-
self.glob_import_bindings.push((
1617+
self.outputs.glob_import_bindings.push((
16401618
import.parent_scope.module,
16411619
key,
16421620
imported_binding,
@@ -1646,7 +1624,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
16461624
}
16471625

16481626
// Record the destination of this import
1649-
self.glob_res_outputs.push((id, PartialRes::new(module.res().unwrap())));
1627+
self.outputs.glob_path_res.push((id, PartialRes::new(module.res().unwrap())));
16501628
}
16511629

16521630
// Miscellaneous post-processing, including recording re-exports,

0 commit comments

Comments
 (0)