//! Here we build the "reduced graph": the graph of the module tree without
//! any imports resolved.
-use DefModifiers;
use resolve_imports::ImportDirectiveSubclass::{self, GlobImport};
use Module;
use Namespace::{self, TypeNS, ValueNS};
}
}
-impl<'a> ToNameBinding<'a> for (Def, Span, DefModifiers, ty::Visibility) {
+impl<'a> ToNameBinding<'a> for (Def, Span, ty::Visibility) {
fn to_name_binding(self) -> NameBinding<'a> {
- let kind = NameBindingKind::Def(self.0);
- NameBinding { modifiers: self.2, kind: kind, span: Some(self.1), vis: self.3 }
+ NameBinding { kind: NameBindingKind::Def(self.0), span: Some(self.1), vis: self.2 }
}
}
let parent = *parent_ref;
let name = item.name;
let sp = item.span;
- let modifiers = DefModifiers::IMPORTABLE;
self.current_module = parent;
let vis = self.resolve_visibility(&item.vis);
ItemStatic(_, m, _) => {
let mutbl = m == hir::MutMutable;
let def = Def::Static(self.ast_map.local_def_id(item.id), mutbl);
- self.define(parent, name, ValueNS, (def, sp, modifiers, vis));
+ self.define(parent, name, ValueNS, (def, sp, vis));
}
ItemConst(_, _) => {
let def = Def::Const(self.ast_map.local_def_id(item.id));
- self.define(parent, name, ValueNS, (def, sp, modifiers, vis));
+ self.define(parent, name, ValueNS, (def, sp, vis));
}
ItemFn(_, _, _, _, _, _) => {
let def = Def::Fn(self.ast_map.local_def_id(item.id));
- self.define(parent, name, ValueNS, (def, sp, modifiers, vis));
+ self.define(parent, name, ValueNS, (def, sp, vis));
}
// These items live in the type namespace.
ItemTy(..) => {
let def = Def::TyAlias(self.ast_map.local_def_id(item.id));
- self.define(parent, name, TypeNS, (def, sp, modifiers, vis));
+ self.define(parent, name, TypeNS, (def, sp, vis));
}
ItemEnum(ref enum_definition, _) => {
ItemStruct(ref struct_def, _) => {
// Define a name in the type namespace.
let def = Def::Struct(self.ast_map.local_def_id(item.id));
- self.define(parent, name, TypeNS, (def, sp, modifiers, vis));
+ self.define(parent, name, TypeNS, (def, sp, vis));
// If this is a newtype or unit-like struct, define a name
// in the value namespace as well
if !struct_def.is_struct() {
let def = Def::Struct(self.ast_map.local_def_id(struct_def.id()));
- self.define(parent, name, ValueNS, (def, sp, modifiers, vis));
+ self.define(parent, name, ValueNS, (def, sp, vis));
}
// Record the def ID and fields of this struct.
hir::TypeTraitItem(..) => (Def::AssociatedTy(def_id, item_def_id), TypeNS),
};
- let modifiers = DefModifiers::empty(); // NB: not DefModifiers::IMPORTABLE
- self.define(module_parent, item.name, ns, (def, item.span, modifiers, vis));
+ self.define(module_parent, item.name, ns, (def, item.span, vis));
self.trait_item_map.insert((item.name, def_id), item_def_id);
}
// Variants are always treated as importable to allow them to be glob used.
// All variants are defined in both type and value namespaces as future-proofing.
- let modifiers = DefModifiers::IMPORTABLE;
let def = Def::Variant(item_id, self.ast_map.local_def_id(variant.node.data.id()));
-
- self.define(parent, name, ValueNS, (def, variant.span, modifiers, parent.vis));
- self.define(parent, name, TypeNS, (def, variant.span, modifiers, parent.vis));
+ self.define(parent, name, ValueNS, (def, variant.span, parent.vis));
+ self.define(parent, name, TypeNS, (def, variant.span, parent.vis));
}
/// Constructs the reduced graph for one foreign item.
foreign_item: &ForeignItem,
parent: Module<'b>) {
let name = foreign_item.name;
- let modifiers = DefModifiers::IMPORTABLE;
let def = match foreign_item.node {
ForeignItemFn(..) => {
};
self.current_module = parent;
let vis = self.resolve_visibility(&foreign_item.vis);
- self.define(parent, name, ValueNS, (def, foreign_item.span, modifiers, vis));
+ self.define(parent, name, ValueNS, (def, foreign_item.span, vis));
}
fn build_reduced_graph_for_block(&mut self, block: &Block, parent: &mut Module<'b>) {
let name = xcdef.name;
let vis = if parent.is_trait() { ty::Visibility::Public } else { xcdef.vis };
- let modifiers = match parent.is_normal() {
- true => DefModifiers::IMPORTABLE,
- false => DefModifiers::empty(),
- };
match def {
Def::Mod(_) | Def::ForeignMod(_) | Def::Enum(..) => {
debug!("(building reduced graph for external crate) building variant {}", name);
// Variants are always treated as importable to allow them to be glob used.
// All variants are defined in both type and value namespaces as future-proofing.
- let modifiers = DefModifiers::IMPORTABLE;
- self.try_define(parent, name, TypeNS, (def, DUMMY_SP, modifiers, vis));
- self.try_define(parent, name, ValueNS, (def, DUMMY_SP, modifiers, vis));
+ self.try_define(parent, name, TypeNS, (def, DUMMY_SP, vis));
+ self.try_define(parent, name, ValueNS, (def, DUMMY_SP, vis));
if self.session.cstore.variant_kind(variant_id) == Some(VariantKind::Struct) {
// Not adding fields for variants as they are not accessed with a self receiver
self.structs.insert(variant_id, Vec::new());
Def::Method(..) => {
debug!("(building reduced graph for external crate) building value (fn/static) {}",
name);
- self.try_define(parent, name, ValueNS, (def, DUMMY_SP, modifiers, vis));
+ self.try_define(parent, name, ValueNS, (def, DUMMY_SP, vis));
}
Def::Trait(def_id) => {
debug!("(building reduced graph for external crate) building type {}", name);
}
Def::TyAlias(..) | Def::AssociatedTy(..) => {
debug!("(building reduced graph for external crate) building type {}", name);
- self.try_define(parent, name, TypeNS, (def, DUMMY_SP, modifiers, vis));
+ self.try_define(parent, name, TypeNS, (def, DUMMY_SP, vis));
}
Def::Struct(def_id)
if self.session.cstore.tuple_struct_definition_if_ctor(def_id).is_none() => {
debug!("(building reduced graph for external crate) building type and value for {}",
name);
- self.try_define(parent, name, TypeNS, (def, DUMMY_SP, modifiers, vis));
+ self.try_define(parent, name, TypeNS, (def, DUMMY_SP, vis));
if let Some(ctor_def_id) = self.session.cstore.struct_ctor_def_id(def_id) {
let def = Def::Struct(ctor_def_id);
- self.try_define(parent, name, ValueNS, (def, DUMMY_SP, modifiers, vis));
+ self.try_define(parent, name, ValueNS, (def, DUMMY_SP, vis));
}
// Record the def ID and fields of this struct.
extern crate syntax;
extern crate arena;
#[macro_use]
-#[no_link]
-extern crate rustc_bitflags;
-#[macro_use]
extern crate rustc;
use self::PatternBindingMode::*;
}
}
-bitflags! {
- #[derive(Debug)]
- flags DefModifiers: u8 {
- const IMPORTABLE = 1 << 1,
- const GLOB_IMPORTED = 1 << 3,
- }
-}
-
// Records a possibly-private value, type, or module definition.
#[derive(Clone, Debug)]
pub struct NameBinding<'a> {
- modifiers: DefModifiers,
kind: NameBindingKind<'a>,
span: Option<Span>,
vis: ty::Visibility,
Module(Module<'a>),
Import {
binding: &'a NameBinding<'a>,
- id: NodeId,
+ directive: &'a ImportDirective<'a>,
// Some(error) if using this imported name causes the import to be a privacy error
privacy_error: Option<Box<PrivacyError<'a>>>,
},
impl<'a> NameBinding<'a> {
fn create_from_module(module: Module<'a>, span: Option<Span>) -> Self {
NameBinding {
- modifiers: DefModifiers::IMPORTABLE,
kind: NameBindingKind::Module(module),
span: span,
vis: module.vis,
}
}
- fn defined_with(&self, modifiers: DefModifiers) -> bool {
- self.modifiers.contains(modifiers)
- }
-
fn is_pseudo_public(&self) -> bool {
self.pseudo_vis() == ty::Visibility::Public
}
_ => false,
}
}
+
+ fn is_glob_import(&self) -> bool {
+ match self.kind {
+ NameBindingKind::Import { directive, .. } => directive.is_glob(),
+ _ => false,
+ }
+ }
+
+ fn is_importable(&self) -> bool {
+ match self.def().unwrap() {
+ Def::AssociatedConst(..) | Def::Method(..) | Def::AssociatedTy(..) => false,
+ _ => true,
+ }
+ }
}
/// Interns the names of the primitive types.
self.used_crates.insert(krate);
}
- let (import_id, privacy_error) = match binding.kind {
- NameBindingKind::Import { id, ref privacy_error, .. } => (id, privacy_error),
+ let (directive, privacy_error) = match binding.kind {
+ NameBindingKind::Import { directive, ref privacy_error, .. } =>
+ (directive, privacy_error),
_ => return,
};
- self.used_imports.insert((import_id, ns));
+ self.used_imports.insert((directive.id, ns));
if let Some(error) = privacy_error.as_ref() {
self.privacy_errors.push((**error).clone());
}
if !self.make_glob_map {
return;
}
- if self.glob_map.contains_key(&import_id) {
- self.glob_map.get_mut(&import_id).unwrap().insert(name);
+ if self.glob_map.contains_key(&directive.id) {
+ self.glob_map.get_mut(&directive.id).unwrap().insert(name);
return;
}
let mut new_set = FnvHashSet();
new_set.insert(name);
- self.glob_map.insert(import_id, new_set);
+ self.glob_map.insert(directive.id, new_set);
}
fn get_trait_name(&self, did: DefId) -> Name {
use self::ImportDirectiveSubclass::*;
-use DefModifiers;
use Module;
use Namespace::{self, TypeNS, ValueNS};
use {NameBinding, NameBindingKind, PrivacyError};
/// One import directive.
#[derive(Debug,Clone)]
pub struct ImportDirective<'a> {
+ pub id: NodeId,
module_path: Vec<Name>,
target_module: Cell<Option<Module<'a>>>, // the resolution of `module_path`
subclass: ImportDirectiveSubclass,
span: Span,
- id: NodeId,
vis: ty::Visibility, // see note in ImportResolutionPerNamespace about how to use this
is_prelude: bool,
}
impl<'a> ImportDirective<'a> {
// 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.
- fn import(&self, binding: &'a NameBinding<'a>, privacy_error: Option<Box<PrivacyError<'a>>>)
+ fn import(&'a self, binding: &'a NameBinding<'a>, privacy_error: Option<Box<PrivacyError<'a>>>)
-> NameBinding<'a> {
- let mut modifiers = DefModifiers::IMPORTABLE;
- if let GlobImport = self.subclass {
- modifiers = modifiers | DefModifiers::GLOB_IMPORTED;
- }
-
NameBinding {
kind: NameBindingKind::Import {
binding: binding,
- id: self.id,
+ directive: self,
privacy_error: privacy_error,
},
span: Some(self.span),
- modifiers: modifiers,
vis: self.vis,
}
}
+
+ pub fn is_glob(&self) -> bool {
+ match self.subclass { ImportDirectiveSubclass::GlobImport => true, _ => false }
+ }
}
#[derive(Clone, Default)]
impl<'a> NameResolution<'a> {
fn try_define(&mut self, binding: &'a NameBinding<'a>) -> Result<(), &'a NameBinding<'a>> {
if let Some(old_binding) = self.binding {
- if binding.defined_with(DefModifiers::GLOB_IMPORTED) {
+ if binding.is_glob_import() {
self.duplicate_globs.push(binding);
- } else if old_binding.defined_with(DefModifiers::GLOB_IMPORTED) {
+ } else if old_binding.is_glob_import() {
self.duplicate_globs.push(old_binding);
self.binding = Some(binding);
} else {
fn binding(&self) -> Option<&'a NameBinding<'a>> {
self.binding.and_then(|binding| match self.single_imports {
SingleImports::None => Some(binding),
- _ if !binding.defined_with(DefModifiers::GLOB_IMPORTED) => Some(binding),
+ _ if !binding.is_glob_import() => Some(binding),
_ => None, // The binding could be shadowed by a single import, so it is not known.
})
}
fn try_result(&self, ns: Namespace, allow_private_imports: bool)
-> Option<ResolveResult<&'a NameBinding<'a>>> {
match self.binding {
- Some(binding) if !binding.defined_with(DefModifiers::GLOB_IMPORTED) =>
+ Some(binding) if !binding.is_glob_import() =>
return Some(Success(binding)),
_ => {} // Items and single imports are not shadowable
};
}
fn define_in_glob_importers(&self, name: Name, ns: Namespace, binding: &'a NameBinding<'a>) {
- if !binding.defined_with(DefModifiers::IMPORTABLE) || !binding.is_pseudo_public() { return }
+ if !binding.is_importable() || !binding.is_pseudo_public() { return }
for &(importer, directive) in self.glob_importers.borrow_mut().iter() {
let _ = importer.try_define_child(name, ns, directive.import(binding, None));
}
// resolution for it so that later resolve stages won't complain.
if let SingleImport { target, .. } = e.import_directive.subclass {
let dummy_binding = self.resolver.arenas.alloc_name_binding(NameBinding {
- modifiers: DefModifiers::GLOB_IMPORTED,
kind: NameBindingKind::Def(Def::Err),
span: None,
vis: ty::Visibility::Public,
determined.set(true);
if let Success(binding) = *result {
- if !binding.defined_with(DefModifiers::IMPORTABLE) {
+ if !binding.is_importable() {
let msg = format!("`{}` is not directly importable", target);
span_err!(self.resolver.session, directive.span, E0253, "{}", &msg);
}
resolution.borrow().binding().map(|binding| (*name, binding))
}).collect::<Vec<_>>();
for ((name, ns), binding) in bindings {
- if binding.defined_with(DefModifiers::IMPORTABLE) && binding.is_pseudo_public() {
+ if binding.is_importable() && binding.is_pseudo_public() {
let _ = module_.try_define_child(name, ns, directive.import(binding, None));
}
}
}
}
- if let NameBindingKind::Import { binding: orig_binding, id, .. } = binding.kind {
+ if let NameBindingKind::Import { binding: orig_binding, directive, .. } = binding.kind {
if ns == TypeNS && orig_binding.is_variant() &&
!orig_binding.vis.is_at_least(binding.vis, &self.resolver.ast_map) {
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);
+ self.resolver.session.add_lint(lint, directive.id, binding.span.unwrap(), msg);
}
}
}