2015-03-15 16:44:19 -05:00
|
|
|
// Copyright 2015 The Rust Project Developers. See the COPYRIGHT
|
|
|
|
// file at the top-level directory of this distribution and at
|
|
|
|
// http://rust-lang.org/COPYRIGHT.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
|
|
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
|
|
|
// option. This file may not be copied, modified, or distributed
|
|
|
|
// except according to those terms.
|
|
|
|
|
|
|
|
use self::ImportDirectiveSubclass::*;
|
|
|
|
|
2015-04-28 18:36:22 -05:00
|
|
|
use DefModifiers;
|
2015-03-15 16:44:19 -05:00
|
|
|
use Module;
|
|
|
|
use Namespace::{self, TypeNS, ValueNS};
|
2016-02-07 15:34:23 -06:00
|
|
|
use {NameBinding, NameBindingKind};
|
2015-03-15 16:44:19 -05:00
|
|
|
use ResolveResult;
|
2016-01-13 19:42:45 -06:00
|
|
|
use ResolveResult::*;
|
2015-03-15 16:44:19 -05:00
|
|
|
use Resolver;
|
|
|
|
use UseLexicalScopeFlag;
|
|
|
|
use {names_to_string, module_to_string};
|
2015-07-14 12:42:38 -05:00
|
|
|
use {resolve_error, ResolutionError};
|
2015-03-15 16:44:19 -05:00
|
|
|
|
|
|
|
use build_reduced_graph;
|
|
|
|
|
2015-12-13 12:57:07 -06:00
|
|
|
use rustc::lint;
|
2015-03-15 16:44:19 -05:00
|
|
|
use rustc::middle::def::*;
|
|
|
|
use rustc::middle::privacy::*;
|
|
|
|
|
2015-08-16 05:32:28 -05:00
|
|
|
use syntax::ast::{NodeId, Name};
|
2015-03-15 16:44:19 -05:00
|
|
|
use syntax::attr::AttrMetaMethods;
|
|
|
|
use syntax::codemap::Span;
|
2015-12-14 11:06:31 -06:00
|
|
|
use syntax::util::lev_distance::find_best_match_for_name;
|
2015-03-15 16:44:19 -05:00
|
|
|
|
|
|
|
use std::mem::replace;
|
|
|
|
|
|
|
|
/// Contains data for specific types of import directives.
|
2015-03-30 08:38:44 -05:00
|
|
|
#[derive(Copy, Clone,Debug)]
|
2015-03-15 16:44:19 -05:00
|
|
|
pub enum ImportDirectiveSubclass {
|
|
|
|
SingleImport(Name /* target */, Name /* source */),
|
2015-10-26 14:31:11 -05:00
|
|
|
GlobImport,
|
2015-03-15 16:44:19 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Whether an import can be shadowed by another import.
|
|
|
|
#[derive(Debug,PartialEq,Clone,Copy)]
|
|
|
|
pub enum Shadowable {
|
|
|
|
Always,
|
2015-10-26 14:31:11 -05:00
|
|
|
Never,
|
2015-03-15 16:44:19 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/// One import directive.
|
2016-01-31 21:26:16 -06:00
|
|
|
#[derive(Debug,Clone)]
|
2015-03-15 16:44:19 -05:00
|
|
|
pub struct ImportDirective {
|
|
|
|
pub module_path: Vec<Name>,
|
|
|
|
pub subclass: ImportDirectiveSubclass,
|
|
|
|
pub span: Span,
|
|
|
|
pub id: NodeId,
|
2015-12-10 19:38:13 -06:00
|
|
|
pub is_public: bool, // see note in ImportResolutionPerNamespace about how to use this
|
2015-03-15 16:44:19 -05:00
|
|
|
pub shadowable: Shadowable,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ImportDirective {
|
2015-10-26 14:31:11 -05:00
|
|
|
pub fn new(module_path: Vec<Name>,
|
|
|
|
subclass: ImportDirectiveSubclass,
|
|
|
|
span: Span,
|
|
|
|
id: NodeId,
|
|
|
|
is_public: bool,
|
|
|
|
shadowable: Shadowable)
|
|
|
|
-> ImportDirective {
|
2015-03-15 16:44:19 -05:00
|
|
|
ImportDirective {
|
|
|
|
module_path: module_path,
|
|
|
|
subclass: subclass,
|
|
|
|
span: span,
|
|
|
|
id: id,
|
|
|
|
is_public: is_public,
|
|
|
|
shadowable: shadowable,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-07 15:04:01 -06:00
|
|
|
// Given the binding to which this directive resolves in a particular namespace,
|
|
|
|
// this returns the binding for the name this directive defines in that namespace.
|
2016-02-07 15:23:58 -06:00
|
|
|
fn import<'a>(&self, binding: &'a NameBinding<'a>) -> NameBinding<'a> {
|
2016-02-07 15:34:23 -06:00
|
|
|
let mut modifiers = match self.is_public {
|
|
|
|
true => DefModifiers::PUBLIC | DefModifiers::IMPORTABLE,
|
|
|
|
false => DefModifiers::empty(),
|
|
|
|
};
|
|
|
|
if let GlobImport = self.subclass {
|
|
|
|
modifiers = modifiers | DefModifiers::GLOB_IMPORTED;
|
|
|
|
}
|
2016-02-07 15:04:01 -06:00
|
|
|
if self.shadowable == Shadowable::Always {
|
2016-02-07 15:34:23 -06:00
|
|
|
modifiers = modifiers | DefModifiers::PRELUDE;
|
|
|
|
}
|
|
|
|
|
|
|
|
NameBinding {
|
|
|
|
kind: NameBindingKind::Import { binding: binding, id: self.id },
|
|
|
|
span: Some(self.span),
|
|
|
|
modifiers: modifiers,
|
2015-03-15 16:44:19 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-09 00:26:27 -06:00
|
|
|
#[derive(Clone, Default)]
|
2016-02-07 17:58:14 -06:00
|
|
|
/// Records information about the resolution of a name in a module.
|
2016-02-07 16:28:54 -06:00
|
|
|
pub struct NameResolution<'a> {
|
2016-02-07 17:58:14 -06:00
|
|
|
/// The number of unresolved single imports that could define the name.
|
2015-12-01 17:06:34 -06:00
|
|
|
pub outstanding_references: usize,
|
2016-02-07 17:58:14 -06:00
|
|
|
/// The least shadowable known binding for this name, or None if there are no known bindings.
|
2016-02-07 15:23:58 -06:00
|
|
|
pub binding: Option<&'a NameBinding<'a>>,
|
2015-03-15 16:44:19 -05:00
|
|
|
}
|
|
|
|
|
2016-02-07 16:28:54 -06:00
|
|
|
impl<'a> NameResolution<'a> {
|
2016-02-07 17:58:14 -06:00
|
|
|
pub fn result(&self, outstanding_globs: usize) -> ResolveResult<&'a NameBinding<'a>> {
|
|
|
|
// If no unresolved imports (single or glob) can define the name, self.binding is final.
|
|
|
|
if self.outstanding_references == 0 && outstanding_globs == 0 {
|
|
|
|
return self.binding.map(Success).unwrap_or(Failed(None));
|
|
|
|
}
|
|
|
|
|
|
|
|
if let Some(binding) = self.binding {
|
|
|
|
// Single imports will never be shadowable by other single or glob imports.
|
|
|
|
if !binding.defined_with(DefModifiers::GLOB_IMPORTED) { return Success(binding); }
|
|
|
|
// Non-PRELUDE glob imports will never be shadowable by other glob imports.
|
|
|
|
if self.outstanding_references == 0 && !binding.defined_with(DefModifiers::PRELUDE) {
|
|
|
|
return Success(binding);
|
|
|
|
}
|
2015-03-15 16:44:19 -05:00
|
|
|
}
|
2016-02-07 17:58:14 -06:00
|
|
|
|
|
|
|
Indeterminate
|
|
|
|
}
|
|
|
|
|
|
|
|
// Define the name or return the existing binding if there is a collision.
|
|
|
|
pub fn try_define(&mut self, binding: &'a NameBinding<'a>) -> Result<(), &'a NameBinding<'a>> {
|
|
|
|
let is_prelude = |binding: &NameBinding| binding.defined_with(DefModifiers::PRELUDE);
|
|
|
|
let old_binding = match self.binding {
|
2016-02-09 00:26:27 -06:00
|
|
|
Some(_) if is_prelude(binding) => return Ok(()),
|
|
|
|
Some(old_binding) if !is_prelude(old_binding) => old_binding,
|
2016-02-07 17:58:14 -06:00
|
|
|
_ => { self.binding = Some(binding); return Ok(()); }
|
|
|
|
};
|
|
|
|
|
|
|
|
// FIXME #31337: We currently allow items to shadow glob-imported re-exports.
|
|
|
|
if !old_binding.is_import() && binding.defined_with(DefModifiers::GLOB_IMPORTED) {
|
|
|
|
if let NameBindingKind::Import { binding, .. } = binding.kind {
|
|
|
|
if binding.is_import() { return Ok(()); }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Err(old_binding)
|
2015-03-15 16:44:19 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-31 21:26:16 -06:00
|
|
|
struct ImportResolvingError<'a> {
|
|
|
|
/// Module where the error happened
|
|
|
|
source_module: Module<'a>,
|
|
|
|
import_directive: ImportDirective,
|
2015-08-04 01:14:32 -05:00
|
|
|
span: Span,
|
|
|
|
help: String,
|
|
|
|
}
|
2015-03-15 16:44:19 -05:00
|
|
|
|
2015-10-26 14:31:11 -05:00
|
|
|
struct ImportResolver<'a, 'b: 'a, 'tcx: 'b> {
|
|
|
|
resolver: &'a mut Resolver<'b, 'tcx>,
|
2015-03-15 16:44:19 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a, 'b:'a, 'tcx:'b> ImportResolver<'a, 'b, 'tcx> {
|
|
|
|
// Import resolution
|
|
|
|
//
|
|
|
|
// This is a fixed-point algorithm. We resolve imports until our efforts
|
|
|
|
// are stymied by an unresolved import; then we bail out of the current
|
|
|
|
// module and continue. We terminate successfully once no more imports
|
|
|
|
// remain or unsuccessfully when no forward progress in resolving imports
|
|
|
|
// is made.
|
|
|
|
|
|
|
|
/// Resolves all imports for the crate. This method performs the fixed-
|
|
|
|
/// point iteration.
|
|
|
|
fn resolve_imports(&mut self) {
|
|
|
|
let mut i = 0;
|
|
|
|
let mut prev_unresolved_imports = 0;
|
|
|
|
loop {
|
|
|
|
debug!("(resolving imports) iteration {}, {} imports left",
|
2015-10-26 14:31:11 -05:00
|
|
|
i,
|
|
|
|
self.resolver.unresolved_imports);
|
2015-03-15 16:44:19 -05:00
|
|
|
|
2016-01-11 15:19:29 -06:00
|
|
|
let module_root = self.resolver.graph_root;
|
|
|
|
let errors = self.resolve_imports_for_module_subtree(module_root);
|
2015-03-15 16:44:19 -05:00
|
|
|
|
|
|
|
if self.resolver.unresolved_imports == 0 {
|
|
|
|
debug!("(resolving imports) success");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if self.resolver.unresolved_imports == prev_unresolved_imports {
|
2015-07-31 11:58:59 -05:00
|
|
|
// resolving failed
|
|
|
|
if errors.len() > 0 {
|
2015-08-04 01:14:32 -05:00
|
|
|
for e in errors {
|
2016-01-31 21:26:16 -06:00
|
|
|
self.import_resolving_error(e)
|
2015-07-31 11:58:59 -05:00
|
|
|
}
|
|
|
|
} else {
|
2015-08-04 01:14:32 -05:00
|
|
|
// Report unresolved imports only if no hard error was already reported
|
|
|
|
// to avoid generating multiple errors on the same import.
|
|
|
|
// Imports that are still indeterminate at this point are actually blocked
|
|
|
|
// by errored imports, so there is no point reporting them.
|
2015-07-31 11:58:59 -05:00
|
|
|
self.resolver.report_unresolved_imports(module_root);
|
|
|
|
}
|
2015-03-15 16:44:19 -05:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
i += 1;
|
|
|
|
prev_unresolved_imports = self.resolver.unresolved_imports;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-31 21:26:16 -06:00
|
|
|
/// Resolves an `ImportResolvingError` into the correct enum discriminant
|
|
|
|
/// and passes that on to `resolve_error`.
|
2016-02-07 15:23:58 -06:00
|
|
|
fn import_resolving_error(&self, e: ImportResolvingError<'b>) {
|
2016-01-31 21:26:16 -06:00
|
|
|
// If it's a single failed import then create a "fake" import
|
|
|
|
// resolution for it so that later resolve stages won't complain.
|
|
|
|
if let SingleImport(target, _) = e.import_directive.subclass {
|
2016-02-07 17:58:14 -06:00
|
|
|
let dummy_binding = self.resolver.new_name_binding(NameBinding {
|
|
|
|
modifiers: DefModifiers::PRELUDE,
|
|
|
|
kind: NameBindingKind::Def(Def::Err),
|
|
|
|
span: None,
|
2016-01-31 21:26:16 -06:00
|
|
|
});
|
|
|
|
|
2016-02-07 17:58:14 -06:00
|
|
|
let _ = e.source_module.try_define_child(target, ValueNS, dummy_binding);
|
|
|
|
let _ = e.source_module.try_define_child(target, TypeNS, dummy_binding);
|
2016-01-31 21:26:16 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
let path = import_path_to_string(&e.import_directive.module_path,
|
|
|
|
e.import_directive.subclass);
|
|
|
|
|
|
|
|
resolve_error(self.resolver,
|
|
|
|
e.span,
|
|
|
|
ResolutionError::UnresolvedImport(Some((&path, &e.help))));
|
|
|
|
}
|
|
|
|
|
2015-03-15 16:44:19 -05:00
|
|
|
/// Attempts to resolve imports for the given module and all of its
|
|
|
|
/// submodules.
|
2015-10-26 14:31:11 -05:00
|
|
|
fn resolve_imports_for_module_subtree(&mut self,
|
2016-01-11 15:19:29 -06:00
|
|
|
module_: Module<'b>)
|
2016-01-31 21:26:16 -06:00
|
|
|
-> Vec<ImportResolvingError<'b>> {
|
2015-07-31 11:58:59 -05:00
|
|
|
let mut errors = Vec::new();
|
2015-03-15 16:44:19 -05:00
|
|
|
debug!("(resolving imports for module subtree) resolving {}",
|
2016-02-09 14:27:42 -06:00
|
|
|
module_to_string(&module_));
|
2016-01-11 15:19:29 -06:00
|
|
|
let orig_module = replace(&mut self.resolver.current_module, module_);
|
|
|
|
errors.extend(self.resolve_imports_for_module(module_));
|
2015-03-15 16:44:19 -05:00
|
|
|
self.resolver.current_module = orig_module;
|
|
|
|
|
2016-02-13 17:39:51 -06:00
|
|
|
for (_, child_module) in module_.module_children.borrow().iter() {
|
2016-01-11 15:19:29 -06:00
|
|
|
errors.extend(self.resolve_imports_for_module_subtree(child_module));
|
2015-03-15 16:44:19 -05:00
|
|
|
}
|
2015-07-31 11:58:59 -05:00
|
|
|
|
|
|
|
errors
|
2015-03-15 16:44:19 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Attempts to resolve imports for the given module only.
|
2016-01-31 21:26:16 -06:00
|
|
|
fn resolve_imports_for_module(&mut self, module: Module<'b>) -> Vec<ImportResolvingError<'b>> {
|
2015-07-31 11:58:59 -05:00
|
|
|
let mut errors = Vec::new();
|
|
|
|
|
2015-03-15 16:44:19 -05:00
|
|
|
if module.all_imports_resolved() {
|
2015-10-26 14:31:11 -05:00
|
|
|
debug!("(resolving imports for module) all imports resolved for {}",
|
2016-02-09 14:27:42 -06:00
|
|
|
module_to_string(&module));
|
2015-07-31 11:58:59 -05:00
|
|
|
return errors;
|
2015-03-15 16:44:19 -05:00
|
|
|
}
|
|
|
|
|
2015-07-31 11:58:59 -05:00
|
|
|
let mut imports = module.imports.borrow_mut();
|
2015-03-15 16:44:19 -05:00
|
|
|
let import_count = imports.len();
|
2015-07-31 11:58:59 -05:00
|
|
|
let mut indeterminate_imports = Vec::new();
|
|
|
|
while module.resolved_import_count.get() + indeterminate_imports.len() < import_count {
|
2015-03-15 16:44:19 -05:00
|
|
|
let import_index = module.resolved_import_count.get();
|
2016-01-11 15:19:29 -06:00
|
|
|
match self.resolve_import_for_module(module, &imports[import_index]) {
|
2015-03-15 16:44:19 -05:00
|
|
|
ResolveResult::Failed(err) => {
|
2015-07-31 11:58:59 -05:00
|
|
|
let import_directive = &imports[import_index];
|
2015-03-15 16:44:19 -05:00
|
|
|
let (span, help) = match err {
|
|
|
|
Some((span, msg)) => (span, format!(". {}", msg)),
|
2015-10-26 14:31:11 -05:00
|
|
|
None => (import_directive.span, String::new()),
|
2015-03-15 16:44:19 -05:00
|
|
|
};
|
2015-08-04 01:14:32 -05:00
|
|
|
errors.push(ImportResolvingError {
|
2016-01-31 21:26:16 -06:00
|
|
|
source_module: module,
|
|
|
|
import_directive: import_directive.clone(),
|
2015-10-26 14:31:11 -05:00
|
|
|
span: span,
|
|
|
|
help: help,
|
|
|
|
});
|
2015-07-31 11:58:59 -05:00
|
|
|
}
|
|
|
|
ResolveResult::Indeterminate => {}
|
|
|
|
ResolveResult::Success(()) => {
|
|
|
|
// count success
|
|
|
|
module.resolved_import_count
|
|
|
|
.set(module.resolved_import_count.get() + 1);
|
|
|
|
continue;
|
2015-03-15 16:44:19 -05:00
|
|
|
}
|
|
|
|
}
|
2015-07-31 11:58:59 -05:00
|
|
|
// This resolution was not successful, keep it for later
|
|
|
|
indeterminate_imports.push(imports.swap_remove(import_index));
|
2015-03-15 16:44:19 -05:00
|
|
|
|
|
|
|
}
|
2015-07-31 11:58:59 -05:00
|
|
|
|
|
|
|
imports.extend(indeterminate_imports);
|
|
|
|
|
|
|
|
errors
|
2015-03-15 16:44:19 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Attempts to resolve the given import. The return value indicates
|
|
|
|
/// failure if we're certain the name does not exist, indeterminate if we
|
|
|
|
/// don't know whether the name exists at the moment due to other
|
|
|
|
/// currently-unresolved imports, or success if we know the name exists.
|
|
|
|
/// If successful, the resolved bindings are written into the module.
|
|
|
|
fn resolve_import_for_module(&mut self,
|
2016-01-11 15:19:29 -06:00
|
|
|
module_: Module<'b>,
|
2015-03-15 16:44:19 -05:00
|
|
|
import_directive: &ImportDirective)
|
|
|
|
-> ResolveResult<()> {
|
|
|
|
debug!("(resolving import for module) resolving import `{}::...` in `{}`",
|
2016-01-16 05:41:19 -06:00
|
|
|
names_to_string(&import_directive.module_path),
|
2016-02-09 14:27:42 -06:00
|
|
|
module_to_string(&module_));
|
2015-03-15 16:44:19 -05:00
|
|
|
|
2016-01-16 05:41:19 -06:00
|
|
|
self.resolver
|
|
|
|
.resolve_module_path(module_,
|
|
|
|
&import_directive.module_path,
|
|
|
|
UseLexicalScopeFlag::DontUseLexicalScope,
|
|
|
|
import_directive.span)
|
|
|
|
.and_then(|(containing_module, lp)| {
|
2015-03-15 16:44:19 -05:00
|
|
|
// We found the module that the target is contained
|
|
|
|
// within. Attempt to resolve the import within it.
|
2016-01-16 05:41:19 -06:00
|
|
|
if let SingleImport(target, source) = import_directive.subclass {
|
2016-01-29 17:34:58 -06:00
|
|
|
self.resolve_single_import(module_,
|
2016-01-16 05:41:19 -06:00
|
|
|
containing_module,
|
|
|
|
target,
|
|
|
|
source,
|
|
|
|
import_directive,
|
|
|
|
lp)
|
|
|
|
} else {
|
2016-01-29 17:34:58 -06:00
|
|
|
self.resolve_glob_import(module_, containing_module, import_directive, lp)
|
2015-03-15 16:44:19 -05:00
|
|
|
}
|
2016-01-16 05:41:19 -06:00
|
|
|
})
|
|
|
|
.and_then(|()| {
|
|
|
|
// Decrement the count of unresolved imports.
|
2015-03-15 16:44:19 -05:00
|
|
|
assert!(self.resolver.unresolved_imports >= 1);
|
|
|
|
self.resolver.unresolved_imports -= 1;
|
|
|
|
|
2016-01-16 05:41:19 -06:00
|
|
|
if let GlobImport = import_directive.subclass {
|
2015-08-06 05:47:10 -05:00
|
|
|
module_.dec_glob_count();
|
2015-08-05 14:47:01 -05:00
|
|
|
if import_directive.is_public {
|
2015-08-06 05:47:10 -05:00
|
|
|
module_.dec_pub_glob_count();
|
2015-08-05 14:47:01 -05:00
|
|
|
}
|
2015-03-15 16:44:19 -05:00
|
|
|
}
|
2016-01-16 05:41:19 -06:00
|
|
|
if import_directive.is_public {
|
|
|
|
module_.dec_pub_count();
|
2015-03-15 16:44:19 -05:00
|
|
|
}
|
2016-01-16 05:41:19 -06:00
|
|
|
Success(())
|
|
|
|
})
|
2015-03-15 16:44:19 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
fn resolve_single_import(&mut self,
|
2016-01-11 15:19:29 -06:00
|
|
|
module_: Module<'b>,
|
|
|
|
target_module: Module<'b>,
|
2015-03-15 16:44:19 -05:00
|
|
|
target: Name,
|
|
|
|
source: Name,
|
|
|
|
directive: &ImportDirective,
|
|
|
|
lp: LastPrivate)
|
|
|
|
-> ResolveResult<()> {
|
2015-10-26 14:31:11 -05:00
|
|
|
debug!("(resolving single import) resolving `{}` = `{}::{}` from `{}` id {}, last \
|
|
|
|
private {:?}",
|
2015-07-08 15:55:22 -05:00
|
|
|
target,
|
2016-02-09 14:27:42 -06:00
|
|
|
module_to_string(&target_module),
|
2015-07-08 15:55:22 -05:00
|
|
|
source,
|
2015-03-15 16:44:19 -05:00
|
|
|
module_to_string(module_),
|
|
|
|
directive.id,
|
|
|
|
lp);
|
|
|
|
|
|
|
|
let lp = match lp {
|
|
|
|
LastMod(lp) => lp,
|
|
|
|
LastImport {..} => {
|
2015-10-26 14:31:11 -05:00
|
|
|
self.resolver
|
|
|
|
.session
|
|
|
|
.span_bug(directive.span, "not expecting Import here, must be LastMod")
|
2015-03-15 16:44:19 -05:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2016-02-07 17:58:14 -06:00
|
|
|
// If this is a circular import, we temporarily count it as determined so that
|
|
|
|
// it fails (as opposed to being indeterminate) when nothing else can define it.
|
|
|
|
if target_module.def_id() == module_.def_id() && source == target {
|
|
|
|
module_.decrement_outstanding_references_for(target, ValueNS);
|
|
|
|
module_.decrement_outstanding_references_for(target, TypeNS);
|
|
|
|
}
|
|
|
|
|
2015-03-15 16:44:19 -05:00
|
|
|
// We need to resolve both namespaces for this to succeed.
|
2016-02-07 15:48:24 -06:00
|
|
|
let value_result =
|
2016-02-07 17:58:14 -06:00
|
|
|
self.resolver.resolve_name_in_module(target_module, source, ValueNS, false, true);
|
2016-02-07 15:48:24 -06:00
|
|
|
let type_result =
|
2016-02-07 17:58:14 -06:00
|
|
|
self.resolver.resolve_name_in_module(target_module, source, TypeNS, false, true);
|
|
|
|
|
|
|
|
if target_module.def_id() == module_.def_id() && source == target {
|
|
|
|
module_.increment_outstanding_references_for(target, ValueNS);
|
|
|
|
module_.increment_outstanding_references_for(target, TypeNS);
|
|
|
|
}
|
2015-03-15 16:44:19 -05:00
|
|
|
|
2016-02-11 00:17:01 -06:00
|
|
|
match (&value_result, &type_result) {
|
|
|
|
(&Indeterminate, _) | (_, &Indeterminate) => return Indeterminate,
|
|
|
|
(&Failed(_), &Failed(_)) => {
|
2016-02-14 02:46:54 -06:00
|
|
|
let children = target_module.resolutions.borrow();
|
2016-02-11 00:17:01 -06:00
|
|
|
let names = children.keys().map(|&(ref name, _)| name);
|
|
|
|
let lev_suggestion = match find_best_match_for_name(names, &source.as_str(), None) {
|
|
|
|
Some(name) => format!(". Did you mean to use `{}`?", name),
|
|
|
|
None => "".to_owned(),
|
|
|
|
};
|
|
|
|
let msg = format!("There is no `{}` in `{}`{}",
|
|
|
|
source,
|
|
|
|
module_to_string(target_module), lev_suggestion);
|
|
|
|
return Failed(Some((directive.span, msg)));
|
|
|
|
}
|
|
|
|
_ => (),
|
|
|
|
}
|
|
|
|
|
2016-01-21 21:00:29 -06:00
|
|
|
match (&value_result, &type_result) {
|
2016-02-07 15:48:24 -06:00
|
|
|
(&Success(name_binding), _) if !name_binding.is_import() &&
|
2016-02-07 15:23:58 -06:00
|
|
|
directive.is_public &&
|
|
|
|
!name_binding.is_public() => {
|
2016-01-13 19:42:45 -06:00
|
|
|
let msg = format!("`{}` is private, and cannot be reexported", source);
|
|
|
|
let note_msg = format!("Consider marking `{}` as `pub` in the imported module",
|
|
|
|
source);
|
|
|
|
struct_span_err!(self.resolver.session, directive.span, E0364, "{}", &msg)
|
|
|
|
.span_note(directive.span, ¬e_msg)
|
|
|
|
.emit();
|
2015-03-15 16:44:19 -05:00
|
|
|
}
|
|
|
|
|
2016-02-07 15:48:24 -06:00
|
|
|
(_, &Success(name_binding)) if !name_binding.is_import() && directive.is_public => {
|
2016-01-13 19:42:45 -06:00
|
|
|
if !name_binding.is_public() {
|
|
|
|
let msg = format!("`{}` is private, and cannot be reexported", source);
|
|
|
|
let note_msg =
|
|
|
|
format!("Consider declaring type or module `{}` with `pub`", source);
|
|
|
|
struct_span_err!(self.resolver.session, directive.span, E0365, "{}", &msg)
|
|
|
|
.span_note(directive.span, ¬e_msg)
|
|
|
|
.emit();
|
|
|
|
} else if name_binding.defined_with(DefModifiers::PRIVATE_VARIANT) {
|
|
|
|
let msg = format!("variant `{}` is private, and cannot be reexported \
|
|
|
|
(error E0364), consider declaring its enum as `pub`",
|
|
|
|
source);
|
|
|
|
self.resolver.session.add_lint(lint::builtin::PRIVATE_IN_PUBLIC,
|
|
|
|
directive.id,
|
|
|
|
directive.span,
|
|
|
|
msg);
|
2015-03-15 16:44:19 -05:00
|
|
|
}
|
2016-01-13 19:42:45 -06:00
|
|
|
}
|
2015-12-14 11:06:31 -06:00
|
|
|
|
2016-01-21 21:00:29 -06:00
|
|
|
_ => {}
|
2016-01-13 19:42:45 -06:00
|
|
|
}
|
2015-03-15 16:44:19 -05:00
|
|
|
|
2016-02-07 17:58:14 -06:00
|
|
|
for &(ns, result) in &[(ValueNS, &value_result), (TypeNS, &type_result)] {
|
|
|
|
if let Success(binding) = *result {
|
|
|
|
if !binding.defined_with(DefModifiers::IMPORTABLE) {
|
|
|
|
let msg = format!("`{}` is not directly importable", target);
|
|
|
|
span_err!(self.resolver.session, directive.span, E0253, "{}", &msg);
|
2015-03-15 16:44:19 -05:00
|
|
|
}
|
2016-01-13 19:42:45 -06:00
|
|
|
|
2016-02-07 17:58:14 -06:00
|
|
|
self.define(module_, target, ns, directive.import(binding));
|
|
|
|
}
|
2015-03-15 16:44:19 -05:00
|
|
|
}
|
2016-01-21 21:00:29 -06:00
|
|
|
|
2016-02-07 17:06:10 -06:00
|
|
|
// Record what this import resolves to for later uses in documentation,
|
|
|
|
// this may resolve to either a value or a type, but for documentation
|
|
|
|
// purposes it's good enough to just favor one over the other.
|
|
|
|
module_.decrement_outstanding_references_for(target, ValueNS);
|
|
|
|
module_.decrement_outstanding_references_for(target, TypeNS);
|
|
|
|
|
|
|
|
let def_and_priv = |binding: &NameBinding| {
|
|
|
|
let def = binding.def().unwrap();
|
|
|
|
let last_private = if binding.is_public() { lp } else { DependsOn(def.def_id()) };
|
|
|
|
(def, last_private)
|
2016-01-13 19:42:45 -06:00
|
|
|
};
|
2016-02-07 17:06:10 -06:00
|
|
|
let value_def_and_priv = value_result.success().map(&def_and_priv);
|
|
|
|
let type_def_and_priv = type_result.success().map(&def_and_priv);
|
2015-03-15 16:44:19 -05:00
|
|
|
|
|
|
|
let import_lp = LastImport {
|
|
|
|
value_priv: value_def_and_priv.map(|(_, p)| p),
|
|
|
|
value_used: Used,
|
|
|
|
type_priv: type_def_and_priv.map(|(_, p)| p),
|
2015-10-26 14:31:11 -05:00
|
|
|
type_used: Used,
|
2015-03-15 16:44:19 -05:00
|
|
|
};
|
|
|
|
|
2016-02-07 17:06:10 -06:00
|
|
|
let write_path_resolution = |(def, _)| {
|
|
|
|
let path_resolution =
|
|
|
|
PathResolution { base_def: def, last_private: import_lp, depth: 0 };
|
|
|
|
self.resolver.def_map.borrow_mut().insert(directive.id, path_resolution);
|
|
|
|
};
|
|
|
|
value_def_and_priv.map(&write_path_resolution);
|
|
|
|
type_def_and_priv.map(&write_path_resolution);
|
2015-03-15 16:44:19 -05:00
|
|
|
|
|
|
|
debug!("(resolving single import) successfully resolved import");
|
2016-01-21 21:00:29 -06:00
|
|
|
return Success(());
|
2015-03-15 16:44:19 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Resolves a glob import. Note that this function cannot fail; it either
|
|
|
|
// succeeds or bails out (as importing * from an empty module or a module
|
|
|
|
// that exports nothing is valid). target_module is the module we are
|
|
|
|
// actually importing, i.e., `foo` in `use foo::*`.
|
|
|
|
fn resolve_glob_import(&mut self,
|
2016-01-11 15:19:29 -06:00
|
|
|
module_: Module<'b>,
|
|
|
|
target_module: Module<'b>,
|
2016-02-07 17:58:14 -06:00
|
|
|
directive: &ImportDirective,
|
2015-03-15 16:44:19 -05:00
|
|
|
lp: LastPrivate)
|
|
|
|
-> ResolveResult<()> {
|
2016-02-07 17:58:14 -06:00
|
|
|
// We must bail out if the node has unresolved imports of any kind (including globs).
|
|
|
|
if target_module.pub_count.get() > 0 {
|
2015-10-26 14:31:11 -05:00
|
|
|
debug!("(resolving glob import) target module has unresolved pub imports; bailing out");
|
2016-02-07 17:58:14 -06:00
|
|
|
return Indeterminate;
|
2015-08-05 14:47:01 -05:00
|
|
|
}
|
|
|
|
|
2016-02-07 17:58:14 -06:00
|
|
|
if module_.def_id() == target_module.def_id() {
|
|
|
|
// This means we are trying to glob import a module into itself, and it is a no-go
|
|
|
|
let msg = "Cannot glob-import a module into itself.".into();
|
|
|
|
return Failed(Some((directive.span, msg)));
|
2015-03-15 16:44:19 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Add all children from the containing module.
|
2016-01-29 17:34:58 -06:00
|
|
|
build_reduced_graph::populate_module_if_necessary(self.resolver, target_module);
|
2016-02-07 17:58:14 -06:00
|
|
|
target_module.for_each_child(|name, ns, binding| {
|
|
|
|
if !binding.defined_with(DefModifiers::IMPORTABLE | DefModifiers::PUBLIC) { return }
|
2016-02-11 01:18:41 -06:00
|
|
|
if binding.is_extern_crate() { return }
|
2016-02-07 17:58:14 -06:00
|
|
|
self.define(module_, name, ns, directive.import(binding));
|
|
|
|
|
|
|
|
if ns == TypeNS && directive.is_public &&
|
|
|
|
binding.defined_with(DefModifiers::PRIVATE_VARIANT) {
|
|
|
|
let msg = format!("variant `{}` is private, and cannot be reexported (error \
|
|
|
|
E0364), consider declaring its enum as `pub`", name);
|
|
|
|
self.resolver.session.add_lint(lint::builtin::PRIVATE_IN_PUBLIC,
|
|
|
|
directive.id,
|
|
|
|
directive.span,
|
|
|
|
msg);
|
|
|
|
}
|
2016-01-29 16:21:36 -06:00
|
|
|
});
|
2015-03-15 16:44:19 -05:00
|
|
|
|
|
|
|
// Record the destination of this import
|
2015-11-16 01:59:50 -06:00
|
|
|
if let Some(did) = target_module.def_id() {
|
2016-02-07 17:58:14 -06:00
|
|
|
self.resolver.def_map.borrow_mut().insert(directive.id,
|
2015-10-26 14:31:11 -05:00
|
|
|
PathResolution {
|
2016-01-20 13:31:10 -06:00
|
|
|
base_def: Def::Mod(did),
|
2015-10-26 14:31:11 -05:00
|
|
|
last_private: lp,
|
|
|
|
depth: 0,
|
|
|
|
});
|
2015-03-15 16:44:19 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
debug!("(resolving glob import) successfully resolved import");
|
2016-02-07 17:58:14 -06:00
|
|
|
return Success(());
|
2015-03-15 16:44:19 -05:00
|
|
|
}
|
|
|
|
|
2016-02-07 17:58:14 -06:00
|
|
|
fn define(&mut self,
|
|
|
|
parent: Module<'b>,
|
|
|
|
name: Name,
|
|
|
|
ns: Namespace,
|
|
|
|
binding: NameBinding<'b>) {
|
|
|
|
let binding = self.resolver.new_name_binding(binding);
|
|
|
|
if let Err(old_binding) = parent.try_define_child(name, ns, binding) {
|
|
|
|
self.report_conflict(name, ns, binding, old_binding);
|
2016-02-07 17:06:10 -06:00
|
|
|
} else if binding.is_public() { // Add to the export map
|
|
|
|
if let (Some(parent_def_id), Some(def)) = (parent.def_id(), binding.def()) {
|
|
|
|
let parent_node_id = self.resolver.ast_map.as_local_node_id(parent_def_id).unwrap();
|
|
|
|
let export = Export { name: name, def_id: def.def_id() };
|
|
|
|
self.resolver.export_map.entry(parent_node_id).or_insert(Vec::new()).push(export);
|
|
|
|
}
|
2015-03-15 16:44:19 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-07 17:58:14 -06:00
|
|
|
fn report_conflict(&mut self,
|
|
|
|
name: Name,
|
|
|
|
ns: Namespace,
|
|
|
|
binding: &'b NameBinding<'b>,
|
|
|
|
old_binding: &'b NameBinding<'b>) {
|
|
|
|
if old_binding.is_extern_crate() {
|
|
|
|
let msg = format!("import `{0}` conflicts with imported crate \
|
|
|
|
in this module (maybe you meant `use {0}::*`?)",
|
|
|
|
name);
|
|
|
|
span_err!(self.resolver.session, binding.span.unwrap(), E0254, "{}", &msg);
|
|
|
|
} else if old_binding.is_import() {
|
|
|
|
let ns_word = match (ns, old_binding.module()) {
|
|
|
|
(ValueNS, _) => "value",
|
|
|
|
(TypeNS, Some(module)) if module.is_normal() => "module",
|
|
|
|
(TypeNS, Some(module)) if module.is_trait() => "trait",
|
|
|
|
(TypeNS, _) => "type",
|
|
|
|
};
|
|
|
|
let mut err = struct_span_err!(self.resolver.session,
|
|
|
|
binding.span.unwrap(),
|
|
|
|
E0252,
|
|
|
|
"a {} named `{}` has already been imported \
|
|
|
|
in this module",
|
|
|
|
ns_word,
|
|
|
|
name);
|
|
|
|
err.span_note(old_binding.span.unwrap(),
|
|
|
|
&format!("previous import of `{}` here", name));
|
|
|
|
err.emit();
|
|
|
|
} else if ns == ValueNS { // Check for item conflicts in the value namespace
|
|
|
|
let mut err = struct_span_err!(self.resolver.session,
|
|
|
|
binding.span.unwrap(),
|
|
|
|
E0255,
|
|
|
|
"import `{}` conflicts with value in this module",
|
|
|
|
name);
|
|
|
|
err.span_note(old_binding.span.unwrap(), "conflicting value here");
|
|
|
|
err.emit();
|
|
|
|
} else { // Check for item conflicts in the type namespace
|
|
|
|
let (what, note) = match old_binding.module() {
|
|
|
|
Some(ref module) if module.is_normal() =>
|
|
|
|
("existing submodule", "note conflicting module here"),
|
|
|
|
Some(ref module) if module.is_trait() =>
|
|
|
|
("trait in this module", "note conflicting trait here"),
|
|
|
|
_ => ("type in this module", "note conflicting type here"),
|
|
|
|
};
|
|
|
|
let mut err = struct_span_err!(self.resolver.session,
|
|
|
|
binding.span.unwrap(),
|
|
|
|
E0256,
|
|
|
|
"import `{}` conflicts with {}",
|
|
|
|
name,
|
|
|
|
what);
|
|
|
|
err.span_note(old_binding.span.unwrap(), note);
|
|
|
|
err.emit();
|
2015-03-15 16:44:19 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-26 14:31:11 -05:00
|
|
|
fn import_path_to_string(names: &[Name], subclass: ImportDirectiveSubclass) -> String {
|
2015-03-15 16:44:19 -05:00
|
|
|
if names.is_empty() {
|
|
|
|
import_directive_subclass_to_string(subclass)
|
|
|
|
} else {
|
|
|
|
(format!("{}::{}",
|
|
|
|
names_to_string(names),
|
2015-10-26 14:31:11 -05:00
|
|
|
import_directive_subclass_to_string(subclass)))
|
|
|
|
.to_string()
|
2015-03-15 16:44:19 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn import_directive_subclass_to_string(subclass: ImportDirectiveSubclass) -> String {
|
|
|
|
match subclass {
|
2015-07-28 11:07:20 -05:00
|
|
|
SingleImport(_, source) => source.to_string(),
|
2015-10-26 14:31:11 -05:00
|
|
|
GlobImport => "*".to_string(),
|
2015-03-15 16:44:19 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn resolve_imports(resolver: &mut Resolver) {
|
2015-10-26 14:31:11 -05:00
|
|
|
let mut import_resolver = ImportResolver { resolver: resolver };
|
2015-03-15 16:44:19 -05:00
|
|
|
import_resolver.resolve_imports();
|
|
|
|
}
|