1 // Copyright 2015 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
11 use self::ImportDirectiveSubclass::*;
16 use Namespace::{self, TypeNS, ValueNS};
18 use NamespaceResult::{BoundResult, UnboundResult, UnknownResult};
23 use UseLexicalScopeFlag;
24 use {names_to_string, module_to_string};
25 use {resolve_error, ResolutionError};
27 use build_reduced_graph;
29 use rustc::middle::def::*;
30 use rustc::middle::def_id::DefId;
31 use rustc::middle::privacy::*;
33 use syntax::ast::{NodeId, Name};
34 use syntax::attr::AttrMetaMethods;
35 use syntax::codemap::Span;
37 use std::mem::replace;
41 /// Contains data for specific types of import directives.
42 #[derive(Copy, Clone,Debug)]
43 pub enum ImportDirectiveSubclass {
44 SingleImport(Name /* target */, Name /* source */),
48 /// Whether an import can be shadowed by another import.
49 #[derive(Debug,PartialEq,Clone,Copy)]
55 /// One import directive.
57 pub struct ImportDirective {
58 pub module_path: Vec<Name>,
59 pub subclass: ImportDirectiveSubclass,
62 pub is_public: bool, // see note in ImportResolution about how to use this
63 pub shadowable: Shadowable,
66 impl ImportDirective {
67 pub fn new(module_path: Vec<Name> ,
68 subclass: ImportDirectiveSubclass,
72 shadowable: Shadowable)
75 module_path: module_path,
80 shadowable: shadowable,
85 /// The item that an import resolves to.
86 #[derive(Clone,Debug)]
88 pub target_module: Rc<Module>,
89 pub bindings: Rc<NameBindings>,
90 pub shadowable: Shadowable,
94 pub fn new(target_module: Rc<Module>,
95 bindings: Rc<NameBindings>,
96 shadowable: Shadowable)
99 target_module: target_module,
101 shadowable: shadowable,
106 /// An ImportResolution represents a particular `use` directive.
108 pub struct ImportResolution {
109 /// Whether this resolution came from a `use` or a `pub use`. Note that this
110 /// should *not* be used whenever resolution is being performed. Privacy
111 /// testing occurs during a later phase of compilation.
114 // The number of outstanding references to this name. When this reaches
115 // zero, outside modules can count on the targets being correct. Before
116 // then, all bets are off; future imports could override this name.
117 // Note that this is usually either 0 or 1 - shadowing is forbidden the only
118 // way outstanding_references is > 1 in a legal program is if the name is
119 // used in both namespaces.
120 pub outstanding_references: usize,
122 /// The value that this `use` directive names, if there is one.
123 pub value_target: Option<Target>,
124 /// The source node of the `use` directive leading to the value target
126 pub value_id: NodeId,
128 /// The type that this `use` directive names, if there is one.
129 pub type_target: Option<Target>,
130 /// The source node of the `use` directive leading to the type target
135 impl ImportResolution {
136 pub fn new(id: NodeId, is_public: bool) -> ImportResolution {
140 outstanding_references: 0,
143 is_public: is_public,
147 pub fn target_for_namespace(&self, namespace: Namespace)
150 TypeNS => self.type_target.clone(),
151 ValueNS => self.value_target.clone(),
155 pub fn id(&self, namespace: Namespace) -> NodeId {
157 TypeNS => self.type_id,
158 ValueNS => self.value_id,
162 pub fn shadowable(&self, namespace: Namespace) -> Shadowable {
163 let target = self.target_for_namespace(namespace);
164 if target.is_none() {
165 return Shadowable::Always;
168 target.unwrap().shadowable
171 pub fn set_target_and_id(&mut self,
172 namespace: Namespace,
173 target: Option<Target>,
177 self.type_target = target;
181 self.value_target = target;
188 struct ImportResolvingError {
194 struct ImportResolver<'a, 'b:'a, 'tcx:'b> {
195 resolver: &'a mut Resolver<'b, 'tcx>
198 impl<'a, 'b:'a, 'tcx:'b> ImportResolver<'a, 'b, 'tcx> {
201 // This is a fixed-point algorithm. We resolve imports until our efforts
202 // are stymied by an unresolved import; then we bail out of the current
203 // module and continue. We terminate successfully once no more imports
204 // remain or unsuccessfully when no forward progress in resolving imports
207 /// Resolves all imports for the crate. This method performs the fixed-
209 fn resolve_imports(&mut self) {
211 let mut prev_unresolved_imports = 0;
213 debug!("(resolving imports) iteration {}, {} imports left",
214 i, self.resolver.unresolved_imports);
216 let module_root = self.resolver.graph_root.get_module();
217 let errors = self.resolve_imports_for_module_subtree(module_root.clone());
219 if self.resolver.unresolved_imports == 0 {
220 debug!("(resolving imports) success");
224 if self.resolver.unresolved_imports == prev_unresolved_imports {
226 if errors.len() > 0 {
228 resolve_error(self.resolver,
230 ResolutionError::UnresolvedImport(Some((&e.path, &e.help))));
233 // Report unresolved imports only if no hard error was already reported
234 // to avoid generating multiple errors on the same import.
235 // Imports that are still indeterminate at this point are actually blocked
236 // by errored imports, so there is no point reporting them.
237 self.resolver.report_unresolved_imports(module_root);
243 prev_unresolved_imports = self.resolver.unresolved_imports;
247 /// Attempts to resolve imports for the given module and all of its
249 fn resolve_imports_for_module_subtree(&mut self, module_: Rc<Module>)
250 -> Vec<ImportResolvingError> {
251 let mut errors = Vec::new();
252 debug!("(resolving imports for module subtree) resolving {}",
253 module_to_string(&*module_));
254 let orig_module = replace(&mut self.resolver.current_module, module_.clone());
255 errors.extend(self.resolve_imports_for_module(module_.clone()));
256 self.resolver.current_module = orig_module;
258 build_reduced_graph::populate_module_if_necessary(self.resolver, &module_);
259 for (_, child_node) in module_.children.borrow().iter() {
260 match child_node.get_module_if_available() {
264 Some(child_module) => {
265 errors.extend(self.resolve_imports_for_module_subtree(child_module));
270 for (_, child_module) in module_.anonymous_children.borrow().iter() {
271 errors.extend(self.resolve_imports_for_module_subtree(child_module.clone()));
277 /// Attempts to resolve imports for the given module only.
278 fn resolve_imports_for_module(&mut self, module: Rc<Module>) -> Vec<ImportResolvingError> {
279 let mut errors = Vec::new();
281 if module.all_imports_resolved() {
282 debug!("(resolving imports for module) all imports resolved for \
284 module_to_string(&*module));
288 let mut imports = module.imports.borrow_mut();
289 let import_count = imports.len();
290 let mut indeterminate_imports = Vec::new();
291 while module.resolved_import_count.get() + indeterminate_imports.len() < import_count {
292 let import_index = module.resolved_import_count.get();
293 match self.resolve_import_for_module(module.clone(),
294 &imports[import_index]) {
295 ResolveResult::Failed(err) => {
296 let import_directive = &imports[import_index];
297 let (span, help) = match err {
298 Some((span, msg)) => (span, format!(". {}", msg)),
299 None => (import_directive.span, String::new())
301 errors.push(ImportResolvingError {
303 path: import_path_to_string(
304 &import_directive.module_path,
305 import_directive.subclass
310 ResolveResult::Indeterminate => {}
311 ResolveResult::Success(()) => {
313 module.resolved_import_count
314 .set(module.resolved_import_count.get() + 1);
318 // This resolution was not successful, keep it for later
319 indeterminate_imports.push(imports.swap_remove(import_index));
323 imports.extend(indeterminate_imports);
328 /// Attempts to resolve the given import. The return value indicates
329 /// failure if we're certain the name does not exist, indeterminate if we
330 /// don't know whether the name exists at the moment due to other
331 /// currently-unresolved imports, or success if we know the name exists.
332 /// If successful, the resolved bindings are written into the module.
333 fn resolve_import_for_module(&mut self,
335 import_directive: &ImportDirective)
336 -> ResolveResult<()> {
337 let mut resolution_result = ResolveResult::Failed(None);
338 let module_path = &import_directive.module_path;
340 debug!("(resolving import for module) resolving import `{}::...` in `{}`",
341 names_to_string(&module_path[..]),
342 module_to_string(&*module_));
344 // First, resolve the module path for the directive, if necessary.
345 let container = if module_path.is_empty() {
346 // Use the crate root.
347 Some((self.resolver.graph_root.get_module(), LastMod(AllPublic)))
349 match self.resolver.resolve_module_path(module_.clone(),
351 UseLexicalScopeFlag::DontUseLexicalScope,
352 import_directive.span,
353 NameSearchType::ImportSearch) {
354 ResolveResult::Failed(err) => {
355 resolution_result = ResolveResult::Failed(err);
358 ResolveResult::Indeterminate => {
359 resolution_result = ResolveResult::Indeterminate;
362 ResolveResult::Success(container) => Some(container),
368 Some((containing_module, lp)) => {
369 // We found the module that the target is contained
370 // within. Attempt to resolve the import within it.
372 match import_directive.subclass {
373 SingleImport(target, source) => {
375 self.resolve_single_import(&module_,
384 self.resolve_glob_import(&module_,
393 // Decrement the count of unresolved imports.
394 match resolution_result {
395 ResolveResult::Success(()) => {
396 assert!(self.resolver.unresolved_imports >= 1);
397 self.resolver.unresolved_imports -= 1;
400 // Nothing to do here; just return the error.
404 // Decrement the count of unresolved globs if necessary. But only if
405 // the resolution result is a success -- other cases will
406 // be handled by the main loop.
408 if resolution_result.success() {
409 match import_directive.subclass {
411 module_.dec_glob_count();
412 if import_directive.is_public {
413 module_.dec_pub_glob_count();
416 SingleImport(..) => {
420 if import_directive.is_public {
421 module_.dec_pub_count();
425 return resolution_result;
428 fn resolve_single_import(&mut self,
430 target_module: Rc<Module>,
433 directive: &ImportDirective,
435 -> ResolveResult<()> {
436 debug!("(resolving single import) resolving `{}` = `{}::{}` from \
437 `{}` id {}, last private {:?}",
439 module_to_string(&*target_module),
441 module_to_string(module_),
448 self.resolver.session
449 .span_bug(directive.span,
450 "not expecting Import here, must be LastMod")
454 // We need to resolve both namespaces for this to succeed.
457 let mut value_result = UnknownResult;
458 let mut type_result = UnknownResult;
460 // Search for direct children of the containing module.
461 build_reduced_graph::populate_module_if_necessary(self.resolver, &target_module);
463 match target_module.children.borrow().get(&source) {
467 Some(ref child_name_bindings) => {
468 // pub_err makes sure we don't give the same error twice.
469 let mut pub_err = false;
470 if child_name_bindings.defined_in_namespace(ValueNS) {
471 debug!("(resolving single import) found value binding");
472 value_result = BoundResult(target_module.clone(),
473 (*child_name_bindings).clone());
474 if directive.is_public && !child_name_bindings.is_public(ValueNS) {
475 let msg = format!("`{}` is private, and cannot be reexported",
478 format!("Consider marking `{}` as `pub` in the imported module",
480 span_err!(self.resolver.session, directive.span, E0364, "{}", &msg);
481 self.resolver.session.span_note(directive.span, ¬e_msg);
485 if child_name_bindings.defined_in_namespace(TypeNS) {
486 debug!("(resolving single import) found type binding");
487 type_result = BoundResult(target_module.clone(),
488 (*child_name_bindings).clone());
489 if !pub_err && directive.is_public && !child_name_bindings.is_public(TypeNS) {
490 let msg = format!("`{}` is private, and cannot be reexported",
492 let note_msg = format!("Consider declaring module `{}` as a `pub mod`",
494 span_err!(self.resolver.session, directive.span, E0365, "{}", &msg);
495 self.resolver.session.span_note(directive.span, ¬e_msg);
501 // Unless we managed to find a result in both namespaces (unlikely),
502 // search imports as well.
503 let mut value_used_reexport = false;
504 let mut type_used_reexport = false;
505 match (value_result.clone(), type_result.clone()) {
506 (BoundResult(..), BoundResult(..)) => {} // Continue.
508 // If there is an unresolved glob at this point in the
509 // containing module, bail out. We don't know enough to be
510 // able to resolve this import.
512 if target_module.pub_glob_count.get() > 0 {
513 debug!("(resolving single import) unresolved pub glob; \
515 return ResolveResult::Indeterminate;
518 // Now search the exported imports within the containing module.
519 match target_module.import_resolutions.borrow().get(&source) {
521 debug!("(resolving single import) no import");
522 // The containing module definitely doesn't have an
523 // exported import with the name in question. We can
524 // therefore accurately report that the names are
527 if value_result.is_unknown() {
528 value_result = UnboundResult;
530 if type_result.is_unknown() {
531 type_result = UnboundResult;
534 Some(import_resolution)
535 if import_resolution.outstanding_references == 0 => {
537 fn get_binding(this: &mut Resolver,
538 import_resolution: &ImportResolution,
539 namespace: Namespace,
543 // Import resolutions must be declared with "pub"
544 // in order to be exported.
545 if !import_resolution.is_public {
546 return UnboundResult;
549 match import_resolution.target_for_namespace(namespace) {
551 return UnboundResult;
558 debug!("(resolving single import) found \
559 import in ns {:?}", namespace);
560 let id = import_resolution.id(namespace);
561 // track used imports and extern crates as well
562 this.used_imports.insert((id, namespace));
563 this.record_import_use(id, *source);
564 match target_module.def_id.get() {
565 Some(DefId{krate: kid, ..}) => {
566 this.used_crates.insert(kid);
570 return BoundResult(target_module, bindings);
575 // The name is an import which has been fully
576 // resolved. We can, therefore, just follow it.
577 if value_result.is_unknown() {
578 value_result = get_binding(self.resolver,
582 value_used_reexport = import_resolution.is_public;
584 if type_result.is_unknown() {
585 type_result = get_binding(self.resolver,
589 type_used_reexport = import_resolution.is_public;
594 // If target_module is the same module whose import we are resolving
595 // and there it has an unresolved import with the same name as `source`,
596 // then the user is actually trying to import an item that is declared
600 // use self::submodule;
601 // pub mod submodule;
603 // In this case we continue as if we resolved the import and let the
604 // check_for_conflicts_between_imports_and_items call below handle
606 match (module_.def_id.get(), target_module.def_id.get()) {
607 (Some(id1), Some(id2)) if id1 == id2 => {
608 if value_result.is_unknown() {
609 value_result = UnboundResult;
611 if type_result.is_unknown() {
612 type_result = UnboundResult;
616 // The import is unresolved. Bail out.
617 debug!("(resolving single import) unresolved import; \
619 return ResolveResult::Indeterminate;
627 let mut value_used_public = false;
628 let mut type_used_public = false;
630 // If we didn't find a result in the type namespace, search the
633 BoundResult(..) => {}
635 match target_module.external_module_children.borrow_mut().get(&source).cloned() {
636 None => {} // Continue.
638 debug!("(resolving single import) found external module");
639 // track the module as used.
640 match module.def_id.get() {
641 Some(DefId{krate: kid, ..}) => {
642 self.resolver.used_crates.insert(kid);
647 Rc::new(Resolver::create_name_bindings_from_module(module));
648 type_result = BoundResult(target_module.clone(), name_bindings);
649 type_used_public = true;
655 // We've successfully resolved the import. Write the results in.
656 let mut import_resolutions = module_.import_resolutions.borrow_mut();
657 let import_resolution = import_resolutions.get_mut(&target).unwrap();
660 let mut check_and_write_import = |namespace, result: &_, used_public: &mut bool| {
661 let namespace_name = match namespace {
667 BoundResult(ref target_module, ref name_bindings) => {
668 debug!("(resolving single import) found {:?} target: {:?}",
670 name_bindings.def_for_namespace(namespace));
671 self.check_for_conflicting_import(
677 self.check_that_import_is_importable(
683 let target = Some(Target::new(target_module.clone(),
684 name_bindings.clone(),
685 directive.shadowable));
686 import_resolution.set_target_and_id(namespace, target, directive.id);
687 import_resolution.is_public = directive.is_public;
688 *used_public = name_bindings.defined_in_public_namespace(namespace);
690 UnboundResult => { /* Continue. */ }
692 panic!("{:?} result should be known at this point", namespace_name);
696 check_and_write_import(ValueNS, &value_result, &mut value_used_public);
697 check_and_write_import(TypeNS, &type_result, &mut type_used_public);
700 self.check_for_conflicts_between_imports_and_items(
706 if value_result.is_unbound() && type_result.is_unbound() {
707 let msg = format!("There is no `{}` in `{}`",
709 module_to_string(&target_module));
710 return ResolveResult::Failed(Some((directive.span, msg)));
712 let value_used_public = value_used_reexport || value_used_public;
713 let type_used_public = type_used_reexport || type_used_public;
715 assert!(import_resolution.outstanding_references >= 1);
716 import_resolution.outstanding_references -= 1;
718 // Record what this import resolves to for later uses in documentation,
719 // this may resolve to either a value or a type, but for documentation
720 // purposes it's good enough to just favor one over the other.
721 let value_def_and_priv = import_resolution.value_target.as_ref().map(|target| {
722 let def = target.bindings.def_for_namespace(ValueNS).unwrap();
723 (def, if value_used_public { lp } else { DependsOn(def.def_id()) })
725 let type_def_and_priv = import_resolution.type_target.as_ref().map(|target| {
726 let def = target.bindings.def_for_namespace(TypeNS).unwrap();
727 (def, if type_used_public { lp } else { DependsOn(def.def_id()) })
730 let import_lp = LastImport {
731 value_priv: value_def_and_priv.map(|(_, p)| p),
733 type_priv: type_def_and_priv.map(|(_, p)| p),
737 if let Some((def, _)) = value_def_and_priv {
738 self.resolver.def_map.borrow_mut().insert(directive.id, PathResolution {
740 last_private: import_lp,
744 if let Some((def, _)) = type_def_and_priv {
745 self.resolver.def_map.borrow_mut().insert(directive.id, PathResolution {
747 last_private: import_lp,
752 debug!("(resolving single import) successfully resolved import");
753 return ResolveResult::Success(());
756 // Resolves a glob import. Note that this function cannot fail; it either
757 // succeeds or bails out (as importing * from an empty module or a module
758 // that exports nothing is valid). target_module is the module we are
759 // actually importing, i.e., `foo` in `use foo::*`.
760 fn resolve_glob_import(&mut self,
762 target_module: Rc<Module>,
763 import_directive: &ImportDirective,
765 -> ResolveResult<()> {
766 let id = import_directive.id;
767 let is_public = import_directive.is_public;
769 // This function works in a highly imperative manner; it eagerly adds
770 // everything it can to the list of import resolutions of the module
772 debug!("(resolving glob import) resolving glob import {}", id);
774 // We must bail out if the node has unresolved imports of any kind
775 // (including globs).
776 if (*target_module).pub_count.get() > 0 {
777 debug!("(resolving glob import) target module has unresolved \
778 pub imports; bailing out");
779 return ResolveResult::Indeterminate;
782 // Add all resolved imports from the containing module.
783 let import_resolutions = target_module.import_resolutions.borrow();
785 if module_.import_resolutions.borrow_state() != ::std::cell::BorrowState::Unused {
786 // In this case, target_module == module_
787 // This means we are trying to glob import a module into itself,
789 debug!("(resolving glob imports) target module is current module; giving up");
790 return ResolveResult::Failed(Some((
791 import_directive.span,
792 "Cannot glob-import a module into itself.".into()
796 for (ident, target_import_resolution) in import_resolutions.iter() {
797 debug!("(resolving glob import) writing module resolution \
800 module_to_string(module_));
802 if !target_import_resolution.is_public {
803 debug!("(resolving glob import) nevermind, just kidding");
807 // Here we merge two import resolutions.
808 let mut import_resolutions = module_.import_resolutions.borrow_mut();
809 match import_resolutions.get_mut(ident) {
810 Some(dest_import_resolution) => {
811 // Merge the two import resolutions at a finer-grained
814 match target_import_resolution.value_target {
818 Some(ref value_target) => {
819 self.check_for_conflicting_import(&dest_import_resolution,
820 import_directive.span,
823 dest_import_resolution.value_target = Some(value_target.clone());
826 match target_import_resolution.type_target {
830 Some(ref type_target) => {
831 self.check_for_conflicting_import(&dest_import_resolution,
832 import_directive.span,
835 dest_import_resolution.type_target = Some(type_target.clone());
838 dest_import_resolution.is_public = is_public;
844 // Simple: just copy the old import resolution.
845 let mut new_import_resolution = ImportResolution::new(id, is_public);
846 new_import_resolution.value_target =
847 target_import_resolution.value_target.clone();
848 new_import_resolution.type_target =
849 target_import_resolution.type_target.clone();
851 import_resolutions.insert(*ident, new_import_resolution);
854 // Add all children from the containing module.
855 build_reduced_graph::populate_module_if_necessary(self.resolver, &target_module);
857 for (&name, name_bindings) in target_module.children.borrow().iter() {
858 self.merge_import_resolution(module_,
859 target_module.clone(),
862 name_bindings.clone());
866 // Add external module children from the containing module.
867 for (&name, module) in target_module.external_module_children.borrow().iter() {
869 Rc::new(Resolver::create_name_bindings_from_module(module.clone()));
870 self.merge_import_resolution(module_,
871 target_module.clone(),
877 // Record the destination of this import
878 if let Some(did) = target_module.def_id.get() {
879 self.resolver.def_map.borrow_mut().insert(id, PathResolution {
880 base_def: DefMod(did),
886 debug!("(resolving glob import) successfully resolved import");
887 return ResolveResult::Success(());
890 fn merge_import_resolution(&mut self,
892 containing_module: Rc<Module>,
893 import_directive: &ImportDirective,
895 name_bindings: Rc<NameBindings>) {
896 let id = import_directive.id;
897 let is_public = import_directive.is_public;
899 let mut import_resolutions = module_.import_resolutions.borrow_mut();
900 let dest_import_resolution = import_resolutions.entry(name)
901 .or_insert_with(|| ImportResolution::new(id, is_public));
903 debug!("(resolving glob import) writing resolution `{}` in `{}` \
906 module_to_string(&*containing_module),
907 module_to_string(module_));
909 // Merge the child item into the import resolution.
911 let mut merge_child_item = |namespace| {
912 let modifier = DefModifiers::IMPORTABLE | DefModifiers::PUBLIC;
914 if name_bindings.defined_in_namespace_with(namespace, modifier) {
915 let namespace_name = match namespace {
919 debug!("(resolving glob import) ... for {} target", namespace_name);
920 if dest_import_resolution.shadowable(namespace) == Shadowable::Never {
921 let msg = format!("a {} named `{}` has already been imported \
925 span_err!(self.resolver.session, import_directive.span, E0251, "{}", msg);
927 let target = Target::new(containing_module.clone(),
928 name_bindings.clone(),
929 import_directive.shadowable);
930 dest_import_resolution.set_target_and_id(namespace,
936 merge_child_item(ValueNS);
937 merge_child_item(TypeNS);
940 dest_import_resolution.is_public = is_public;
942 self.check_for_conflicts_between_imports_and_items(
944 dest_import_resolution,
945 import_directive.span,
949 /// Checks that imported names and items don't have the same name.
950 fn check_for_conflicting_import(&mut self,
951 import_resolution: &ImportResolution,
954 namespace: Namespace) {
955 let target = import_resolution.target_for_namespace(namespace);
956 debug!("check_for_conflicting_import: {}; target exists: {}",
961 Some(ref target) if target.shadowable != Shadowable::Always => {
962 let ns_word = match namespace {
964 if let Some(ref ty_def) = *target.bindings.type_def.borrow() {
965 match ty_def.module_def {
967 if module.kind.get() == ModuleKind::NormalModuleKind =>
970 if module.kind.get() == ModuleKind::TraitModuleKind =>
978 span_err!(self.resolver.session, import_span, E0252,
979 "a {} named `{}` has already been imported \
980 in this module", ns_word,
982 let use_id = import_resolution.id(namespace);
983 let item = self.resolver.ast_map.expect_item(use_id);
984 // item is syntax::ast::Item;
985 span_note!(self.resolver.session, item.span,
986 "previous import of `{}` here",
993 /// Checks that an import is actually importable
994 fn check_that_import_is_importable(&mut self,
995 name_bindings: &NameBindings,
998 namespace: Namespace) {
999 if !name_bindings.defined_in_namespace_with(namespace, DefModifiers::IMPORTABLE) {
1000 let msg = format!("`{}` is not directly importable",
1002 span_err!(self.resolver.session, import_span, E0253, "{}", &msg[..]);
1006 /// Checks that imported names and items don't have the same name.
1007 fn check_for_conflicts_between_imports_and_items(&mut self,
1013 // First, check for conflicts between imports and `extern crate`s.
1014 if module.external_module_children
1016 .contains_key(&name) {
1017 match import_resolution.type_target {
1018 Some(ref target) if target.shadowable != Shadowable::Always => {
1019 let msg = format!("import `{0}` conflicts with imported \
1020 crate in this module \
1021 (maybe you meant `use {0}::*`?)",
1023 span_err!(self.resolver.session, import_span, E0254, "{}", &msg[..]);
1025 Some(_) | None => {}
1029 // Check for item conflicts.
1030 let children = module.children.borrow();
1031 let name_bindings = match children.get(&name) {
1033 // There can't be any conflicts.
1036 Some(ref name_bindings) => (*name_bindings).clone(),
1039 match import_resolution.value_target {
1040 Some(ref target) if target.shadowable != Shadowable::Always => {
1041 if let Some(ref value) = *name_bindings.value_def.borrow() {
1042 span_err!(self.resolver.session, import_span, E0255,
1043 "import `{}` conflicts with value in this module",
1045 if let Some(span) = value.value_span {
1046 self.resolver.session.span_note(span, "conflicting value here");
1050 Some(_) | None => {}
1053 match import_resolution.type_target {
1054 Some(ref target) if target.shadowable != Shadowable::Always => {
1055 if let Some(ref ty) = *name_bindings.type_def.borrow() {
1056 let (what, note) = match ty.module_def {
1058 if module.kind.get() == ModuleKind::NormalModuleKind =>
1059 ("existing submodule", "note conflicting module here"),
1061 if module.kind.get() == ModuleKind::TraitModuleKind =>
1062 ("trait in this module", "note conflicting trait here"),
1063 _ => ("type in this module", "note conflicting type here"),
1065 span_err!(self.resolver.session, import_span, E0256,
1066 "import `{}` conflicts with {}",
1068 if let Some(span) = ty.type_span {
1069 self.resolver.session.span_note(span, note);
1073 Some(_) | None => {}
1078 fn import_path_to_string(names: &[Name],
1079 subclass: ImportDirectiveSubclass)
1081 if names.is_empty() {
1082 import_directive_subclass_to_string(subclass)
1085 names_to_string(names),
1086 import_directive_subclass_to_string(subclass))).to_string()
1090 fn import_directive_subclass_to_string(subclass: ImportDirectiveSubclass) -> String {
1092 SingleImport(_, source) => source.to_string(),
1093 GlobImport => "*".to_string()
1097 pub fn resolve_imports(resolver: &mut Resolver) {
1098 let mut import_resolver = ImportResolver {
1101 import_resolver.resolve_imports();