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};
16 use {NameBinding, NameBindingKind, PrivacyError};
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::*;
29 use syntax::ast::{NodeId, Name};
30 use syntax::attr::AttrMetaMethods;
31 use syntax::codemap::Span;
32 use syntax::util::lev_distance::find_best_match_for_name;
34 use std::mem::replace;
37 /// Contains data for specific types of import directives.
38 #[derive(Clone, Debug)]
39 pub enum ImportDirectiveSubclass {
43 type_determined: Cell<bool>,
44 value_determined: Cell<bool>,
49 impl ImportDirectiveSubclass {
50 pub fn single(target: Name, source: Name) -> Self {
54 type_determined: Cell::new(false),
55 value_determined: Cell::new(false),
60 /// Whether an import can be shadowed by another import.
61 #[derive(Debug,PartialEq,Clone,Copy)]
67 /// One import directive.
68 #[derive(Debug,Clone)]
69 pub struct ImportDirective {
70 pub module_path: Vec<Name>,
71 pub subclass: ImportDirectiveSubclass,
74 pub is_public: bool, // see note in ImportResolutionPerNamespace about how to use this
75 pub shadowable: Shadowable,
78 impl ImportDirective {
79 pub fn new(module_path: Vec<Name>,
80 subclass: ImportDirectiveSubclass,
84 shadowable: Shadowable)
87 module_path: module_path,
92 shadowable: shadowable,
96 // Given the binding to which this directive resolves in a particular namespace,
97 // this returns the binding for the name this directive defines in that namespace.
99 binding: &'a NameBinding<'a>,
100 privacy_error: Option<Box<PrivacyError<'a>>>) -> NameBinding<'a> {
101 let mut modifiers = match self.is_public {
102 true => DefModifiers::PUBLIC | DefModifiers::IMPORTABLE,
103 false => DefModifiers::empty(),
105 if let GlobImport = self.subclass {
106 modifiers = modifiers | DefModifiers::GLOB_IMPORTED;
108 if self.shadowable == Shadowable::Always {
109 modifiers = modifiers | DefModifiers::PRELUDE;
113 kind: NameBindingKind::Import {
116 privacy_error: privacy_error,
118 span: Some(self.span),
119 modifiers: modifiers,
124 #[derive(Clone, Default)]
125 /// Records information about the resolution of a name in a module.
126 pub struct NameResolution<'a> {
127 /// The number of unresolved single imports that could define the name.
128 pub outstanding_references: usize,
129 /// The least shadowable known binding for this name, or None if there are no known bindings.
130 pub binding: Option<&'a NameBinding<'a>>,
133 impl<'a> NameResolution<'a> {
134 pub fn result(&self, outstanding_globs: usize) -> ResolveResult<&'a NameBinding<'a>> {
135 // If no unresolved imports (single or glob) can define the name, self.binding is final.
136 if self.outstanding_references == 0 && outstanding_globs == 0 {
137 return self.binding.map(Success).unwrap_or(Failed(None));
140 if let Some(binding) = self.binding {
141 // Single imports will never be shadowable by other single or glob imports.
142 if !binding.defined_with(DefModifiers::GLOB_IMPORTED) { return Success(binding); }
143 // Non-PRELUDE glob imports will never be shadowable by other glob imports.
144 if self.outstanding_references == 0 && !binding.defined_with(DefModifiers::PRELUDE) {
145 return Success(binding);
152 // Define the name or return the existing binding if there is a collision.
153 pub fn try_define(&mut self, binding: &'a NameBinding<'a>) -> Result<(), &'a NameBinding<'a>> {
154 let is_prelude = |binding: &NameBinding| binding.defined_with(DefModifiers::PRELUDE);
155 let old_binding = match self.binding {
156 Some(_) if is_prelude(binding) => return Ok(()),
157 Some(old_binding) if !is_prelude(old_binding) => old_binding,
158 _ => { self.binding = Some(binding); return Ok(()); }
161 // FIXME #31337: We currently allow items to shadow glob-imported re-exports.
162 if !old_binding.is_import() && binding.defined_with(DefModifiers::GLOB_IMPORTED) {
163 if let NameBindingKind::Import { binding, .. } = binding.kind {
164 if binding.is_import() { return Ok(()); }
172 struct ImportResolvingError<'a> {
173 /// Module where the error happened
174 source_module: Module<'a>,
175 import_directive: &'a ImportDirective,
180 struct ImportResolver<'a, 'b: 'a, 'tcx: 'b> {
181 resolver: &'a mut Resolver<'b, 'tcx>,
184 impl<'a, 'b:'a, 'tcx:'b> ImportResolver<'a, 'b, 'tcx> {
187 // This is a fixed-point algorithm. We resolve imports until our efforts
188 // are stymied by an unresolved import; then we bail out of the current
189 // module and continue. We terminate successfully once no more imports
190 // remain or unsuccessfully when no forward progress in resolving imports
193 /// Resolves all imports for the crate. This method performs the fixed-
195 fn resolve_imports(&mut self) {
197 let mut prev_unresolved_imports = 0;
198 let mut errors = Vec::new();
201 debug!("(resolving imports) iteration {}, {} imports left",
203 self.resolver.unresolved_imports);
205 self.resolve_imports_for_module_subtree(self.resolver.graph_root, &mut errors);
207 if self.resolver.unresolved_imports == 0 {
208 debug!("(resolving imports) success");
212 if self.resolver.unresolved_imports == prev_unresolved_imports {
214 if errors.len() > 0 {
216 self.import_resolving_error(e)
219 // Report unresolved imports only if no hard error was already reported
220 // to avoid generating multiple errors on the same import.
221 // Imports that are still indeterminate at this point are actually blocked
222 // by errored imports, so there is no point reporting them.
223 self.resolver.report_unresolved_imports(self.resolver.graph_root);
229 prev_unresolved_imports = self.resolver.unresolved_imports;
233 /// Resolves an `ImportResolvingError` into the correct enum discriminant
234 /// and passes that on to `resolve_error`.
235 fn import_resolving_error(&self, e: ImportResolvingError<'b>) {
236 // If it's a single failed import then create a "fake" import
237 // resolution for it so that later resolve stages won't complain.
238 if let SingleImport { target, .. } = e.import_directive.subclass {
239 let dummy_binding = self.resolver.arenas.alloc_name_binding(NameBinding {
240 modifiers: DefModifiers::PRELUDE,
241 kind: NameBindingKind::Def(Def::Err),
244 let dummy_binding = e.import_directive.import(dummy_binding, None);
246 let _ = e.source_module.try_define_child(target, ValueNS, dummy_binding.clone());
247 let _ = e.source_module.try_define_child(target, TypeNS, dummy_binding);
250 let path = import_path_to_string(&e.import_directive.module_path,
251 &e.import_directive.subclass);
253 resolve_error(self.resolver,
255 ResolutionError::UnresolvedImport(Some((&path, &e.help))));
258 /// Attempts to resolve imports for the given module and all of its
260 fn resolve_imports_for_module_subtree(&mut self,
262 errors: &mut Vec<ImportResolvingError<'b>>) {
263 debug!("(resolving imports for module subtree) resolving {}",
264 module_to_string(&module_));
265 let orig_module = replace(&mut self.resolver.current_module, module_);
266 self.resolve_imports_for_module(module_, errors);
267 self.resolver.current_module = orig_module;
269 for (_, child_module) in module_.module_children.borrow().iter() {
270 self.resolve_imports_for_module_subtree(child_module, errors);
274 /// Attempts to resolve imports for the given module only.
275 fn resolve_imports_for_module(&mut self,
277 errors: &mut Vec<ImportResolvingError<'b>>) {
278 let mut imports = Vec::new();
279 let mut unresolved_imports = module.unresolved_imports.borrow_mut();
280 ::std::mem::swap(&mut imports, &mut unresolved_imports);
282 for import_directive in imports {
283 match self.resolve_import_for_module(module, &import_directive) {
285 let (span, help) = match err {
286 Some((span, msg)) => (span, format!(". {}", msg)),
287 None => (import_directive.span, String::new()),
289 errors.push(ImportResolvingError {
290 source_module: module,
291 import_directive: import_directive,
296 Indeterminate => unresolved_imports.push(import_directive),
302 /// Attempts to resolve the given import. The return value indicates
303 /// failure if we're certain the name does not exist, indeterminate if we
304 /// don't know whether the name exists at the moment due to other
305 /// currently-unresolved imports, or success if we know the name exists.
306 /// If successful, the resolved bindings are written into the module.
307 fn resolve_import_for_module(&mut self,
309 import_directive: &ImportDirective)
310 -> ResolveResult<()> {
311 debug!("(resolving import for module) resolving import `{}::...` in `{}`",
312 names_to_string(&import_directive.module_path),
313 module_to_string(&module_));
316 .resolve_module_path(module_,
317 &import_directive.module_path,
318 UseLexicalScopeFlag::DontUseLexicalScope,
319 import_directive.span)
320 .and_then(|containing_module| {
321 // We found the module that the target is contained
322 // within. Attempt to resolve the import within it.
323 self.resolve_import(module_, containing_module, import_directive)
326 // Decrement the count of unresolved imports.
327 assert!(self.resolver.unresolved_imports >= 1);
328 self.resolver.unresolved_imports -= 1;
330 if let GlobImport = import_directive.subclass {
331 module_.dec_glob_count();
332 if import_directive.is_public {
333 module_.dec_pub_glob_count();
336 if import_directive.is_public {
337 module_.dec_pub_count();
343 fn resolve_import(&mut self,
345 target_module: Module<'b>,
346 directive: &ImportDirective)
347 -> ResolveResult<()> {
348 let (source, target, value_determined, type_determined) = match directive.subclass {
349 SingleImport { source, target, ref value_determined, ref type_determined } =>
350 (source, target, value_determined, type_determined),
351 GlobImport => return self.resolve_glob_import(module_, target_module, directive),
354 // We need to resolve both namespaces for this to succeed.
355 let (value_result, type_result) = {
356 let mut resolve_in_ns = |ns, determined: bool| {
357 // Temporarily count the directive as determined so that the resolution fails
358 // (as opposed to being indeterminate) when it can only be defined by the directive.
359 if !determined { module_.decrement_outstanding_references_for(target, ns) }
361 self.resolver.resolve_name_in_module(target_module, source, ns, false, true);
362 if !determined { module_.increment_outstanding_references_for(target, ns) }
365 (resolve_in_ns(ValueNS, value_determined.get()),
366 resolve_in_ns(TypeNS, type_determined.get()))
369 for &(ns, result, determined) in &[(ValueNS, &value_result, value_determined),
370 (TypeNS, &type_result, type_determined)] {
371 if determined.get() { continue }
372 if let Indeterminate = *result { continue }
374 determined.set(true);
375 if let Success(binding) = *result {
376 if !binding.defined_with(DefModifiers::IMPORTABLE) {
377 let msg = format!("`{}` is not directly importable", target);
378 span_err!(self.resolver.session, directive.span, E0253, "{}", &msg);
381 let privacy_error = if !self.resolver.is_visible(binding, target_module) {
382 Some(Box::new(PrivacyError(directive.span, source, binding)))
387 self.define(module_, target, ns, directive.import(binding, privacy_error));
389 module_.decrement_outstanding_references_for(target, ns);
392 match (&value_result, &type_result) {
393 (&Indeterminate, _) | (_, &Indeterminate) => return Indeterminate,
394 (&Failed(_), &Failed(_)) => {
395 let children = target_module.resolutions.borrow();
396 let names = children.keys().map(|&(ref name, _)| name);
397 let lev_suggestion = match find_best_match_for_name(names, &source.as_str(), None) {
398 Some(name) => format!(". Did you mean to use `{}`?", name),
399 None => "".to_owned(),
401 let msg = format!("There is no `{}` in `{}`{}",
403 module_to_string(target_module), lev_suggestion);
404 return Failed(Some((directive.span, msg)));
409 match (&value_result, &type_result) {
410 (&Success(name_binding), _) if !name_binding.is_import() &&
411 directive.is_public &&
412 !name_binding.is_public() => {
413 let msg = format!("`{}` is private, and cannot be reexported", source);
414 let note_msg = format!("consider marking `{}` as `pub` in the imported module",
416 struct_span_err!(self.resolver.session, directive.span, E0364, "{}", &msg)
417 .span_note(directive.span, ¬e_msg)
421 (_, &Success(name_binding)) if !name_binding.is_import() && directive.is_public => {
422 if !name_binding.is_public() {
423 if name_binding.is_extern_crate() {
424 let msg = format!("extern crate `{}` is private, and cannot be reexported \
425 (error E0364), consider declaring with `pub`",
427 self.resolver.session.add_lint(lint::builtin::PRIVATE_IN_PUBLIC,
432 let msg = format!("`{}` is private, and cannot be reexported", source);
434 format!("consider declaring type or module `{}` with `pub`", source);
435 struct_span_err!(self.resolver.session, directive.span, E0365, "{}", &msg)
436 .span_note(directive.span, ¬e_msg)
439 } else if name_binding.defined_with(DefModifiers::PRIVATE_VARIANT) {
440 let msg = format!("variant `{}` is private, and cannot be reexported \
441 (error E0364), consider declaring its enum as `pub`",
443 self.resolver.session.add_lint(lint::builtin::PRIVATE_IN_PUBLIC,
453 // Report a privacy error here if all successful namespaces are privacy errors.
454 let mut privacy_error = None;
455 for &ns in &[ValueNS, TypeNS] {
456 privacy_error = match module_.resolve_name(target, ns, true) {
457 Success(&NameBinding {
458 kind: NameBindingKind::Import { ref privacy_error, .. }, ..
459 }) => privacy_error.as_ref().map(|error| (**error).clone()),
462 if privacy_error.is_none() { break }
464 privacy_error.map(|error| self.resolver.privacy_errors.push(error));
466 // Record what this import resolves to for later uses in documentation,
467 // this may resolve to either a value or a type, but for documentation
468 // purposes it's good enough to just favor one over the other.
469 let def = match type_result.success().and_then(NameBinding::def) {
471 None => value_result.success().and_then(NameBinding::def).unwrap(),
473 let path_resolution = PathResolution { base_def: def, depth: 0 };
474 self.resolver.def_map.borrow_mut().insert(directive.id, path_resolution);
476 debug!("(resolving single import) successfully resolved import");
480 // Resolves a glob import. Note that this function cannot fail; it either
481 // succeeds or bails out (as importing * from an empty module or a module
482 // that exports nothing is valid). target_module is the module we are
483 // actually importing, i.e., `foo` in `use foo::*`.
484 fn resolve_glob_import(&mut self,
486 target_module: Module<'b>,
487 directive: &ImportDirective)
488 -> ResolveResult<()> {
489 // We must bail out if the node has unresolved imports of any kind (including globs).
490 if target_module.pub_count.get() > 0 {
491 debug!("(resolving glob import) target module has unresolved pub imports; bailing out");
492 return Indeterminate;
495 if module_.def_id() == target_module.def_id() {
496 // This means we are trying to glob import a module into itself, and it is a no-go
497 let msg = "Cannot glob-import a module into itself.".into();
498 return Failed(Some((directive.span, msg)));
501 // Add all children from the containing module.
502 build_reduced_graph::populate_module_if_necessary(self.resolver, target_module);
503 target_module.for_each_child(|name, ns, binding| {
504 if !binding.defined_with(DefModifiers::IMPORTABLE | DefModifiers::PUBLIC) { return }
505 self.define(module_, name, ns, directive.import(binding, None));
507 if ns == TypeNS && directive.is_public &&
508 binding.defined_with(DefModifiers::PRIVATE_VARIANT) {
509 let msg = format!("variant `{}` is private, and cannot be reexported (error \
510 E0364), consider declaring its enum as `pub`", name);
511 self.resolver.session.add_lint(lint::builtin::PRIVATE_IN_PUBLIC,
518 // Record the destination of this import
519 if let Some(did) = target_module.def_id() {
520 self.resolver.def_map.borrow_mut().insert(directive.id,
522 base_def: Def::Mod(did),
527 debug!("(resolving glob import) successfully resolved import");
535 binding: NameBinding<'b>) {
536 if let Err(old_binding) = parent.try_define_child(name, ns, binding.clone()) {
537 self.report_conflict(name, ns, &binding, old_binding);
538 } else if binding.is_public() { // Add to the export map
539 if let (Some(parent_def_id), Some(def)) = (parent.def_id(), binding.def()) {
540 let parent_node_id = self.resolver.ast_map.as_local_node_id(parent_def_id).unwrap();
541 let export = Export { name: name, def_id: def.def_id() };
542 self.resolver.export_map.entry(parent_node_id).or_insert(Vec::new()).push(export);
547 fn report_conflict(&mut self,
550 binding: &NameBinding,
551 old_binding: &NameBinding) {
552 // Error on the second of two conflicting imports
553 if old_binding.is_import() && binding.is_import() &&
554 old_binding.span.unwrap().lo > binding.span.unwrap().lo {
555 self.report_conflict(name, ns, old_binding, binding);
559 if old_binding.is_extern_crate() {
560 let msg = format!("import `{0}` conflicts with imported crate \
561 in this module (maybe you meant `use {0}::*`?)",
563 span_err!(self.resolver.session, binding.span.unwrap(), E0254, "{}", &msg);
564 } else if old_binding.is_import() {
565 let ns_word = match (ns, old_binding.module()) {
566 (ValueNS, _) => "value",
567 (TypeNS, Some(module)) if module.is_normal() => "module",
568 (TypeNS, Some(module)) if module.is_trait() => "trait",
569 (TypeNS, _) => "type",
571 let mut err = struct_span_err!(self.resolver.session,
572 binding.span.unwrap(),
574 "a {} named `{}` has already been imported \
578 err.span_note(old_binding.span.unwrap(),
579 &format!("previous import of `{}` here", name));
581 } else if ns == ValueNS { // Check for item conflicts in the value namespace
582 let mut err = struct_span_err!(self.resolver.session,
583 binding.span.unwrap(),
585 "import `{}` conflicts with value in this module",
587 err.span_note(old_binding.span.unwrap(), "conflicting value here");
589 } else { // Check for item conflicts in the type namespace
590 let (what, note) = match old_binding.module() {
591 Some(ref module) if module.is_normal() =>
592 ("existing submodule", "note conflicting module here"),
593 Some(ref module) if module.is_trait() =>
594 ("trait in this module", "note conflicting trait here"),
595 _ => ("type in this module", "note conflicting type here"),
597 let mut err = struct_span_err!(self.resolver.session,
598 binding.span.unwrap(),
600 "import `{}` conflicts with {}",
603 err.span_note(old_binding.span.unwrap(), note);
609 fn import_path_to_string(names: &[Name], subclass: &ImportDirectiveSubclass) -> String {
610 if names.is_empty() {
611 import_directive_subclass_to_string(subclass)
614 names_to_string(names),
615 import_directive_subclass_to_string(subclass)))
620 fn import_directive_subclass_to_string(subclass: &ImportDirectiveSubclass) -> String {
622 SingleImport { source, .. } => source.to_string(),
623 GlobImport => "*".to_string(),
627 pub fn resolve_imports(resolver: &mut Resolver) {
628 let mut import_resolver = ImportResolver { resolver: resolver };
629 import_resolver.resolve_imports();