use rustc::session::Session;
use rustc::lint;
use rustc::hir::def::*;
-use rustc::hir::def_id::{CrateNum, CRATE_DEF_INDEX, LOCAL_CRATE, DefId};
+use rustc::hir::def_id::{CRATE_DEF_INDEX, LOCAL_CRATE, DefId};
use rustc::ty;
use rustc::hir::{Freevar, FreevarMap, TraitCandidate, TraitMap, GlobMap};
use rustc::util::nodemap::{NodeMap, NodeSet, FxHashMap, FxHashSet, DefIdMap};
use syntax::ext::hygiene::{Mark, SyntaxContext};
use syntax::ast::{self, Name, NodeId, Ident, SpannedIdent, FloatTy, IntTy, UintTy};
use syntax::ext::base::SyntaxExtension;
-use syntax::ext::base::Determinacy::{Determined, Undetermined};
+use syntax::ext::base::Determinacy::{self, Determined, Undetermined};
use syntax::ext::base::MacroKind;
use syntax::symbol::{Symbol, keywords};
use syntax::util::lev_distance::find_best_match_for_name;
/// Span of the module itself. Used for error reporting.
span: Span,
+
+ expansion: Mark,
}
pub type Module<'a> = &'a ModuleData<'a>;
fn new(parent: Option<Module<'a>>,
kind: ModuleKind,
normal_ancestor_id: DefId,
+ expansion: Mark,
span: Span) -> Self {
ModuleData {
parent: parent,
traits: RefCell::new(None),
populated: Cell::new(normal_ancestor_id.is_local()),
span: span,
+ expansion: expansion,
}
}
// entry block for `f`.
block_map: NodeMap<Module<'a>>,
module_map: FxHashMap<DefId, Module<'a>>,
- extern_crate_roots: FxHashMap<(CrateNum, bool /* MacrosOnly? */), Module<'a>>,
+ extern_module_map: FxHashMap<(DefId, bool /* MacrosOnly? */), Module<'a>>,
pub make_glob_map: bool,
// Maps imports to the names of items actually imported (this actually maps
use_extern_macros: bool, // true if `#![feature(use_extern_macros)]`
crate_loader: &'a mut CrateLoader,
- macro_names: FxHashSet<Name>,
+ macro_names: FxHashSet<Ident>,
global_macros: FxHashMap<Name, &'a NameBinding<'a>>,
- lexical_macro_resolutions: Vec<(Name, &'a Cell<LegacyScope<'a>>)>,
+ lexical_macro_resolutions: Vec<(Ident, &'a Cell<LegacyScope<'a>>)>,
macro_map: FxHashMap<DefId, Rc<SyntaxExtension>>,
macro_defs: FxHashMap<Mark, DefId>,
local_macro_def_scopes: FxHashMap<NodeId, Module<'a>>,
let root_module_kind = ModuleKind::Def(Def::Mod(root_def_id), keywords::Invalid.name());
let graph_root = arenas.alloc_module(ModuleData {
no_implicit_prelude: attr::contains_name(&krate.attrs, "no_implicit_prelude"),
- ..ModuleData::new(None, root_module_kind, root_def_id, krate.span)
+ ..ModuleData::new(None, root_module_kind, root_def_id, Mark::root(), krate.span)
});
let mut module_map = FxHashMap();
module_map.insert(DefId::local(CRATE_DEF_INDEX), graph_root);
trait_map: NodeMap(),
module_map: module_map,
block_map: NodeMap(),
- extern_crate_roots: FxHashMap(),
+ extern_module_map: FxHashMap(),
make_glob_map: make_glob_map == MakeGlobMap::Yes,
glob_map: NodeMap(),
parent: Module<'a>,
kind: ModuleKind,
normal_ancestor_id: DefId,
+ expansion: Mark,
span: Span,
) -> Module<'a> {
- self.arenas.alloc_module(ModuleData::new(Some(parent), kind, normal_ancestor_id, span))
+ let module = ModuleData::new(Some(parent), kind, normal_ancestor_id, expansion, span);
+ self.arenas.alloc_module(module)
}
fn record_use(&mut self, ident: Ident, ns: Namespace, binding: &'a NameBinding<'a>, span: Span)
path_span: Span)
-> Option<LexicalScopeBinding<'a>> {
if ns == TypeNS {
- ident = ident.unhygienize();
+ ident.ctxt = ident.ctxt.modern();
}
// Walk backwards up the ribs in scope.
+ let mut module = self.graph_root;
for i in (0 .. self.ribs[ns].len()).rev() {
if let Some(def) = self.ribs[ns][i].bindings.get(&ident).cloned() {
// The ident resolves to a type parameter or local variable.
));
}
- if let ModuleRibKind(module) = self.ribs[ns][i].kind {
- let item = self.resolve_ident_in_module(module, ident, ns, false,
- record_used, path_span);
- if let Ok(binding) = item {
- // The ident resolves to an item.
- return Some(LexicalScopeBinding::Item(binding));
+ module = match self.ribs[ns][i].kind {
+ ModuleRibKind(module) => module,
+ MacroDefinition(def) if def == self.macro_defs[&ident.ctxt.outer()] => {
+ // If an invocation of this macro created `ident`, give up on `ident`
+ // and switch to `ident`'s source from the macro definition.
+ ident.ctxt.remove_mark();
+ continue
}
+ _ => continue,
+ };
- if let ModuleKind::Block(..) = module.kind { // We can see through blocks
- } else if !module.no_implicit_prelude {
- return self.prelude.and_then(|prelude| {
- self.resolve_ident_in_module(prelude, ident, ns, false,
- false, path_span).ok()
- }).map(LexicalScopeBinding::Item)
- } else {
- return None;
- }
+ let item = self.resolve_ident_in_module_unadjusted(
+ module, ident, ns, false, record_used, path_span,
+ );
+ if let Ok(binding) = item {
+ // The ident resolves to an item.
+ return Some(LexicalScopeBinding::Item(binding));
}
- if let MacroDefinition(def) = self.ribs[ns][i].kind {
- // If an invocation of this macro created `ident`, give up on `ident`
- // and switch to `ident`'s source from the macro definition.
- let ctxt_data = ident.ctxt.data();
- if def == self.macro_defs[&ctxt_data.outer_mark] {
- ident.ctxt = ctxt_data.prev_ctxt;
- }
+ match module.kind {
+ ModuleKind::Block(..) => {}, // We can see through blocks
+ _ => break,
+ }
+ }
+
+ ident.ctxt = ident.ctxt.modern();
+ loop {
+ module = unwrap_or!(self.hygienic_lexical_parent(module, &mut ident.ctxt), break);
+ let orig_current_module = self.current_module;
+ self.current_module = module; // Lexical resolutions can never be a privacy error.
+ let result = self.resolve_ident_in_module_unadjusted(
+ module, ident, ns, false, record_used, path_span,
+ );
+ self.current_module = orig_current_module;
+
+ match result {
+ Ok(binding) => return Some(LexicalScopeBinding::Item(binding)),
+ Err(Undetermined) => return None,
+ Err(Determined) => {}
+ }
+ }
+
+ match self.prelude {
+ Some(prelude) if !module.no_implicit_prelude => {
+ self.resolve_ident_in_module_unadjusted(prelude, ident, ns, false, false, path_span)
+ .ok().map(LexicalScopeBinding::Item)
+ }
+ _ => None,
+ }
+ }
+
+ fn hygienic_lexical_parent(&mut self, mut module: Module<'a>, ctxt: &mut SyntaxContext)
+ -> Option<Module<'a>> {
+ if !module.expansion.is_descendant_of(ctxt.outer()) {
+ return Some(self.macro_def_scope(ctxt.remove_mark()));
+ }
+
+ if let ModuleKind::Block(..) = module.kind {
+ return Some(module.parent.unwrap());
+ }
+
+ let mut module_expansion = module.expansion.modern(); // for backward compatability
+ while let Some(parent) = module.parent {
+ let parent_expansion = parent.expansion.modern();
+ if module_expansion.is_descendant_of(parent_expansion) &&
+ parent_expansion != module_expansion {
+ return if parent_expansion.is_descendant_of(ctxt.outer()) {
+ Some(parent)
+ } else {
+ None
+ };
}
+ module = parent;
+ module_expansion = parent_expansion;
}
None
}
- fn resolve_crate_var(&mut self, crate_var_ctxt: SyntaxContext, span: Span) -> Module<'a> {
- let mut ctxt_data = crate_var_ctxt.data();
- while ctxt_data.prev_ctxt != SyntaxContext::empty() {
- ctxt_data = ctxt_data.prev_ctxt.data();
+ fn resolve_ident_in_module(&mut self,
+ module: Module<'a>,
+ mut ident: Ident,
+ ns: Namespace,
+ ignore_unresolved_invocations: bool,
+ record_used: bool,
+ span: Span)
+ -> Result<&'a NameBinding<'a>, Determinacy> {
+ ident.ctxt = ident.ctxt.modern();
+ let orig_current_module = self.current_module;
+ if let Some(def) = ident.ctxt.adjust(module.expansion) {
+ self.current_module = self.macro_def_scope(def);
+ }
+ let result = self.resolve_ident_in_module_unadjusted(
+ module, ident, ns, ignore_unresolved_invocations, record_used, span,
+ );
+ self.current_module = orig_current_module;
+ result
+ }
+
+ fn resolve_crate_root(&mut self, mut ctxt: SyntaxContext) -> Module<'a> {
+ let module = match ctxt.adjust(Mark::root()) {
+ Some(def) => self.macro_def_scope(def),
+ None => return self.graph_root,
+ };
+ self.get_module(DefId { index: CRATE_DEF_INDEX, ..module.normal_ancestor_id })
+ }
+
+ fn resolve_self(&mut self, ctxt: &mut SyntaxContext, module: Module<'a>) -> Module<'a> {
+ let mut module = self.get_module(module.normal_ancestor_id);
+ while module.span.ctxt.modern() != *ctxt {
+ let parent = module.parent.unwrap_or_else(|| self.macro_def_scope(ctxt.remove_mark()));
+ module = self.get_module(parent.normal_ancestor_id);
}
- let module = self.macro_def_scope(ctxt_data.outer_mark, span);
- if module.is_local() { self.graph_root } else { module }
+ module
}
// AST resolution
fn search_label(&self, mut ident: Ident) -> Option<Def> {
for rib in self.label_ribs.iter().rev() {
match rib.kind {
- NormalRibKind => {
- // Continue
- }
+ NormalRibKind => {}
+ // If an invocation of this macro created `ident`, give up on `ident`
+ // and switch to `ident`'s source from the macro definition.
MacroDefinition(def) => {
- // If an invocation of this macro created `ident`, give up on `ident`
- // and switch to `ident`'s source from the macro definition.
- let ctxt_data = ident.ctxt.data();
- if def == self.macro_defs[&ctxt_data.outer_mark] {
- ident.ctxt = ctxt_data.prev_ctxt;
+ if def == self.macro_defs[&ident.ctxt.outer()] {
+ ident.ctxt.remove_mark();
}
}
_ => {
let mut function_type_rib = Rib::new(rib_kind);
let mut seen_bindings = FxHashMap();
for type_parameter in &generics.ty_params {
- let ident = type_parameter.ident.unhygienize();
+ let ident = type_parameter.ident.modern();
debug!("with_type_parameter_rib: {}", type_parameter.id);
if seen_bindings.contains_key(&ident) {
}
let is_global = self.global_macros.get(&path[0].name).cloned()
.map(|binding| binding.get_macro(self).kind() == MacroKind::Bang).unwrap_or(false);
- if primary_ns != MacroNS && (is_global || self.macro_names.contains(&path[0].name)) {
+ if primary_ns != MacroNS && (is_global || self.macro_names.contains(&path[0].modern())) {
// Return some dummy definition, it's enough for error reporting.
return Some(
PathResolution::new(Def::Macro(DefId::local(CRATE_DEF_INDEX), MacroKind::Bang))
let ns = if is_last { opt_ns.unwrap_or(TypeNS) } else { TypeNS };
if i == 0 && ns == TypeNS && ident.name == keywords::SelfValue.name() {
- module = Some(self.module_map[&self.current_module.normal_ancestor_id]);
+ let mut ctxt = ident.ctxt.modern();
+ module = Some(self.resolve_self(&mut ctxt, self.current_module));
continue
} else if allow_super && ns == TypeNS && ident.name == keywords::Super.name() {
- let current_module = if i == 0 { self.current_module } else { module.unwrap() };
- let self_module = self.module_map[¤t_module.normal_ancestor_id];
+ let mut ctxt = ident.ctxt.modern();
+ let self_module = match i {
+ 0 => self.resolve_self(&mut ctxt, self.current_module),
+ _ => module.unwrap(),
+ };
if let Some(parent) = self_module.parent {
- module = Some(self.module_map[&parent.normal_ancestor_id]);
+ module = Some(self.resolve_self(&mut ctxt, parent));
continue
} else {
let msg = "There are too many initial `super`s.".to_string();
allow_super = false;
if i == 0 && ns == TypeNS && ident.name == keywords::CrateRoot.name() {
- module = Some(self.graph_root);
+ module = Some(self.resolve_crate_root(ident.ctxt.modern()));
continue
} else if i == 0 && ns == TypeNS && ident.name == "$crate" {
- module = Some(self.resolve_crate_var(ident.ctxt, path_span));
+ module = Some(self.resolve_crate_root(ident.ctxt));
continue
}
}
}
- fn get_traits_containing_item(&mut self, ident: Ident, ns: Namespace) -> Vec<TraitCandidate> {
+ fn get_traits_containing_item(&mut self, mut ident: Ident, ns: Namespace)
+ -> Vec<TraitCandidate> {
debug!("(getting traits containing item) looking for '{}'", ident.name);
let mut found_traits = Vec::new();
}
}
+ ident.ctxt = ident.ctxt.modern();
let mut search_module = self.current_module;
loop {
self.get_traits_in_module_containing_item(ident, ns, search_module, &mut found_traits);
- match search_module.kind {
- ModuleKind::Block(..) => search_module = search_module.parent.unwrap(),
- _ => break,
- }
+ search_module =
+ unwrap_or!(self.hygienic_lexical_parent(search_module, &mut ident.ctxt), break);
}
if let Some(prelude) = self.prelude {
for &(trait_name, binding) in traits.as_ref().unwrap().iter() {
let module = binding.module().unwrap();
- if self.resolve_ident_in_module(module, ident, ns, false, false, module.span).is_ok() {
+ let mut ident = ident;
+ if ident.ctxt.glob_adjust(module.expansion, binding.span.ctxt.modern()).is_none() {
+ continue
+ }
+ if self.resolve_ident_in_module_unadjusted(module, ident, ns, false, false, module.span)
+ .is_ok() {
let import_id = match binding.kind {
NameBindingKind::Import { directive, .. } => {
self.maybe_unused_trait_imports.insert(directive.id);
}
fn report_shadowing_errors(&mut self) {
- for (name, scope) in replace(&mut self.lexical_macro_resolutions, Vec::new()) {
- self.resolve_legacy_scope(scope, name, true);
+ for (ident, scope) in replace(&mut self.lexical_macro_resolutions, Vec::new()) {
+ self.resolve_legacy_scope(scope, ident, true);
}
let mut reported_errors = FxHashSet();
for binding in replace(&mut self.disallowed_shadowing, Vec::new()) {
- if self.resolve_legacy_scope(&binding.parent, binding.name, false).is_some() &&
- reported_errors.insert((binding.name, binding.span)) {
- let msg = format!("`{}` is already in scope", binding.name);
+ if self.resolve_legacy_scope(&binding.parent, binding.ident, false).is_some() &&
+ reported_errors.insert((binding.ident, binding.span)) {
+ let msg = format!("`{}` is already in scope", binding.ident);
self.session.struct_span_err(binding.span, &msg)
.note("macro-expanded `macro_rules!`s may not shadow \
existing macros (see RFC 1560)")