Skip to content

Commit 674d287

Browse files
committed
resolve: Replace enum Weak with ops::ControlFlow
1 parent 56e1e24 commit 674d287

File tree

2 files changed

+22
-24
lines changed

2 files changed

+22
-24
lines changed

compiler/rustc_resolve/src/ident.rs

Lines changed: 21 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,6 @@ use crate::{
2525
Resolver, Scope, ScopeSet, Segment, Stage, Used, errors,
2626
};
2727

28-
#[derive(Debug)]
29-
enum Weak {
30-
Yes,
31-
No,
32-
}
33-
3428
#[derive(Copy, Clone)]
3529
pub enum UsePrelude {
3630
No,
@@ -628,11 +622,11 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
628622
};
629623
Ok((binding, Flags::MODULE | misc_flags))
630624
}
631-
Err((Determinacy::Undetermined, Weak::No)) => {
625+
Err(ControlFlow::Continue(determinacy)) => Err(determinacy),
626+
Err(ControlFlow::Break(Determinacy::Undetermined)) => {
632627
return ControlFlow::Break(Err(Determinacy::determined(force)));
633628
}
634-
Err((Determinacy::Undetermined, Weak::Yes)) => Err(Determinacy::Undetermined),
635-
Err((Determinacy::Determined, _)) => Err(Determinacy::Determined),
629+
Err(ControlFlow::Break(Determinacy::Determined)) => Err(Determined),
636630
}
637631
}
638632
Scope::MacroUsePrelude => match self.macro_use_prelude.get(&ident.name).cloned() {
@@ -886,7 +880,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
886880
ignore_binding,
887881
ignore_import,
888882
)
889-
.map_err(|(determinacy, _)| determinacy),
883+
.map_err(|determinacy| determinacy.into_value()),
890884
ModuleOrUniformRoot::ModuleAndExternPrelude(module) => self.resolve_ident_in_scope_set(
891885
ident,
892886
ScopeSet::ModuleAndExternPrelude(ns, module),
@@ -949,15 +943,15 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
949943
// "self-confirming" import resolutions during import validation and checking.
950944
ignore_binding: Option<NameBinding<'ra>>,
951945
ignore_import: Option<Import<'ra>>,
952-
) -> Result<NameBinding<'ra>, (Determinacy, Weak)> {
946+
) -> Result<NameBinding<'ra>, ControlFlow<Determinacy, Determinacy>> {
953947
let key = BindingKey::new(ident, ns);
954948
// `try_borrow_mut` is required to ensure exclusive access, even if the resulting binding
955949
// doesn't need to be mutable. It will fail when there is a cycle of imports, and without
956950
// the exclusive access infinite recursion will crash the compiler with stack overflow.
957951
let resolution = &*self
958952
.resolution_or_default(module, key)
959953
.try_borrow_mut_unchecked()
960-
.map_err(|_| (Determined, Weak::No))?;
954+
.map_err(|_| ControlFlow::Break(Determined))?;
961955

962956
// If the primary binding is unusable, search further and return the shadowed glob
963957
// binding if it exists. What we really want here is having two separate scopes in
@@ -981,7 +975,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
981975

982976
let check_usable = |this: CmResolver<'r, 'ra, 'tcx>, binding: NameBinding<'ra>| {
983977
let usable = this.is_accessible_from(binding.vis, parent_scope.module);
984-
if usable { Ok(binding) } else { Err((Determined, Weak::No)) }
978+
if usable { Ok(binding) } else { Err(ControlFlow::Break(Determined)) }
985979
};
986980

987981
// Items and single imports are not shadowable, if we have one, then it's determined.
@@ -1003,7 +997,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
1003997
ignore_binding,
1004998
parent_scope,
1005999
) {
1006-
return Err((Undetermined, Weak::No));
1000+
return Err(ControlFlow::Break(Undetermined));
10071001
}
10081002

10091003
// So we have a resolution that's from a glob import. This resolution is determined
@@ -1022,7 +1016,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
10221016
if binding.determined() || ns == MacroNS || shadowing == Shadowing::Restricted {
10231017
return check_usable(self, binding);
10241018
} else {
1025-
return Err((Undetermined, Weak::No));
1019+
return Err(ControlFlow::Break(Undetermined));
10261020
}
10271021
}
10281022

@@ -1032,12 +1026,12 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
10321026
// expansion. With restricted shadowing names from globs and macro expansions cannot
10331027
// shadow names from outer scopes, so we can freely fallback from module search to search
10341028
// in outer scopes. For `resolve_ident_in_scope_set` to continue search in outer
1035-
// scopes we return `Undetermined` with `Weak::Yes`.
1029+
// scopes we return `Undetermined` with `ControlFlow::Continue`.
10361030

10371031
// Check if one of unexpanded macros can still define the name,
10381032
// if it can then our "no resolution" result is not determined and can be invalidated.
10391033
if !module.unexpanded_invocations.borrow().is_empty() {
1040-
return Err((Undetermined, Weak::Yes));
1034+
return Err(ControlFlow::Continue(Undetermined));
10411035
}
10421036

10431037
// Check if one of glob imports can still define the name,
@@ -1052,7 +1046,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
10521046
let module = match glob_import.imported_module.get() {
10531047
Some(ModuleOrUniformRoot::Module(module)) => module,
10541048
Some(_) => continue,
1055-
None => return Err((Undetermined, Weak::Yes)),
1049+
None => return Err(ControlFlow::Continue(Undetermined)),
10561050
};
10571051
let tmp_parent_scope;
10581052
let (mut adjusted_parent_scope, mut ident) =
@@ -1078,18 +1072,21 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
10781072
);
10791073

10801074
match result {
1081-
Err((Determined, _)) => continue,
1075+
Err(ControlFlow::Break(Determined) | ControlFlow::Continue(Determined)) => continue,
10821076
Ok(binding)
10831077
if !self.is_accessible_from(binding.vis, glob_import.parent_scope.module) =>
10841078
{
10851079
continue;
10861080
}
1087-
Ok(_) | Err((Undetermined, _)) => return Err((Undetermined, Weak::Yes)),
1081+
Ok(_)
1082+
| Err(ControlFlow::Break(Undetermined) | ControlFlow::Continue(Undetermined)) => {
1083+
return Err(ControlFlow::Continue(Undetermined));
1084+
}
10881085
}
10891086
}
10901087

10911088
// No resolution and no one else can define the name - determinate error.
1092-
Err((Determined, Weak::No))
1089+
Err(ControlFlow::Break(Determined))
10931090
}
10941091

10951092
fn finalize_module_binding(
@@ -1101,11 +1098,11 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
11011098
module: Module<'ra>,
11021099
finalize: Finalize,
11031100
shadowing: Shadowing,
1104-
) -> Result<NameBinding<'ra>, (Determinacy, Weak)> {
1101+
) -> Result<NameBinding<'ra>, ControlFlow<Determinacy, Determinacy>> {
11051102
let Finalize { path_span, report_private, used, root_span, .. } = finalize;
11061103

11071104
let Some(binding) = binding else {
1108-
return Err((Determined, Weak::No));
1105+
return Err(ControlFlow::Break(Determined));
11091106
};
11101107

11111108
if !self.is_accessible_from(binding.vis, parent_scope.module) {
@@ -1120,7 +1117,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
11201117
single_nested: path_span != root_span,
11211118
});
11221119
} else {
1123-
return Err((Determined, Weak::No));
1120+
return Err(ControlFlow::Break(Determined));
11241121
}
11251122
}
11261123

compiler/rustc_resolve/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#![feature(arbitrary_self_types)]
1414
#![feature(assert_matches)]
1515
#![feature(box_patterns)]
16+
#![feature(control_flow_into_value)]
1617
#![feature(decl_macro)]
1718
#![feature(default_field_values)]
1819
#![feature(if_let_guard)]

0 commit comments

Comments
 (0)