Skip to content

Commit 43dffc3

Browse files
committed
Avoid passing around the current module as an argument in resolve_imports
1 parent bb09ae2 commit 43dffc3

File tree

1 file changed

+17
-31
lines changed

1 file changed

+17
-31
lines changed

src/librustc_resolve/resolve_imports.rs

Lines changed: 17 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ use {NameBinding, NameBindingKind, PrivacyError};
1717
use ResolveResult;
1818
use ResolveResult::*;
1919
use Resolver;
20-
use UseLexicalScopeFlag;
20+
use UseLexicalScopeFlag::DontUseLexicalScope;
2121
use {names_to_string, module_to_string};
2222
use {resolve_error, ResolutionError};
2323

@@ -382,7 +382,7 @@ impl<'a, 'b:'a, 'tcx:'b> ImportResolver<'a, 'b, 'tcx> {
382382
debug!("(resolving imports for module subtree) resolving {}",
383383
module_to_string(&module_));
384384
let orig_module = replace(&mut self.resolver.current_module, module_);
385-
self.resolve_imports_for_module(module_, errors);
385+
self.resolve_imports_in_current_module(errors);
386386
self.resolver.current_module = orig_module;
387387

388388
for (_, child_module) in module_.module_children.borrow().iter() {
@@ -391,22 +391,20 @@ impl<'a, 'b:'a, 'tcx:'b> ImportResolver<'a, 'b, 'tcx> {
391391
}
392392

393393
/// Attempts to resolve imports for the given module only.
394-
fn resolve_imports_for_module(&mut self,
395-
module: Module<'b>,
396-
errors: &mut Vec<ImportResolvingError<'b>>) {
394+
fn resolve_imports_in_current_module(&mut self, errors: &mut Vec<ImportResolvingError<'b>>) {
397395
let mut imports = Vec::new();
398-
let mut unresolved_imports = module.unresolved_imports.borrow_mut();
396+
let mut unresolved_imports = self.resolver.current_module.unresolved_imports.borrow_mut();
399397
::std::mem::swap(&mut imports, &mut unresolved_imports);
400398

401399
for import_directive in imports {
402-
match self.resolve_import_for_module(module, &import_directive) {
400+
match self.resolve_import_for_module(&import_directive) {
403401
Failed(err) => {
404402
let (span, help) = match err {
405403
Some((span, msg)) => (span, format!(". {}", msg)),
406404
None => (import_directive.span, String::new()),
407405
};
408406
errors.push(ImportResolvingError {
409-
source_module: module,
407+
source_module: self.resolver.current_module,
410408
import_directive: import_directive,
411409
span: span,
412410
help: help,
@@ -423,23 +421,15 @@ impl<'a, 'b:'a, 'tcx:'b> ImportResolver<'a, 'b, 'tcx> {
423421
/// don't know whether the name exists at the moment due to other
424422
/// currently-unresolved imports, or success if we know the name exists.
425423
/// If successful, the resolved bindings are written into the module.
426-
fn resolve_import_for_module(&mut self,
427-
module_: Module<'b>,
428-
import_directive: &'b ImportDirective)
429-
-> ResolveResult<()> {
424+
fn resolve_import_for_module(&mut self, directive: &'b ImportDirective) -> ResolveResult<()> {
430425
debug!("(resolving import for module) resolving import `{}::...` in `{}`",
431-
names_to_string(&import_directive.module_path),
432-
module_to_string(&module_));
426+
names_to_string(&directive.module_path),
427+
module_to_string(self.resolver.current_module));
433428

434429
self.resolver
435-
.resolve_module_path(&import_directive.module_path,
436-
UseLexicalScopeFlag::DontUseLexicalScope,
437-
import_directive.span)
438-
.and_then(|containing_module| {
439-
// We found the module that the target is contained
440-
// within. Attempt to resolve the import within it.
441-
self.resolve_import(module_, containing_module, import_directive)
442-
})
430+
.resolve_module_path(&directive.module_path, DontUseLexicalScope, directive.span)
431+
// Once we have the module that contains the target, we can resolve the import.
432+
.and_then(|containing_module| self.resolve_import(containing_module, directive))
443433
.and_then(|()| {
444434
// Decrement the count of unresolved imports.
445435
assert!(self.resolver.unresolved_imports >= 1);
@@ -448,18 +438,16 @@ impl<'a, 'b:'a, 'tcx:'b> ImportResolver<'a, 'b, 'tcx> {
448438
})
449439
}
450440

451-
fn resolve_import(&mut self,
452-
module_: Module<'b>,
453-
target_module: Module<'b>,
454-
directive: &'b ImportDirective)
441+
fn resolve_import(&mut self, target_module: Module<'b>, directive: &'b ImportDirective)
455442
-> ResolveResult<()> {
456443
let (source, target, value_determined, type_determined) = match directive.subclass {
457444
SingleImport { source, target, ref value_determined, ref type_determined } =>
458445
(source, target, value_determined, type_determined),
459-
GlobImport => return self.resolve_glob_import(module_, target_module, directive),
446+
GlobImport => return self.resolve_glob_import(target_module, directive),
460447
};
461448

462449
// We need to resolve both namespaces for this to succeed.
450+
let module_ = self.resolver.current_module;
463451
let (value_result, type_result) = {
464452
let mut resolve_in_ns = |ns, determined: bool| {
465453
// Temporarily count the directive as determined so that the resolution fails
@@ -594,15 +582,13 @@ impl<'a, 'b:'a, 'tcx:'b> ImportResolver<'a, 'b, 'tcx> {
594582
// succeeds or bails out (as importing * from an empty module or a module
595583
// that exports nothing is valid). target_module is the module we are
596584
// actually importing, i.e., `foo` in `use foo::*`.
597-
fn resolve_glob_import(&mut self,
598-
module_: Module<'b>,
599-
target_module: Module<'b>,
600-
directive: &'b ImportDirective)
585+
fn resolve_glob_import(&mut self, target_module: Module<'b>, directive: &'b ImportDirective)
601586
-> ResolveResult<()> {
602587
if let Some(Def::Trait(_)) = target_module.def {
603588
self.resolver.session.span_err(directive.span, "items in traits are not importable.");
604589
}
605590

591+
let module_ = self.resolver.current_module;
606592
if module_.def_id() == target_module.def_id() {
607593
// This means we are trying to glob import a module into itself, and it is a no-go
608594
let msg = "Cannot glob-import a module into itself.".into();

0 commit comments

Comments
 (0)