Skip to content

Commit 89322c9

Browse files
extract source_binding out of bindings
1 parent 3c23b46 commit 89322c9

File tree

2 files changed

+48
-26
lines changed

2 files changed

+48
-26
lines changed

compiler/rustc_resolve/src/imports.rs

Lines changed: 41 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -571,6 +571,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
571571
if let ImportKind::Single { source, ref bindings, .. } = import.kind
572572
&& source.name == kw::SelfLower
573573
// Silence `unresolved import` error if E0429 is already emitted
574+
// `indings` here is "target_bindings", if it is "Err(Determined)" then `source_bindings` as well.
574575
&& let Progress::Ready(None) = bindings.value_ns.get()
575576
{
576577
continue;
@@ -835,15 +836,16 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
835836
self.per_ns(|this, ns| {
836837
if !type_ns_only || ns == TypeNS {
837838
if let Progress::Pending = bindings[ns].get() {
838-
let binding = this.maybe_resolve_ident_in_module(
839+
let binding_result = this.maybe_resolve_ident_in_module(
839840
module,
840841
source,
841842
ns,
842843
&import.parent_scope,
843844
Some(import),
844845
);
845-
let binding = match binding {
846-
Ok(binding) => Poll::Ready(Some(binding)),
846+
let binding = match binding_result {
847+
// We need the `target`, `source` can be extracted.
848+
Ok(binding) => Poll::Ready(Some(this.import(binding, import))),
847849
Err(Determinacy::Determined) => Poll::Ready(None),
848850
Err(Determinacy::Undetermined) => Poll::Pending,
849851
};
@@ -866,10 +868,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
866868
)
867869
.emit();
868870
}
869-
870-
let imported_binding = this.import(binding, import);
871-
// target_bindings[ns].set(Some(imported_binding));
872-
this.define(parent, target, ns, imported_binding);
871+
this.define(parent, target, ns, binding);
873872
}
874873
Progress::Ready(None) => {
875874
// Don't update the resolution for underscores, because it was never added.
@@ -1104,21 +1103,25 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
11041103
match binding {
11051104
Ok(binding) => {
11061105
// Consistency checks, analogous to `finalize_macro_resolutions`.
1107-
let initial_res = bindings[ns].get().map(|initial_binding| {
1108-
all_ns_err = false;
1109-
if let Some(binding) = initial_binding
1110-
&& target.name == kw::Underscore
1111-
&& binding.is_extern_crate()
1112-
&& !binding.is_import()
1113-
{
1114-
let used = if import.module_path.is_empty() {
1115-
Used::Scope
1116-
} else {
1117-
Used::Other
1118-
};
1119-
this.record_use(ident, binding, used);
1120-
}
1121-
binding.res()
1106+
let initial_res = bindings[ns].get().map(|maybe_binding| {
1107+
maybe_binding.and_then(|binding| binding.maybe_source_binding()).map(
1108+
|initial_binding| {
1109+
all_ns_err = false;
1110+
if let Progress::Ready(Some(binding)) = bindings[ns].get()
1111+
&& target.name == kw::Underscore
1112+
&& initial_binding.is_extern_crate()
1113+
&& !initial_binding.is_import()
1114+
{
1115+
let used = if import.module_path.is_empty() {
1116+
Used::Scope
1117+
} else {
1118+
Used::Other
1119+
};
1120+
this.record_use(ident, binding, used);
1121+
}
1122+
binding.res()
1123+
},
1124+
)
11221125
});
11231126
let res = binding.res();
11241127
let has_ambiguity_error =
@@ -1128,7 +1131,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
11281131
.span_delayed_bug(import.span, "some error happened for an import");
11291132
return;
11301133
}
1131-
if let Progress::Ready(initial_res) = initial_res {
1134+
if let Progress::Ready(Some(initial_res)) = initial_res {
11321135
if res != initial_res {
11331136
span_bug!(import.span, "inconsistent resolution for an import");
11341137
}
@@ -1271,7 +1274,11 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
12711274
let mut any_successful_reexport = false;
12721275
let mut crate_private_reexport = false;
12731276
self.per_ns(|this, ns| {
1274-
let Progress::Ready(Some(binding)) = bindings[ns].get() else {
1277+
let binding = match bindings[ns].get() {
1278+
Poll::Ready(Some(binding)) => binding.maybe_source_binding(),
1279+
_ => None,
1280+
};
1281+
let Some(binding) = binding else {
12751282
return;
12761283
};
12771284

@@ -1352,7 +1359,11 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
13521359
// this may resolve to either a value or a type, but for documentation
13531360
// purposes it's good enough to just favor one over the other.
13541361
self.per_ns(|this, ns| {
1355-
if let Progress::Ready(Some(binding)) = bindings[ns].get() {
1362+
let binding = match bindings[ns].get() {
1363+
Poll::Ready(Some(binding)) => binding.maybe_source_binding(),
1364+
_ => None,
1365+
};
1366+
if let Some(binding) = binding {
13561367
this.import_res_map.entry(import_id).or_default()[ns] = Some(binding.res());
13571368
}
13581369
});
@@ -1390,7 +1401,11 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
13901401
let mut is_redundant = true;
13911402
let mut redundant_span = PerNS { value_ns: None, type_ns: None, macro_ns: None };
13921403
self.per_ns(|this, ns| {
1393-
if is_redundant && let Poll::Ready(Some(binding)) = bindings[ns].get() {
1404+
let binding = match bindings[ns].get() {
1405+
Poll::Ready(Some(binding)) => binding.maybe_source_binding(),
1406+
_ => None,
1407+
};
1408+
if is_redundant && let Some(binding) = binding {
13941409
if binding.res() == Res::Err {
13951410
return;
13961411
}

compiler/rustc_resolve/src/lib.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -891,6 +891,13 @@ impl<'ra> NameBindingData<'ra> {
891891
}
892892
}
893893

894+
fn maybe_source_binding(&self) -> Option<NameBinding<'ra>> {
895+
match self.kind {
896+
NameBindingKind::Import { binding, .. } => Some(binding),
897+
_ => None,
898+
}
899+
}
900+
894901
fn is_ambiguity_recursive(&self) -> bool {
895902
self.ambiguity.is_some()
896903
|| match self.kind {

0 commit comments

Comments
 (0)