Skip to content

Commit 79e82b5

Browse files
committed
Implement Deref for ImportResolver to Resolver.
1 parent 2c1612c commit 79e82b5

File tree

1 file changed

+62
-53
lines changed

1 file changed

+62
-53
lines changed

src/librustc_resolve/resolve_imports.rs

Lines changed: 62 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ use {names_to_string, module_to_string};
2121
use {resolve_error, ResolutionError};
2222

2323
use rustc::ty;
24-
use rustc::lint;
24+
use rustc::lint::builtin::PRIVATE_IN_PUBLIC;
2525
use rustc::hir::def::*;
2626

2727
use syntax::ast::{NodeId, Name};
@@ -343,6 +343,25 @@ struct ImportResolver<'a, 'b: 'a> {
343343
resolver: &'a mut Resolver<'b>,
344344
}
345345

346+
impl<'a, 'b: 'a> ::std::ops::Deref for ImportResolver<'a, 'b> {
347+
type Target = Resolver<'b>;
348+
fn deref(&self) -> &Resolver<'b> {
349+
self.resolver
350+
}
351+
}
352+
353+
impl<'a, 'b: 'a> ::std::ops::DerefMut for ImportResolver<'a, 'b> {
354+
fn deref_mut(&mut self) -> &mut Resolver<'b> {
355+
self.resolver
356+
}
357+
}
358+
359+
impl<'a, 'b: 'a> ty::NodeIdTree for ImportResolver<'a, 'b> {
360+
fn is_descendant_of(&self, node: NodeId, ancestor: NodeId) -> bool {
361+
self.resolver.is_descendant_of(node, ancestor)
362+
}
363+
}
364+
346365
impl<'a, 'b:'a> ImportResolver<'a, 'b> {
347366
// Import resolution
348367
//
@@ -360,31 +379,29 @@ impl<'a, 'b:'a> ImportResolver<'a, 'b> {
360379
let mut errors = Vec::new();
361380

362381
loop {
363-
debug!("(resolving imports) iteration {}, {} imports left",
364-
i,
365-
self.resolver.unresolved_imports);
382+
debug!("(resolving imports) iteration {}, {} imports left", i, self.unresolved_imports);
366383

367384
// Attempt to resolve imports in all local modules.
368-
for module in self.resolver.arenas.local_modules().iter() {
369-
self.resolver.current_module = module;
385+
for module in self.arenas.local_modules().iter() {
386+
self.current_module = module;
370387
self.resolve_imports_in_current_module(&mut errors);
371388
}
372389

373-
if self.resolver.unresolved_imports == 0 {
390+
if self.unresolved_imports == 0 {
374391
debug!("(resolving imports) success");
375-
for module in self.resolver.arenas.local_modules().iter() {
392+
for module in self.arenas.local_modules().iter() {
376393
self.finalize_resolutions_in(module, false);
377394
}
378395
break;
379396
}
380397

381-
if self.resolver.unresolved_imports == prev_unresolved_imports {
398+
if self.unresolved_imports == prev_unresolved_imports {
382399
// resolving failed
383400
// Report unresolved imports only if no hard error was already reported
384401
// to avoid generating multiple errors on the same import.
385402
// Imports that are still indeterminate at this point are actually blocked
386403
// by errored imports, so there is no point reporting them.
387-
for module in self.resolver.arenas.local_modules().iter() {
404+
for module in self.arenas.local_modules().iter() {
388405
self.finalize_resolutions_in(module, errors.len() == 0);
389406
}
390407
for e in errors {
@@ -394,15 +411,15 @@ impl<'a, 'b:'a> ImportResolver<'a, 'b> {
394411
}
395412

396413
i += 1;
397-
prev_unresolved_imports = self.resolver.unresolved_imports;
414+
prev_unresolved_imports = self.unresolved_imports;
398415
}
399416
}
400417

401418
// Define a "dummy" resolution containing a Def::Err as a placeholder for a
402419
// failed resolution
403420
fn import_dummy_binding(&self, source_module: Module<'b>, directive: &'b ImportDirective<'b>) {
404421
if let SingleImport { target, .. } = directive.subclass {
405-
let dummy_binding = self.resolver.arenas.alloc_name_binding(NameBinding {
422+
let dummy_binding = self.arenas.alloc_name_binding(NameBinding {
406423
kind: NameBindingKind::Def(Def::Err),
407424
span: DUMMY_SP,
408425
vis: ty::Visibility::Public,
@@ -430,7 +447,7 @@ impl<'a, 'b:'a> ImportResolver<'a, 'b> {
430447
/// Attempts to resolve imports for the given module only.
431448
fn resolve_imports_in_current_module(&mut self, errors: &mut Vec<ImportResolvingError<'b>>) {
432449
let mut imports = Vec::new();
433-
let mut unresolved_imports = self.resolver.current_module.unresolved_imports.borrow_mut();
450+
let mut unresolved_imports = self.current_module.unresolved_imports.borrow_mut();
434451
::std::mem::swap(&mut imports, &mut unresolved_imports);
435452

436453
for import_directive in imports {
@@ -441,7 +458,7 @@ impl<'a, 'b:'a> ImportResolver<'a, 'b> {
441458
None => (import_directive.span, String::new()),
442459
};
443460
errors.push(ImportResolvingError {
444-
source_module: self.resolver.current_module,
461+
source_module: self.current_module,
445462
import_directive: import_directive,
446463
span: span,
447464
help: help,
@@ -450,8 +467,8 @@ impl<'a, 'b:'a> ImportResolver<'a, 'b> {
450467
Indeterminate => unresolved_imports.push(import_directive),
451468
Success(()) => {
452469
// Decrement the count of unresolved imports.
453-
assert!(self.resolver.unresolved_imports >= 1);
454-
self.resolver.unresolved_imports -= 1;
470+
assert!(self.unresolved_imports >= 1);
471+
self.unresolved_imports -= 1;
455472
}
456473
}
457474
}
@@ -465,13 +482,13 @@ impl<'a, 'b:'a> ImportResolver<'a, 'b> {
465482
fn resolve_import(&mut self, directive: &'b ImportDirective<'b>) -> ResolveResult<()> {
466483
debug!("(resolving import for module) resolving import `{}::...` in `{}`",
467484
names_to_string(&directive.module_path),
468-
module_to_string(self.resolver.current_module));
485+
module_to_string(self.current_module));
469486

470487
let target_module = match directive.target_module.get() {
471488
Some(module) => module,
472-
_ => match self.resolver.resolve_module_path(&directive.module_path,
473-
DontUseLexicalScope,
474-
directive.span) {
489+
_ => match self.resolve_module_path(&directive.module_path,
490+
DontUseLexicalScope,
491+
directive.span) {
475492
Success(module) => module,
476493
Indeterminate => return Indeterminate,
477494
Failed(err) => return Failed(err),
@@ -486,12 +503,10 @@ impl<'a, 'b:'a> ImportResolver<'a, 'b> {
486503
};
487504

488505
// We need to resolve both namespaces for this to succeed.
489-
let value_result =
490-
self.resolver.resolve_name_in_module(target_module, source, ValueNS, false, true);
491-
let type_result =
492-
self.resolver.resolve_name_in_module(target_module, source, TypeNS, false, true);
506+
let value_result = self.resolve_name_in_module(target_module, source, ValueNS, false, true);
507+
let type_result = self.resolve_name_in_module(target_module, source, TypeNS, false, true);
493508

494-
let module_ = self.resolver.current_module;
509+
let module_ = self.current_module;
495510
let mut privacy_error = true;
496511
for &(ns, result, determined) in &[(ValueNS, &value_result, value_determined),
497512
(TypeNS, &type_result, type_determined)] {
@@ -504,20 +519,20 @@ impl<'a, 'b:'a> ImportResolver<'a, 'b> {
504519
}
505520
Success(binding) if !binding.is_importable() => {
506521
let msg = format!("`{}` is not directly importable", target);
507-
span_err!(self.resolver.session, directive.span, E0253, "{}", &msg);
522+
span_err!(self.session, directive.span, E0253, "{}", &msg);
508523
// Do not import this illegal binding. Import a dummy binding and pretend
509524
// everything is fine
510525
self.import_dummy_binding(module_, directive);
511526
return Success(());
512527
}
513-
Success(binding) if !self.resolver.is_accessible(binding.vis) => {}
528+
Success(binding) if !self.is_accessible(binding.vis) => {}
514529
Success(binding) if !determined.get() => {
515530
determined.set(true);
516531
let imported_binding = directive.import(binding);
517532
let conflict = module_.try_define_child(target, ns, imported_binding);
518533
if let Err(old_binding) = conflict {
519534
let binding = &directive.import(binding);
520-
self.resolver.report_conflict(module_, target, ns, binding, old_binding);
535+
self.report_conflict(module_, target, ns, binding, old_binding);
521536
}
522537
privacy_error = false;
523538
}
@@ -556,39 +571,34 @@ impl<'a, 'b:'a> ImportResolver<'a, 'b> {
556571
if privacy_error {
557572
for &(ns, result) in &[(ValueNS, &value_result), (TypeNS, &type_result)] {
558573
let binding = match *result { Success(binding) => binding, _ => continue };
559-
self.resolver.privacy_errors.push(PrivacyError(directive.span, source, binding));
574+
self.privacy_errors.push(PrivacyError(directive.span, source, binding));
560575
let _ = module_.try_define_child(target, ns, directive.import(binding));
561576
}
562577
}
563578

564579
match (&value_result, &type_result) {
565-
(&Success(binding), _) if !binding.pseudo_vis()
566-
.is_at_least(directive.vis, self.resolver) &&
567-
self.resolver.is_accessible(binding.vis) => {
580+
(&Success(binding), _) if !binding.pseudo_vis().is_at_least(directive.vis, self) &&
581+
self.is_accessible(binding.vis) => {
568582
let msg = format!("`{}` is private, and cannot be reexported", source);
569583
let note_msg = format!("consider marking `{}` as `pub` in the imported module",
570584
source);
571-
struct_span_err!(self.resolver.session, directive.span, E0364, "{}", &msg)
585+
struct_span_err!(self.session, directive.span, E0364, "{}", &msg)
572586
.span_note(directive.span, &note_msg)
573587
.emit();
574588
}
575589

576-
(_, &Success(binding)) if !binding.pseudo_vis()
577-
.is_at_least(directive.vis, self.resolver) &&
578-
self.resolver.is_accessible(binding.vis) => {
590+
(_, &Success(binding)) if !binding.pseudo_vis().is_at_least(directive.vis, self) &&
591+
self.is_accessible(binding.vis) => {
579592
if binding.is_extern_crate() {
580593
let msg = format!("extern crate `{}` is private, and cannot be reexported \
581594
(error E0364), consider declaring with `pub`",
582595
source);
583-
self.resolver.session.add_lint(lint::builtin::PRIVATE_IN_PUBLIC,
584-
directive.id,
585-
directive.span,
586-
msg);
596+
self.session.add_lint(PRIVATE_IN_PUBLIC, directive.id, directive.span, msg);
587597
} else {
588598
let msg = format!("`{}` is private, and cannot be reexported", source);
589599
let note_msg =
590600
format!("consider declaring type or module `{}` with `pub`", source);
591-
struct_span_err!(self.resolver.session, directive.span, E0365, "{}", &msg)
601+
struct_span_err!(self.session, directive.span, E0365, "{}", &msg)
592602
.span_note(directive.span, &note_msg)
593603
.emit();
594604
}
@@ -605,7 +615,7 @@ impl<'a, 'b:'a> ImportResolver<'a, 'b> {
605615
None => value_result.success().and_then(NameBinding::def).unwrap(),
606616
};
607617
let path_resolution = PathResolution::new(def);
608-
self.resolver.def_map.insert(directive.id, path_resolution);
618+
self.def_map.insert(directive.id, path_resolution);
609619

610620
debug!("(resolving single import) successfully resolved import");
611621
return Success(());
@@ -618,19 +628,19 @@ impl<'a, 'b:'a> ImportResolver<'a, 'b> {
618628
fn resolve_glob_import(&mut self, target_module: Module<'b>, directive: &'b ImportDirective<'b>)
619629
-> ResolveResult<()> {
620630
if let Some(Def::Trait(_)) = target_module.def {
621-
self.resolver.session.span_err(directive.span, "items in traits are not importable.");
631+
self.session.span_err(directive.span, "items in traits are not importable.");
622632
}
623633

624-
let module_ = self.resolver.current_module;
634+
let module_ = self.current_module;
625635
if module_.def_id() == target_module.def_id() {
626636
// This means we are trying to glob import a module into itself, and it is a no-go
627637
let msg = "Cannot glob-import a module into itself.".into();
628638
return Failed(Some((directive.span, msg)));
629639
}
630-
self.resolver.populate_module_if_necessary(target_module);
640+
self.populate_module_if_necessary(target_module);
631641

632642
if let GlobImport { is_prelude: true } = directive.subclass {
633-
self.resolver.prelude = Some(target_module);
643+
self.prelude = Some(target_module);
634644
return Success(());
635645
}
636646

@@ -651,7 +661,7 @@ impl<'a, 'b:'a> ImportResolver<'a, 'b> {
651661
// Record the destination of this import
652662
if let Some(did) = target_module.def_id() {
653663
let resolution = PathResolution::new(Def::Mod(did));
654-
self.resolver.def_map.insert(directive.id, resolution);
664+
self.def_map.insert(directive.id, resolution);
655665
}
656666

657667
debug!("(resolving glob import) successfully resolved import");
@@ -668,7 +678,7 @@ impl<'a, 'b:'a> ImportResolver<'a, 'b> {
668678
for (&(name, ns), resolution) in module.resolutions.borrow().iter() {
669679
let resolution = resolution.borrow();
670680
resolution.report_conflicts(|b1, b2| {
671-
self.resolver.report_conflict(module, name, ns, b1, b2)
681+
self.report_conflict(module, name, ns, b1, b2)
672682
});
673683

674684
let binding = match resolution.binding {
@@ -685,20 +695,19 @@ impl<'a, 'b:'a> ImportResolver<'a, 'b> {
685695

686696
if let NameBindingKind::Import { binding: orig_binding, directive, .. } = binding.kind {
687697
if ns == TypeNS && orig_binding.is_variant() &&
688-
!orig_binding.vis.is_at_least(binding.vis, self.resolver) {
698+
!orig_binding.vis.is_at_least(binding.vis, self) {
689699
let msg = format!("variant `{}` is private, and cannot be reexported \
690700
(error E0364), consider declaring its enum as `pub`",
691701
name);
692-
let lint = lint::builtin::PRIVATE_IN_PUBLIC;
693-
self.resolver.session.add_lint(lint, directive.id, binding.span, msg);
702+
self.session.add_lint(PRIVATE_IN_PUBLIC, directive.id, binding.span, msg);
694703
}
695704
}
696705
}
697706

698707
if reexports.len() > 0 {
699708
if let Some(def_id) = module.def_id() {
700-
let node_id = self.resolver.definitions.as_local_node_id(def_id).unwrap();
701-
self.resolver.export_map.insert(node_id, reexports);
709+
let node_id = self.definitions.as_local_node_id(def_id).unwrap();
710+
self.export_map.insert(node_id, reexports);
702711
}
703712
}
704713

0 commit comments

Comments
 (0)