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};
|
2015-11-15 20:10:09 -06:00
|
|
|
use {NameBindings, NameBinding};
|
2015-03-15 16:44:19 -05:00
|
|
|
use NamespaceResult::{BoundResult, UnboundResult, UnknownResult};
|
|
|
|
use NamespaceResult;
|
|
|
|
use NameSearchType;
|
|
|
|
use ResolveResult;
|
|
|
|
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::*;
|
2015-08-16 05:32:28 -05:00
|
|
|
use rustc::middle::def_id::DefId;
|
2015-03-15 16:44:19 -05:00
|
|
|
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;
|
|
|
|
use std::rc::Rc;
|
|
|
|
|
|
|
|
/// 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.
|
|
|
|
#[derive(Debug)]
|
|
|
|
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,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// The item that an import resolves to.
|
|
|
|
#[derive(Clone,Debug)]
|
|
|
|
pub struct Target {
|
|
|
|
pub target_module: Rc<Module>,
|
2015-11-15 20:10:09 -06:00
|
|
|
pub binding: NameBinding,
|
2015-03-15 16:44:19 -05:00
|
|
|
pub shadowable: Shadowable,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Target {
|
|
|
|
pub fn new(target_module: Rc<Module>,
|
2015-11-15 20:10:09 -06:00
|
|
|
binding: NameBinding,
|
2015-10-26 14:31:11 -05:00
|
|
|
shadowable: Shadowable)
|
|
|
|
-> Target {
|
2015-03-15 16:44:19 -05:00
|
|
|
Target {
|
|
|
|
target_module: target_module,
|
2015-11-15 20:10:09 -06:00
|
|
|
binding: binding,
|
2015-03-15 16:44:19 -05:00
|
|
|
shadowable: shadowable,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug)]
|
2015-12-10 19:38:13 -06:00
|
|
|
/// An ImportResolutionPerNamespace records what we know about an imported name.
|
2015-12-01 17:06:34 -06:00
|
|
|
/// More specifically, it records the number of unresolved `use` directives that import the name,
|
|
|
|
/// and for each namespace, it records the `use` directive importing the name in the namespace
|
|
|
|
/// and the `Target` to which the name in the namespace resolves (if applicable).
|
|
|
|
/// Different `use` directives may import the same name in different namespaces.
|
2015-12-10 19:38:13 -06:00
|
|
|
pub struct ImportResolutionPerNamespace {
|
2015-12-01 17:06:34 -06:00
|
|
|
// When outstanding_references reaches zero, outside modules can count on the targets being
|
|
|
|
// correct. Before then, all bets are off; future `use` directives could override the name.
|
|
|
|
// Since shadowing is forbidden, the only way outstanding_references > 1 in a legal program
|
|
|
|
// is if the name is imported by exactly two `use` directives, one of which resolves to a
|
|
|
|
// value and the other of which resolves to a type.
|
|
|
|
pub outstanding_references: usize,
|
2015-12-10 19:38:13 -06:00
|
|
|
pub type_ns: ImportResolution,
|
|
|
|
pub value_ns: ImportResolution,
|
2015-12-01 17:06:34 -06:00
|
|
|
}
|
|
|
|
|
2015-12-10 19:38:13 -06:00
|
|
|
/// Records what we know about an imported name in a namespace (see `ImportResolutionPerNamespace`).
|
2015-12-01 17:06:34 -06:00
|
|
|
#[derive(Clone,Debug)]
|
2015-12-10 19:38:13 -06:00
|
|
|
pub struct ImportResolution {
|
2015-12-01 17:06:34 -06:00
|
|
|
/// Whether the name in the namespace was imported with a `use` or a `pub use`.
|
2015-03-15 16:44:19 -05:00
|
|
|
pub is_public: bool,
|
|
|
|
|
2015-12-01 17:06:34 -06:00
|
|
|
/// Resolution of the name in the namespace
|
|
|
|
pub target: Option<Target>,
|
2015-03-15 16:44:19 -05:00
|
|
|
|
2015-12-01 17:06:34 -06:00
|
|
|
/// The source node of the `use` directive
|
|
|
|
pub id: NodeId,
|
2015-03-15 16:44:19 -05:00
|
|
|
}
|
|
|
|
|
2015-12-10 19:38:13 -06:00
|
|
|
impl ::std::ops::Index<Namespace> for ImportResolutionPerNamespace {
|
|
|
|
type Output = ImportResolution;
|
|
|
|
fn index(&self, ns: Namespace) -> &ImportResolution {
|
2015-12-01 17:06:34 -06:00
|
|
|
match ns { TypeNS => &self.type_ns, ValueNS => &self.value_ns }
|
2015-03-15 16:44:19 -05:00
|
|
|
}
|
2015-12-01 17:06:34 -06:00
|
|
|
}
|
2015-03-15 16:44:19 -05:00
|
|
|
|
2015-12-10 19:38:13 -06:00
|
|
|
impl ::std::ops::IndexMut<Namespace> for ImportResolutionPerNamespace {
|
|
|
|
fn index_mut(&mut self, ns: Namespace) -> &mut ImportResolution {
|
2015-12-01 17:06:34 -06:00
|
|
|
match ns { TypeNS => &mut self.type_ns, ValueNS => &mut self.value_ns }
|
2015-03-15 16:44:19 -05:00
|
|
|
}
|
2015-12-01 17:06:34 -06:00
|
|
|
}
|
2015-03-15 16:44:19 -05:00
|
|
|
|
2015-12-10 19:38:13 -06:00
|
|
|
impl ImportResolutionPerNamespace {
|
|
|
|
pub fn new(id: NodeId, is_public: bool) -> Self {
|
|
|
|
let resolution = ImportResolution { id: id, is_public: is_public, target: None };
|
|
|
|
ImportResolutionPerNamespace {
|
2015-12-01 17:06:34 -06:00
|
|
|
outstanding_references: 0, type_ns: resolution.clone(), value_ns: resolution,
|
2015-03-15 16:44:19 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn shadowable(&self, namespace: Namespace) -> Shadowable {
|
2015-12-01 17:06:34 -06:00
|
|
|
match self[namespace].target {
|
|
|
|
Some(ref target) => target.shadowable,
|
|
|
|
None => Shadowable::Always,
|
2015-03-15 16:44:19 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-04 01:14:32 -05:00
|
|
|
struct ImportResolvingError {
|
|
|
|
span: Span,
|
|
|
|
path: String,
|
|
|
|
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
|
|
|
|
2015-11-17 19:22:32 -06:00
|
|
|
let module_root = self.resolver.graph_root.clone();
|
2015-07-31 11:58:59 -05:00
|
|
|
let errors = self.resolve_imports_for_module_subtree(module_root.clone());
|
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 {
|
2015-07-31 11:58:59 -05:00
|
|
|
resolve_error(self.resolver,
|
2015-08-04 01:14:32 -05:00
|
|
|
e.span,
|
|
|
|
ResolutionError::UnresolvedImport(Some((&e.path, &e.help))));
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// 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,
|
|
|
|
module_: Rc<Module>)
|
2015-08-04 01:14:32 -05:00
|
|
|
-> Vec<ImportResolvingError> {
|
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 {}",
|
|
|
|
module_to_string(&*module_));
|
|
|
|
let orig_module = replace(&mut self.resolver.current_module, module_.clone());
|
2015-07-31 11:58:59 -05:00
|
|
|
errors.extend(self.resolve_imports_for_module(module_.clone()));
|
2015-03-15 16:44:19 -05:00
|
|
|
self.resolver.current_module = orig_module;
|
|
|
|
|
|
|
|
build_reduced_graph::populate_module_if_necessary(self.resolver, &module_);
|
2015-06-11 07:56:07 -05:00
|
|
|
for (_, child_node) in module_.children.borrow().iter() {
|
2015-11-17 19:22:32 -06:00
|
|
|
match child_node.type_ns.module() {
|
2015-03-15 16:44:19 -05:00
|
|
|
None => {
|
|
|
|
// Nothing to do.
|
|
|
|
}
|
|
|
|
Some(child_module) => {
|
2015-07-31 11:58:59 -05:00
|
|
|
errors.extend(self.resolve_imports_for_module_subtree(child_module));
|
2015-03-15 16:44:19 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-06-11 07:56:07 -05:00
|
|
|
for (_, child_module) in module_.anonymous_children.borrow().iter() {
|
2015-07-31 11:58:59 -05:00
|
|
|
errors.extend(self.resolve_imports_for_module_subtree(child_module.clone()));
|
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.
|
2015-08-04 01:14:32 -05:00
|
|
|
fn resolve_imports_for_module(&mut self, module: Rc<Module>) -> Vec<ImportResolvingError> {
|
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 {}",
|
2015-03-15 16:44:19 -05: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();
|
2015-10-26 14:31:11 -05:00
|
|
|
match self.resolve_import_for_module(module.clone(), &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 {
|
2015-10-26 14:31:11 -05:00
|
|
|
span: span,
|
|
|
|
path: import_path_to_string(&import_directive.module_path,
|
|
|
|
import_directive.subclass),
|
|
|
|
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,
|
|
|
|
module_: Rc<Module>,
|
|
|
|
import_directive: &ImportDirective)
|
|
|
|
-> ResolveResult<()> {
|
|
|
|
let mut resolution_result = ResolveResult::Failed(None);
|
|
|
|
let module_path = &import_directive.module_path;
|
|
|
|
|
|
|
|
debug!("(resolving import for module) resolving import `{}::...` in `{}`",
|
|
|
|
names_to_string(&module_path[..]),
|
|
|
|
module_to_string(&*module_));
|
|
|
|
|
|
|
|
// First, resolve the module path for the directive, if necessary.
|
2015-03-24 18:53:34 -05:00
|
|
|
let container = if module_path.is_empty() {
|
2015-03-15 16:44:19 -05:00
|
|
|
// Use the crate root.
|
2015-11-17 19:22:32 -06:00
|
|
|
Some((self.resolver.graph_root.clone(), LastMod(AllPublic)))
|
2015-03-15 16:44:19 -05:00
|
|
|
} else {
|
|
|
|
match self.resolver.resolve_module_path(module_.clone(),
|
|
|
|
&module_path[..],
|
|
|
|
UseLexicalScopeFlag::DontUseLexicalScope,
|
|
|
|
import_directive.span,
|
|
|
|
NameSearchType::ImportSearch) {
|
|
|
|
ResolveResult::Failed(err) => {
|
|
|
|
resolution_result = ResolveResult::Failed(err);
|
|
|
|
None
|
2015-10-26 14:31:11 -05:00
|
|
|
}
|
2015-03-15 16:44:19 -05:00
|
|
|
ResolveResult::Indeterminate => {
|
|
|
|
resolution_result = ResolveResult::Indeterminate;
|
|
|
|
None
|
|
|
|
}
|
|
|
|
ResolveResult::Success(container) => Some(container),
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
match container {
|
|
|
|
None => {}
|
|
|
|
Some((containing_module, lp)) => {
|
|
|
|
// We found the module that the target is contained
|
|
|
|
// within. Attempt to resolve the import within it.
|
|
|
|
|
|
|
|
match import_directive.subclass {
|
|
|
|
SingleImport(target, source) => {
|
2015-10-26 14:31:11 -05:00
|
|
|
resolution_result = self.resolve_single_import(&module_,
|
|
|
|
containing_module,
|
|
|
|
target,
|
|
|
|
source,
|
|
|
|
import_directive,
|
|
|
|
lp);
|
2015-03-15 16:44:19 -05:00
|
|
|
}
|
|
|
|
GlobImport => {
|
2015-10-26 14:31:11 -05:00
|
|
|
resolution_result = self.resolve_glob_import(&module_,
|
|
|
|
containing_module,
|
|
|
|
import_directive,
|
|
|
|
lp);
|
2015-03-15 16:44:19 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Decrement the count of unresolved imports.
|
|
|
|
match resolution_result {
|
|
|
|
ResolveResult::Success(()) => {
|
|
|
|
assert!(self.resolver.unresolved_imports >= 1);
|
|
|
|
self.resolver.unresolved_imports -= 1;
|
|
|
|
}
|
|
|
|
_ => {
|
|
|
|
// Nothing to do here; just return the error.
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Decrement the count of unresolved globs if necessary. But only if
|
2015-07-31 11:58:59 -05:00
|
|
|
// the resolution result is a success -- other cases will
|
|
|
|
// be handled by the main loop.
|
2015-03-15 16:44:19 -05:00
|
|
|
|
2015-07-31 11:58:59 -05:00
|
|
|
if resolution_result.success() {
|
2015-03-15 16:44:19 -05:00
|
|
|
match import_directive.subclass {
|
|
|
|
GlobImport => {
|
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
|
|
|
}
|
|
|
|
SingleImport(..) => {
|
|
|
|
// Ignore.
|
|
|
|
}
|
|
|
|
}
|
2015-08-05 14:47:01 -05:00
|
|
|
if import_directive.is_public {
|
2015-08-06 05:47:10 -05:00
|
|
|
module_.dec_pub_count();
|
2015-08-05 14:47:01 -05:00
|
|
|
}
|
2015-03-15 16:44:19 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return resolution_result;
|
|
|
|
}
|
|
|
|
|
|
|
|
fn resolve_single_import(&mut self,
|
|
|
|
module_: &Module,
|
|
|
|
target_module: Rc<Module>,
|
|
|
|
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,
|
2015-03-15 16:44:19 -05: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
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// We need to resolve both namespaces for this to succeed.
|
|
|
|
|
|
|
|
let mut value_result = UnknownResult;
|
|
|
|
let mut type_result = UnknownResult;
|
2015-12-14 11:06:31 -06:00
|
|
|
let mut lev_suggestion = "".to_owned();
|
2015-03-15 16:44:19 -05:00
|
|
|
|
|
|
|
// Search for direct children of the containing module.
|
|
|
|
build_reduced_graph::populate_module_if_necessary(self.resolver, &target_module);
|
|
|
|
|
|
|
|
match target_module.children.borrow().get(&source) {
|
|
|
|
None => {
|
2015-12-14 11:06:31 -06:00
|
|
|
let names = target_module.children.borrow();
|
|
|
|
if let Some(name) = find_best_match_for_name(names.keys(),
|
|
|
|
&source.as_str(),
|
|
|
|
None) {
|
|
|
|
lev_suggestion = format!(". Did you mean to use `{}`?", name);
|
|
|
|
}
|
2015-03-15 16:44:19 -05:00
|
|
|
}
|
|
|
|
Some(ref child_name_bindings) => {
|
|
|
|
// pub_err makes sure we don't give the same error twice.
|
|
|
|
let mut pub_err = false;
|
2015-11-15 20:10:09 -06:00
|
|
|
if child_name_bindings.value_ns.defined() {
|
2015-03-15 16:44:19 -05:00
|
|
|
debug!("(resolving single import) found value binding");
|
|
|
|
value_result = BoundResult(target_module.clone(),
|
2015-11-15 20:10:09 -06:00
|
|
|
child_name_bindings.value_ns.clone());
|
2015-12-13 12:57:07 -06:00
|
|
|
if directive.is_public && !child_name_bindings.value_ns.is_public() {
|
2015-10-26 14:31:11 -05:00
|
|
|
let msg = format!("`{}` is private, and cannot be reexported", source);
|
|
|
|
let note_msg = format!("Consider marking `{}` as `pub` in the imported \
|
|
|
|
module",
|
|
|
|
source);
|
2015-03-15 16:44:19 -05:00
|
|
|
span_err!(self.resolver.session, directive.span, E0364, "{}", &msg);
|
2015-07-16 13:21:11 -05:00
|
|
|
self.resolver.session.span_note(directive.span, ¬e_msg);
|
2015-03-15 16:44:19 -05:00
|
|
|
pub_err = true;
|
|
|
|
}
|
2015-12-13 12:57:07 -06:00
|
|
|
if directive.is_public && child_name_bindings.value_ns.
|
|
|
|
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);
|
|
|
|
pub_err = true;
|
|
|
|
}
|
2015-03-15 16:44:19 -05:00
|
|
|
}
|
2015-11-15 20:10:09 -06:00
|
|
|
if child_name_bindings.type_ns.defined() {
|
2015-03-15 16:44:19 -05:00
|
|
|
debug!("(resolving single import) found type binding");
|
|
|
|
type_result = BoundResult(target_module.clone(),
|
2015-11-15 20:10:09 -06:00
|
|
|
child_name_bindings.type_ns.clone());
|
|
|
|
if !pub_err && directive.is_public &&
|
2015-12-13 12:57:07 -06:00
|
|
|
!child_name_bindings.type_ns.is_public() {
|
2015-10-26 14:31:11 -05:00
|
|
|
let msg = format!("`{}` is private, and cannot be reexported", source);
|
2015-07-17 16:36:35 -05:00
|
|
|
let note_msg = format!("Consider declaring module `{}` as a `pub mod`",
|
2015-07-28 11:07:20 -05:00
|
|
|
source);
|
2015-03-15 16:44:19 -05:00
|
|
|
span_err!(self.resolver.session, directive.span, E0365, "{}", &msg);
|
2015-07-16 13:21:11 -05:00
|
|
|
self.resolver.session.span_note(directive.span, ¬e_msg);
|
2015-03-15 16:44:19 -05:00
|
|
|
}
|
2015-12-13 12:57:07 -06:00
|
|
|
if !pub_err && directive.is_public && child_name_bindings.type_ns.
|
|
|
|
defined_with(DefModifiers::PRIVATE_VARIANT) {
|
|
|
|
let msg = format!("variant `{}` is private, and cannot be reexported ( \
|
|
|
|
error E0365), 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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Unless we managed to find a result in both namespaces (unlikely),
|
|
|
|
// search imports as well.
|
|
|
|
let mut value_used_reexport = false;
|
|
|
|
let mut type_used_reexport = false;
|
|
|
|
match (value_result.clone(), type_result.clone()) {
|
|
|
|
(BoundResult(..), BoundResult(..)) => {} // Continue.
|
|
|
|
_ => {
|
|
|
|
// If there is an unresolved glob at this point in the
|
|
|
|
// containing module, bail out. We don't know enough to be
|
|
|
|
// able to resolve this import.
|
|
|
|
|
2015-08-05 14:47:01 -05:00
|
|
|
if target_module.pub_glob_count.get() > 0 {
|
2015-10-26 14:31:11 -05:00
|
|
|
debug!("(resolving single import) unresolved pub glob; bailing out");
|
2015-03-15 16:44:19 -05:00
|
|
|
return ResolveResult::Indeterminate;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Now search the exported imports within the containing module.
|
|
|
|
match target_module.import_resolutions.borrow().get(&source) {
|
|
|
|
None => {
|
|
|
|
debug!("(resolving single import) no import");
|
|
|
|
// The containing module definitely doesn't have an
|
|
|
|
// exported import with the name in question. We can
|
|
|
|
// therefore accurately report that the names are
|
|
|
|
// unbound.
|
|
|
|
|
2015-12-14 11:06:31 -06:00
|
|
|
if lev_suggestion.is_empty() { // skip if we already have a suggestion
|
|
|
|
let names = target_module.import_resolutions.borrow();
|
|
|
|
if let Some(name) = find_best_match_for_name(names.keys(),
|
|
|
|
&source.as_str(),
|
|
|
|
None) {
|
|
|
|
lev_suggestion =
|
|
|
|
format!(". Did you mean to use the re-exported import `{}`?",
|
|
|
|
name);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-15 16:44:19 -05:00
|
|
|
if value_result.is_unknown() {
|
|
|
|
value_result = UnboundResult;
|
|
|
|
}
|
|
|
|
if type_result.is_unknown() {
|
|
|
|
type_result = UnboundResult;
|
|
|
|
}
|
|
|
|
}
|
2015-10-26 14:31:11 -05:00
|
|
|
Some(import_resolution) if import_resolution.outstanding_references == 0 => {
|
2015-03-15 16:44:19 -05:00
|
|
|
|
|
|
|
fn get_binding(this: &mut Resolver,
|
2015-12-10 19:38:13 -06:00
|
|
|
import_resolution: &ImportResolutionPerNamespace,
|
2015-03-15 16:44:19 -05:00
|
|
|
namespace: Namespace,
|
2015-09-24 15:05:02 -05:00
|
|
|
source: Name)
|
2015-10-26 14:31:11 -05:00
|
|
|
-> NamespaceResult {
|
2015-03-15 16:44:19 -05:00
|
|
|
|
|
|
|
// Import resolutions must be declared with "pub"
|
|
|
|
// in order to be exported.
|
2015-12-01 17:06:34 -06:00
|
|
|
if !import_resolution[namespace].is_public {
|
2015-03-15 16:44:19 -05:00
|
|
|
return UnboundResult;
|
|
|
|
}
|
|
|
|
|
2015-12-01 17:06:34 -06:00
|
|
|
match import_resolution[namespace].target.clone() {
|
2015-03-15 16:44:19 -05:00
|
|
|
None => {
|
|
|
|
return UnboundResult;
|
|
|
|
}
|
|
|
|
Some(Target {
|
|
|
|
target_module,
|
2015-11-15 20:10:09 -06:00
|
|
|
binding,
|
2015-03-15 16:44:19 -05:00
|
|
|
shadowable: _
|
|
|
|
}) => {
|
2015-10-26 14:31:11 -05:00
|
|
|
debug!("(resolving single import) found import in ns {:?}",
|
|
|
|
namespace);
|
2015-12-01 17:06:34 -06:00
|
|
|
let id = import_resolution[namespace].id;
|
2015-03-15 16:44:19 -05:00
|
|
|
// track used imports and extern crates as well
|
|
|
|
this.used_imports.insert((id, namespace));
|
2015-09-24 15:05:02 -05:00
|
|
|
this.record_import_use(id, source);
|
2015-11-16 01:59:50 -06:00
|
|
|
match target_module.def_id() {
|
2015-03-15 16:44:19 -05:00
|
|
|
Some(DefId{krate: kid, ..}) => {
|
|
|
|
this.used_crates.insert(kid);
|
2015-10-26 14:31:11 -05:00
|
|
|
}
|
2015-03-15 16:44:19 -05:00
|
|
|
_ => {}
|
|
|
|
}
|
2015-11-15 20:10:09 -06:00
|
|
|
return BoundResult(target_module, binding);
|
2015-03-15 16:44:19 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// The name is an import which has been fully
|
|
|
|
// resolved. We can, therefore, just follow it.
|
|
|
|
if value_result.is_unknown() {
|
|
|
|
value_result = get_binding(self.resolver,
|
|
|
|
import_resolution,
|
|
|
|
ValueNS,
|
2015-09-24 15:05:02 -05:00
|
|
|
source);
|
2015-12-01 17:06:34 -06:00
|
|
|
value_used_reexport = import_resolution.value_ns.is_public;
|
2015-03-15 16:44:19 -05:00
|
|
|
}
|
|
|
|
if type_result.is_unknown() {
|
|
|
|
type_result = get_binding(self.resolver,
|
|
|
|
import_resolution,
|
|
|
|
TypeNS,
|
2015-09-24 15:05:02 -05:00
|
|
|
source);
|
2015-12-01 17:06:34 -06:00
|
|
|
type_used_reexport = import_resolution.type_ns.is_public;
|
2015-03-15 16:44:19 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
Some(_) => {
|
|
|
|
// If target_module is the same module whose import we are resolving
|
|
|
|
// and there it has an unresolved import with the same name as `source`,
|
|
|
|
// then the user is actually trying to import an item that is declared
|
|
|
|
// in the same scope
|
|
|
|
//
|
|
|
|
// e.g
|
|
|
|
// use self::submodule;
|
|
|
|
// pub mod submodule;
|
|
|
|
//
|
|
|
|
// In this case we continue as if we resolved the import and let the
|
|
|
|
// check_for_conflicts_between_imports_and_items call below handle
|
|
|
|
// the conflict
|
2015-11-16 01:59:50 -06:00
|
|
|
match (module_.def_id(), target_module.def_id()) {
|
2015-10-26 14:31:11 -05:00
|
|
|
(Some(id1), Some(id2)) if id1 == id2 => {
|
2015-03-15 16:44:19 -05:00
|
|
|
if value_result.is_unknown() {
|
|
|
|
value_result = UnboundResult;
|
|
|
|
}
|
|
|
|
if type_result.is_unknown() {
|
|
|
|
type_result = UnboundResult;
|
|
|
|
}
|
|
|
|
}
|
2015-10-26 14:31:11 -05:00
|
|
|
_ => {
|
2015-03-15 16:44:19 -05:00
|
|
|
// The import is unresolved. Bail out.
|
2015-10-26 14:31:11 -05:00
|
|
|
debug!("(resolving single import) unresolved import; bailing out");
|
2015-03-15 16:44:19 -05:00
|
|
|
return ResolveResult::Indeterminate;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let mut value_used_public = false;
|
|
|
|
let mut type_used_public = false;
|
|
|
|
|
|
|
|
// If we didn't find a result in the type namespace, search the
|
|
|
|
// external modules.
|
|
|
|
match type_result {
|
|
|
|
BoundResult(..) => {}
|
|
|
|
_ => {
|
|
|
|
match target_module.external_module_children.borrow_mut().get(&source).cloned() {
|
|
|
|
None => {} // Continue.
|
|
|
|
Some(module) => {
|
|
|
|
debug!("(resolving single import) found external module");
|
|
|
|
// track the module as used.
|
2015-11-16 01:59:50 -06:00
|
|
|
match module.def_id() {
|
2015-03-15 16:44:19 -05:00
|
|
|
Some(DefId{krate: kid, ..}) => {
|
|
|
|
self.resolver.used_crates.insert(kid);
|
|
|
|
}
|
|
|
|
_ => {}
|
|
|
|
}
|
2015-11-15 20:10:09 -06:00
|
|
|
let name_binding = NameBinding::create_from_module(module);
|
|
|
|
type_result = BoundResult(target_module.clone(), name_binding);
|
2015-03-15 16:44:19 -05:00
|
|
|
type_used_public = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// We've successfully resolved the import. Write the results in.
|
|
|
|
let mut import_resolutions = module_.import_resolutions.borrow_mut();
|
2015-03-20 12:22:57 -05:00
|
|
|
let import_resolution = import_resolutions.get_mut(&target).unwrap();
|
2015-03-15 16:44:19 -05:00
|
|
|
|
|
|
|
{
|
|
|
|
let mut check_and_write_import = |namespace, result: &_, used_public: &mut bool| {
|
|
|
|
let namespace_name = match namespace {
|
|
|
|
TypeNS => "type",
|
|
|
|
ValueNS => "value",
|
|
|
|
};
|
|
|
|
|
|
|
|
match *result {
|
2015-11-15 20:10:09 -06:00
|
|
|
BoundResult(ref target_module, ref name_binding) => {
|
2015-03-15 16:44:19 -05:00
|
|
|
debug!("(resolving single import) found {:?} target: {:?}",
|
|
|
|
namespace_name,
|
2015-11-15 20:10:09 -06:00
|
|
|
name_binding.def());
|
2015-10-26 14:31:11 -05:00
|
|
|
self.check_for_conflicting_import(&import_resolution,
|
|
|
|
directive.span,
|
|
|
|
target,
|
|
|
|
namespace);
|
|
|
|
|
2015-11-15 20:10:09 -06:00
|
|
|
self.check_that_import_is_importable(&name_binding,
|
2015-10-26 14:31:11 -05:00
|
|
|
directive.span,
|
2015-11-15 20:10:09 -06:00
|
|
|
target);
|
2015-03-15 16:44:19 -05:00
|
|
|
|
2015-12-10 19:38:13 -06:00
|
|
|
import_resolution[namespace] = ImportResolution {
|
2015-12-01 17:06:34 -06:00
|
|
|
target: Some(Target::new(target_module.clone(),
|
|
|
|
name_binding.clone(),
|
|
|
|
directive.shadowable)),
|
|
|
|
id: directive.id,
|
|
|
|
is_public: directive.is_public
|
|
|
|
};
|
2015-11-15 20:10:09 -06:00
|
|
|
*used_public = name_binding.is_public();
|
2015-03-15 16:44:19 -05:00
|
|
|
}
|
2015-10-26 14:31:11 -05:00
|
|
|
UnboundResult => {
|
|
|
|
// Continue.
|
|
|
|
}
|
2015-03-15 16:44:19 -05:00
|
|
|
UnknownResult => {
|
|
|
|
panic!("{:?} result should be known at this point", namespace_name);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
check_and_write_import(ValueNS, &value_result, &mut value_used_public);
|
|
|
|
check_and_write_import(TypeNS, &type_result, &mut type_used_public);
|
|
|
|
}
|
|
|
|
|
2015-10-26 14:31:11 -05:00
|
|
|
self.check_for_conflicts_between_imports_and_items(module_,
|
|
|
|
import_resolution,
|
|
|
|
directive.span,
|
|
|
|
target);
|
2015-03-15 16:44:19 -05:00
|
|
|
|
|
|
|
if value_result.is_unbound() && type_result.is_unbound() {
|
2015-12-14 11:06:31 -06:00
|
|
|
let msg = format!("There is no `{}` in `{}`{}",
|
2015-07-08 15:55:22 -05:00
|
|
|
source,
|
2015-12-14 11:06:31 -06:00
|
|
|
module_to_string(&target_module), lev_suggestion);
|
2015-03-15 16:44:19 -05:00
|
|
|
return ResolveResult::Failed(Some((directive.span, msg)));
|
|
|
|
}
|
|
|
|
let value_used_public = value_used_reexport || value_used_public;
|
|
|
|
let type_used_public = type_used_reexport || type_used_public;
|
|
|
|
|
|
|
|
assert!(import_resolution.outstanding_references >= 1);
|
|
|
|
import_resolution.outstanding_references -= 1;
|
|
|
|
|
|
|
|
// 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.
|
2015-12-01 17:06:34 -06:00
|
|
|
let value_def_and_priv = import_resolution.value_ns.target.as_ref().map(|target| {
|
2015-11-15 20:10:09 -06:00
|
|
|
let def = target.binding.def().unwrap();
|
2015-10-26 14:31:11 -05:00
|
|
|
(def,
|
|
|
|
if value_used_public {
|
|
|
|
lp
|
|
|
|
} else {
|
|
|
|
DependsOn(def.def_id())
|
|
|
|
})
|
2015-03-15 16:44:19 -05:00
|
|
|
});
|
2015-12-01 17:06:34 -06:00
|
|
|
let type_def_and_priv = import_resolution.type_ns.target.as_ref().map(|target| {
|
2015-11-15 20:10:09 -06:00
|
|
|
let def = target.binding.def().unwrap();
|
2015-10-26 14:31:11 -05:00
|
|
|
(def,
|
|
|
|
if type_used_public {
|
|
|
|
lp
|
|
|
|
} else {
|
|
|
|
DependsOn(def.def_id())
|
|
|
|
})
|
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
|
|
|
};
|
|
|
|
|
|
|
|
if let Some((def, _)) = value_def_and_priv {
|
2015-10-26 14:31:11 -05:00
|
|
|
self.resolver.def_map.borrow_mut().insert(directive.id,
|
|
|
|
PathResolution {
|
|
|
|
base_def: def,
|
|
|
|
last_private: import_lp,
|
|
|
|
depth: 0,
|
|
|
|
});
|
2015-03-15 16:44:19 -05:00
|
|
|
}
|
|
|
|
if let Some((def, _)) = type_def_and_priv {
|
2015-10-26 14:31:11 -05:00
|
|
|
self.resolver.def_map.borrow_mut().insert(directive.id,
|
|
|
|
PathResolution {
|
|
|
|
base_def: def,
|
|
|
|
last_private: import_lp,
|
|
|
|
depth: 0,
|
|
|
|
});
|
2015-03-15 16:44:19 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
debug!("(resolving single import) successfully resolved import");
|
|
|
|
return ResolveResult::Success(());
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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,
|
|
|
|
module_: &Module,
|
|
|
|
target_module: Rc<Module>,
|
|
|
|
import_directive: &ImportDirective,
|
|
|
|
lp: LastPrivate)
|
|
|
|
-> ResolveResult<()> {
|
|
|
|
let id = import_directive.id;
|
|
|
|
let is_public = import_directive.is_public;
|
|
|
|
|
|
|
|
// This function works in a highly imperative manner; it eagerly adds
|
|
|
|
// everything it can to the list of import resolutions of the module
|
|
|
|
// node.
|
|
|
|
debug!("(resolving glob import) resolving glob import {}", id);
|
|
|
|
|
|
|
|
// We must bail out if the node has unresolved imports of any kind
|
|
|
|
// (including globs).
|
2015-08-05 14:47:01 -05:00
|
|
|
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");
|
2015-03-15 16:44:19 -05:00
|
|
|
return ResolveResult::Indeterminate;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add all resolved imports from the containing module.
|
|
|
|
let import_resolutions = target_module.import_resolutions.borrow();
|
2015-08-05 14:47:01 -05:00
|
|
|
|
|
|
|
if module_.import_resolutions.borrow_state() != ::std::cell::BorrowState::Unused {
|
|
|
|
// In this case, target_module == module_
|
|
|
|
// This means we are trying to glob import a module into itself,
|
|
|
|
// and it is a no-go
|
2015-08-07 05:00:46 -05:00
|
|
|
debug!("(resolving glob imports) target module is current module; giving up");
|
2015-10-26 14:31:11 -05:00
|
|
|
return ResolveResult::Failed(Some((import_directive.span,
|
|
|
|
"Cannot glob-import a module into itself.".into())));
|
2015-08-05 14:47:01 -05:00
|
|
|
}
|
|
|
|
|
2015-09-23 12:04:49 -05:00
|
|
|
for (name, target_import_resolution) in import_resolutions.iter() {
|
2015-10-26 14:31:11 -05:00
|
|
|
debug!("(resolving glob import) writing module resolution {} into `{}`",
|
2015-09-23 12:04:49 -05:00
|
|
|
*name,
|
2015-03-15 16:44:19 -05:00
|
|
|
module_to_string(module_));
|
|
|
|
|
|
|
|
// Here we merge two import resolutions.
|
|
|
|
let mut import_resolutions = module_.import_resolutions.borrow_mut();
|
2015-12-01 17:06:34 -06:00
|
|
|
let mut dest_import_resolution = import_resolutions.entry(*name).or_insert_with(|| {
|
2015-12-10 19:38:13 -06:00
|
|
|
ImportResolutionPerNamespace::new(id, is_public)
|
2015-12-01 17:06:34 -06:00
|
|
|
});
|
|
|
|
|
|
|
|
for &ns in [TypeNS, ValueNS].iter() {
|
|
|
|
match target_import_resolution[ns].target {
|
|
|
|
Some(ref target) if target_import_resolution[ns].is_public => {
|
|
|
|
self.check_for_conflicting_import(&dest_import_resolution,
|
|
|
|
import_directive.span,
|
|
|
|
*name,
|
|
|
|
ns);
|
2015-12-10 19:38:13 -06:00
|
|
|
dest_import_resolution[ns] = ImportResolution {
|
2015-12-01 17:06:34 -06:00
|
|
|
id: id, is_public: is_public, target: Some(target.clone())
|
|
|
|
};
|
2015-03-15 16:44:19 -05:00
|
|
|
}
|
2015-12-01 17:06:34 -06:00
|
|
|
_ => {}
|
2015-03-15 16:44:19 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add all children from the containing module.
|
|
|
|
build_reduced_graph::populate_module_if_necessary(self.resolver, &target_module);
|
|
|
|
|
2015-06-11 07:56:07 -05:00
|
|
|
for (&name, name_bindings) in target_module.children.borrow().iter() {
|
2015-03-15 16:44:19 -05:00
|
|
|
self.merge_import_resolution(module_,
|
|
|
|
target_module.clone(),
|
|
|
|
import_directive,
|
|
|
|
name,
|
|
|
|
name_bindings.clone());
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// Record the destination of this import
|
2015-11-16 01:59:50 -06:00
|
|
|
if let Some(did) = target_module.def_id() {
|
2015-10-26 14:31:11 -05:00
|
|
|
self.resolver.def_map.borrow_mut().insert(id,
|
|
|
|
PathResolution {
|
|
|
|
base_def: DefMod(did),
|
|
|
|
last_private: lp,
|
|
|
|
depth: 0,
|
|
|
|
});
|
2015-03-15 16:44:19 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
debug!("(resolving glob import) successfully resolved import");
|
|
|
|
return ResolveResult::Success(());
|
|
|
|
}
|
|
|
|
|
|
|
|
fn merge_import_resolution(&mut self,
|
|
|
|
module_: &Module,
|
|
|
|
containing_module: Rc<Module>,
|
|
|
|
import_directive: &ImportDirective,
|
|
|
|
name: Name,
|
2015-11-15 20:10:09 -06:00
|
|
|
name_bindings: NameBindings) {
|
2015-03-15 16:44:19 -05:00
|
|
|
let id = import_directive.id;
|
|
|
|
let is_public = import_directive.is_public;
|
|
|
|
|
|
|
|
let mut import_resolutions = module_.import_resolutions.borrow_mut();
|
2015-12-10 19:38:13 -06:00
|
|
|
let dest_import_resolution = import_resolutions.entry(name).or_insert_with(|| {
|
|
|
|
ImportResolutionPerNamespace::new(id, is_public)
|
|
|
|
});
|
2015-03-15 16:44:19 -05:00
|
|
|
|
2015-10-26 14:31:11 -05:00
|
|
|
debug!("(resolving glob import) writing resolution `{}` in `{}` to `{}`",
|
2015-07-08 15:55:22 -05:00
|
|
|
name,
|
2015-03-15 16:44:19 -05:00
|
|
|
module_to_string(&*containing_module),
|
|
|
|
module_to_string(module_));
|
|
|
|
|
|
|
|
// Merge the child item into the import resolution.
|
2015-12-13 12:57:07 -06:00
|
|
|
// pub_err makes sure we don't give the same error twice.
|
|
|
|
let mut pub_err = false;
|
2015-03-15 16:44:19 -05:00
|
|
|
{
|
|
|
|
let mut merge_child_item = |namespace| {
|
2015-12-13 12:57:07 -06:00
|
|
|
if !pub_err && is_public &&
|
|
|
|
name_bindings[namespace].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,
|
|
|
|
import_directive.id,
|
|
|
|
import_directive.span,
|
|
|
|
msg);
|
|
|
|
pub_err = true;
|
|
|
|
}
|
2015-04-28 18:36:22 -05:00
|
|
|
|
2015-12-13 12:57:07 -06:00
|
|
|
let modifier = DefModifiers::IMPORTABLE | DefModifiers::PUBLIC;
|
2015-11-15 20:10:09 -06:00
|
|
|
if name_bindings[namespace].defined_with(modifier) {
|
2015-03-15 16:44:19 -05:00
|
|
|
let namespace_name = match namespace {
|
|
|
|
TypeNS => "type",
|
|
|
|
ValueNS => "value",
|
|
|
|
};
|
|
|
|
debug!("(resolving glob import) ... for {} target", namespace_name);
|
|
|
|
if dest_import_resolution.shadowable(namespace) == Shadowable::Never {
|
2015-10-26 14:31:11 -05:00
|
|
|
let msg = format!("a {} named `{}` has already been imported in this \
|
|
|
|
module",
|
2015-03-15 16:44:19 -05:00
|
|
|
namespace_name,
|
2015-07-08 15:55:22 -05:00
|
|
|
name);
|
2015-10-26 14:31:11 -05:00
|
|
|
span_err!(self.resolver.session,
|
|
|
|
import_directive.span,
|
|
|
|
E0251,
|
|
|
|
"{}",
|
|
|
|
msg);
|
2015-03-15 16:44:19 -05:00
|
|
|
} else {
|
2015-12-10 19:38:13 -06:00
|
|
|
dest_import_resolution[namespace] = ImportResolution {
|
2015-12-01 17:06:34 -06:00
|
|
|
target: Some(Target::new(containing_module.clone(),
|
|
|
|
name_bindings[namespace].clone(),
|
|
|
|
import_directive.shadowable)),
|
|
|
|
id: id,
|
|
|
|
is_public: is_public
|
|
|
|
};
|
2015-03-15 16:44:19 -05:00
|
|
|
}
|
2015-12-11 15:21:13 -06:00
|
|
|
} else {
|
|
|
|
// FIXME #30159: This is required for backwards compatability.
|
|
|
|
dest_import_resolution[namespace].is_public |= is_public;
|
2015-03-15 16:44:19 -05:00
|
|
|
}
|
|
|
|
};
|
|
|
|
merge_child_item(ValueNS);
|
|
|
|
merge_child_item(TypeNS);
|
|
|
|
}
|
|
|
|
|
2015-10-26 14:31:11 -05:00
|
|
|
self.check_for_conflicts_between_imports_and_items(module_,
|
|
|
|
dest_import_resolution,
|
|
|
|
import_directive.span,
|
|
|
|
name);
|
2015-03-15 16:44:19 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Checks that imported names and items don't have the same name.
|
|
|
|
fn check_for_conflicting_import(&mut self,
|
2015-12-10 19:38:13 -06:00
|
|
|
import_resolution: &ImportResolutionPerNamespace,
|
2015-03-15 16:44:19 -05:00
|
|
|
import_span: Span,
|
|
|
|
name: Name,
|
|
|
|
namespace: Namespace) {
|
2015-12-01 17:06:34 -06:00
|
|
|
let target = &import_resolution[namespace].target;
|
2015-03-15 16:44:19 -05:00
|
|
|
debug!("check_for_conflicting_import: {}; target exists: {}",
|
2015-07-08 15:55:22 -05:00
|
|
|
name,
|
2015-03-15 16:44:19 -05:00
|
|
|
target.is_some());
|
|
|
|
|
2015-12-01 17:06:34 -06:00
|
|
|
match *target {
|
2015-03-15 16:44:19 -05:00
|
|
|
Some(ref target) if target.shadowable != Shadowable::Always => {
|
2015-04-25 13:35:47 -05:00
|
|
|
let ns_word = match namespace {
|
2015-06-17 19:41:55 -05:00
|
|
|
TypeNS => {
|
2015-11-15 20:10:09 -06:00
|
|
|
match target.binding.module() {
|
2015-11-16 01:59:50 -06:00
|
|
|
Some(ref module) if module.is_normal() => "module",
|
|
|
|
Some(ref module) if module.is_trait() => "trait",
|
2015-11-15 20:10:09 -06:00
|
|
|
_ => "type",
|
2015-10-26 14:31:11 -05:00
|
|
|
}
|
|
|
|
}
|
2015-04-25 13:35:47 -05:00
|
|
|
ValueNS => "value",
|
|
|
|
};
|
2015-10-26 14:31:11 -05:00
|
|
|
span_err!(self.resolver.session,
|
|
|
|
import_span,
|
|
|
|
E0252,
|
|
|
|
"a {} named `{}` has already been imported in this module",
|
|
|
|
ns_word,
|
|
|
|
name);
|
2015-12-01 17:06:34 -06:00
|
|
|
let use_id = import_resolution[namespace].id;
|
2015-04-30 21:46:15 -05:00
|
|
|
let item = self.resolver.ast_map.expect_item(use_id);
|
|
|
|
// item is syntax::ast::Item;
|
2015-10-26 14:31:11 -05:00
|
|
|
span_note!(self.resolver.session,
|
|
|
|
item.span,
|
|
|
|
"previous import of `{}` here",
|
|
|
|
name);
|
2015-03-15 16:44:19 -05:00
|
|
|
}
|
|
|
|
Some(_) | None => {}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Checks that an import is actually importable
|
|
|
|
fn check_that_import_is_importable(&mut self,
|
2015-11-15 20:10:09 -06:00
|
|
|
name_binding: &NameBinding,
|
2015-03-15 16:44:19 -05:00
|
|
|
import_span: Span,
|
2015-11-15 20:10:09 -06:00
|
|
|
name: Name) {
|
|
|
|
if !name_binding.defined_with(DefModifiers::IMPORTABLE) {
|
2015-10-26 14:31:11 -05:00
|
|
|
let msg = format!("`{}` is not directly importable", name);
|
2015-03-15 16:44:19 -05:00
|
|
|
span_err!(self.resolver.session, import_span, E0253, "{}", &msg[..]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Checks that imported names and items don't have the same name.
|
|
|
|
fn check_for_conflicts_between_imports_and_items(&mut self,
|
|
|
|
module: &Module,
|
2015-12-10 19:38:13 -06:00
|
|
|
import: &ImportResolutionPerNamespace,
|
2015-03-15 16:44:19 -05:00
|
|
|
import_span: Span,
|
|
|
|
name: Name) {
|
|
|
|
// First, check for conflicts between imports and `extern crate`s.
|
|
|
|
if module.external_module_children
|
|
|
|
.borrow()
|
|
|
|
.contains_key(&name) {
|
2015-12-10 19:38:13 -06:00
|
|
|
match import.type_ns.target {
|
2015-03-15 16:44:19 -05:00
|
|
|
Some(ref target) if target.shadowable != Shadowable::Always => {
|
2015-10-26 14:31:11 -05:00
|
|
|
let msg = format!("import `{0}` conflicts with imported crate in this module \
|
2015-03-15 16:44:19 -05:00
|
|
|
(maybe you meant `use {0}::*`?)",
|
2015-07-08 15:55:22 -05:00
|
|
|
name);
|
2015-03-15 16:44:19 -05:00
|
|
|
span_err!(self.resolver.session, import_span, E0254, "{}", &msg[..]);
|
|
|
|
}
|
|
|
|
Some(_) | None => {}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check for item conflicts.
|
|
|
|
let children = module.children.borrow();
|
|
|
|
let name_bindings = match children.get(&name) {
|
|
|
|
None => {
|
|
|
|
// There can't be any conflicts.
|
2015-10-26 14:31:11 -05:00
|
|
|
return;
|
2015-03-15 16:44:19 -05:00
|
|
|
}
|
|
|
|
Some(ref name_bindings) => (*name_bindings).clone(),
|
|
|
|
};
|
|
|
|
|
2015-12-10 19:38:13 -06:00
|
|
|
match import.value_ns.target {
|
2015-03-15 16:44:19 -05:00
|
|
|
Some(ref target) if target.shadowable != Shadowable::Always => {
|
2015-11-15 20:10:09 -06:00
|
|
|
if let Some(ref value) = *name_bindings.value_ns.borrow() {
|
2015-10-26 14:31:11 -05:00
|
|
|
span_err!(self.resolver.session,
|
|
|
|
import_span,
|
|
|
|
E0255,
|
2015-03-15 16:44:19 -05:00
|
|
|
"import `{}` conflicts with value in this module",
|
2015-07-08 15:55:22 -05:00
|
|
|
name);
|
2015-11-15 20:10:09 -06:00
|
|
|
if let Some(span) = value.span {
|
2015-03-15 16:44:19 -05:00
|
|
|
self.resolver.session.span_note(span, "conflicting value here");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Some(_) | None => {}
|
|
|
|
}
|
|
|
|
|
2015-12-10 19:38:13 -06:00
|
|
|
match import.type_ns.target {
|
2015-03-15 16:44:19 -05:00
|
|
|
Some(ref target) if target.shadowable != Shadowable::Always => {
|
2015-11-15 20:10:09 -06:00
|
|
|
if let Some(ref ty) = *name_bindings.type_ns.borrow() {
|
2015-11-17 03:10:41 -06:00
|
|
|
let (what, note) = match ty.module() {
|
2015-11-16 01:59:50 -06:00
|
|
|
Some(ref module) if module.is_normal() =>
|
2015-10-26 14:31:11 -05:00
|
|
|
("existing submodule", "note conflicting module here"),
|
2015-11-16 01:59:50 -06:00
|
|
|
Some(ref module) if module.is_trait() =>
|
2015-10-26 14:31:11 -05:00
|
|
|
("trait in this module", "note conflicting trait here"),
|
|
|
|
_ => ("type in this module", "note conflicting type here"),
|
2015-03-15 16:44:19 -05:00
|
|
|
};
|
2015-10-26 14:31:11 -05:00
|
|
|
span_err!(self.resolver.session,
|
|
|
|
import_span,
|
|
|
|
E0256,
|
2015-03-15 16:44:19 -05:00
|
|
|
"import `{}` conflicts with {}",
|
2015-10-26 14:31:11 -05:00
|
|
|
name,
|
|
|
|
what);
|
2015-11-15 20:10:09 -06:00
|
|
|
if let Some(span) = ty.span {
|
2015-03-15 16:44:19 -05:00
|
|
|
self.resolver.session.span_note(span, note);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Some(_) | None => {}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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();
|
|
|
|
}
|