Skip to content

Commit 3f8b0d7

Browse files
imported_module is a side effect as well.
1 parent 3ac602d commit 3f8b0d7

File tree

1 file changed

+43
-19
lines changed

1 file changed

+43
-19
lines changed

compiler/rustc_resolve/src/imports.rs

Lines changed: 43 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -41,16 +41,21 @@ use crate::{
4141

4242
type Res = def::Res<NodeId>;
4343

44-
enum SideEffect<'ra> {
44+
enum SideEffectBindings<'ra> {
4545
None,
4646
Single { import_bindings: PerNS<PendingBinding<'ra>> },
4747
Glob { import_bindings: Vec<(NameBinding<'ra>, BindingKey, bool /* warn_ambiguity */)> },
4848
}
4949

50+
struct SideEffect<'ra> {
51+
imported_module: ModuleOrUniformRoot<'ra>,
52+
bindings: SideEffectBindings<'ra>,
53+
}
54+
5055
#[derive(Default)]
5156
struct ImportResolutionOutputs<'ra> {
5257
indeterminate_imports: Vec<Import<'ra>>,
53-
determined_imports: Vec<(Import<'ra>, SideEffect<'ra>)>,
58+
determined_imports: Vec<(Import<'ra>, Option<SideEffect<'ra>>)>,
5459
}
5560

5661
impl<'ra> ImportResolutionOutputs<'ra> {
@@ -65,11 +70,17 @@ impl<'ra> ImportResolutionOutputs<'ra> {
6570
for (import, side_effect) in self.determined_imports {
6671
r.determined_imports.push(import);
6772

73+
let Some(SideEffect { imported_module, bindings: side_effect_bindings }) = side_effect
74+
else {
75+
return;
76+
};
77+
import.imported_module.set(Some(imported_module));
6878
let parent = import.parent_scope.module;
69-
match (&import.kind, side_effect) {
79+
80+
match (&import.kind, side_effect_bindings) {
7081
(
7182
ImportKind::Single { target, bindings, .. },
72-
SideEffect::Single { import_bindings },
83+
SideEffectBindings::Single { import_bindings },
7384
) => {
7485
for (ns, pending_binding) in import_bindings.into_iter_with() {
7586
match pending_binding {
@@ -87,10 +98,11 @@ impl<'ra> ImportResolutionOutputs<'ra> {
8798
bindings[ns].set(pending_binding);
8899
}
89100
}
90-
(ImportKind::Glob { id, .. }, SideEffect::Glob { import_bindings }) => {
101+
(ImportKind::Glob { id, .. }, SideEffectBindings::Glob { import_bindings }) => {
91102
let ModuleOrUniformRoot::Module(module) = import.imported_module.get().unwrap()
92103
else {
93-
unreachable!();
104+
r.dcx().emit_err(CannotGlobImportAllCrates { span: import.span });
105+
continue;
94106
};
95107

96108
module.glob_importers.borrow_mut().push(import);
@@ -108,7 +120,8 @@ impl<'ra> ImportResolutionOutputs<'ra> {
108120
r.record_partial_res(*id, PartialRes::new(module.res().unwrap()));
109121
}
110122

111-
(_, SideEffect::None) => {}
123+
(_, SideEffectBindings::None) => {}
124+
112125
// Something weird happened, which shouldn't have happened.
113126
_ => unreachable!("Mismatched import kind and side effect"),
114127
}
@@ -914,7 +927,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
914927
fn resolve_import<'r>(
915928
self: &mut CmResolver<'r, 'ra, 'tcx>,
916929
import: Import<'ra>,
917-
) -> (SideEffect<'ra>, usize) {
930+
) -> (Option<SideEffect<'ra>>, usize) {
918931
debug!(
919932
"(resolving import for module) resolving import `{}::...` in `{}`",
920933
Segment::names_to_string(&import.module_path),
@@ -932,20 +945,25 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
932945

933946
match path_res {
934947
PathResult::Module(module) => module,
935-
PathResult::Indeterminate => return (SideEffect::None, 3),
948+
PathResult::Indeterminate => return (None, 3),
936949
PathResult::NonModule(..) | PathResult::Failed { .. } => {
937-
return (SideEffect::None, 0);
950+
return (None, 0);
938951
}
939952
}
940953
};
941954

942-
import.imported_module.set(Some(module));
943955
let (source, target, bindings, type_ns_only) = match import.kind {
944956
ImportKind::Single { source, target, ref bindings, type_ns_only, .. } => {
945957
(source, target, bindings, type_ns_only)
946958
}
947959
ImportKind::Glob { .. } => {
948-
return (self.resolve_glob_import(import), 0);
960+
return (
961+
Some(SideEffect {
962+
imported_module: module,
963+
bindings: self.resolve_glob_import(import, module),
964+
}),
965+
0,
966+
);
949967
}
950968
_ => unreachable!(),
951969
};
@@ -997,7 +1015,13 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
9971015
}
9981016
});
9991017

1000-
(SideEffect::Single { import_bindings }, indeterminate_count)
1018+
(
1019+
Some(SideEffect {
1020+
imported_module: module,
1021+
bindings: SideEffectBindings::Single { import_bindings },
1022+
}),
1023+
indeterminate_count,
1024+
)
10011025
}
10021026

10031027
/// Performs final import resolution, consistency checks and error reporting.
@@ -1540,13 +1564,13 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
15401564
fn resolve_glob_import<'r>(
15411565
self: &mut CmResolver<'r, 'ra, 'tcx>,
15421566
import: Import<'ra>,
1543-
) -> SideEffect<'ra> {
1567+
imported_module: ModuleOrUniformRoot<'ra>,
1568+
) -> SideEffectBindings<'ra> {
15441569
// This function is only called for glob imports.
15451570
let ImportKind::Glob { .. } = import.kind else { unreachable!() };
15461571

1547-
let ModuleOrUniformRoot::Module(module) = import.imported_module.get().unwrap() else {
1548-
self.dcx().emit_err(CannotGlobImportAllCrates { span: import.span });
1549-
return SideEffect::None;
1572+
let ModuleOrUniformRoot::Module(module) = imported_module else {
1573+
return SideEffectBindings::None;
15501574
};
15511575

15521576
if module.is_trait() && !self.tcx.features().import_trait_associated_functions() {
@@ -1560,7 +1584,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
15601584
}
15611585

15621586
if module == import.parent_scope.module {
1563-
return SideEffect::None;
1587+
return SideEffectBindings::None;
15641588
}
15651589

15661590
// Ensure that `resolutions` isn't borrowed during `try_define`,
@@ -1591,7 +1615,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
15911615
}
15921616

15931617
// Record the destination of this import
1594-
SideEffect::Glob { import_bindings }
1618+
SideEffectBindings::Glob { import_bindings }
15951619
}
15961620

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

0 commit comments

Comments
 (0)