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::*;
15 use Namespace::{self, TypeNS, ValueNS};
20 use UseLexicalScopeFlag;
21 use {names_to_string, module_to_string};
22 use {resolve_error, ResolutionError};
24 use build_reduced_graph;
27 use rustc::middle::def::*;
28 use rustc::middle::def_id::DefId;
29 use rustc::middle::privacy::*;
31 use syntax::ast::{NodeId, Name};
32 use syntax::attr::AttrMetaMethods;
33 use syntax::codemap::Span;
34 use syntax::util::lev_distance::find_best_match_for_name;
36 use std::mem::replace;
38 /// Contains data for specific types of import directives.
39 #[derive(Copy, Clone,Debug)]
40 pub enum ImportDirectiveSubclass {
41 SingleImport(Name /* target */, Name /* source */),
45 /// Whether an import can be shadowed by another import.
46 #[derive(Debug,PartialEq,Clone,Copy)]
52 /// One import directive.
54 pub struct ImportDirective {
55 pub module_path: Vec<Name>,
56 pub subclass: ImportDirectiveSubclass,
59 pub is_public: bool, // see note in ImportResolutionPerNamespace about how to use this
60 pub shadowable: Shadowable,
63 impl ImportDirective {
64 pub fn new(module_path: Vec<Name>,
65 subclass: ImportDirectiveSubclass,
69 shadowable: Shadowable)
72 module_path: module_path,
77 shadowable: shadowable,
82 /// The item that an import resolves to.
83 #[derive(Clone,Debug)]
84 pub struct Target<'a> {
85 pub target_module: Module<'a>,
86 pub binding: NameBinding<'a>,
87 pub shadowable: Shadowable,
91 pub fn new(target_module: Module<'a>, binding: NameBinding<'a>, shadowable: Shadowable)
94 target_module: target_module,
96 shadowable: shadowable,
102 /// An ImportResolution records what we know about an imported name in a given namespace.
103 /// More specifically, it records the number of unresolved `use` directives that import the name,
104 /// the `use` directive importing the name in the namespace, and the `NameBinding` to which the
105 /// name in the namespace resolves (if applicable).
106 /// Different `use` directives may import the same name in different namespaces.
107 pub struct ImportResolution<'a> {
108 // When outstanding_references reaches zero, outside modules can count on the targets being
109 // correct. Before then, all bets are off; future `use` directives could override the name.
110 // Since shadowing is forbidden, the only way outstanding_references > 1 in a legal program
111 // is if the name is imported by exactly two `use` directives, one of which resolves to a
112 // value and the other of which resolves to a type.
113 pub outstanding_references: usize,
115 /// Whether this resolution came from a `use` or a `pub use`.
118 /// Resolution of the name in the namespace
119 pub target: Option<Target<'a>>,
121 /// The source node of the `use` directive
125 impl<'a> ImportResolution<'a> {
126 pub fn new(id: NodeId, is_public: bool) -> Self {
128 outstanding_references: 0,
131 is_public: is_public,
135 pub fn shadowable(&self) -> Shadowable {
137 Some(ref target) => target.shadowable,
138 None => Shadowable::Always,
143 struct ImportResolvingError {
149 struct ImportResolver<'a, 'b: 'a, 'tcx: 'b> {
150 resolver: &'a mut Resolver<'b, 'tcx>,
153 impl<'a, 'b:'a, 'tcx:'b> ImportResolver<'a, 'b, 'tcx> {
156 // This is a fixed-point algorithm. We resolve imports until our efforts
157 // are stymied by an unresolved import; then we bail out of the current
158 // module and continue. We terminate successfully once no more imports
159 // remain or unsuccessfully when no forward progress in resolving imports
162 /// Resolves all imports for the crate. This method performs the fixed-
164 fn resolve_imports(&mut self) {
166 let mut prev_unresolved_imports = 0;
168 debug!("(resolving imports) iteration {}, {} imports left",
170 self.resolver.unresolved_imports);
172 let module_root = self.resolver.graph_root;
173 let errors = self.resolve_imports_for_module_subtree(module_root);
175 if self.resolver.unresolved_imports == 0 {
176 debug!("(resolving imports) success");
180 if self.resolver.unresolved_imports == prev_unresolved_imports {
182 if errors.len() > 0 {
184 resolve_error(self.resolver,
186 ResolutionError::UnresolvedImport(Some((&e.path, &e.help))));
189 // Report unresolved imports only if no hard error was already reported
190 // to avoid generating multiple errors on the same import.
191 // Imports that are still indeterminate at this point are actually blocked
192 // by errored imports, so there is no point reporting them.
193 self.resolver.report_unresolved_imports(module_root);
199 prev_unresolved_imports = self.resolver.unresolved_imports;
203 /// Attempts to resolve imports for the given module and all of its
205 fn resolve_imports_for_module_subtree(&mut self,
207 -> Vec<ImportResolvingError> {
208 let mut errors = Vec::new();
209 debug!("(resolving imports for module subtree) resolving {}",
210 module_to_string(&*module_));
211 let orig_module = replace(&mut self.resolver.current_module, module_);
212 errors.extend(self.resolve_imports_for_module(module_));
213 self.resolver.current_module = orig_module;
215 build_reduced_graph::populate_module_if_necessary(self.resolver, &module_);
216 module_.for_each_local_child(|_, _, child_node| {
217 match child_node.module() {
221 Some(child_module) => {
222 errors.extend(self.resolve_imports_for_module_subtree(child_module));
227 for (_, child_module) in module_.anonymous_children.borrow().iter() {
228 errors.extend(self.resolve_imports_for_module_subtree(child_module));
234 /// Attempts to resolve imports for the given module only.
235 fn resolve_imports_for_module(&mut self, module: Module<'b>) -> Vec<ImportResolvingError> {
236 let mut errors = Vec::new();
238 if module.all_imports_resolved() {
239 debug!("(resolving imports for module) all imports resolved for {}",
240 module_to_string(&*module));
244 let mut imports = module.imports.borrow_mut();
245 let import_count = imports.len();
246 let mut indeterminate_imports = Vec::new();
247 while module.resolved_import_count.get() + indeterminate_imports.len() < import_count {
248 let import_index = module.resolved_import_count.get();
249 match self.resolve_import_for_module(module, &imports[import_index]) {
250 ResolveResult::Failed(err) => {
251 let import_directive = &imports[import_index];
252 let (span, help) = match err {
253 Some((span, msg)) => (span, format!(". {}", msg)),
254 None => (import_directive.span, String::new()),
256 errors.push(ImportResolvingError {
258 path: import_path_to_string(&import_directive.module_path,
259 import_directive.subclass),
263 ResolveResult::Indeterminate => {}
264 ResolveResult::Success(()) => {
266 module.resolved_import_count
267 .set(module.resolved_import_count.get() + 1);
271 // This resolution was not successful, keep it for later
272 indeterminate_imports.push(imports.swap_remove(import_index));
276 imports.extend(indeterminate_imports);
281 /// Attempts to resolve the given import. The return value indicates
282 /// failure if we're certain the name does not exist, indeterminate if we
283 /// don't know whether the name exists at the moment due to other
284 /// currently-unresolved imports, or success if we know the name exists.
285 /// If successful, the resolved bindings are written into the module.
286 fn resolve_import_for_module(&mut self,
288 import_directive: &ImportDirective)
289 -> ResolveResult<()> {
290 let mut resolution_result = ResolveResult::Failed(None);
291 let module_path = &import_directive.module_path;
293 debug!("(resolving import for module) resolving import `{}::...` in `{}`",
294 names_to_string(&module_path[..]),
295 module_to_string(&*module_));
297 // First, resolve the module path for the directive, if necessary.
298 let container = if module_path.is_empty() {
299 // Use the crate root.
300 Some((self.resolver.graph_root, LastMod(AllPublic)))
302 match self.resolver.resolve_module_path(module_,
304 UseLexicalScopeFlag::DontUseLexicalScope,
305 import_directive.span) {
306 ResolveResult::Failed(err) => {
307 resolution_result = ResolveResult::Failed(err);
310 ResolveResult::Indeterminate => {
311 resolution_result = ResolveResult::Indeterminate;
314 ResolveResult::Success(container) => Some(container),
320 Some((containing_module, lp)) => {
321 // We found the module that the target is contained
322 // within. Attempt to resolve the import within it.
324 match import_directive.subclass {
325 SingleImport(target, source) => {
326 resolution_result = self.resolve_single_import(&module_,
334 resolution_result = self.resolve_glob_import(&module_,
343 // Decrement the count of unresolved imports.
344 match resolution_result {
345 ResolveResult::Success(()) => {
346 assert!(self.resolver.unresolved_imports >= 1);
347 self.resolver.unresolved_imports -= 1;
350 // Nothing to do here; just return the error.
354 // Decrement the count of unresolved globs if necessary. But only if
355 // the resolution result is a success -- other cases will
356 // be handled by the main loop.
358 if resolution_result.success() {
359 match import_directive.subclass {
361 module_.dec_glob_count();
362 if import_directive.is_public {
363 module_.dec_pub_glob_count();
366 SingleImport(..) => {
370 if import_directive.is_public {
371 module_.dec_pub_count();
375 return resolution_result;
378 /// Resolves the name in the namespace of the module because it is being imported by
379 /// importing_module. Returns the module in which the name was defined (as opposed to imported),
380 /// the name bindings defining the name, and whether or not the name was imported into `module`.
381 fn resolve_name_in_module(&mut self,
382 module: Module<'b>, // Module containing the name
385 importing_module: Module<'b>) // Module importing the name
386 -> (ResolveResult<(Module<'b>, NameBinding<'b>)>, bool) {
387 build_reduced_graph::populate_module_if_necessary(self.resolver, module);
388 if let Some(name_binding) = module.get_child(name, ns) {
389 if name_binding.is_extern_crate() {
390 // track the extern crate as used.
391 if let Some(DefId { krate, .. }) = name_binding.module().unwrap().def_id() {
392 self.resolver.used_crates.insert(krate);
395 return (Success((module, name_binding)), false)
398 // If there is an unresolved glob at this point in the containing module, bail out.
399 // We don't know enough to be able to resolve the name.
400 if module.pub_glob_count.get() > 0 {
401 return (Indeterminate, false);
404 match module.import_resolutions.borrow().get(&(name, ns)) {
405 // The containing module definitely doesn't have an exported import with the
406 // name in question. We can therefore accurately report that names are unbound.
407 None => (Failed(None), false),
409 // The name is an import which has been fully resolved, so we just follow it.
410 Some(resolution) if resolution.outstanding_references == 0 => {
411 // Import resolutions must be declared with "pub" in order to be exported.
412 if !resolution.is_public {
413 return (Failed(None), false);
416 let target = resolution.target.clone();
417 if let Some(Target { target_module, binding, shadowable: _ }) = target {
418 // track used imports and extern crates as well
419 self.resolver.used_imports.insert((resolution.id, ns));
420 self.resolver.record_import_use(resolution.id, name);
421 if let Some(DefId { krate, .. }) = target_module.def_id() {
422 self.resolver.used_crates.insert(krate);
424 (Success((target_module, binding)), true)
426 (Failed(None), false)
430 // If module is the same module whose import we are resolving and
431 // it has an unresolved import with the same name as `name`, then the user
432 // is actually trying to import an item that is declared in the same scope
435 // use self::submodule;
436 // pub mod submodule;
438 // In this case we continue as if we resolved the import and let
439 // check_for_conflicts_between_imports_and_items handle the conflict
440 Some(_) => match (importing_module.def_id(), module.def_id()) {
441 (Some(id1), Some(id2)) if id1 == id2 => (Failed(None), false),
442 _ => (Indeterminate, false)
447 fn resolve_single_import(&mut self,
449 target_module: Module<'b>,
452 directive: &ImportDirective,
454 -> ResolveResult<()> {
455 debug!("(resolving single import) resolving `{}` = `{}::{}` from `{}` id {}, last \
458 module_to_string(&*target_module),
460 module_to_string(module_),
469 .span_bug(directive.span, "not expecting Import here, must be LastMod")
473 // We need to resolve both namespaces for this to succeed.
474 let (value_result, value_used_reexport) =
475 self.resolve_name_in_module(&target_module, source, ValueNS, module_);
476 let (type_result, type_used_reexport) =
477 self.resolve_name_in_module(&target_module, source, TypeNS, module_);
479 match (&value_result, &type_result) {
480 (&Success((_, ref name_binding)), _) if !value_used_reexport &&
481 directive.is_public &&
482 !name_binding.is_public() => {
483 let msg = format!("`{}` is private, and cannot be reexported", source);
484 let note_msg = format!("Consider marking `{}` as `pub` in the imported module",
486 struct_span_err!(self.resolver.session, directive.span, E0364, "{}", &msg)
487 .span_note(directive.span, ¬e_msg)
491 (_, &Success((_, ref name_binding))) if !type_used_reexport &&
492 directive.is_public => {
493 if !name_binding.is_public() {
494 let msg = format!("`{}` is private, and cannot be reexported", source);
496 format!("Consider declaring type or module `{}` with `pub`", source);
497 struct_span_err!(self.resolver.session, directive.span, E0365, "{}", &msg)
498 .span_note(directive.span, ¬e_msg)
500 } else if name_binding.defined_with(DefModifiers::PRIVATE_VARIANT) {
501 let msg = format!("variant `{}` is private, and cannot be reexported \
502 (error E0364), consider declaring its enum as `pub`",
504 self.resolver.session.add_lint(lint::builtin::PRIVATE_IN_PUBLIC,
514 let mut lev_suggestion = "".to_owned();
515 match (&value_result, &type_result) {
516 (&Indeterminate, _) | (_, &Indeterminate) => return Indeterminate,
517 (&Failed(_), &Failed(_)) => {
518 let children = target_module.children.borrow();
519 let names = children.keys().map(|&(ref name, _)| name);
520 if let Some(name) = find_best_match_for_name(names, &source.as_str(), None) {
521 lev_suggestion = format!(". Did you mean to use `{}`?", name);
523 let resolutions = target_module.import_resolutions.borrow();
524 let names = resolutions.keys().map(|&(ref name, _)| name);
525 if let Some(name) = find_best_match_for_name(names,
529 format!(". Did you mean to use the re-exported import `{}`?", name);
536 let mut value_used_public = false;
537 let mut type_used_public = false;
539 // We've successfully resolved the import. Write the results in.
540 let mut import_resolutions = module_.import_resolutions.borrow_mut();
543 let mut check_and_write_import = |namespace, result, used_public: &mut bool| {
544 let result: &ResolveResult<(Module<'b>, NameBinding)> = result;
546 let import_resolution = import_resolutions.get_mut(&(target, namespace)).unwrap();
547 let namespace_name = match namespace {
553 Success((ref target_module, ref name_binding)) => {
554 debug!("(resolving single import) found {:?} target: {:?}",
557 self.check_for_conflicting_import(&import_resolution,
562 self.check_that_import_is_importable(&name_binding,
566 import_resolution.target = Some(Target::new(target_module,
567 name_binding.clone(),
568 directive.shadowable));
569 import_resolution.id = directive.id;
570 import_resolution.is_public = directive.is_public;
572 self.add_export(module_, target, &import_resolution);
573 *used_public = name_binding.is_public();
579 panic!("{:?} result should be known at this point", namespace_name);
583 self.check_for_conflicts_between_imports_and_items(module_,
586 (target, namespace));
588 check_and_write_import(ValueNS, &value_result, &mut value_used_public);
589 check_and_write_import(TypeNS, &type_result, &mut type_used_public);
592 if let (&Failed(_), &Failed(_)) = (&value_result, &type_result) {
593 let msg = format!("There is no `{}` in `{}`{}",
595 module_to_string(&target_module), lev_suggestion);
596 return Failed(Some((directive.span, msg)));
599 let value_used_public = value_used_reexport || value_used_public;
600 let type_used_public = type_used_reexport || type_used_public;
602 let value_def_and_priv = {
603 let import_resolution_value = import_resolutions.get_mut(&(target, ValueNS)).unwrap();
604 assert!(import_resolution_value.outstanding_references >= 1);
605 import_resolution_value.outstanding_references -= 1;
607 // Record what this import resolves to for later uses in documentation,
608 // this may resolve to either a value or a type, but for documentation
609 // purposes it's good enough to just favor one over the other.
610 import_resolution_value.target.as_ref().map(|target| {
611 let def = target.binding.def().unwrap();
612 let last_private = if value_used_public { lp } else { DependsOn(def.def_id()) };
617 let type_def_and_priv = {
618 let import_resolution_type = import_resolutions.get_mut(&(target, TypeNS)).unwrap();
619 assert!(import_resolution_type.outstanding_references >= 1);
620 import_resolution_type.outstanding_references -= 1;
622 import_resolution_type.target.as_ref().map(|target| {
623 let def = target.binding.def().unwrap();
624 let last_private = if type_used_public { lp } else { DependsOn(def.def_id()) };
629 let import_lp = LastImport {
630 value_priv: value_def_and_priv.map(|(_, p)| p),
632 type_priv: type_def_and_priv.map(|(_, p)| p),
636 if let Some((def, _)) = value_def_and_priv {
637 self.resolver.def_map.borrow_mut().insert(directive.id,
640 last_private: import_lp,
644 if let Some((def, _)) = type_def_and_priv {
645 self.resolver.def_map.borrow_mut().insert(directive.id,
648 last_private: import_lp,
653 debug!("(resolving single import) successfully resolved import");
657 // Resolves a glob import. Note that this function cannot fail; it either
658 // succeeds or bails out (as importing * from an empty module or a module
659 // that exports nothing is valid). target_module is the module we are
660 // actually importing, i.e., `foo` in `use foo::*`.
661 fn resolve_glob_import(&mut self,
663 target_module: Module<'b>,
664 import_directive: &ImportDirective,
666 -> ResolveResult<()> {
667 let id = import_directive.id;
668 let is_public = import_directive.is_public;
670 // This function works in a highly imperative manner; it eagerly adds
671 // everything it can to the list of import resolutions of the module
673 debug!("(resolving glob import) resolving glob import {}", id);
675 // We must bail out if the node has unresolved imports of any kind
676 // (including globs).
677 if (*target_module).pub_count.get() > 0 {
678 debug!("(resolving glob import) target module has unresolved pub imports; bailing out");
679 return ResolveResult::Indeterminate;
682 // Add all resolved imports from the containing module.
683 let import_resolutions = target_module.import_resolutions.borrow();
685 if module_.import_resolutions.borrow_state() != ::std::cell::BorrowState::Unused {
686 // In this case, target_module == module_
687 // This means we are trying to glob import a module into itself,
689 debug!("(resolving glob imports) target module is current module; giving up");
690 return ResolveResult::Failed(Some((import_directive.span,
691 "Cannot glob-import a module into itself.".into())));
694 for (&(name, ns), target_import_resolution) in import_resolutions.iter() {
695 debug!("(resolving glob import) writing module resolution {} into `{}`",
697 module_to_string(module_));
699 // Here we merge two import resolutions.
700 let mut import_resolutions = module_.import_resolutions.borrow_mut();
701 let mut dest_import_resolution =
702 import_resolutions.entry((name, ns))
703 .or_insert_with(|| ImportResolution::new(id, is_public));
705 match target_import_resolution.target {
706 Some(ref target) if target_import_resolution.is_public => {
707 self.check_for_conflicting_import(&dest_import_resolution,
708 import_directive.span,
711 dest_import_resolution.id = id;
712 dest_import_resolution.is_public = is_public;
713 dest_import_resolution.target = Some(target.clone());
714 self.add_export(module_, name, &dest_import_resolution);
720 // Add all children from the containing module.
721 build_reduced_graph::populate_module_if_necessary(self.resolver, &target_module);
723 target_module.for_each_local_child(|name, ns, name_binding| {
724 self.merge_import_resolution(module_,
728 name_binding.clone());
731 // Record the destination of this import
732 if let Some(did) = target_module.def_id() {
733 self.resolver.def_map.borrow_mut().insert(id,
735 base_def: Def::Mod(did),
741 debug!("(resolving glob import) successfully resolved import");
742 return ResolveResult::Success(());
745 fn merge_import_resolution(&mut self,
747 containing_module: Module<'b>,
748 import_directive: &ImportDirective,
749 (name, ns): (Name, Namespace),
750 name_binding: NameBinding<'b>) {
751 let id = import_directive.id;
752 let is_public = import_directive.is_public;
754 let mut import_resolutions = module_.import_resolutions.borrow_mut();
755 let dest_import_resolution = import_resolutions.entry((name, ns)).or_insert_with(|| {
756 ImportResolution::new(id, is_public)
759 debug!("(resolving glob import) writing resolution `{}` in `{}` to `{}`",
761 module_to_string(&*containing_module),
762 module_to_string(module_));
764 // Merge the child item into the import resolution.
765 let modifier = DefModifiers::IMPORTABLE | DefModifiers::PUBLIC;
767 if ns == TypeNS && is_public && name_binding.defined_with(DefModifiers::PRIVATE_VARIANT) {
768 let msg = format!("variant `{}` is private, and cannot be reexported (error \
769 E0364), consider declaring its enum as `pub`", name);
770 self.resolver.session.add_lint(lint::builtin::PRIVATE_IN_PUBLIC,
772 import_directive.span,
776 if name_binding.defined_with(modifier) {
777 let namespace_name = match ns {
781 debug!("(resolving glob import) ... for {} target", namespace_name);
782 if dest_import_resolution.shadowable() == Shadowable::Never {
783 let msg = format!("a {} named `{}` has already been imported in this module",
786 span_err!(self.resolver.session, import_directive.span, E0251, "{}", msg);
788 let target = Target::new(containing_module,
789 name_binding.clone(),
790 import_directive.shadowable);
791 dest_import_resolution.target = Some(target);
792 dest_import_resolution.id = id;
793 dest_import_resolution.is_public = is_public;
794 self.add_export(module_, name, &dest_import_resolution);
798 self.check_for_conflicts_between_imports_and_items(module_,
799 dest_import_resolution,
800 import_directive.span,
804 fn add_export(&mut self, module: Module<'b>, name: Name, resolution: &ImportResolution<'b>) {
805 if !resolution.is_public { return }
806 let node_id = match module.def_id() {
807 Some(def_id) => self.resolver.ast_map.as_local_node_id(def_id).unwrap(),
810 let export = match resolution.target.as_ref().unwrap().binding.def() {
811 Some(def) => Export { name: name, def_id: def.def_id() },
814 self.resolver.export_map.entry(node_id).or_insert(Vec::new()).push(export);
817 /// Checks that imported names and items don't have the same name.
818 fn check_for_conflicting_import(&mut self,
819 import_resolution: &ImportResolution,
822 namespace: Namespace) {
823 let target = &import_resolution.target;
824 debug!("check_for_conflicting_import: {}; target exists: {}",
829 Some(ref target) if target.shadowable != Shadowable::Always => {
830 let ns_word = match namespace {
832 match target.binding.module() {
833 Some(ref module) if module.is_normal() => "module",
834 Some(ref module) if module.is_trait() => "trait",
840 let use_id = import_resolution.id;
841 let item = self.resolver.ast_map.expect_item(use_id);
842 let mut err = struct_span_err!(self.resolver.session,
845 "a {} named `{}` has already been imported \
851 "previous import of `{}` here",
859 /// Checks that an import is actually importable
860 fn check_that_import_is_importable(&mut self,
861 name_binding: &NameBinding,
864 if !name_binding.defined_with(DefModifiers::IMPORTABLE) {
865 let msg = format!("`{}` is not directly importable", name);
866 span_err!(self.resolver.session, import_span, E0253, "{}", &msg[..]);
870 /// Checks that imported names and items don't have the same name.
871 fn check_for_conflicts_between_imports_and_items(&mut self,
873 import: &ImportResolution<'b>,
875 (name, ns): (Name, Namespace)) {
876 // Check for item conflicts.
877 let name_binding = match module.get_child(name, ns) {
879 // There can't be any conflicts.
882 Some(name_binding) => name_binding,
886 match import.target {
887 Some(ref target) if target.shadowable != Shadowable::Always => {
888 let mut err = struct_span_err!(self.resolver.session,
891 "import `{}` conflicts with \
892 value in this module",
894 if let Some(span) = name_binding.span {
895 err.span_note(span, "conflicting value here");
902 match import.target {
903 Some(ref target) if target.shadowable != Shadowable::Always => {
904 if name_binding.is_extern_crate() {
905 let msg = format!("import `{0}` conflicts with imported crate \
906 in this module (maybe you meant `use {0}::*`?)",
908 span_err!(self.resolver.session, import_span, E0254, "{}", &msg[..]);
912 let (what, note) = match name_binding.module() {
913 Some(ref module) if module.is_normal() =>
914 ("existing submodule", "note conflicting module here"),
915 Some(ref module) if module.is_trait() =>
916 ("trait in this module", "note conflicting trait here"),
917 _ => ("type in this module", "note conflicting type here"),
919 let mut err = struct_span_err!(self.resolver.session,
922 "import `{}` conflicts with {}",
925 if let Some(span) = name_binding.span {
926 err.span_note(span, note);
936 fn import_path_to_string(names: &[Name], subclass: ImportDirectiveSubclass) -> String {
937 if names.is_empty() {
938 import_directive_subclass_to_string(subclass)
941 names_to_string(names),
942 import_directive_subclass_to_string(subclass)))
947 fn import_directive_subclass_to_string(subclass: ImportDirectiveSubclass) -> String {
949 SingleImport(_, source) => source.to_string(),
950 GlobImport => "*".to_string(),
954 pub fn resolve_imports(resolver: &mut Resolver) {
955 let mut import_resolver = ImportResolver { resolver: resolver };
956 import_resolver.resolve_imports();