Skip to content

Commit bb09ae2

Browse files
committed
Refactor away resolve_name_in_lexical_scope and resolve_identifier_in_local_ribs.
1 parent 7341785 commit bb09ae2

File tree

1 file changed

+42
-81
lines changed

1 file changed

+42
-81
lines changed

src/librustc_resolve/lib.rs

Lines changed: 42 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -789,6 +789,26 @@ enum LexicalScopeBinding<'a> {
789789
LocalDef(LocalDef),
790790
}
791791

792+
impl<'a> LexicalScopeBinding<'a> {
793+
fn local_def(self) -> LocalDef {
794+
match self {
795+
LexicalScopeBinding::LocalDef(local_def) => local_def,
796+
LexicalScopeBinding::Item(binding) => LocalDef::from_def(binding.def().unwrap()),
797+
}
798+
}
799+
800+
fn def(self) -> Def {
801+
self.local_def().def
802+
}
803+
804+
fn module(self) -> Option<Module<'a>> {
805+
match self {
806+
LexicalScopeBinding::Item(binding) => binding.module(),
807+
_ => None,
808+
}
809+
}
810+
}
811+
792812
/// The link from a module up to its nearest parent node.
793813
#[derive(Clone,Debug)]
794814
enum ParentLink<'a> {
@@ -1404,20 +1424,13 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
14041424
// This is not a crate-relative path. We resolve the
14051425
// first component of the path in the current lexical
14061426
// scope and then proceed to resolve below that.
1407-
match self.resolve_item_in_lexical_scope(module_path[0],
1408-
TypeNS,
1409-
true) {
1410-
Failed(err) => return Failed(err),
1411-
Indeterminate => {
1412-
debug!("(resolving module path for import) indeterminate; bailing");
1413-
return Indeterminate;
1414-
}
1415-
Success(binding) => match binding.module() {
1416-
Some(containing_module) => {
1417-
search_module = containing_module;
1418-
start_index = 1;
1419-
}
1420-
None => return Failed(None),
1427+
let ident = hir::Ident::from_name(module_path[0]);
1428+
match self.resolve_ident_in_lexical_scope(ident, TypeNS, true)
1429+
.and_then(LexicalScopeBinding::module) {
1430+
None => return Failed(None),
1431+
Some(containing_module) => {
1432+
search_module = containing_module;
1433+
start_index = 1;
14211434
}
14221435
}
14231436
}
@@ -1485,18 +1498,6 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
14851498
None
14861499
}
14871500

1488-
fn resolve_item_in_lexical_scope(&mut self,
1489-
name: Name,
1490-
namespace: Namespace,
1491-
record_used: bool)
1492-
-> ResolveResult<&'a NameBinding<'a>> {
1493-
let ident = hir::Ident::from_name(name);
1494-
match self.resolve_ident_in_lexical_scope(ident, namespace, record_used) {
1495-
Some(LexicalScopeBinding::Item(binding)) => Success(binding),
1496-
_ => Failed(None),
1497-
}
1498-
}
1499-
15001501
/// Returns the nearest normal module parent of the given module.
15011502
fn get_nearest_normal_module_parent(&mut self, module_: Module<'a>) -> Option<Module<'a>> {
15021503
let mut module_ = module_;
@@ -2288,8 +2289,7 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
22882289
let ident = path1.node;
22892290
let renamed = ident.name;
22902291

2291-
match self.resolve_bare_identifier_pattern(ident.unhygienic_name,
2292-
pattern.span) {
2292+
match self.resolve_bare_identifier_pattern(ident, pattern.span) {
22932293
FoundStructOrEnumVariant(def) if const_ok => {
22942294
debug!("(resolving pattern) resolving `{}` to struct or enum variant",
22952295
renamed);
@@ -2540,49 +2540,21 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
25402540
});
25412541
}
25422542

2543-
fn resolve_bare_identifier_pattern(&mut self,
2544-
name: Name,
2545-
span: Span)
2543+
fn resolve_bare_identifier_pattern(&mut self, ident: hir::Ident, span: Span)
25462544
-> BareIdentifierPatternResolution {
2547-
match self.resolve_item_in_lexical_scope(name, ValueNS, true) {
2548-
Success(binding) => {
2549-
debug!("(resolve bare identifier pattern) succeeded in finding {} at {:?}",
2550-
name,
2551-
binding);
2552-
match binding.def() {
2553-
None => {
2554-
panic!("resolved name in the value namespace to a set of name bindings \
2555-
with no def?!");
2556-
}
2557-
// For the two success cases, this lookup can be
2558-
// considered as not having a private component because
2559-
// the lookup happened only within the current module.
2560-
Some(def @ Def::Variant(..)) | Some(def @ Def::Struct(..)) => {
2561-
return FoundStructOrEnumVariant(def);
2562-
}
2563-
Some(def @ Def::Const(..)) | Some(def @ Def::AssociatedConst(..)) => {
2564-
return FoundConst(def, name);
2565-
}
2566-
Some(Def::Static(..)) => {
2567-
resolve_error(self, span, ResolutionError::StaticVariableReference);
2568-
return BareIdentifierPatternUnresolved;
2569-
}
2570-
_ => return BareIdentifierPatternUnresolved
2571-
}
2545+
match self.resolve_ident_in_lexical_scope(ident, ValueNS, true)
2546+
.map(LexicalScopeBinding::def) {
2547+
Some(def @ Def::Variant(..)) | Some(def @ Def::Struct(..)) => {
2548+
FoundStructOrEnumVariant(def)
25722549
}
2573-
2574-
Indeterminate => return BareIdentifierPatternUnresolved,
2575-
Failed(err) => {
2576-
match err {
2577-
Some((span, msg)) => {
2578-
resolve_error(self, span, ResolutionError::FailedToResolve(&msg));
2579-
}
2580-
None => (),
2581-
}
2582-
2583-
debug!("(resolve bare identifier pattern) failed to find {}", name);
2584-
return BareIdentifierPatternUnresolved;
2550+
Some(def @ Def::Const(..)) | Some(def @ Def::AssociatedConst(..)) => {
2551+
FoundConst(def, ident.unhygienic_name)
25852552
}
2553+
Some(Def::Static(..)) => {
2554+
resolve_error(self, span, ResolutionError::StaticVariableReference);
2555+
BareIdentifierPatternUnresolved
2556+
}
2557+
_ => BareIdentifierPatternUnresolved,
25862558
}
25872559
}
25882560

@@ -2703,7 +2675,8 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
27032675
return Some(LocalDef::from_def(Def::Err));
27042676
}
27052677

2706-
self.resolve_identifier_in_local_ribs(identifier, namespace, record_used)
2678+
self.resolve_ident_in_lexical_scope(identifier, namespace, record_used)
2679+
.map(LexicalScopeBinding::local_def)
27072680
}
27082681

27092682
// Resolve a local definition, potentially adjusting for closures.
@@ -2887,18 +2860,6 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
28872860
})
28882861
}
28892862

2890-
fn resolve_identifier_in_local_ribs(&mut self,
2891-
ident: hir::Ident,
2892-
namespace: Namespace,
2893-
record_used: bool)
2894-
-> Option<LocalDef> {
2895-
Some(match self.resolve_ident_in_lexical_scope(ident, namespace, record_used) {
2896-
Some(LexicalScopeBinding::LocalDef(local_def)) => local_def,
2897-
Some(LexicalScopeBinding::Item(binding)) => LocalDef::from_def(binding.def().unwrap()),
2898-
None => return None,
2899-
})
2900-
}
2901-
29022863
fn with_no_errors<T, F>(&mut self, f: F) -> T
29032864
where F: FnOnce(&mut Resolver) -> T
29042865
{

0 commit comments

Comments
 (0)