Navigation Menu

Skip to content

Commit

Permalink
Refactor resolve_import_for_module
Browse files Browse the repository at this point in the history
  • Loading branch information
jseyfried committed Feb 3, 2016
1 parent 0c40621 commit 964b72b
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 77 deletions.
14 changes: 8 additions & 6 deletions src/librustc_resolve/lib.rs
Expand Up @@ -662,10 +662,11 @@ enum ResolveResult<T> {
}

impl<T> ResolveResult<T> {
fn success(&self) -> bool {
match *self {
Success(_) => true,
_ => false,
fn and_then<U, F: FnOnce(T) -> ResolveResult<U>>(self, f: F) -> ResolveResult<U> {
match self {
Failed(msg) => Failed(msg),
Indeterminate => Indeterminate,
Success(t) => f(t),
}
}
}
Expand Down Expand Up @@ -1335,8 +1336,9 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
use_lexical_scope: UseLexicalScopeFlag,
span: Span)
-> ResolveResult<(Module<'a>, LastPrivate)> {
let module_path_len = module_path.len();
assert!(module_path_len > 0);
if module_path.len() == 0 {
return Success((self.graph_root, LastMod(AllPublic))) // Use the crate root
}

debug!("(resolving module path for import) processing `{}` rooted at `{}`",
names_to_string(module_path),
Expand Down
95 changes: 24 additions & 71 deletions src/librustc_resolve/resolve_imports.rs
Expand Up @@ -332,92 +332,45 @@ impl<'a, 'b:'a, 'tcx:'b> ImportResolver<'a, 'b, 'tcx> {
module_: Module<'b>,
import_directive: &ImportDirective)
-> ResolveResult<()> {
let mut resolution_result = ResolveResult::Failed(None);
let module_path = &import_directive.module_path;

debug!("(resolving import for module) resolving import `{}::...` in `{}`",
names_to_string(&module_path[..]),
names_to_string(&import_directive.module_path),
module_to_string(&*module_));

// First, resolve the module path for the directive, if necessary.
let container = if module_path.is_empty() {
// Use the crate root.
Some((self.resolver.graph_root, LastMod(AllPublic)))
} else {
match self.resolver.resolve_module_path(module_,
&module_path[..],
UseLexicalScopeFlag::DontUseLexicalScope,
import_directive.span) {
ResolveResult::Failed(err) => {
resolution_result = ResolveResult::Failed(err);
None
}
ResolveResult::Indeterminate => {
resolution_result = ResolveResult::Indeterminate;
None
}
ResolveResult::Success(container) => Some(container),
}
};

match container {
None => {}
Some((containing_module, lp)) => {
self.resolver
.resolve_module_path(module_,
&import_directive.module_path,
UseLexicalScopeFlag::DontUseLexicalScope,
import_directive.span)
.and_then(|(containing_module, lp)| {
// We found the module that the target is contained
// within. Attempt to resolve the import within it.

match import_directive.subclass {
SingleImport(target, source) => {
resolution_result = self.resolve_single_import(&module_,
containing_module,
target,
source,
import_directive,
lp);
}
GlobImport => {
resolution_result = self.resolve_glob_import(&module_,
containing_module,
import_directive,
lp);
}
if let SingleImport(target, source) = import_directive.subclass {
self.resolve_single_import(&module_,
containing_module,
target,
source,
import_directive,
lp)
} else {
self.resolve_glob_import(&module_, containing_module, import_directive, lp)
}
}
}

// Decrement the count of unresolved imports.
match resolution_result {
ResolveResult::Success(()) => {
})
.and_then(|()| {
// Decrement the count of unresolved imports.
assert!(self.resolver.unresolved_imports >= 1);
self.resolver.unresolved_imports -= 1;
}
_ => {
// Nothing to do here; just return the error.
}
}

// Decrement the count of unresolved globs if necessary. But only if
// the resolution result is a success -- other cases will
// be handled by the main loop.

if resolution_result.success() {
match import_directive.subclass {
GlobImport => {
if let GlobImport = import_directive.subclass {
module_.dec_glob_count();
if import_directive.is_public {
module_.dec_pub_glob_count();
}
}
SingleImport(..) => {
// Ignore.
if import_directive.is_public {
module_.dec_pub_count();
}
}
if import_directive.is_public {
module_.dec_pub_count();
}
}

return resolution_result;
Success(())
})
}

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

0 comments on commit 964b72b

Please sign in to comment.