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};
21 use UseLexicalScopeFlag;
22 use {names_to_string, module_to_string};
23 use {resolve_error, ResolutionError};
25 use build_reduced_graph;
28 use rustc::middle::def::*;
29 use rustc::middle::def_id::DefId;
30 use rustc::middle::privacy::*;
32 use syntax::ast::{NodeId, Name};
33 use syntax::attr::AttrMetaMethods;
34 use syntax::codemap::Span;
35 use syntax::util::lev_distance::find_best_match_for_name;
37 use std::mem::replace;
39 /// Contains data for specific types of import directives.
40 #[derive(Copy, Clone,Debug)]
41 pub enum ImportDirectiveSubclass {
42 SingleImport(Name /* target */, Name /* source */),
46 /// Whether an import can be shadowed by another import.
47 #[derive(Debug,PartialEq,Clone,Copy)]
53 /// One import directive.
54 #[derive(Debug,Clone)]
55 pub struct ImportDirective {
56 pub module_path: Vec<Name>,
57 pub subclass: ImportDirectiveSubclass,
60 pub is_public: bool, // see note in ImportResolutionPerNamespace about how to use this
61 pub shadowable: Shadowable,
64 impl ImportDirective {
65 pub fn new(module_path: Vec<Name>,
66 subclass: ImportDirectiveSubclass,
70 shadowable: Shadowable)
73 module_path: module_path,
78 shadowable: shadowable,
83 /// The item that an import resolves to.
84 #[derive(Clone,Debug)]
85 pub struct Target<'a> {
86 pub target_module: Module<'a>,
87 pub binding: NameBinding<'a>,
88 pub shadowable: Shadowable,
92 pub fn new(target_module: Module<'a>, binding: NameBinding<'a>, shadowable: Shadowable)
95 target_module: target_module,
97 shadowable: shadowable,
103 /// An ImportResolution records what we know about an imported name in a given namespace.
104 /// More specifically, it records the number of unresolved `use` directives that import the name,
105 /// the `use` directive importing the name in the namespace, and the `NameBinding` to which the
106 /// name in the namespace resolves (if applicable).
107 /// Different `use` directives may import the same name in different namespaces.
108 pub struct ImportResolution<'a> {
109 // When outstanding_references reaches zero, outside modules can count on the targets being
110 // correct. Before then, all bets are off; future `use` directives could override the name.
111 // Since shadowing is forbidden, the only way outstanding_references > 1 in a legal program
112 // is if the name is imported by exactly two `use` directives, one of which resolves to a
113 // value and the other of which resolves to a type.
114 pub outstanding_references: usize,
116 /// Whether this resolution came from a `use` or a `pub use`.
119 /// Resolution of the name in the namespace
120 pub target: Option<Target<'a>>,
122 /// The source node of the `use` directive
126 impl<'a> ImportResolution<'a> {
127 pub fn new(id: NodeId, is_public: bool) -> Self {
129 outstanding_references: 0,
132 is_public: is_public,
136 pub fn shadowable(&self) -> Shadowable {
138 Some(ref target) => target.shadowable,
139 None => Shadowable::Always,
144 struct ImportResolvingError<'a> {
145 /// Module where the error happened
146 source_module: Module<'a>,
147 import_directive: ImportDirective,
152 struct ImportResolver<'a, 'b: 'a, 'tcx: 'b> {
153 resolver: &'a mut Resolver<'b, 'tcx>,
156 impl<'a, 'b:'a, 'tcx:'b> ImportResolver<'a, 'b, 'tcx> {
159 // This is a fixed-point algorithm. We resolve imports until our efforts
160 // are stymied by an unresolved import; then we bail out of the current
161 // module and continue. We terminate successfully once no more imports
162 // remain or unsuccessfully when no forward progress in resolving imports
165 /// Resolves all imports for the crate. This method performs the fixed-
167 fn resolve_imports(&mut self) {
169 let mut prev_unresolved_imports = 0;
171 debug!("(resolving imports) iteration {}, {} imports left",
173 self.resolver.unresolved_imports);
175 let module_root = self.resolver.graph_root;
176 let errors = self.resolve_imports_for_module_subtree(module_root);
178 if self.resolver.unresolved_imports == 0 {
179 debug!("(resolving imports) success");
183 if self.resolver.unresolved_imports == prev_unresolved_imports {
185 if errors.len() > 0 {
187 self.import_resolving_error(e)
190 // Report unresolved imports only if no hard error was already reported
191 // to avoid generating multiple errors on the same import.
192 // Imports that are still indeterminate at this point are actually blocked
193 // by errored imports, so there is no point reporting them.
194 self.resolver.report_unresolved_imports(module_root);
200 prev_unresolved_imports = self.resolver.unresolved_imports;
204 /// Resolves an `ImportResolvingError` into the correct enum discriminant
205 /// and passes that on to `resolve_error`.
206 fn import_resolving_error(&self, e: ImportResolvingError) {
207 // If it's a single failed import then create a "fake" import
208 // resolution for it so that later resolve stages won't complain.
209 if let SingleImport(target, _) = e.import_directive.subclass {
210 let mut import_resolutions = e.source_module.import_resolutions.borrow_mut();
212 let resolution = import_resolutions.entry((target, ValueNS)).or_insert_with(|| {
213 debug!("(resolving import error) adding import resolution for `{}`",
216 ImportResolution::new(e.import_directive.id,
217 e.import_directive.is_public)
220 if resolution.target.is_none() {
221 debug!("(resolving import error) adding fake target to import resolution of `{}`",
224 let name_binding = NameBinding {
225 modifiers: DefModifiers::IMPORTABLE,
226 def_or_module: DefOrModule::Def(Def::Err),
230 // Create a fake target pointing to a fake name binding in our
232 let target = Target::new(e.source_module,
236 resolution.target = Some(target);
240 let path = import_path_to_string(&e.import_directive.module_path,
241 e.import_directive.subclass);
243 resolve_error(self.resolver,
245 ResolutionError::UnresolvedImport(Some((&path, &e.help))));
248 /// Attempts to resolve imports for the given module and all of its
250 fn resolve_imports_for_module_subtree(&mut self,
252 -> Vec<ImportResolvingError<'b>> {
253 let mut errors = Vec::new();
254 debug!("(resolving imports for module subtree) resolving {}",
255 module_to_string(&*module_));
256 let orig_module = replace(&mut self.resolver.current_module, module_);
257 errors.extend(self.resolve_imports_for_module(module_));
258 self.resolver.current_module = orig_module;
260 build_reduced_graph::populate_module_if_necessary(self.resolver, &module_);
261 module_.for_each_local_child(|_, _, child_node| {
262 match child_node.module() {
266 Some(child_module) => {
267 errors.extend(self.resolve_imports_for_module_subtree(child_module));
272 for (_, child_module) in module_.anonymous_children.borrow().iter() {
273 errors.extend(self.resolve_imports_for_module_subtree(child_module));
279 /// Attempts to resolve imports for the given module only.
280 fn resolve_imports_for_module(&mut self, module: Module<'b>) -> Vec<ImportResolvingError<'b>> {
281 let mut errors = Vec::new();
283 if module.all_imports_resolved() {
284 debug!("(resolving imports for module) all imports resolved for {}",
285 module_to_string(&*module));
289 let mut imports = module.imports.borrow_mut();
290 let import_count = imports.len();
291 let mut indeterminate_imports = Vec::new();
292 while module.resolved_import_count.get() + indeterminate_imports.len() < import_count {
293 let import_index = module.resolved_import_count.get();
294 match self.resolve_import_for_module(module, &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 {
302 source_module: module,
303 import_directive: import_directive.clone(),
308 ResolveResult::Indeterminate => {}
309 ResolveResult::Success(()) => {
311 module.resolved_import_count
312 .set(module.resolved_import_count.get() + 1);
316 // This resolution was not successful, keep it for later
317 indeterminate_imports.push(imports.swap_remove(import_index));
321 imports.extend(indeterminate_imports);
326 /// Attempts to resolve the given import. The return value indicates
327 /// failure if we're certain the name does not exist, indeterminate if we
328 /// don't know whether the name exists at the moment due to other
329 /// currently-unresolved imports, or success if we know the name exists.
330 /// If successful, the resolved bindings are written into the module.
331 fn resolve_import_for_module(&mut self,
333 import_directive: &ImportDirective)
334 -> ResolveResult<()> {
335 debug!("(resolving import for module) resolving import `{}::...` in `{}`",
336 names_to_string(&import_directive.module_path),
337 module_to_string(&*module_));
340 .resolve_module_path(module_,
341 &import_directive.module_path,
342 UseLexicalScopeFlag::DontUseLexicalScope,
343 import_directive.span)
344 .and_then(|(containing_module, lp)| {
345 // We found the module that the target is contained
346 // within. Attempt to resolve the import within it.
347 if let SingleImport(target, source) = import_directive.subclass {
348 self.resolve_single_import(&module_,
355 self.resolve_glob_import(&module_, containing_module, import_directive, lp)
359 // Decrement the count of unresolved imports.
360 assert!(self.resolver.unresolved_imports >= 1);
361 self.resolver.unresolved_imports -= 1;
363 if let GlobImport = import_directive.subclass {
364 module_.dec_glob_count();
365 if import_directive.is_public {
366 module_.dec_pub_glob_count();
369 if import_directive.is_public {
370 module_.dec_pub_count();
376 /// Resolves the name in the namespace of the module because it is being imported by
377 /// importing_module. Returns the module in which the name was defined (as opposed to imported),
378 /// the name bindings defining the name, and whether or not the name was imported into `module`.
379 fn resolve_name_in_module(&mut self,
380 module: Module<'b>, // Module containing the name
383 importing_module: Module<'b>) // Module importing the name
384 -> (ResolveResult<(Module<'b>, NameBinding<'b>)>, bool) {
385 build_reduced_graph::populate_module_if_necessary(self.resolver, module);
386 if let Some(name_binding) = module.get_child(name, ns) {
387 if name_binding.is_extern_crate() {
388 // track the extern crate as used.
389 if let Some(DefId { krate, .. }) = name_binding.module().unwrap().def_id() {
390 self.resolver.used_crates.insert(krate);
393 return (Success((module, name_binding)), false)
396 // If there is an unresolved glob at this point in the containing module, bail out.
397 // We don't know enough to be able to resolve the name.
398 if module.pub_glob_count.get() > 0 {
399 return (Indeterminate, false);
402 match module.import_resolutions.borrow().get(&(name, ns)) {
403 // The containing module definitely doesn't have an exported import with the
404 // name in question. We can therefore accurately report that names are unbound.
405 None => (Failed(None), false),
407 // The name is an import which has been fully resolved, so we just follow it.
408 Some(resolution) if resolution.outstanding_references == 0 => {
409 // Import resolutions must be declared with "pub" in order to be exported.
410 if !resolution.is_public {
411 return (Failed(None), false);
414 let target = resolution.target.clone();
415 if let Some(Target { target_module, binding, shadowable: _ }) = target {
416 self.resolver.record_import_use(name, ns, &resolution);
417 (Success((target_module, binding)), true)
419 (Failed(None), false)
423 // If module is the same module whose import we are resolving and
424 // it has an unresolved import with the same name as `name`, then the user
425 // is actually trying to import an item that is declared in the same scope
428 // use self::submodule;
429 // pub mod submodule;
431 // In this case we continue as if we resolved the import and let
432 // check_for_conflicts_between_imports_and_items handle the conflict
433 Some(_) => match (importing_module.def_id(), module.def_id()) {
434 (Some(id1), Some(id2)) if id1 == id2 => (Failed(None), false),
435 _ => (Indeterminate, false)
440 fn resolve_single_import(&mut self,
442 target_module: Module<'b>,
445 directive: &ImportDirective,
447 -> ResolveResult<()> {
448 debug!("(resolving single import) resolving `{}` = `{}::{}` from `{}` id {}, last \
451 module_to_string(&*target_module),
453 module_to_string(module_),
462 .span_bug(directive.span, "not expecting Import here, must be LastMod")
466 // We need to resolve both namespaces for this to succeed.
467 let (value_result, value_used_reexport) =
468 self.resolve_name_in_module(&target_module, source, ValueNS, module_);
469 let (type_result, type_used_reexport) =
470 self.resolve_name_in_module(&target_module, source, TypeNS, module_);
472 match (&value_result, &type_result) {
473 (&Success((_, ref name_binding)), _) if !value_used_reexport &&
474 directive.is_public &&
475 !name_binding.is_public() => {
476 let msg = format!("`{}` is private, and cannot be reexported", source);
477 let note_msg = format!("Consider marking `{}` as `pub` in the imported module",
479 struct_span_err!(self.resolver.session, directive.span, E0364, "{}", &msg)
480 .span_note(directive.span, ¬e_msg)
484 (_, &Success((_, ref name_binding))) if !type_used_reexport &&
485 directive.is_public => {
486 if !name_binding.is_public() {
487 let msg = format!("`{}` is private, and cannot be reexported", source);
489 format!("Consider declaring type or module `{}` with `pub`", source);
490 struct_span_err!(self.resolver.session, directive.span, E0365, "{}", &msg)
491 .span_note(directive.span, ¬e_msg)
493 } else if name_binding.defined_with(DefModifiers::PRIVATE_VARIANT) {
494 let msg = format!("variant `{}` is private, and cannot be reexported \
495 (error E0364), consider declaring its enum as `pub`",
497 self.resolver.session.add_lint(lint::builtin::PRIVATE_IN_PUBLIC,
507 let mut lev_suggestion = "".to_owned();
508 match (&value_result, &type_result) {
509 (&Indeterminate, _) | (_, &Indeterminate) => return Indeterminate,
510 (&Failed(_), &Failed(_)) => {
511 let children = target_module.children.borrow();
512 let names = children.keys().map(|&(ref name, _)| name);
513 if let Some(name) = find_best_match_for_name(names, &source.as_str(), None) {
514 lev_suggestion = format!(". Did you mean to use `{}`?", name);
516 let resolutions = target_module.import_resolutions.borrow();
517 let names = resolutions.keys().map(|&(ref name, _)| name);
518 if let Some(name) = find_best_match_for_name(names,
522 format!(". Did you mean to use the re-exported import `{}`?", name);
529 let mut value_used_public = false;
530 let mut type_used_public = false;
532 // We've successfully resolved the import. Write the results in.
533 let mut import_resolutions = module_.import_resolutions.borrow_mut();
536 let mut check_and_write_import = |namespace, result, used_public: &mut bool| {
537 let result: &ResolveResult<(Module<'b>, NameBinding)> = result;
539 let import_resolution = import_resolutions.get_mut(&(target, namespace)).unwrap();
540 let namespace_name = match namespace {
546 Success((ref target_module, ref name_binding)) => {
547 debug!("(resolving single import) found {:?} target: {:?}",
550 self.check_for_conflicting_import(&import_resolution,
555 self.check_that_import_is_importable(&name_binding,
559 import_resolution.target = Some(Target::new(target_module,
560 name_binding.clone(),
561 directive.shadowable));
562 import_resolution.id = directive.id;
563 import_resolution.is_public = directive.is_public;
565 self.add_export(module_, target, &import_resolution);
566 *used_public = name_binding.is_public();
572 panic!("{:?} result should be known at this point", namespace_name);
576 self.check_for_conflicts_between_imports_and_items(module_,
579 (target, namespace));
581 check_and_write_import(ValueNS, &value_result, &mut value_used_public);
582 check_and_write_import(TypeNS, &type_result, &mut type_used_public);
585 if let (&Failed(_), &Failed(_)) = (&value_result, &type_result) {
586 let msg = format!("There is no `{}` in `{}`{}",
588 module_to_string(&target_module), lev_suggestion);
589 return Failed(Some((directive.span, msg)));
592 let value_used_public = value_used_reexport || value_used_public;
593 let type_used_public = type_used_reexport || type_used_public;
595 let value_def_and_priv = {
596 let import_resolution_value = import_resolutions.get_mut(&(target, ValueNS)).unwrap();
597 assert!(import_resolution_value.outstanding_references >= 1);
598 import_resolution_value.outstanding_references -= 1;
600 // Record what this import resolves to for later uses in documentation,
601 // this may resolve to either a value or a type, but for documentation
602 // purposes it's good enough to just favor one over the other.
603 import_resolution_value.target.as_ref().map(|target| {
604 let def = target.binding.def().unwrap();
605 let last_private = if value_used_public { lp } else { DependsOn(def.def_id()) };
610 let type_def_and_priv = {
611 let import_resolution_type = import_resolutions.get_mut(&(target, TypeNS)).unwrap();
612 assert!(import_resolution_type.outstanding_references >= 1);
613 import_resolution_type.outstanding_references -= 1;
615 import_resolution_type.target.as_ref().map(|target| {
616 let def = target.binding.def().unwrap();
617 let last_private = if type_used_public { lp } else { DependsOn(def.def_id()) };
622 let import_lp = LastImport {
623 value_priv: value_def_and_priv.map(|(_, p)| p),
625 type_priv: type_def_and_priv.map(|(_, p)| p),
629 if let Some((def, _)) = value_def_and_priv {
630 self.resolver.def_map.borrow_mut().insert(directive.id,
633 last_private: import_lp,
637 if let Some((def, _)) = type_def_and_priv {
638 self.resolver.def_map.borrow_mut().insert(directive.id,
641 last_private: import_lp,
646 debug!("(resolving single import) successfully resolved import");
650 // Resolves a glob import. Note that this function cannot fail; it either
651 // succeeds or bails out (as importing * from an empty module or a module
652 // that exports nothing is valid). target_module is the module we are
653 // actually importing, i.e., `foo` in `use foo::*`.
654 fn resolve_glob_import(&mut self,
656 target_module: Module<'b>,
657 import_directive: &ImportDirective,
659 -> ResolveResult<()> {
660 let id = import_directive.id;
661 let is_public = import_directive.is_public;
663 // This function works in a highly imperative manner; it eagerly adds
664 // everything it can to the list of import resolutions of the module
666 debug!("(resolving glob import) resolving glob import {}", id);
668 // We must bail out if the node has unresolved imports of any kind
669 // (including globs).
670 if (*target_module).pub_count.get() > 0 {
671 debug!("(resolving glob import) target module has unresolved pub imports; bailing out");
672 return ResolveResult::Indeterminate;
675 // Add all resolved imports from the containing module.
676 let import_resolutions = target_module.import_resolutions.borrow();
678 if module_.import_resolutions.borrow_state() != ::std::cell::BorrowState::Unused {
679 // In this case, target_module == module_
680 // This means we are trying to glob import a module into itself,
682 debug!("(resolving glob imports) target module is current module; giving up");
683 return ResolveResult::Failed(Some((import_directive.span,
684 "Cannot glob-import a module into itself.".into())));
687 for (&(name, ns), target_import_resolution) in import_resolutions.iter() {
688 debug!("(resolving glob import) writing module resolution {} into `{}`",
690 module_to_string(module_));
692 // Here we merge two import resolutions.
693 let mut import_resolutions = module_.import_resolutions.borrow_mut();
694 let mut dest_import_resolution =
695 import_resolutions.entry((name, ns))
696 .or_insert_with(|| ImportResolution::new(id, is_public));
698 match target_import_resolution.target {
699 Some(ref target) if target_import_resolution.is_public => {
700 self.check_for_conflicting_import(&dest_import_resolution,
701 import_directive.span,
704 dest_import_resolution.id = id;
705 dest_import_resolution.is_public = is_public;
706 dest_import_resolution.target = Some(target.clone());
707 self.add_export(module_, name, &dest_import_resolution);
713 // Add all children from the containing module.
714 build_reduced_graph::populate_module_if_necessary(self.resolver, &target_module);
716 target_module.for_each_local_child(|name, ns, name_binding| {
717 self.merge_import_resolution(module_,
721 name_binding.clone());
724 // Record the destination of this import
725 if let Some(did) = target_module.def_id() {
726 self.resolver.def_map.borrow_mut().insert(id,
728 base_def: Def::Mod(did),
734 debug!("(resolving glob import) successfully resolved import");
735 return ResolveResult::Success(());
738 fn merge_import_resolution(&mut self,
740 containing_module: Module<'b>,
741 import_directive: &ImportDirective,
742 (name, ns): (Name, Namespace),
743 name_binding: NameBinding<'b>) {
744 let id = import_directive.id;
745 let is_public = import_directive.is_public;
747 let mut import_resolutions = module_.import_resolutions.borrow_mut();
748 let dest_import_resolution = import_resolutions.entry((name, ns)).or_insert_with(|| {
749 ImportResolution::new(id, is_public)
752 debug!("(resolving glob import) writing resolution `{}` in `{}` to `{}`",
754 module_to_string(&*containing_module),
755 module_to_string(module_));
757 // Merge the child item into the import resolution.
758 let modifier = DefModifiers::IMPORTABLE | DefModifiers::PUBLIC;
760 if ns == TypeNS && is_public && name_binding.defined_with(DefModifiers::PRIVATE_VARIANT) {
761 let msg = format!("variant `{}` is private, and cannot be reexported (error \
762 E0364), consider declaring its enum as `pub`", name);
763 self.resolver.session.add_lint(lint::builtin::PRIVATE_IN_PUBLIC,
765 import_directive.span,
769 if name_binding.defined_with(modifier) {
770 let namespace_name = match ns {
774 debug!("(resolving glob import) ... for {} target", namespace_name);
775 if dest_import_resolution.shadowable() == Shadowable::Never {
776 let msg = format!("a {} named `{}` has already been imported in this module",
779 span_err!(self.resolver.session, import_directive.span, E0251, "{}", msg);
781 let target = Target::new(containing_module,
782 name_binding.clone(),
783 import_directive.shadowable);
784 dest_import_resolution.target = Some(target);
785 dest_import_resolution.id = id;
786 dest_import_resolution.is_public = is_public;
787 self.add_export(module_, name, &dest_import_resolution);
791 self.check_for_conflicts_between_imports_and_items(module_,
792 dest_import_resolution,
793 import_directive.span,
797 fn add_export(&mut self, module: Module<'b>, name: Name, resolution: &ImportResolution<'b>) {
798 if !resolution.is_public { return }
799 let node_id = match module.def_id() {
800 Some(def_id) => self.resolver.ast_map.as_local_node_id(def_id).unwrap(),
803 let export = match resolution.target.as_ref().unwrap().binding.def() {
804 Some(def) => Export { name: name, def_id: def.def_id() },
807 self.resolver.export_map.entry(node_id).or_insert(Vec::new()).push(export);
810 /// Checks that imported names and items don't have the same name.
811 fn check_for_conflicting_import(&mut self,
812 import_resolution: &ImportResolution,
815 namespace: Namespace) {
816 let target = &import_resolution.target;
817 debug!("check_for_conflicting_import: {}; target exists: {}",
822 Some(ref target) if target.shadowable != Shadowable::Always => {
823 let ns_word = match namespace {
825 match target.binding.module() {
826 Some(ref module) if module.is_normal() => "module",
827 Some(ref module) if module.is_trait() => "trait",
833 let use_id = import_resolution.id;
834 let item = self.resolver.ast_map.expect_item(use_id);
835 let mut err = struct_span_err!(self.resolver.session,
838 "a {} named `{}` has already been imported \
844 "previous import of `{}` here",
852 /// Checks that an import is actually importable
853 fn check_that_import_is_importable(&mut self,
854 name_binding: &NameBinding,
857 if !name_binding.defined_with(DefModifiers::IMPORTABLE) {
858 let msg = format!("`{}` is not directly importable", name);
859 span_err!(self.resolver.session, import_span, E0253, "{}", &msg[..]);
863 /// Checks that imported names and items don't have the same name.
864 fn check_for_conflicts_between_imports_and_items(&mut self,
866 import: &ImportResolution<'b>,
868 (name, ns): (Name, Namespace)) {
869 // Check for item conflicts.
870 let name_binding = match module.get_child(name, ns) {
872 // There can't be any conflicts.
875 Some(name_binding) => name_binding,
879 match import.target {
880 Some(ref target) if target.shadowable != Shadowable::Always => {
881 let mut err = struct_span_err!(self.resolver.session,
884 "import `{}` conflicts with \
885 value in this module",
887 if let Some(span) = name_binding.span {
888 err.span_note(span, "conflicting value here");
895 match import.target {
896 Some(ref target) if target.shadowable != Shadowable::Always => {
897 if name_binding.is_extern_crate() {
898 let msg = format!("import `{0}` conflicts with imported crate \
899 in this module (maybe you meant `use {0}::*`?)",
901 span_err!(self.resolver.session, import_span, E0254, "{}", &msg[..]);
905 let (what, note) = match name_binding.module() {
906 Some(ref module) if module.is_normal() =>
907 ("existing submodule", "note conflicting module here"),
908 Some(ref module) if module.is_trait() =>
909 ("trait in this module", "note conflicting trait here"),
910 _ => ("type in this module", "note conflicting type here"),
912 let mut err = struct_span_err!(self.resolver.session,
915 "import `{}` conflicts with {}",
918 if let Some(span) = name_binding.span {
919 err.span_note(span, note);
929 fn import_path_to_string(names: &[Name], subclass: ImportDirectiveSubclass) -> String {
930 if names.is_empty() {
931 import_directive_subclass_to_string(subclass)
934 names_to_string(names),
935 import_directive_subclass_to_string(subclass)))
940 fn import_directive_subclass_to_string(subclass: ImportDirectiveSubclass) -> String {
942 SingleImport(_, source) => source.to_string(),
943 GlobImport => "*".to_string(),
947 pub fn resolve_imports(resolver: &mut Resolver) {
948 let mut import_resolver = ImportResolver { resolver: resolver };
949 import_resolver.resolve_imports();