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-24 22:40:46 -06:00
|
|
|
use {NameBinding, NameBindingKind, PrivacyError};
|
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;
|
2016-03-12 20:53:22 -06:00
|
|
|
use UseLexicalScopeFlag::DontUseLexicalScope;
|
2015-03-15 16:44:19 -05:00
|
|
|
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
|
|
|
|
2015-12-13 12:57:07 -06:00
|
|
|
use rustc::lint;
|
2015-03-15 16:44:19 -05:00
|
|
|
use rustc::middle::def::*;
|
|
|
|
|
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;
|
2016-02-13 15:49:16 -06:00
|
|
|
use std::cell::Cell;
|
2015-03-15 16:44:19 -05:00
|
|
|
|
|
|
|
/// Contains data for specific types of import directives.
|
2016-02-13 15:49:16 -06:00
|
|
|
#[derive(Clone, Debug)]
|
2015-03-15 16:44:19 -05:00
|
|
|
pub enum ImportDirectiveSubclass {
|
2016-02-13 15:49:16 -06:00
|
|
|
SingleImport {
|
|
|
|
target: Name,
|
|
|
|
source: Name,
|
|
|
|
type_determined: Cell<bool>,
|
|
|
|
value_determined: Cell<bool>,
|
|
|
|
},
|
2015-10-26 14:31:11 -05:00
|
|
|
GlobImport,
|
2015-03-15 16:44:19 -05:00
|
|
|
}
|
|
|
|
|
2016-02-13 15:49:16 -06:00
|
|
|
impl ImportDirectiveSubclass {
|
|
|
|
pub fn single(target: Name, source: Name) -> Self {
|
|
|
|
SingleImport {
|
|
|
|
target: target,
|
|
|
|
source: source,
|
|
|
|
type_determined: Cell::new(false),
|
|
|
|
value_determined: Cell::new(false),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-15 16:44:19 -05:00
|
|
|
/// One import directive.
|
2016-01-31 21:26:16 -06:00
|
|
|
#[derive(Debug,Clone)]
|
2016-03-16 20:13:31 -05:00
|
|
|
pub struct ImportDirective<'a> {
|
2016-03-16 20:05:29 -05:00
|
|
|
module_path: Vec<Name>,
|
2016-03-16 20:16:33 -05:00
|
|
|
target_module: Cell<Option<Module<'a>>>, // the resolution of `module_path`
|
2016-03-16 20:05:29 -05:00
|
|
|
subclass: ImportDirectiveSubclass,
|
|
|
|
span: Span,
|
|
|
|
id: NodeId,
|
|
|
|
is_public: bool, // see note in ImportResolutionPerNamespace about how to use this
|
|
|
|
is_prelude: bool,
|
2015-03-15 16:44:19 -05:00
|
|
|
}
|
|
|
|
|
2016-03-16 20:13:31 -05:00
|
|
|
impl<'a> ImportDirective<'a> {
|
2015-10-26 14:31:11 -05:00
|
|
|
pub fn new(module_path: Vec<Name>,
|
|
|
|
subclass: ImportDirectiveSubclass,
|
|
|
|
span: Span,
|
|
|
|
id: NodeId,
|
|
|
|
is_public: bool,
|
2016-02-29 19:43:10 -06:00
|
|
|
is_prelude: bool)
|
2016-03-16 20:13:31 -05:00
|
|
|
-> Self {
|
2015-03-15 16:44:19 -05:00
|
|
|
ImportDirective {
|
|
|
|
module_path: module_path,
|
2016-03-16 20:16:33 -05:00
|
|
|
target_module: Cell::new(None),
|
2015-03-15 16:44:19 -05:00
|
|
|
subclass: subclass,
|
|
|
|
span: span,
|
|
|
|
id: id,
|
|
|
|
is_public: is_public,
|
2016-02-29 19:43:10 -06:00
|
|
|
is_prelude: is_prelude,
|
2015-03-15 16:44:19 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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-03-16 20:13:31 -05:00
|
|
|
fn import(&self, binding: &'a NameBinding<'a>, privacy_error: Option<Box<PrivacyError<'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;
|
|
|
|
}
|
|
|
|
|
|
|
|
NameBinding {
|
2016-02-24 22:40:46 -06:00
|
|
|
kind: NameBindingKind::Import {
|
|
|
|
binding: binding,
|
|
|
|
id: self.id,
|
|
|
|
privacy_error: privacy_error,
|
|
|
|
},
|
2016-02-07 15:34:23 -06:00
|
|
|
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-03-07 16:43:56 -06:00
|
|
|
/// The number of unresolved single imports of any visibility that could define the name.
|
2016-03-07 03:59:08 -06:00
|
|
|
outstanding_references: u32,
|
2016-03-07 16:43:56 -06:00
|
|
|
/// The number of unresolved `pub` single imports that could define the name.
|
2016-03-07 03:59:08 -06:00
|
|
|
pub_outstanding_references: u32,
|
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>>,
|
2016-02-15 21:54:14 -06:00
|
|
|
duplicate_globs: Vec<&'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-15 21:54:14 -06:00
|
|
|
fn try_define(&mut self, binding: &'a NameBinding<'a>) -> Result<(), &'a NameBinding<'a>> {
|
2016-03-08 19:55:21 -06:00
|
|
|
if let Some(old_binding) = self.binding {
|
|
|
|
if binding.defined_with(DefModifiers::GLOB_IMPORTED) {
|
|
|
|
self.duplicate_globs.push(binding);
|
|
|
|
} else if old_binding.defined_with(DefModifiers::GLOB_IMPORTED) {
|
|
|
|
self.duplicate_globs.push(old_binding);
|
|
|
|
self.binding = Some(binding);
|
|
|
|
} else {
|
|
|
|
return Err(old_binding);
|
2016-02-07 17:58:14 -06:00
|
|
|
}
|
2016-03-08 19:55:21 -06:00
|
|
|
} else {
|
|
|
|
self.binding = Some(binding);
|
2015-03-15 16:44:19 -05:00
|
|
|
}
|
2016-02-07 17:58:14 -06:00
|
|
|
|
2016-02-15 21:54:14 -06:00
|
|
|
Ok(())
|
2016-02-07 17:58:14 -06:00
|
|
|
}
|
|
|
|
|
2016-02-15 21:54:14 -06:00
|
|
|
// Returns Some(the resolution of the name), or None if the resolution depends
|
|
|
|
// on whether more globs can define the name.
|
2016-03-07 03:59:08 -06:00
|
|
|
fn try_result(&self, allow_private_imports: bool)
|
|
|
|
-> Option<ResolveResult<&'a NameBinding<'a>>> {
|
2016-03-08 22:51:33 -06:00
|
|
|
match self.binding {
|
|
|
|
Some(binding) if !binding.defined_with(DefModifiers::GLOB_IMPORTED) =>
|
|
|
|
Some(Success(binding)),
|
|
|
|
// If (1) we don't allow private imports, (2) no public single import can define the
|
|
|
|
// name, and (3) no public glob has defined the name, the resolution depends on globs.
|
|
|
|
_ if !allow_private_imports && self.pub_outstanding_references == 0 &&
|
|
|
|
!self.binding.map(NameBinding::is_public).unwrap_or(false) => None,
|
|
|
|
_ if self.outstanding_references > 0 => Some(Indeterminate),
|
|
|
|
Some(binding) => Some(Success(binding)),
|
|
|
|
None => None,
|
2016-02-15 21:54:14 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-13 04:59:16 -05:00
|
|
|
fn increment_outstanding_references(&mut self, is_public: bool) {
|
|
|
|
self.outstanding_references += 1;
|
|
|
|
if is_public {
|
|
|
|
self.pub_outstanding_references += 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn decrement_outstanding_references(&mut self, is_public: bool) {
|
|
|
|
let decrement_references = |count: &mut _| {
|
|
|
|
assert!(*count > 0);
|
|
|
|
*count -= 1;
|
|
|
|
};
|
|
|
|
|
|
|
|
decrement_references(&mut self.outstanding_references);
|
|
|
|
if is_public {
|
|
|
|
decrement_references(&mut self.pub_outstanding_references);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-15 21:54:14 -06:00
|
|
|
fn report_conflicts<F: FnMut(&NameBinding, &NameBinding)>(&self, mut report: F) {
|
|
|
|
let binding = match self.binding {
|
|
|
|
Some(binding) => binding,
|
|
|
|
None => return,
|
2016-02-07 17:58:14 -06:00
|
|
|
};
|
|
|
|
|
2016-02-15 21:54:14 -06:00
|
|
|
for duplicate_glob in self.duplicate_globs.iter() {
|
|
|
|
// FIXME #31337: We currently allow items to shadow glob-imported re-exports.
|
|
|
|
if !binding.is_import() {
|
|
|
|
if let NameBindingKind::Import { binding, .. } = duplicate_glob.kind {
|
|
|
|
if binding.is_import() { continue }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
report(duplicate_glob, binding);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> ::ModuleS<'a> {
|
|
|
|
pub fn resolve_name(&self, name: Name, ns: Namespace, allow_private_imports: bool)
|
|
|
|
-> ResolveResult<&'a NameBinding<'a>> {
|
|
|
|
let resolutions = match self.resolutions.borrow_state() {
|
|
|
|
::std::cell::BorrowState::Unused => self.resolutions.borrow(),
|
|
|
|
_ => return Failed(None), // This happens when there is a cycle of glob imports
|
|
|
|
};
|
|
|
|
|
|
|
|
let resolution = resolutions.get(&(name, ns)).cloned().unwrap_or_default();
|
2016-03-07 03:59:08 -06:00
|
|
|
if let Some(result) = resolution.try_result(allow_private_imports) {
|
2016-02-15 21:54:14 -06:00
|
|
|
// If the resolution doesn't depend on glob definability, check privacy and return.
|
|
|
|
return result.and_then(|binding| {
|
|
|
|
let allowed = allow_private_imports || !binding.is_import() || binding.is_public();
|
|
|
|
if allowed { Success(binding) } else { Failed(None) }
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
let (ref mut public_globs, ref mut private_globs) = *self.resolved_globs.borrow_mut();
|
|
|
|
|
|
|
|
// Check if the public globs are determined
|
2016-02-16 05:48:38 -06:00
|
|
|
if public_globs.len() < self.public_glob_count.get() {
|
2016-02-15 21:54:14 -06:00
|
|
|
return Indeterminate;
|
|
|
|
}
|
|
|
|
for module in public_globs.iter() {
|
|
|
|
if let Indeterminate = module.resolve_name(name, ns, false) {
|
|
|
|
return Indeterminate;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if !allow_private_imports {
|
|
|
|
return Failed(None);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check if the private globs are determined
|
2016-02-16 05:48:38 -06:00
|
|
|
if private_globs.len() < self.private_glob_count.get() {
|
2016-02-15 21:54:14 -06:00
|
|
|
return Indeterminate;
|
|
|
|
}
|
|
|
|
for module in private_globs.iter() {
|
|
|
|
if let Indeterminate = module.resolve_name(name, ns, false) {
|
|
|
|
return Indeterminate;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-08 22:21:54 -06:00
|
|
|
Failed(None)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Invariant: this may not be called until import resolution is complete.
|
|
|
|
pub fn resolve_name_in_lexical_scope(&self, name: Name, ns: Namespace)
|
|
|
|
-> Option<&'a NameBinding<'a>> {
|
|
|
|
self.resolutions.borrow().get(&(name, ns)).and_then(|resolution| resolution.binding)
|
|
|
|
.or_else(|| self.prelude.borrow().and_then(|prelude| {
|
|
|
|
prelude.resolve_name(name, ns, false).success()
|
|
|
|
}))
|
2016-02-15 21:54:14 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// Define the name or return the existing binding if there is a collision.
|
|
|
|
pub fn try_define_child(&self, name: Name, ns: Namespace, binding: NameBinding<'a>)
|
|
|
|
-> Result<(), &'a NameBinding<'a>> {
|
|
|
|
if self.resolutions.borrow_state() != ::std::cell::BorrowState::Unused { return Ok(()); }
|
|
|
|
self.update_resolution(name, ns, |resolution| {
|
|
|
|
resolution.try_define(self.arenas.alloc_name_binding(binding))
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2016-03-07 03:59:08 -06:00
|
|
|
pub fn increment_outstanding_references_for(&self, name: Name, ns: Namespace, is_public: bool) {
|
2016-03-13 04:59:16 -05:00
|
|
|
self.resolutions.borrow_mut().entry((name, ns)).or_insert_with(Default::default)
|
|
|
|
.increment_outstanding_references(is_public);
|
2016-02-15 21:54:14 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// Use `update` to mutate the resolution for the name.
|
|
|
|
// If the resolution becomes a success, define it in the module's glob importers.
|
|
|
|
fn update_resolution<T, F>(&self, name: Name, ns: Namespace, update: F) -> T
|
|
|
|
where F: FnOnce(&mut NameResolution<'a>) -> T
|
|
|
|
{
|
|
|
|
let mut resolutions = self.resolutions.borrow_mut();
|
|
|
|
let resolution = resolutions.entry((name, ns)).or_insert_with(Default::default);
|
2016-03-07 03:59:08 -06:00
|
|
|
let was_success = resolution.try_result(false).and_then(ResolveResult::success).is_some();
|
2016-02-15 21:54:14 -06:00
|
|
|
|
|
|
|
let t = update(resolution);
|
|
|
|
if !was_success {
|
2016-03-07 03:59:08 -06:00
|
|
|
if let Some(Success(binding)) = resolution.try_result(false) {
|
2016-02-15 21:54:14 -06:00
|
|
|
self.define_in_glob_importers(name, ns, binding);
|
2016-02-07 17:58:14 -06:00
|
|
|
}
|
|
|
|
}
|
2016-02-15 21:54:14 -06:00
|
|
|
t
|
|
|
|
}
|
2016-02-07 17:58:14 -06:00
|
|
|
|
2016-02-15 21:54:14 -06:00
|
|
|
fn define_in_glob_importers(&self, name: Name, ns: Namespace, binding: &'a NameBinding<'a>) {
|
|
|
|
if !binding.defined_with(DefModifiers::PUBLIC | DefModifiers::IMPORTABLE) { return }
|
|
|
|
for &(importer, directive) in self.glob_importers.borrow_mut().iter() {
|
|
|
|
let _ = importer.try_define_child(name, ns, directive.import(binding, None));
|
|
|
|
}
|
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>,
|
2016-03-16 20:13:31 -05:00
|
|
|
import_directive: &'a ImportDirective<'a>,
|
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;
|
2016-02-17 03:29:17 -06:00
|
|
|
let mut errors = Vec::new();
|
|
|
|
|
2015-03-15 16:44:19 -05:00
|
|
|
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-02-17 03:29:17 -06:00
|
|
|
self.resolve_imports_for_module_subtree(self.resolver.graph_root, &mut errors);
|
2015-03-15 16:44:19 -05:00
|
|
|
|
|
|
|
if self.resolver.unresolved_imports == 0 {
|
|
|
|
debug!("(resolving imports) success");
|
2016-02-16 05:48:38 -06:00
|
|
|
self.finalize_resolutions(self.resolver.graph_root, false);
|
2015-03-15 16:44:19 -05:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if self.resolver.unresolved_imports == prev_unresolved_imports {
|
2015-07-31 11:58:59 -05:00
|
|
|
// resolving failed
|
2016-02-16 05:48:38 -06: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.
|
|
|
|
self.finalize_resolutions(self.resolver.graph_root, errors.len() == 0);
|
|
|
|
for e in errors {
|
|
|
|
self.import_resolving_error(e)
|
2015-07-31 11:58:59 -05:00
|
|
|
}
|
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.
|
2016-02-13 15:49:16 -06:00
|
|
|
if let SingleImport { target, .. } = e.import_directive.subclass {
|
2016-02-14 23:18:55 -06:00
|
|
|
let dummy_binding = self.resolver.arenas.alloc_name_binding(NameBinding {
|
2016-03-08 19:55:21 -06:00
|
|
|
modifiers: DefModifiers::GLOB_IMPORTED,
|
2016-02-07 17:58:14 -06:00
|
|
|
kind: NameBindingKind::Def(Def::Err),
|
|
|
|
span: None,
|
2016-01-31 21:26:16 -06:00
|
|
|
});
|
2016-02-14 23:18:55 -06:00
|
|
|
let dummy_binding = e.import_directive.import(dummy_binding, None);
|
2016-01-31 21:26:16 -06:00
|
|
|
|
2016-02-14 23:18:55 -06:00
|
|
|
let _ = e.source_module.try_define_child(target, ValueNS, dummy_binding.clone());
|
2016-02-07 17:58:14 -06:00
|
|
|
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,
|
2016-02-14 20:22:59 -06:00
|
|
|
&e.import_directive.subclass);
|
2016-01-31 21:26:16 -06:00
|
|
|
|
|
|
|
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-02-17 03:29:17 -06:00
|
|
|
module_: Module<'b>,
|
|
|
|
errors: &mut Vec<ImportResolvingError<'b>>) {
|
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_);
|
2016-03-12 20:53:22 -06:00
|
|
|
self.resolve_imports_in_current_module(errors);
|
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-02-17 03:29:17 -06:00
|
|
|
self.resolve_imports_for_module_subtree(child_module, errors);
|
2015-03-15 16:44:19 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Attempts to resolve imports for the given module only.
|
2016-03-12 20:53:22 -06:00
|
|
|
fn resolve_imports_in_current_module(&mut self, errors: &mut Vec<ImportResolvingError<'b>>) {
|
2016-02-17 16:45:05 -06:00
|
|
|
let mut imports = Vec::new();
|
2016-03-12 20:53:22 -06:00
|
|
|
let mut unresolved_imports = self.resolver.current_module.unresolved_imports.borrow_mut();
|
2016-02-17 16:45:05 -06:00
|
|
|
::std::mem::swap(&mut imports, &mut unresolved_imports);
|
2015-03-15 16:44:19 -05:00
|
|
|
|
2016-02-17 16:45:05 -06:00
|
|
|
for import_directive in imports {
|
2016-03-12 20:53:22 -06:00
|
|
|
match self.resolve_import(&import_directive) {
|
2016-02-17 16:45:05 -06:00
|
|
|
Failed(err) => {
|
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-03-12 20:53:22 -06:00
|
|
|
source_module: self.resolver.current_module,
|
2016-02-17 16:45:05 -06:00
|
|
|
import_directive: import_directive,
|
2015-10-26 14:31:11 -05:00
|
|
|
span: span,
|
|
|
|
help: help,
|
|
|
|
});
|
2015-07-31 11:58:59 -05:00
|
|
|
}
|
2016-02-17 16:45:05 -06:00
|
|
|
Indeterminate => unresolved_imports.push(import_directive),
|
2016-03-12 20:53:22 -06:00
|
|
|
Success(()) => {
|
|
|
|
// Decrement the count of unresolved imports.
|
|
|
|
assert!(self.resolver.unresolved_imports >= 1);
|
|
|
|
self.resolver.unresolved_imports -= 1;
|
|
|
|
}
|
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.
|
2016-03-16 20:13:31 -05:00
|
|
|
fn resolve_import(&mut self, directive: &'b ImportDirective<'b>) -> ResolveResult<()> {
|
2015-03-15 16:44:19 -05:00
|
|
|
debug!("(resolving import for module) resolving import `{}::...` in `{}`",
|
2016-03-12 20:53:22 -06:00
|
|
|
names_to_string(&directive.module_path),
|
|
|
|
module_to_string(self.resolver.current_module));
|
2015-03-15 16:44:19 -05:00
|
|
|
|
2016-03-12 20:53:22 -06:00
|
|
|
let target_module = match self.resolver.resolve_module_path(&directive.module_path,
|
|
|
|
DontUseLexicalScope,
|
|
|
|
directive.span) {
|
|
|
|
Success(module) => module,
|
|
|
|
Indeterminate => return Indeterminate,
|
|
|
|
Failed(err) => return Failed(err),
|
|
|
|
};
|
2016-03-16 20:16:33 -05:00
|
|
|
directive.target_module.set(Some(target_module));
|
2015-03-15 16:44:19 -05:00
|
|
|
|
2016-02-13 15:49:16 -06:00
|
|
|
let (source, target, value_determined, type_determined) = match directive.subclass {
|
|
|
|
SingleImport { source, target, ref value_determined, ref type_determined } =>
|
|
|
|
(source, target, value_determined, type_determined),
|
2016-03-12 20:53:22 -06:00
|
|
|
GlobImport => return self.resolve_glob_import(target_module, directive),
|
2016-02-13 15:49:16 -06:00
|
|
|
};
|
2016-02-07 17:58:14 -06:00
|
|
|
|
2015-03-15 16:44:19 -05:00
|
|
|
// We need to resolve both namespaces for this to succeed.
|
2016-03-12 20:53:22 -06:00
|
|
|
let module_ = self.resolver.current_module;
|
2016-02-13 15:49:16 -06:00
|
|
|
let (value_result, type_result) = {
|
|
|
|
let mut resolve_in_ns = |ns, determined: bool| {
|
|
|
|
// Temporarily count the directive as determined so that the resolution fails
|
|
|
|
// (as opposed to being indeterminate) when it can only be defined by the directive.
|
2016-03-07 03:59:08 -06:00
|
|
|
if !determined {
|
2016-03-13 05:31:40 -05:00
|
|
|
module_.resolutions.borrow_mut().get_mut(&(target, ns)).unwrap()
|
|
|
|
.decrement_outstanding_references(directive.is_public);
|
2016-03-07 03:59:08 -06:00
|
|
|
}
|
2016-02-13 15:49:16 -06:00
|
|
|
let result =
|
|
|
|
self.resolver.resolve_name_in_module(target_module, source, ns, false, true);
|
2016-03-07 03:59:08 -06:00
|
|
|
if !determined {
|
|
|
|
module_.increment_outstanding_references_for(target, ns, directive.is_public)
|
|
|
|
}
|
2016-02-13 15:49:16 -06:00
|
|
|
result
|
|
|
|
};
|
|
|
|
(resolve_in_ns(ValueNS, value_determined.get()),
|
|
|
|
resolve_in_ns(TypeNS, type_determined.get()))
|
|
|
|
};
|
|
|
|
|
|
|
|
for &(ns, result, determined) in &[(ValueNS, &value_result, value_determined),
|
|
|
|
(TypeNS, &type_result, type_determined)] {
|
|
|
|
if determined.get() { continue }
|
|
|
|
if let Indeterminate = *result { continue }
|
|
|
|
|
|
|
|
determined.set(true);
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
let privacy_error = if !self.resolver.is_visible(binding, target_module) {
|
|
|
|
Some(Box::new(PrivacyError(directive.span, source, binding)))
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
};
|
|
|
|
|
2016-02-16 07:14:32 -06:00
|
|
|
let imported_binding = directive.import(binding, privacy_error);
|
|
|
|
let conflict = module_.try_define_child(target, ns, imported_binding);
|
|
|
|
if let Err(old_binding) = conflict {
|
2016-03-16 00:20:58 -05:00
|
|
|
let binding = &directive.import(binding, None);
|
|
|
|
self.resolver.report_conflict(module_, target, ns, binding, old_binding);
|
2016-02-16 07:14:32 -06:00
|
|
|
}
|
2016-02-13 15:49:16 -06:00
|
|
|
}
|
2016-03-13 05:31:40 -05:00
|
|
|
|
|
|
|
module_.update_resolution(target, ns, |resolution| {
|
|
|
|
resolution.decrement_outstanding_references(directive.is_public);
|
|
|
|
})
|
2016-02-07 17:58:14 -06:00
|
|
|
}
|
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);
|
2016-02-24 02:47:45 -06:00
|
|
|
let note_msg = format!("consider marking `{}` as `pub` in the imported module",
|
2016-01-13 19:42:45 -06:00
|
|
|
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-15 21:54:14 -06:00
|
|
|
(_, &Success(name_binding)) if !name_binding.is_import() &&
|
|
|
|
directive.is_public &&
|
|
|
|
!name_binding.is_public() => {
|
|
|
|
if name_binding.is_extern_crate() {
|
|
|
|
let msg = format!("extern crate `{}` is private, and cannot be reexported \
|
|
|
|
(error E0364), consider declaring with `pub`",
|
2016-01-13 19:42:45 -06:00
|
|
|
source);
|
|
|
|
self.resolver.session.add_lint(lint::builtin::PRIVATE_IN_PUBLIC,
|
|
|
|
directive.id,
|
|
|
|
directive.span,
|
|
|
|
msg);
|
2016-02-15 21:54:14 -06:00
|
|
|
} else {
|
|
|
|
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();
|
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-13 15:49:16 -06:00
|
|
|
// Report a privacy error here if all successful namespaces are privacy errors.
|
2016-02-24 22:40:46 -06:00
|
|
|
let mut privacy_error = None;
|
2016-02-13 15:49:16 -06:00
|
|
|
for &ns in &[ValueNS, TypeNS] {
|
|
|
|
privacy_error = match module_.resolve_name(target, ns, true) {
|
|
|
|
Success(&NameBinding {
|
|
|
|
kind: NameBindingKind::Import { ref privacy_error, .. }, ..
|
|
|
|
}) => privacy_error.as_ref().map(|error| (**error).clone()),
|
|
|
|
_ => continue,
|
|
|
|
};
|
|
|
|
if privacy_error.is_none() { break }
|
2016-02-24 22:40:46 -06:00
|
|
|
}
|
2016-02-13 15:49:16 -06:00
|
|
|
privacy_error.map(|error| self.resolver.privacy_errors.push(error));
|
2016-02-24 22:40:46 -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.
|
2016-02-24 21:36:17 -06:00
|
|
|
let def = match type_result.success().and_then(NameBinding::def) {
|
|
|
|
Some(def) => def,
|
|
|
|
None => value_result.success().and_then(NameBinding::def).unwrap(),
|
2016-02-07 17:06:10 -06:00
|
|
|
};
|
2016-02-24 21:36:17 -06:00
|
|
|
let path_resolution = PathResolution { base_def: def, depth: 0 };
|
|
|
|
self.resolver.def_map.borrow_mut().insert(directive.id, 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::*`.
|
2016-03-16 20:13:31 -05:00
|
|
|
fn resolve_glob_import(&mut self, target_module: Module<'b>, directive: &'b ImportDirective<'b>)
|
2015-03-15 16:44:19 -05:00
|
|
|
-> ResolveResult<()> {
|
2016-03-08 16:27:12 -06:00
|
|
|
if let Some(Def::Trait(_)) = target_module.def {
|
|
|
|
self.resolver.session.span_err(directive.span, "items in traits are not importable.");
|
|
|
|
}
|
|
|
|
|
2016-03-12 20:53:22 -06:00
|
|
|
let module_ = self.resolver.current_module;
|
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
|
|
|
}
|
2016-03-02 03:18:47 -06:00
|
|
|
self.resolver.populate_module_if_necessary(target_module);
|
2016-02-15 21:54:14 -06:00
|
|
|
|
2016-03-08 19:46:46 -06:00
|
|
|
if directive.is_prelude {
|
|
|
|
*module_.prelude.borrow_mut() = Some(target_module);
|
|
|
|
return Success(());
|
|
|
|
}
|
|
|
|
|
2016-02-15 21:54:14 -06:00
|
|
|
// Add to target_module's glob_importers and module_'s resolved_globs
|
|
|
|
target_module.glob_importers.borrow_mut().push((module_, directive));
|
|
|
|
match *module_.resolved_globs.borrow_mut() {
|
|
|
|
(ref mut public_globs, _) if directive.is_public => public_globs.push(target_module),
|
|
|
|
(_, ref mut private_globs) => private_globs.push(target_module),
|
|
|
|
}
|
|
|
|
|
|
|
|
for (&(name, ns), resolution) in target_module.resolutions.borrow().iter() {
|
2016-03-07 03:59:08 -06:00
|
|
|
if let Some(Success(binding)) = resolution.try_result(false) {
|
2016-02-15 21:54:14 -06:00
|
|
|
if binding.defined_with(DefModifiers::IMPORTABLE | DefModifiers::PUBLIC) {
|
2016-02-16 07:14:32 -06:00
|
|
|
let _ = module_.try_define_child(name, ns, directive.import(binding, None));
|
2016-02-15 21:54:14 -06:00
|
|
|
}
|
2016-02-07 17:58:14 -06:00
|
|
|
}
|
2016-02-15 21:54:14 -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
|
|
|
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-15 21:54:14 -06:00
|
|
|
// Miscellaneous post-processing, including recording reexports, recording shadowed traits,
|
2016-02-16 05:48:38 -06:00
|
|
|
// reporting conflicts, reporting the PRIVATE_IN_PUBLIC lint, and reporting unresolved imports.
|
|
|
|
fn finalize_resolutions(&mut self, module: Module<'b>, report_unresolved_imports: bool) {
|
2016-02-15 21:54:14 -06:00
|
|
|
// Since import resolution is finished, globs will not define any more names.
|
2016-02-16 05:48:38 -06:00
|
|
|
module.public_glob_count.set(0); module.private_glob_count.set(0);
|
2016-02-15 21:54:14 -06:00
|
|
|
*module.resolved_globs.borrow_mut() = (Vec::new(), Vec::new());
|
|
|
|
|
|
|
|
let mut reexports = Vec::new();
|
|
|
|
for (&(name, ns), resolution) in module.resolutions.borrow().iter() {
|
2016-03-16 00:20:58 -05:00
|
|
|
resolution.report_conflicts(|b1, b2| {
|
|
|
|
self.resolver.report_conflict(module, name, ns, b1, b2)
|
|
|
|
});
|
|
|
|
|
2016-02-15 21:54:14 -06:00
|
|
|
let binding = match resolution.binding {
|
|
|
|
Some(binding) => binding,
|
|
|
|
None => continue,
|
|
|
|
};
|
|
|
|
|
|
|
|
if binding.is_public() && (binding.is_import() || binding.is_extern_crate()) {
|
|
|
|
if let Some(def) = binding.def() {
|
|
|
|
reexports.push(Export { name: name, def_id: def.def_id() });
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if let NameBindingKind::Import { binding: orig_binding, id, .. } = binding.kind {
|
|
|
|
if ns == TypeNS && binding.is_public() &&
|
|
|
|
orig_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);
|
|
|
|
let lint = lint::builtin::PRIVATE_IN_PUBLIC;
|
|
|
|
self.resolver.session.add_lint(lint, id, binding.span.unwrap(), msg);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if reexports.len() > 0 {
|
|
|
|
if let Some(def_id) = module.def_id() {
|
|
|
|
let node_id = self.resolver.ast_map.as_local_node_id(def_id).unwrap();
|
|
|
|
self.resolver.export_map.insert(node_id, reexports);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-16 05:48:38 -06:00
|
|
|
if report_unresolved_imports {
|
|
|
|
for import in module.unresolved_imports.borrow().iter() {
|
|
|
|
resolve_error(self.resolver, import.span, ResolutionError::UnresolvedImport(None));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-15 21:54:14 -06:00
|
|
|
for (_, child) in module.module_children.borrow().iter() {
|
2016-02-16 05:48:38 -06:00
|
|
|
self.finalize_resolutions(child, report_unresolved_imports);
|
2016-02-15 21:54:14 -06:00
|
|
|
}
|
|
|
|
}
|
2015-03-15 16:44:19 -05:00
|
|
|
}
|
|
|
|
|
2016-02-14 20:22:59 -06: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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-14 20:22:59 -06:00
|
|
|
fn import_directive_subclass_to_string(subclass: &ImportDirectiveSubclass) -> String {
|
|
|
|
match *subclass {
|
2016-02-13 15:49:16 -06: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();
|
|
|
|
}
|