Skip to content

Commit 9aa347e

Browse files
isolate single import
1 parent 75c497c commit 9aa347e

File tree

3 files changed

+56
-25
lines changed

3 files changed

+56
-25
lines changed

compiler/rustc_hir/src/def.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -619,6 +619,24 @@ impl<T> PerNS<T> {
619619
pub fn iter(&self) -> IntoIter<&T, 3> {
620620
[&self.value_ns, &self.type_ns, &self.macro_ns].into_iter()
621621
}
622+
623+
pub fn into_iter_with(self) -> IntoIter<(Namespace, T), 3> {
624+
[
625+
(Namespace::TypeNS, self.type_ns),
626+
(Namespace::ValueNS, self.value_ns),
627+
(Namespace::MacroNS, self.macro_ns),
628+
]
629+
.into_iter()
630+
}
631+
632+
pub fn iter_with(&self) -> IntoIter<(Namespace, &T), 3> {
633+
[
634+
(Namespace::TypeNS, &self.type_ns),
635+
(Namespace::ValueNS, &self.value_ns),
636+
(Namespace::MacroNS, &self.macro_ns),
637+
]
638+
.into_iter()
639+
}
622640
}
623641

624642
impl<T> ::std::ops::Index<Namespace> for PerNS<T> {

compiler/rustc_resolve/src/imports.rs

Lines changed: 37 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -49,24 +49,33 @@ pub(crate) struct ImportResolver<'r, 'ra, 'tcx> {
4949
// outputs
5050
determined_imports: Vec<Import<'ra>>,
5151
glob_imports: Vec<Import<'ra>>,
52+
import_bindings: PerNS<Vec<(Module<'ra>, Import<'ra>, PendingBinding<'ra>)>>,
5253
}
5354

5455
struct ImportResolutionOutputs<'ra> {
5556
indeterminate_imports: Vec<Import<'ra>>,
5657
determined_imports: Vec<Import<'ra>>,
5758
glob_imports: Vec<Import<'ra>>,
59+
import_bindings: PerNS<Vec<(Module<'ra>, Import<'ra>, PendingBinding<'ra>)>>,
5860
}
5961

6062
impl<'r, 'ra, 'tcx> ImportResolver<'r, 'ra, 'tcx> {
6163
pub(crate) fn new(cmr: CmResolver<'r, 'ra, 'tcx>, batch: Vec<Import<'ra>>) -> Self {
62-
ImportResolver { r: cmr, batch, determined_imports: Vec::new(), glob_imports: Vec::new() }
64+
ImportResolver {
65+
r: cmr,
66+
batch,
67+
determined_imports: Vec::new(),
68+
glob_imports: Vec::new(),
69+
import_bindings: PerNS::default(),
70+
}
6371
}
6472

6573
fn into_outputs(self) -> ImportResolutionOutputs<'ra> {
6674
ImportResolutionOutputs {
6775
indeterminate_imports: self.batch,
6876
determined_imports: self.determined_imports,
6977
glob_imports: self.glob_imports,
78+
import_bindings: self.import_bindings,
7079
}
7180
}
7281
}
@@ -78,6 +87,27 @@ impl<'ra> ImportResolutionOutputs<'ra> {
7887
for glob in self.glob_imports {
7988
r.resolve_glob_import(glob);
8089
}
90+
91+
for (ns, import_bindings) in self.import_bindings.into_iter_with() {
92+
for (parent, import, pending_binding) in import_bindings {
93+
let ImportKind::Single { target, ref bindings, .. } = import.kind else {
94+
unreachable!();
95+
};
96+
match pending_binding {
97+
PendingBinding::Ready(Some(binding)) => {
98+
r.define_binding_local(parent, target, ns, binding);
99+
}
100+
PendingBinding::Ready(None) => {
101+
let key = BindingKey::new(target, ns);
102+
r.update_local_resolution(parent, key, false, |_, resolution| {
103+
resolution.single_imports.swap_remove(&import);
104+
});
105+
}
106+
_ => {}
107+
}
108+
bindings[ns].set(pending_binding);
109+
}
110+
}
81111
}
82112
}
83113

@@ -927,6 +957,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
927957
}
928958
};
929959

960+
// FIXME: isolate if not a cache.
930961
import.imported_module.set(Some(module));
931962
let (source, target, bindings, type_ns_only) = match import.kind {
932963
ImportKind::Single { source, target, ref bindings, type_ns_only, .. } => {
@@ -953,7 +984,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
953984
Some(import),
954985
);
955986
let parent = import.parent_scope.module;
956-
let binding = match binding_result {
987+
let pending_binding = match binding_result {
957988
Ok(binding) => {
958989
if binding.is_assoc_item()
959990
&& !this.tcx.features().import_trait_associated_functions()
@@ -968,39 +999,21 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
968999
}
9691000
// We need the `target`, `source` can be extracted.
9701001
let imported_binding = this.import(binding, import);
971-
// FIXME: Use mutable resolver directly as a hack, this should be an output of
972-
// specualtive resolution.
973-
this.get_mut_unchecked().define_binding_local(
974-
parent,
975-
target,
976-
ns,
977-
imported_binding,
978-
);
9791002
PendingBinding::Ready(Some(imported_binding))
9801003
}
9811004
Err(Determinacy::Determined) => {
9821005
// Don't remove underscores from `single_imports`, they were never added.
983-
if target.name != kw::Underscore {
984-
let key = BindingKey::new(target, ns);
985-
// FIXME: Use mutable resolver directly as a hack, this should be an output of
986-
// specualtive resolution.
987-
this.get_mut_unchecked().update_local_resolution(
988-
parent,
989-
key,
990-
false,
991-
|_, resolution| {
992-
resolution.single_imports.swap_remove(&import);
993-
},
994-
);
1006+
if target.name == kw::Underscore {
1007+
return;
9951008
}
9961009
PendingBinding::Ready(None)
9971010
}
9981011
Err(Determinacy::Undetermined) => {
9991012
indeterminate_count += 1;
1000-
PendingBinding::Pending
1013+
return;
10011014
}
10021015
};
1003-
bindings[ns].set(binding);
1016+
self.import_bindings[ns].push((parent, import, pending_binding));
10041017
}
10051018
});
10061019

compiler/rustc_resolve/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2504,7 +2504,7 @@ mod ref_mut {
25042504

25052505
/// Returns a mutable reference to the inner value without checking if
25062506
/// it's in a mutable state.
2507-
pub(crate) fn get_mut_unchecked(&mut self) -> &mut T {
2507+
pub(crate) fn _get_mut_unchecked(&mut self) -> &mut T {
25082508
self.p
25092509
}
25102510
}

0 commit comments

Comments
 (0)