Skip to content

Commit 03effcd

Browse files
use PendingBinding enum instead of Poll
1 parent da399ea commit 03effcd

File tree

3 files changed

+51
-84
lines changed

3 files changed

+51
-84
lines changed

compiler/rustc_resolve/src/build_reduced_graph.rs

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
88
use std::cell::Cell;
99
use std::sync::Arc;
10-
use std::task::Poll;
1110

1211
use rustc_ast::visit::{self, AssocCtxt, Visitor, WalkItemKind};
1312
use rustc_ast::{
@@ -620,11 +619,7 @@ impl<'a, 'ra, 'tcx> BuildReducedGraphVisitor<'a, 'ra, 'tcx> {
620619
let kind = ImportKind::Single {
621620
source: source.ident,
622621
target: ident,
623-
bindings: PerNS {
624-
value_ns: Cell::new(Poll::Pending),
625-
type_ns: Cell::new(Poll::Pending),
626-
macro_ns: Cell::new(Poll::Pending),
627-
},
622+
bindings: Default::default(),
628623
type_ns_only,
629624
nested,
630625
id,

compiler/rustc_resolve/src/ident.rs

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ use rustc_span::{Ident, Span, kw, sym};
1313
use tracing::{debug, instrument};
1414

1515
use crate::errors::{ParamKindInEnumDiscriminant, ParamKindInNonTrivialAnonConst};
16-
use crate::imports::{Import, NameResolution, Progress};
16+
use crate::imports::{Import, NameResolution};
1717
use crate::late::{ConstantHasGenerics, NoConstantGenericsReason, PathSource, Rib, RibKind};
1818
use crate::macros::{MacroRulesScope, sub_namespace_match};
1919
use crate::{
@@ -1094,13 +1094,9 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
10941094
unreachable!();
10951095
};
10961096
if source != target {
1097-
if bindings.iter().all(|binding| {
1098-
matches!(binding.get(), Progress::Ready(None) | Progress::Pending)
1099-
}) {
1097+
if bindings.iter().all(|binding| binding.get().binding().is_none()) {
11001098
return true;
1101-
} else if matches!(bindings[ns].get(), Progress::Ready(None) | Progress::Pending)
1102-
&& binding.is_some()
1103-
{
1099+
} else if bindings[ns].get().binding().is_none() && binding.is_some() {
11041100
return true;
11051101
}
11061102
}

compiler/rustc_resolve/src/imports.rs

Lines changed: 47 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
33
use std::cell::Cell;
44
use std::mem;
5-
use std::task::Poll;
65

76
use rustc_ast::NodeId;
87
use rustc_data_structures::fx::{FxHashSet, FxIndexSet};
@@ -40,9 +39,22 @@ use crate::{
4039

4140
type Res = def::Res<NodeId>;
4241

43-
// Poll is a weird name.
44-
// FIXME: define/use something else because we are not doing async.
45-
pub(crate) use std::task::Poll as Progress;
42+
/// A [`NameBinding`] in the process of being resolved.
43+
#[derive(Clone, Copy, Default, PartialEq)]
44+
pub(crate) enum PendingBinding<'ra> {
45+
Ready(Option<NameBinding<'ra>>),
46+
#[default]
47+
Pending,
48+
}
49+
50+
impl<'ra> PendingBinding<'ra> {
51+
pub(crate) fn binding(self) -> Option<NameBinding<'ra>> {
52+
match self {
53+
PendingBinding::Ready(binding) => binding,
54+
PendingBinding::Pending => None,
55+
}
56+
}
57+
}
4658

4759
/// Contains data for specific kinds of imports.
4860
#[derive(Clone)]
@@ -54,11 +66,7 @@ pub(crate) enum ImportKind<'ra> {
5466
/// It will directly use `source` when the format is `use prefix::source`.
5567
target: Ident,
5668
/// Bindings introduced by the import.
57-
/// `Progress` (Poll) because it's clearer semantically:
58-
/// - `Progress::Ready(Some)`: Determined and resolved
59-
/// - `Progress::Ready(None)`: Determined and not resolved
60-
/// - `Progress::Pending`: Undetermined
61-
bindings: PerNS<Cell<Progress<Option<NameBinding<'ra>>>>>,
69+
bindings: PerNS<Cell<PendingBinding<'ra>>>,
6270
/// `true` for `...::{self [as target]}` imports, `false` otherwise.
6371
type_ns_only: bool,
6472
/// Did this import result from a nested import? ie. `use foo::{bar, baz};`
@@ -109,7 +117,7 @@ impl<'ra> std::fmt::Debug for ImportKind<'ra> {
109117
// Ignore the nested bindings to avoid an infinite loop while printing.
110118
.field(
111119
"bindings",
112-
&bindings.clone().map(|b| b.into_inner().map(|_| format_args!(".."))),
120+
&bindings.clone().map(|b| b.into_inner().binding().map(|_| format_args!(".."))),
113121
)
114122
.field("type_ns_only", type_ns_only)
115123
.field("nested", nested)
@@ -487,9 +495,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
487495
fn import_dummy_binding(&mut self, import: Import<'ra>, is_indeterminate: bool) {
488496
if let ImportKind::Single { target, ref bindings, .. } = import.kind {
489497
if !(is_indeterminate
490-
|| bindings.iter().all(|binding| {
491-
matches!(binding.get(), Progress::Pending | Progress::Ready(None))
492-
}))
498+
|| bindings.iter().all(|binding| binding.get().binding().is_none()))
493499
{
494500
return; // Has resolution, do not create the dummy binding
495501
}
@@ -567,7 +573,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
567573
if let ImportKind::Single { source, ref bindings, .. } = import.kind
568574
&& source.name == kw::SelfLower
569575
// Silence `unresolved import` error if E0429 is already emitted
570-
&& let Progress::Ready(None) = bindings.value_ns.get()
576+
&& let PendingBinding::Ready(None) = bindings.value_ns.get()
571577
{
572578
continue;
573579
}
@@ -830,7 +836,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
830836
let mut indeterminate_count = 0;
831837
self.per_ns(|this, ns| {
832838
if !type_ns_only || ns == TypeNS {
833-
if bindings[ns].get() != Progress::Pending {
839+
if bindings[ns].get() != PendingBinding::Pending {
834840
return;
835841
};
836842
let binding_result = this.maybe_resolve_ident_in_module(
@@ -857,7 +863,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
857863
// We need the `target`, `source` can be extracted.
858864
let imported_binding = this.import(binding, import);
859865
this.define(parent, target, ns, imported_binding);
860-
Poll::Ready(Some(imported_binding))
866+
PendingBinding::Ready(Some(imported_binding))
861867
}
862868
Err(Determinacy::Determined) => {
863869
// Don't update the resolution for underscores, because it was never added.
@@ -867,11 +873,11 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
867873
resolution.single_imports.swap_remove(&import);
868874
});
869875
}
870-
Poll::Ready(None)
876+
PendingBinding::Ready(None)
871877
}
872878
Err(Determinacy::Undetermined) => {
873879
indeterminate_count += 1;
874-
Poll::Pending
880+
PendingBinding::Pending
875881
}
876882
};
877883
bindings[ns].set(binding);
@@ -887,10 +893,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
887893
/// consolidate multiple unresolved import errors into a single diagnostic.
888894
fn finalize_import(&mut self, import: Import<'ra>) -> Option<UnresolvedImportError> {
889895
let ignore_binding = if let ImportKind::Single { bindings, .. } = &import.kind {
890-
match bindings[TypeNS].get() {
891-
Poll::Ready(binding) => binding,
892-
_ => None,
893-
}
896+
bindings[TypeNS].get().binding()
894897
} else {
895898
None
896899
};
@@ -1080,42 +1083,34 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
10801083
let mut all_ns_err = true;
10811084
self.per_ns(|this, ns| {
10821085
if !type_ns_only || ns == TypeNS {
1083-
let ignore_binding = match bindings[ns].get() {
1084-
Poll::Ready(binding) => binding,
1085-
_ => None,
1086-
};
10871086
let binding = this.resolve_ident_in_module(
10881087
module,
10891088
ident,
10901089
ns,
10911090
&import.parent_scope,
10921091
Some(Finalize { report_private: false, ..finalize }),
1093-
ignore_binding,
1092+
bindings[ns].get().binding(),
10941093
Some(import),
10951094
);
10961095

10971096
match binding {
10981097
Ok(binding) => {
10991098
// Consistency checks, analogous to `finalize_macro_resolutions`.
1100-
let initial_res = bindings[ns].get().map(|maybe_binding| {
1101-
maybe_binding.map(|binding| binding.import_source()).map(
1102-
|initial_binding| {
1103-
all_ns_err = false;
1104-
if let Progress::Ready(Some(binding)) = bindings[ns].get()
1105-
&& target.name == kw::Underscore
1106-
&& initial_binding.is_extern_crate()
1107-
&& !initial_binding.is_import()
1108-
{
1109-
let used = if import.module_path.is_empty() {
1110-
Used::Scope
1111-
} else {
1112-
Used::Other
1113-
};
1114-
this.record_use(ident, binding, used);
1115-
}
1116-
binding.res()
1117-
},
1118-
)
1099+
let initial_res = bindings[ns].get().binding().map(|binding| {
1100+
let initial_binding = binding.import_source();
1101+
all_ns_err = false;
1102+
if target.name == kw::Underscore
1103+
&& initial_binding.is_extern_crate()
1104+
&& !initial_binding.is_import()
1105+
{
1106+
let used = if import.module_path.is_empty() {
1107+
Used::Scope
1108+
} else {
1109+
Used::Other
1110+
};
1111+
this.record_use(ident, binding, used);
1112+
}
1113+
initial_binding.res()
11191114
});
11201115
let res = binding.res();
11211116
let has_ambiguity_error =
@@ -1125,7 +1120,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
11251120
.span_delayed_bug(import.span, "some error happened for an import");
11261121
return;
11271122
}
1128-
if let Progress::Ready(Some(initial_res)) = initial_res {
1123+
if let Some(initial_res) = initial_res {
11291124
if res != initial_res {
11301125
span_bug!(import.span, "inconsistent resolution for an import");
11311126
}
@@ -1268,11 +1263,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
12681263
let mut any_successful_reexport = false;
12691264
let mut crate_private_reexport = false;
12701265
self.per_ns(|this, ns| {
1271-
let binding = match bindings[ns].get() {
1272-
Poll::Ready(Some(binding)) => Some(binding.import_source()),
1273-
_ => None,
1274-
};
1275-
let Some(binding) = binding else {
1266+
let Some(binding) = bindings[ns].get().binding().map(|b| b.import_source()) else {
12761267
return;
12771268
};
12781269

@@ -1343,11 +1334,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
13431334
let mut full_path = import.module_path.clone();
13441335
full_path.push(Segment::from_ident(ident));
13451336
self.per_ns(|this, ns| {
1346-
let binding = match bindings[ns].get() {
1347-
Poll::Ready(Some(binding)) => Some(binding.import_source()),
1348-
_ => None,
1349-
};
1350-
if let Some(binding) = binding {
1337+
if let Some(binding) = bindings[ns].get().binding().map(|b| b.import_source()) {
13511338
this.lint_if_path_starts_with_module(Some(finalize), &full_path, Some(binding));
13521339
}
13531340
});
@@ -1357,11 +1344,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
13571344
// this may resolve to either a value or a type, but for documentation
13581345
// purposes it's good enough to just favor one over the other.
13591346
self.per_ns(|this, ns| {
1360-
let binding = match bindings[ns].get() {
1361-
Poll::Ready(Some(binding)) => Some(binding.import_source()),
1362-
_ => None,
1363-
};
1364-
if let Some(binding) = binding {
1347+
if let Some(binding) = bindings[ns].get().binding().map(|b| b.import_source()) {
13651348
this.import_res_map.entry(import_id).or_default()[ns] = Some(binding.res());
13661349
}
13671350
});
@@ -1399,26 +1382,19 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
13991382
let mut is_redundant = true;
14001383
let mut redundant_span = PerNS { value_ns: None, type_ns: None, macro_ns: None };
14011384
self.per_ns(|this, ns| {
1402-
let binding = match bindings[ns].get() {
1403-
Poll::Ready(Some(binding)) => Some(binding.import_source()),
1404-
_ => None,
1405-
};
1385+
let binding = bindings[ns].get().binding().map(|b| b.import_source());
14061386
if is_redundant && let Some(binding) = binding {
14071387
if binding.res() == Res::Err {
14081388
return;
14091389
}
14101390

1411-
let ignore_binding = match bindings[ns].get() {
1412-
Poll::Ready(binding) => binding,
1413-
_ => None,
1414-
};
14151391
match this.early_resolve_ident_in_lexical_scope(
14161392
target,
14171393
ScopeSet::All(ns),
14181394
&import.parent_scope,
14191395
None,
14201396
false,
1421-
ignore_binding,
1397+
bindings[ns].get().binding(),
14221398
None,
14231399
) {
14241400
Ok(other_binding) => {

0 commit comments

Comments
 (0)