// option. This file may not be copied, modified, or distributed
// except according to those terms.
+use self::Determinacy::*;
use self::ImportDirectiveSubclass::*;
use Module;
use syntax::ast::{NodeId, Name};
use syntax::util::lev_distance::find_best_match_for_name;
-use syntax_pos::{Span, DUMMY_SP};
+use syntax_pos::Span;
use std::cell::{Cell, RefCell};
}
}
+#[derive(Copy, Clone, Debug)]
+pub enum Determinacy {
+ Determined,
+ Undetermined,
+}
+
/// Contains data for specific types of import directives.
#[derive(Clone, Debug)]
-pub enum ImportDirectiveSubclass {
+pub enum ImportDirectiveSubclass<'a> {
SingleImport {
target: Name,
source: Name,
- type_determined: Cell<bool>,
- value_determined: Cell<bool>,
+ value_result: Cell<Result<&'a NameBinding<'a>, Determinacy>>,
+ type_result: Cell<Result<&'a NameBinding<'a>, Determinacy>>,
+ },
+ GlobImport {
+ is_prelude: bool,
+ max_vis: Cell<ty::Visibility>, // The visibility of the greatest reexport.
+ // n.b. `max_vis` is only used in `finalize_import` to check for reexport errors.
},
- GlobImport { is_prelude: bool },
}
-impl ImportDirectiveSubclass {
+impl<'a> ImportDirectiveSubclass<'a> {
pub fn single(target: Name, source: Name) -> Self {
SingleImport {
target: target,
source: source,
- type_determined: Cell::new(false),
- value_determined: Cell::new(false),
+ type_result: Cell::new(Err(Undetermined)),
+ value_result: Cell::new(Err(Undetermined)),
}
}
}
#[derive(Debug,Clone)]
pub struct ImportDirective<'a> {
pub id: NodeId,
+ parent: Module<'a>,
module_path: Vec<Name>,
- target_module: Cell<Option<Module<'a>>>, // the resolution of `module_path`
- subclass: ImportDirectiveSubclass,
+ imported_module: Cell<Option<Module<'a>>>, // the resolution of `module_path`
+ subclass: ImportDirectiveSubclass<'a>,
span: Span,
- vis: ty::Visibility, // see note in ImportResolutionPerNamespace about how to use this
+ vis: Cell<ty::Visibility>,
}
impl<'a> ImportDirective<'a> {
_ => None, // The binding could be shadowed by a single import, so it is not known.
})
}
+}
+
+impl<'a> Resolver<'a> {
+ fn resolution(&self, module: Module<'a>, name: Name, ns: Namespace)
+ -> &'a RefCell<NameResolution<'a>> {
+ *module.resolutions.borrow_mut().entry((name, ns))
+ .or_insert_with(|| self.arenas.alloc_name_resolution())
+ }
+
+ /// Attempts to resolve the supplied name in the given module for the given namespace.
+ /// If successful, returns the binding corresponding to the name.
+ /// Invariant: if `record_used` is `Some`, import resolution must be complete.
+ pub fn resolve_name_in_module(&mut self,
+ module: Module<'a>,
+ name: Name,
+ ns: Namespace,
+ allow_private_imports: bool,
+ record_used: Option<Span>)
+ -> ResolveResult<&'a NameBinding<'a>> {
+ self.populate_module_if_necessary(module);
+
+ let resolution = self.resolution(module, name, ns);
+ let resolution = match resolution.borrow_state() {
+ ::std::cell::BorrowState::Unused => resolution.borrow_mut(),
+ _ => return Failed(None), // This happens when there is a cycle of imports
+ };
+
+ let new_import_semantics = self.new_import_semantics;
+ let is_disallowed_private_import = |binding: &NameBinding| {
+ !new_import_semantics && !allow_private_imports && // disallowed
+ binding.vis != ty::Visibility::Public && binding.is_import() // non-`pub` import
+ };
+
+ if let Some(span) = record_used {
+ if let Some(binding) = resolution.binding {
+ if is_disallowed_private_import(binding) {
+ return Failed(None);
+ }
+ if self.record_use(name, ns, binding, span) {
+ return Success(self.dummy_binding);
+ }
+ if !self.is_accessible(binding.vis) {
+ self.privacy_errors.push(PrivacyError(span, name, binding));
+ }
+ }
+
+ return resolution.binding.map(Success).unwrap_or(Failed(None));
+ }
+
+ // If the resolution doesn't depend on glob definability, check privacy and return.
+ if let Some(result) = self.try_result(&resolution, ns) {
+ return result.and_then(|binding| {
+ if self.is_accessible(binding.vis) && !is_disallowed_private_import(binding) {
+ Success(binding)
+ } else {
+ Failed(None)
+ }
+ });
+ }
+
+ // Check if the globs are determined
+ for directive in module.globs.borrow().iter() {
+ if self.is_accessible(directive.vis.get()) {
+ if let Some(module) = directive.imported_module.get() {
+ let result = self.resolve_name_in_module(module, name, ns, true, None);
+ if let Indeterminate = result {
+ return Indeterminate;
+ }
+ } else {
+ return Indeterminate;
+ }
+ }
+ }
+
+ Failed(None)
+ }
// Returns Some(the resolution of the name), or None if the resolution depends
// on whether more globs can define the name.
- fn try_result(&self, ns: Namespace, allow_private_imports: bool)
+ fn try_result(&mut self, resolution: &NameResolution<'a>, ns: Namespace)
-> Option<ResolveResult<&'a NameBinding<'a>>> {
- match self.binding {
+ match resolution.binding {
Some(binding) if !binding.is_glob_import() =>
- return Some(Success(binding)),
- _ => {} // Items and single imports are not shadowable
+ return Some(Success(binding)), // Items and single imports are not shadowable.
+ _ => {}
};
// Check if a single import can still define the name.
- match self.single_imports {
- SingleImports::None => {},
+ match resolution.single_imports {
SingleImports::AtLeastOne => return Some(Indeterminate),
- SingleImports::MaybeOne(directive) => {
- // 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 whether more globs can define the name.
- if !allow_private_imports && directive.vis != ty::Visibility::Public &&
- !self.binding.map(NameBinding::is_pseudo_public).unwrap_or(false) {
- return None;
- }
-
- let target_module = match directive.target_module.get() {
- Some(target_module) => target_module,
+ SingleImports::MaybeOne(directive) if self.is_accessible(directive.vis.get()) => {
+ let module = match directive.imported_module.get() {
+ Some(module) => module,
None => return Some(Indeterminate),
};
let name = match directive.subclass {
SingleImport { source, .. } => source,
GlobImport { .. } => unreachable!(),
};
- match target_module.resolve_name(name, ns, false) {
+ match self.resolve_name_in_module(module, name, ns, true, None) {
Failed(_) => {}
_ => return Some(Indeterminate),
}
}
+ SingleImports::MaybeOne(_) | SingleImports::None => {},
}
- self.binding.map(Success)
- }
-}
-
-impl<'a> ::ModuleS<'a> {
- fn resolution(&self, name: Name, ns: Namespace) -> &'a RefCell<NameResolution<'a>> {
- *self.resolutions.borrow_mut().entry((name, ns))
- .or_insert_with(|| self.arenas.alloc_name_resolution())
+ resolution.binding.map(Success)
}
- pub fn resolve_name(&self, name: Name, ns: Namespace, allow_private_imports: bool)
- -> ResolveResult<&'a NameBinding<'a>> {
- let resolution = self.resolution(name, ns);
- let resolution = match resolution.borrow_state() {
- ::std::cell::BorrowState::Unused => resolution.borrow_mut(),
- _ => return Failed(None), // This happens when there is a cycle of imports
- };
-
- if let Some(result) = resolution.try_result(ns, allow_private_imports) {
- // 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_pseudo_public();
- if allowed { Success(binding) } else { Failed(None) }
- });
- }
-
- // Check if the globs are determined
- for directive in self.globs.borrow().iter() {
- if !allow_private_imports && directive.vis != ty::Visibility::Public { continue }
- match directive.target_module.get() {
- None => return Indeterminate,
- Some(target_module) => match target_module.resolve_name(name, ns, false) {
- Indeterminate => return Indeterminate,
- _ => {}
- }
- }
- }
-
- Failed(None)
- }
-
- pub fn add_import_directive(&self,
+ // Add an import directive to the current module.
+ pub fn add_import_directive(&mut self,
module_path: Vec<Name>,
- subclass: ImportDirectiveSubclass,
+ subclass: ImportDirectiveSubclass<'a>,
span: Span,
id: NodeId,
vis: ty::Visibility) {
+ let current_module = self.current_module;
let directive = self.arenas.alloc_import_directive(ImportDirective {
+ parent: current_module,
module_path: module_path,
- target_module: Cell::new(None),
+ imported_module: Cell::new(None),
subclass: subclass,
span: span,
id: id,
- vis: vis,
+ vis: Cell::new(vis),
});
- self.unresolved_imports.borrow_mut().push(directive);
+ self.indeterminate_imports.push(directive);
match directive.subclass {
SingleImport { target, .. } => {
for &ns in &[ValueNS, TypeNS] {
- self.resolution(target, ns).borrow_mut().single_imports
- .add_directive(directive);
+ let mut resolution = self.resolution(current_module, target, ns).borrow_mut();
+ resolution.single_imports.add_directive(directive);
}
}
// We don't add prelude imports to the globs since they only affect lexical scopes,
// which are not relevant to import resolution.
- GlobImport { is_prelude: true } => {}
- GlobImport { .. } => self.globs.borrow_mut().push(directive),
+ GlobImport { is_prelude: true, .. } => {}
+ GlobImport { .. } => self.current_module.globs.borrow_mut().push(directive),
}
}
-}
-impl<'a> Resolver<'a> {
// Given a binding and an import directive that resolves to it,
// return the corresponding binding defined by the import directive.
fn import(&mut self, binding: &'a NameBinding<'a>, directive: &'a ImportDirective<'a>)
-> NameBinding<'a> {
+ let vis = if binding.pseudo_vis().is_at_least(directive.vis.get(), self) ||
+ !directive.is_glob() && binding.is_extern_crate() { // c.f. `PRIVATE_IN_PUBLIC`
+ directive.vis.get()
+ } else {
+ binding.pseudo_vis()
+ };
+
+ if let GlobImport { ref max_vis, .. } = directive.subclass {
+ if vis == directive.vis.get() || vis.is_at_least(max_vis.get(), self) {
+ max_vis.set(vis)
+ }
+ }
+
NameBinding {
kind: NameBindingKind::Import {
binding: binding,
directive: directive,
},
span: directive.span,
- vis: directive.vis,
+ vis: vis,
}
}
where T: ToNameBinding<'a>
{
let binding = self.arenas.alloc_name_binding(binding.to_name_binding());
- self.update_resolution(module, name, ns, |_, resolution| {
+ self.update_resolution(module, name, ns, |this, resolution| {
if let Some(old_binding) = resolution.binding {
if binding.is_glob_import() {
- resolution.duplicate_globs.push(binding);
+ if !this.new_import_semantics || !old_binding.is_glob_import() {
+ resolution.duplicate_globs.push(binding);
+ } else if binding.def() != old_binding.def() {
+ resolution.binding = Some(this.arenas.alloc_name_binding(NameBinding {
+ kind: NameBindingKind::Ambiguity {
+ b1: old_binding,
+ b2: binding,
+ },
+ vis: if old_binding.vis.is_at_least(binding.vis, this) {
+ old_binding.vis
+ } else {
+ binding.vis
+ },
+ span: old_binding.span,
+ }));
+ } else if !old_binding.vis.is_at_least(binding.vis, this) {
+ // We are glob-importing the same item but with greater visibility.
+ resolution.binding = Some(binding);
+ }
} else if old_binding.is_glob_import() {
resolution.duplicate_globs.push(old_binding);
resolution.binding = Some(binding);
{
// Ensure that `resolution` isn't borrowed when defining in the module's glob importers,
// during which the resolution might end up getting re-defined via a glob cycle.
- let (new_binding, t) = {
- let mut resolution = &mut *module.resolution(name, ns).borrow_mut();
- let was_known = resolution.binding().is_some();
+ let (binding, t) = {
+ let mut resolution = &mut *self.resolution(module, name, ns).borrow_mut();
+ let old_binding = resolution.binding();
let t = f(self, resolution);
- if was_known { return t; }
match resolution.binding() {
- Some(binding) => (binding, t),
+ _ if !self.new_import_semantics && old_binding.is_some() => return t,
None => return t,
+ Some(binding) => match old_binding {
+ Some(old_binding) if old_binding as *const _ == binding as *const _ => return t,
+ _ => (binding, t),
+ }
}
};
- // Define `new_binding` in `module`s glob importers.
- if new_binding.is_importable() && new_binding.is_pseudo_public() {
- for &(importer, directive) in module.glob_importers.borrow_mut().iter() {
- let imported_binding = self.import(new_binding, directive);
- let _ = self.try_define(importer, name, ns, imported_binding);
+ // Define `binding` in `module`s glob importers.
+ for directive in module.glob_importers.borrow_mut().iter() {
+ if match self.new_import_semantics {
+ true => self.is_accessible_from(binding.vis, directive.parent),
+ false => binding.vis == ty::Visibility::Public,
+ } {
+ let imported_binding = self.import(binding, directive);
+ let _ = self.try_define(directive.parent, name, ns, imported_binding);
}
}
}
}
-struct ImportResolvingError<'a> {
- /// Module where the error happened
- source_module: Module<'a>,
- import_directive: &'a ImportDirective<'a>,
- span: Span,
- help: String,
-}
-
struct ImportResolver<'a, 'b: 'a> {
resolver: &'a mut Resolver<'b>,
}
/// point iteration.
fn resolve_imports(&mut self) {
let mut i = 0;
- let mut prev_unresolved_imports = 0;
- let mut errors = Vec::new();
+ let mut prev_num_indeterminates = self.indeterminate_imports.len() + 1;
- loop {
- debug!("(resolving imports) iteration {}, {} imports left", i, self.unresolved_imports);
+ while self.indeterminate_imports.len() < prev_num_indeterminates {
+ prev_num_indeterminates = self.indeterminate_imports.len();
+ debug!("(resolving imports) iteration {}, {} imports left", i, prev_num_indeterminates);
- // Attempt to resolve imports in all local modules.
- for module in self.arenas.local_modules().iter() {
- self.current_module = module;
- self.resolve_imports_in_current_module(&mut errors);
- }
+ let mut imports = Vec::new();
+ ::std::mem::swap(&mut imports, &mut self.indeterminate_imports);
- if self.unresolved_imports == 0 {
- debug!("(resolving imports) success");
- for module in self.arenas.local_modules().iter() {
- self.finalize_resolutions_in(module, false);
+ for import in imports {
+ match self.resolve_import(&import) {
+ Failed(_) => self.determined_imports.push(import),
+ Indeterminate => self.indeterminate_imports.push(import),
+ Success(()) => self.determined_imports.push(import),
}
- break;
}
- if self.unresolved_imports == prev_unresolved_imports {
- // resolving failed
- // 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.
- for module in self.arenas.local_modules().iter() {
- self.finalize_resolutions_in(module, errors.len() == 0);
- }
- for e in errors {
- self.import_resolving_error(e)
- }
- break;
+ i += 1;
+ }
+
+ for module in self.arenas.local_modules().iter() {
+ self.finalize_resolutions_in(module);
+ }
+
+ let mut errors = false;
+ for i in 0 .. self.determined_imports.len() {
+ let import = self.determined_imports[i];
+ if let Failed(err) = self.finalize_import(import) {
+ errors = true;
+ let (span, help) = match err {
+ Some((span, msg)) => (span, msg),
+ None => continue,
+ };
+
+ // If the error is a single failed import then create a "fake" import
+ // resolution for it so that later resolve stages won't complain.
+ self.import_dummy_binding(import);
+ let path = import_path_to_string(&import.module_path, &import.subclass);
+ let error = ResolutionError::UnresolvedImport(Some((&path, &help)));
+ resolve_error(self.resolver, span, error);
}
+ }
- i += 1;
- prev_unresolved_imports = self.unresolved_imports;
+ // Report unresolved imports only if no hard error was already reported
+ // to avoid generating multiple errors on the same import.
+ if !errors {
+ if let Some(import) = self.indeterminate_imports.iter().next() {
+ let error = ResolutionError::UnresolvedImport(None);
+ resolve_error(self.resolver, import.span, error);
+ }
}
}
// Define a "dummy" resolution containing a Def::Err as a placeholder for a
// failed resolution
- fn import_dummy_binding(&mut self,
- source_module: Module<'b>,
- directive: &'b ImportDirective<'b>) {
+ fn import_dummy_binding(&mut self, directive: &'b ImportDirective<'b>) {
if let SingleImport { target, .. } = directive.subclass {
- let dummy_binding = self.arenas.alloc_name_binding(NameBinding {
- kind: NameBindingKind::Def(Def::Err),
- span: DUMMY_SP,
- vis: ty::Visibility::Public,
- });
+ let dummy_binding = self.dummy_binding;
let dummy_binding = self.import(dummy_binding, directive);
-
- let _ = self.try_define(source_module, target, ValueNS, dummy_binding.clone());
- let _ = self.try_define(source_module, target, TypeNS, dummy_binding);
- }
- }
-
- /// Resolves an `ImportResolvingError` into the correct enum discriminant
- /// and passes that on to `resolve_error`.
- fn import_resolving_error(&mut self, e: ImportResolvingError<'b>) {
- // If the error is a single failed import then create a "fake" import
- // resolution for it so that later resolve stages won't complain.
- self.import_dummy_binding(e.source_module, e.import_directive);
- 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))));
- }
-
- /// Attempts to resolve imports for the given module only.
- fn resolve_imports_in_current_module(&mut self, errors: &mut Vec<ImportResolvingError<'b>>) {
- let mut imports = Vec::new();
- let mut unresolved_imports = self.current_module.unresolved_imports.borrow_mut();
- ::std::mem::swap(&mut imports, &mut unresolved_imports);
-
- for import_directive in imports {
- match self.resolve_import(&import_directive) {
- Failed(err) => {
- let (span, help) = match err {
- Some((span, msg)) => (span, format!(". {}", msg)),
- None => (import_directive.span, String::new()),
- };
- errors.push(ImportResolvingError {
- source_module: self.current_module,
- import_directive: import_directive,
- span: span,
- help: help,
- });
- }
- Indeterminate => unresolved_imports.push(import_directive),
- Success(()) => {
- // Decrement the count of unresolved imports.
- assert!(self.unresolved_imports >= 1);
- self.unresolved_imports -= 1;
- }
- }
+ let _ = self.try_define(directive.parent, target, ValueNS, dummy_binding.clone());
+ let _ = self.try_define(directive.parent, target, TypeNS, dummy_binding);
}
}
names_to_string(&directive.module_path),
module_to_string(self.current_module));
- let target_module = match directive.target_module.get() {
- Some(module) => module,
- _ => match self.resolve_module_path(&directive.module_path,
- DontUseLexicalScope,
- directive.span) {
+ self.current_module = directive.parent;
+
+ let module = if let Some(module) = directive.imported_module.get() {
+ module
+ } else {
+ let vis = directive.vis.get();
+ // For better failure detection, pretend that the import will not define any names
+ // while resolving its module path.
+ directive.vis.set(ty::Visibility::PrivateExternal);
+ let result =
+ self.resolve_module_path(&directive.module_path, DontUseLexicalScope, None);
+ directive.vis.set(vis);
+
+ match result {
Success(module) => module,
Indeterminate => return Indeterminate,
Failed(err) => return Failed(err),
- },
+ }
};
- directive.target_module.set(Some(target_module));
- 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),
- GlobImport { .. } => return self.resolve_glob_import(target_module, directive),
+ directive.imported_module.set(Some(module));
+ let (source, target, value_result, type_result) = match directive.subclass {
+ SingleImport { source, target, ref value_result, ref type_result } =>
+ (source, target, value_result, type_result),
+ GlobImport { .. } => {
+ self.resolve_glob_import(directive);
+ return Success(());
+ }
};
- // We need to resolve both namespaces for this to succeed.
- let value_result = self.resolve_name_in_module(target_module, source, ValueNS, false, true);
- let type_result = self.resolve_name_in_module(target_module, source, TypeNS, false, true);
-
- let module = self.current_module;
- let mut privacy_error = true;
- for &(ns, result, determined) in &[(ValueNS, &value_result, value_determined),
- (TypeNS, &type_result, type_determined)] {
- match *result {
- Failed(..) if !determined.get() => {
- determined.set(true);
- self.update_resolution(module, target, ns, |_, resolution| {
+ let mut indeterminate = false;
+ for &(ns, result) in &[(ValueNS, value_result), (TypeNS, type_result)] {
+ if let Err(Undetermined) = result.get() {
+ result.set({
+ match self.resolve_name_in_module(module, source, ns, false, None) {
+ Success(binding) => Ok(binding),
+ Indeterminate => Err(Undetermined),
+ Failed(_) => Err(Determined),
+ }
+ });
+ } else {
+ continue
+ };
+
+ match result.get() {
+ Err(Undetermined) => indeterminate = true,
+ Err(Determined) => {
+ self.update_resolution(directive.parent, target, ns, |_, resolution| {
resolution.single_imports.directive_failed()
});
}
- Success(binding) if !binding.is_importable() => {
+ Ok(binding) if !binding.is_importable() => {
let msg = format!("`{}` is not directly importable", target);
struct_span_err!(self.session, directive.span, E0253, "{}", &msg)
.span_label(directive.span, &format!("cannot be imported directly"))
.emit();
// Do not import this illegal binding. Import a dummy binding and pretend
// everything is fine
- self.import_dummy_binding(module, directive);
+ self.import_dummy_binding(directive);
return Success(());
}
- Success(binding) if !self.is_accessible(binding.vis) => {}
- Success(binding) if !determined.get() => {
- determined.set(true);
+ Ok(binding) => {
let imported_binding = self.import(binding, directive);
- let conflict = self.try_define(module, target, ns, imported_binding);
+ let conflict = self.try_define(directive.parent, target, ns, imported_binding);
if let Err(old_binding) = conflict {
let binding = &self.import(binding, directive);
- self.report_conflict(module, target, ns, binding, old_binding);
+ self.report_conflict(directive.parent, target, ns, binding, old_binding);
}
- privacy_error = false;
}
- Success(_) => privacy_error = false,
- _ => {}
}
}
- match (&value_result, &type_result) {
- (&Indeterminate, _) | (_, &Indeterminate) => return Indeterminate,
- (&Failed(_), &Failed(_)) => {
- let resolutions = target_module.resolutions.borrow();
- let names = resolutions.iter().filter_map(|(&(ref name, _), resolution)| {
- if *name == source { return None; } // Never suggest the same name
+ if indeterminate { Indeterminate } else { Success(()) }
+ }
+
+ fn finalize_import(&mut self, directive: &'b ImportDirective<'b>) -> ResolveResult<()> {
+ self.current_module = directive.parent;
+
+ let ImportDirective { ref module_path, span, .. } = *directive;
+ let module_result = self.resolve_module_path(&module_path, DontUseLexicalScope, Some(span));
+ let module = match module_result {
+ Success(module) => module,
+ Indeterminate => return Indeterminate,
+ Failed(err) => return Failed(err),
+ };
+
+ let (name, value_result, type_result) = match directive.subclass {
+ SingleImport { source, ref value_result, ref type_result, .. } =>
+ (source, value_result.get(), type_result.get()),
+ GlobImport { .. } if module.def_id() == directive.parent.def_id() => {
+ // Importing a module into itself is not allowed.
+ let msg = "Cannot glob-import a module into itself.".into();
+ return Failed(Some((directive.span, msg)));
+ }
+ GlobImport { is_prelude, ref max_vis } => {
+ if !is_prelude &&
+ max_vis.get() != ty::Visibility::PrivateExternal && // Allow empty globs.
+ !max_vis.get().is_at_least(directive.vis.get(), self) {
+ let msg = "A non-empty glob must import something with the glob's visibility";
+ self.session.span_err(directive.span, msg);
+ }
+ return Success(());
+ }
+ };
+
+ for &(ns, result) in &[(ValueNS, value_result), (TypeNS, type_result)] {
+ if let Ok(binding) = result {
+ if self.record_use(name, ns, binding, directive.span) {
+ self.resolution(module, name, ns).borrow_mut().binding =
+ Some(self.dummy_binding);
+ }
+ }
+ }
+
+ if value_result.is_err() && type_result.is_err() {
+ let (value_result, type_result);
+ value_result = self.resolve_name_in_module(module, name, ValueNS, false, Some(span));
+ type_result = self.resolve_name_in_module(module, name, TypeNS, false, Some(span));
+
+ return if let (Failed(_), Failed(_)) = (value_result, type_result) {
+ let resolutions = module.resolutions.borrow();
+ let names = resolutions.iter().filter_map(|(&(ref n, _), resolution)| {
+ if *n == name { return None; } // Never suggest the same name
match *resolution.borrow() {
- NameResolution { binding: Some(_), .. } => Some(name),
+ NameResolution { binding: Some(_), .. } => Some(n),
NameResolution { single_imports: SingleImports::None, .. } => None,
- _ => Some(name),
+ _ => Some(n),
}
});
- let lev_suggestion = match find_best_match_for_name(names, &source.as_str(), None) {
+ let lev_suggestion = match find_best_match_for_name(names, &name.as_str(), None) {
Some(name) => format!(". Did you mean to use `{}`?", name),
None => "".to_owned(),
};
- let module_str = module_to_string(target_module);
+ let module_str = module_to_string(module);
let msg = if &module_str == "???" {
- format!("There is no `{}` in the crate root{}", source, lev_suggestion)
+ format!("no `{}` in the root{}", name, lev_suggestion)
} else {
- format!("There is no `{}` in `{}`{}", source, module_str, lev_suggestion)
+ format!("no `{}` in `{}`{}", name, module_str, lev_suggestion)
};
- return Failed(Some((directive.span, msg)));
+ Failed(Some((directive.span, msg)))
+ } else {
+ // `resolve_name_in_module` reported a privacy error.
+ self.import_dummy_binding(directive);
+ Success(())
}
- _ => (),
}
- if privacy_error {
- for &(ns, result) in &[(ValueNS, &value_result), (TypeNS, &type_result)] {
- let binding = match *result { Success(binding) => binding, _ => continue };
- self.privacy_errors.push(PrivacyError(directive.span, source, binding));
- let imported_binding = self.import(binding, directive);
- let _ = self.try_define(module, target, ns, imported_binding);
+ let session = self.session;
+ let reexport_error = || {
+ let msg = format!("`{}` is private, and cannot be reexported", name);
+ let note_msg =
+ format!("consider marking `{}` as `pub` in the imported module", name);
+ struct_span_err!(session, directive.span, E0364, "{}", &msg)
+ .span_note(directive.span, ¬e_msg)
+ .emit();
+ };
+
+ let extern_crate_lint = || {
+ let msg = format!("extern crate `{}` is private, and cannot be reexported \
+ (error E0364), consider declaring with `pub`",
+ name);
+ session.add_lint(PRIVATE_IN_PUBLIC, directive.id, directive.span, msg);
+ };
+
+ match (value_result, type_result) {
+ // With `#![feature(item_like_imports)]`, all namespaces
+ // must be re-exported with extra visibility for an error to occur.
+ (Ok(value_binding), Ok(type_binding)) if self.new_import_semantics => {
+ let vis = directive.vis.get();
+ if !value_binding.pseudo_vis().is_at_least(vis, self) &&
+ !type_binding.pseudo_vis().is_at_least(vis, self) {
+ reexport_error();
+ } else if type_binding.is_extern_crate() &&
+ !type_binding.vis.is_at_least(vis, self) {
+ extern_crate_lint();
+ }
}
- }
- match (&value_result, &type_result) {
- (&Success(binding), _) if !binding.pseudo_vis().is_at_least(directive.vis, self) &&
- self.is_accessible(binding.vis) => {
- 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.session, directive.span, E0364, "{}", &msg)
- .span_note(directive.span, ¬e_msg)
- .emit();
+ (Ok(binding), _) if !binding.pseudo_vis().is_at_least(directive.vis.get(), self) => {
+ reexport_error();
}
- (_, &Success(binding)) if !binding.pseudo_vis().is_at_least(directive.vis, self) &&
- self.is_accessible(binding.vis) => {
+ (_, Ok(binding)) if !binding.pseudo_vis().is_at_least(directive.vis.get(), self) => {
if binding.is_extern_crate() {
- let msg = format!("extern crate `{}` is private, and cannot be reexported \
- (error E0364), consider declaring with `pub`",
- source);
- self.session.add_lint(PRIVATE_IN_PUBLIC, directive.id, directive.span, msg);
+ extern_crate_lint();
} else {
- let mut err = struct_span_err!(self.session, directive.span, E0365,
- "`{}` is private, and cannot be reexported",
- source);
- err.span_label(directive.span, &format!("reexport of private `{}`", source));
- err.note(&format!("consider declaring type or module `{}` with `pub`", source));
- err.emit();
+ struct_span_err!(self.session, directive.span, E0365,
+ "`{}` is private, and cannot be reexported", name)
+ .span_label(directive.span, &format!("reexport of private `{}`", name))
+ .note(&format!("consider declaring type or module `{}` with `pub`", name))
+ .emit();
}
}
// 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.
- let def = match type_result.success().and_then(NameBinding::def) {
+ let def = match type_result.ok().map(NameBinding::def) {
Some(def) => def,
- None => value_result.success().and_then(NameBinding::def).unwrap(),
+ None => value_result.ok().map(NameBinding::def).unwrap(),
};
let path_resolution = PathResolution::new(def);
self.def_map.insert(directive.id, path_resolution);
return 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, target_module: Module<'b>, directive: &'b ImportDirective<'b>)
- -> ResolveResult<()> {
- if let Some(Def::Trait(_)) = target_module.def {
- self.session.span_err(directive.span, "items in traits are not importable.");
- }
-
- let module = self.current_module;
- 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)));
- }
- self.populate_module_if_necessary(target_module);
+ fn resolve_glob_import(&mut self, directive: &'b ImportDirective<'b>) {
+ let module = directive.imported_module.get().unwrap();
+ self.populate_module_if_necessary(module);
- if let GlobImport { is_prelude: true } = directive.subclass {
- self.prelude = Some(target_module);
- return Success(());
+ if let Some(Def::Trait(_)) = module.def {
+ self.session.span_err(directive.span, "items in traits are not importable.");
+ return;
+ } else if module.def_id() == directive.parent.def_id() {
+ return;
+ } else if let GlobImport { is_prelude: true, .. } = directive.subclass {
+ self.prelude = Some(module);
+ return;
}
- // Add to target_module's glob_importers
- target_module.glob_importers.borrow_mut().push((module, directive));
+ // Add to module's glob_importers
+ module.glob_importers.borrow_mut().push(directive);
// Ensure that `resolutions` isn't borrowed during `try_define`,
// since it might get updated via a glob cycle.
- let bindings = target_module.resolutions.borrow().iter().filter_map(|(name, resolution)| {
+ let bindings = module.resolutions.borrow().iter().filter_map(|(name, resolution)| {
resolution.borrow().binding().map(|binding| (*name, binding))
}).collect::<Vec<_>>();
for ((name, ns), binding) in bindings {
- if binding.is_importable() && binding.is_pseudo_public() {
+ if binding.pseudo_vis() == ty::Visibility::Public ||
+ self.new_import_semantics && self.is_accessible(binding.vis) {
let imported_binding = self.import(binding, directive);
- let _ = self.try_define(module, name, ns, imported_binding);
+ let _ = self.try_define(directive.parent, name, ns, imported_binding);
}
}
// Record the destination of this import
- if let Some(did) = target_module.def_id() {
+ if let Some(did) = module.def_id() {
let resolution = PathResolution::new(Def::Mod(did));
self.def_map.insert(directive.id, resolution);
}
-
- debug!("(resolving glob import) successfully resolved import");
- return Success(());
}
// Miscellaneous post-processing, including recording reexports, reporting conflicts,
// reporting the PRIVATE_IN_PUBLIC lint, and reporting unresolved imports.
- fn finalize_resolutions_in(&mut self, module: Module<'b>, report_unresolved_imports: bool) {
+ fn finalize_resolutions_in(&mut self, module: Module<'b>) {
// Since import resolution is finished, globs will not define any more names.
*module.globs.borrow_mut() = Vec::new();
};
// Report conflicts
- for duplicate_glob in resolution.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 }
+ if !self.new_import_semantics {
+ for duplicate_glob in resolution.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 }
+ }
}
- }
- self.report_conflict(module, name, ns, duplicate_glob, binding);
+ self.report_conflict(module, name, ns, duplicate_glob, binding);
+ }
}
if binding.vis == ty::Visibility::Public &&
(binding.is_import() || binding.is_extern_crate()) {
- if let Some(def) = binding.def() {
+ let def = binding.def();
+ if def != Def::Err {
reexports.push(Export { name: name, def_id: def.def_id() });
}
}
self.export_map.insert(node_id, reexports);
}
}
-
- if report_unresolved_imports {
- for import in module.unresolved_imports.borrow().iter() {
- resolve_error(self.resolver, import.span, ResolutionError::UnresolvedImport(None));
- break;
- }
- }
}
}