Skip to content

Commit 964b72b

Browse files
committed
Refactor resolve_import_for_module
1 parent 0c40621 commit 964b72b

File tree

2 files changed

+32
-77
lines changed

2 files changed

+32
-77
lines changed

src/librustc_resolve/lib.rs

+8-6
Original file line numberDiff line numberDiff line change
@@ -662,10 +662,11 @@ enum ResolveResult<T> {
662662
}
663663

664664
impl<T> ResolveResult<T> {
665-
fn success(&self) -> bool {
666-
match *self {
667-
Success(_) => true,
668-
_ => false,
665+
fn and_then<U, F: FnOnce(T) -> ResolveResult<U>>(self, f: F) -> ResolveResult<U> {
666+
match self {
667+
Failed(msg) => Failed(msg),
668+
Indeterminate => Indeterminate,
669+
Success(t) => f(t),
669670
}
670671
}
671672
}
@@ -1335,8 +1336,9 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
13351336
use_lexical_scope: UseLexicalScopeFlag,
13361337
span: Span)
13371338
-> ResolveResult<(Module<'a>, LastPrivate)> {
1338-
let module_path_len = module_path.len();
1339-
assert!(module_path_len > 0);
1339+
if module_path.len() == 0 {
1340+
return Success((self.graph_root, LastMod(AllPublic))) // Use the crate root
1341+
}
13401342

13411343
debug!("(resolving module path for import) processing `{}` rooted at `{}`",
13421344
names_to_string(module_path),

src/librustc_resolve/resolve_imports.rs

+24-71
Original file line numberDiff line numberDiff line change
@@ -332,92 +332,45 @@ impl<'a, 'b:'a, 'tcx:'b> ImportResolver<'a, 'b, 'tcx> {
332332
module_: Module<'b>,
333333
import_directive: &ImportDirective)
334334
-> ResolveResult<()> {
335-
let mut resolution_result = ResolveResult::Failed(None);
336-
let module_path = &import_directive.module_path;
337-
338335
debug!("(resolving import for module) resolving import `{}::...` in `{}`",
339-
names_to_string(&module_path[..]),
336+
names_to_string(&import_directive.module_path),
340337
module_to_string(&*module_));
341338

342-
// First, resolve the module path for the directive, if necessary.
343-
let container = if module_path.is_empty() {
344-
// Use the crate root.
345-
Some((self.resolver.graph_root, LastMod(AllPublic)))
346-
} else {
347-
match self.resolver.resolve_module_path(module_,
348-
&module_path[..],
349-
UseLexicalScopeFlag::DontUseLexicalScope,
350-
import_directive.span) {
351-
ResolveResult::Failed(err) => {
352-
resolution_result = ResolveResult::Failed(err);
353-
None
354-
}
355-
ResolveResult::Indeterminate => {
356-
resolution_result = ResolveResult::Indeterminate;
357-
None
358-
}
359-
ResolveResult::Success(container) => Some(container),
360-
}
361-
};
362-
363-
match container {
364-
None => {}
365-
Some((containing_module, lp)) => {
339+
self.resolver
340+
.resolve_module_path(module_,
341+
&import_directive.module_path,
342+
UseLexicalScopeFlag::DontUseLexicalScope,
343+
import_directive.span)
344+
.and_then(|(containing_module, lp)| {
366345
// We found the module that the target is contained
367346
// within. Attempt to resolve the import within it.
368-
369-
match import_directive.subclass {
370-
SingleImport(target, source) => {
371-
resolution_result = self.resolve_single_import(&module_,
372-
containing_module,
373-
target,
374-
source,
375-
import_directive,
376-
lp);
377-
}
378-
GlobImport => {
379-
resolution_result = self.resolve_glob_import(&module_,
380-
containing_module,
381-
import_directive,
382-
lp);
383-
}
347+
if let SingleImport(target, source) = import_directive.subclass {
348+
self.resolve_single_import(&module_,
349+
containing_module,
350+
target,
351+
source,
352+
import_directive,
353+
lp)
354+
} else {
355+
self.resolve_glob_import(&module_, containing_module, import_directive, lp)
384356
}
385-
}
386-
}
387-
388-
// Decrement the count of unresolved imports.
389-
match resolution_result {
390-
ResolveResult::Success(()) => {
357+
})
358+
.and_then(|()| {
359+
// Decrement the count of unresolved imports.
391360
assert!(self.resolver.unresolved_imports >= 1);
392361
self.resolver.unresolved_imports -= 1;
393-
}
394-
_ => {
395-
// Nothing to do here; just return the error.
396-
}
397-
}
398362

399-
// Decrement the count of unresolved globs if necessary. But only if
400-
// the resolution result is a success -- other cases will
401-
// be handled by the main loop.
402-
403-
if resolution_result.success() {
404-
match import_directive.subclass {
405-
GlobImport => {
363+
if let GlobImport = import_directive.subclass {
406364
module_.dec_glob_count();
407365
if import_directive.is_public {
408366
module_.dec_pub_glob_count();
409367
}
410368
}
411-
SingleImport(..) => {
412-
// Ignore.
369+
if import_directive.is_public {
370+
module_.dec_pub_count();
413371
}
414-
}
415-
if import_directive.is_public {
416-
module_.dec_pub_count();
417-
}
418-
}
419-
420-
return resolution_result;
372+
Success(())
373+
})
421374
}
422375

423376
/// Resolves the name in the namespace of the module because it is being imported by

0 commit comments

Comments
 (0)