2016-09-07 18:21:59 -05:00
|
|
|
// Copyright 2016 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.
|
|
|
|
|
2016-11-27 04:58:46 -06:00
|
|
|
use {AmbiguityError, Resolver, ResolutionError, resolve_error};
|
2016-12-04 21:51:11 -06:00
|
|
|
use {Module, ModuleKind, NameBinding, NameBindingKind, PathResult};
|
2016-11-10 04:29:36 -06:00
|
|
|
use Namespace::{self, MacroNS};
|
2016-09-14 16:03:09 -05:00
|
|
|
use build_reduced_graph::BuildReducedGraphVisitor;
|
2016-11-10 04:11:25 -06:00
|
|
|
use resolve_imports::ImportResolver;
|
2016-10-28 01:52:45 -05:00
|
|
|
use rustc::hir::def_id::{DefId, BUILTIN_MACROS_CRATE, CRATE_DEF_INDEX, DefIndex};
|
|
|
|
use rustc::hir::def::{Def, Export};
|
2016-09-23 16:13:59 -05:00
|
|
|
use rustc::hir::map::{self, DefCollector};
|
2016-11-07 16:08:26 -06:00
|
|
|
use rustc::ty;
|
2016-09-16 03:50:34 -05:00
|
|
|
use std::cell::Cell;
|
2016-09-07 18:21:59 -05:00
|
|
|
use std::rc::Rc;
|
2016-11-28 20:07:12 -06:00
|
|
|
use syntax::ast::{self, Name, Ident};
|
2016-11-29 17:36:27 -06:00
|
|
|
use syntax::attr;
|
2016-09-07 18:21:59 -05:00
|
|
|
use syntax::errors::DiagnosticBuilder;
|
2016-10-15 15:50:30 -05:00
|
|
|
use syntax::ext::base::{self, Determinacy, MultiModifier, MultiDecorator};
|
2017-02-23 03:48:20 -06:00
|
|
|
use syntax::ext::base::{Resolver as SyntaxResolver, SyntaxExtension};
|
2017-02-06 05:44:38 -06:00
|
|
|
use syntax::ext::base::MacroKind;
|
2017-03-04 23:15:58 -06:00
|
|
|
use syntax::ext::expand::Expansion;
|
2016-10-06 03:04:30 -05:00
|
|
|
use syntax::ext::hygiene::Mark;
|
2016-09-21 01:25:09 -05:00
|
|
|
use syntax::ext::tt::macro_rules;
|
2017-02-02 01:01:15 -06:00
|
|
|
use syntax::feature_gate::{self, emit_feature_err, GateIssue};
|
2016-12-31 01:25:59 -06:00
|
|
|
use syntax::fold::{self, Folder};
|
2016-11-07 21:16:54 -06:00
|
|
|
use syntax::ptr::P;
|
2017-02-01 18:33:42 -06:00
|
|
|
use syntax::symbol::{Symbol, keywords};
|
2016-09-07 18:21:59 -05:00
|
|
|
use syntax::util::lev_distance::find_best_match_for_name;
|
2016-11-07 16:08:26 -06:00
|
|
|
use syntax_pos::{Span, DUMMY_SP};
|
2016-09-07 18:21:59 -05:00
|
|
|
|
2016-09-14 04:55:20 -05:00
|
|
|
#[derive(Clone)]
|
2016-10-03 18:48:19 -05:00
|
|
|
pub struct InvocationData<'a> {
|
2016-09-16 03:50:34 -05:00
|
|
|
pub module: Cell<Module<'a>>,
|
2016-10-15 22:39:52 -05:00
|
|
|
pub def_index: DefIndex,
|
2017-03-05 02:14:21 -06:00
|
|
|
// True if this expansion is in a `const_expr` position, for example `[u32; m!()]`.
|
|
|
|
// c.f. `DefCollector::visit_const_expr`.
|
|
|
|
pub const_expr: bool,
|
2016-10-06 03:04:30 -05:00
|
|
|
// The scope in which the invocation path is resolved.
|
|
|
|
pub legacy_scope: Cell<LegacyScope<'a>>,
|
|
|
|
// The smallest scope that includes this invocation's expansion,
|
|
|
|
// or `Empty` if this invocation has not been expanded yet.
|
|
|
|
pub expansion: Cell<LegacyScope<'a>>,
|
2016-09-14 04:55:20 -05:00
|
|
|
}
|
|
|
|
|
2016-10-03 18:48:19 -05:00
|
|
|
impl<'a> InvocationData<'a> {
|
2016-09-14 16:03:09 -05:00
|
|
|
pub fn root(graph_root: Module<'a>) -> Self {
|
2016-10-03 18:48:19 -05:00
|
|
|
InvocationData {
|
2016-09-16 03:50:34 -05:00
|
|
|
module: Cell::new(graph_root),
|
2016-09-14 04:55:20 -05:00
|
|
|
def_index: CRATE_DEF_INDEX,
|
2017-03-05 02:14:21 -06:00
|
|
|
const_expr: false,
|
2016-10-06 03:04:30 -05:00
|
|
|
legacy_scope: Cell::new(LegacyScope::Empty),
|
|
|
|
expansion: Cell::new(LegacyScope::Empty),
|
2016-09-14 04:55:20 -05:00
|
|
|
}
|
|
|
|
}
|
2016-09-07 18:21:59 -05:00
|
|
|
}
|
|
|
|
|
2016-10-06 03:04:30 -05:00
|
|
|
#[derive(Copy, Clone)]
|
|
|
|
pub enum LegacyScope<'a> {
|
|
|
|
Empty,
|
|
|
|
Invocation(&'a InvocationData<'a>), // The scope of the invocation, not including its expansion
|
|
|
|
Expansion(&'a InvocationData<'a>), // The scope of the invocation, including its expansion
|
|
|
|
Binding(&'a LegacyBinding<'a>),
|
|
|
|
}
|
|
|
|
|
|
|
|
pub struct LegacyBinding<'a> {
|
2016-11-22 19:51:37 -06:00
|
|
|
pub parent: Cell<LegacyScope<'a>>,
|
2016-10-31 01:48:59 -05:00
|
|
|
pub name: ast::Name,
|
2016-10-10 22:48:54 -05:00
|
|
|
ext: Rc<SyntaxExtension>,
|
2016-10-31 01:48:59 -05:00
|
|
|
pub span: Span,
|
2016-10-06 03:04:30 -05:00
|
|
|
}
|
|
|
|
|
2016-11-10 04:29:36 -06:00
|
|
|
pub enum MacroBinding<'a> {
|
|
|
|
Legacy(&'a LegacyBinding<'a>),
|
|
|
|
Modern(&'a NameBinding<'a>),
|
|
|
|
}
|
|
|
|
|
2016-09-07 18:21:59 -05:00
|
|
|
impl<'a> base::Resolver for Resolver<'a> {
|
2016-09-04 19:10:27 -05:00
|
|
|
fn next_node_id(&mut self) -> ast::NodeId {
|
|
|
|
self.session.next_node_id()
|
|
|
|
}
|
|
|
|
|
2016-09-23 02:23:01 -05:00
|
|
|
fn get_module_scope(&mut self, id: ast::NodeId) -> Mark {
|
|
|
|
let mark = Mark::fresh();
|
2016-12-20 02:32:15 -06:00
|
|
|
let module = self.module_map[&self.definitions.local_def_id(id)];
|
2016-10-03 18:48:19 -05:00
|
|
|
self.invocations.insert(mark, self.arenas.alloc_invocation_data(InvocationData {
|
2016-09-16 03:50:34 -05:00
|
|
|
module: Cell::new(module),
|
2016-09-23 02:23:01 -05:00
|
|
|
def_index: module.def_id().unwrap().index,
|
2017-03-05 02:14:21 -06:00
|
|
|
const_expr: false,
|
2016-10-06 03:04:30 -05:00
|
|
|
legacy_scope: Cell::new(LegacyScope::Empty),
|
|
|
|
expansion: Cell::new(LegacyScope::Empty),
|
2016-09-16 03:50:34 -05:00
|
|
|
}));
|
2016-09-23 02:23:01 -05:00
|
|
|
mark
|
|
|
|
}
|
|
|
|
|
2016-11-07 21:16:54 -06:00
|
|
|
fn eliminate_crate_var(&mut self, item: P<ast::Item>) -> P<ast::Item> {
|
|
|
|
struct EliminateCrateVar<'b, 'a: 'b>(&'b mut Resolver<'a>);
|
|
|
|
|
|
|
|
impl<'a, 'b> Folder for EliminateCrateVar<'a, 'b> {
|
|
|
|
fn fold_path(&mut self, mut path: ast::Path) -> ast::Path {
|
|
|
|
let ident = path.segments[0].identifier;
|
2016-11-17 08:04:20 -06:00
|
|
|
if ident.name == "$crate" {
|
2016-12-04 21:51:11 -06:00
|
|
|
path.segments[0].identifier.name = keywords::CrateRoot.name();
|
2016-11-07 21:16:54 -06:00
|
|
|
let module = self.0.resolve_crate_var(ident.ctxt);
|
2016-12-04 21:51:11 -06:00
|
|
|
if !module.is_local() {
|
|
|
|
path.segments.insert(1, match module.kind {
|
|
|
|
ModuleKind::Def(_, name) => ast::Ident::with_empty_ctxt(name).into(),
|
2016-11-07 21:16:54 -06:00
|
|
|
_ => unreachable!(),
|
2016-12-04 21:51:11 -06:00
|
|
|
})
|
2016-11-07 21:16:54 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
path
|
|
|
|
}
|
2016-12-30 23:32:06 -06:00
|
|
|
|
|
|
|
fn fold_mac(&mut self, mac: ast::Mac) -> ast::Mac {
|
|
|
|
fold::noop_fold_mac(mac, self)
|
|
|
|
}
|
2016-11-07 21:16:54 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
EliminateCrateVar(self).fold_item(item).expect_one("")
|
|
|
|
}
|
|
|
|
|
2016-12-22 00:03:19 -06:00
|
|
|
fn is_whitelisted_legacy_custom_derive(&self, name: Name) -> bool {
|
|
|
|
self.whitelisted_legacy_custom_derives.contains(&name)
|
|
|
|
}
|
|
|
|
|
2017-02-02 01:01:15 -06:00
|
|
|
fn visit_expansion(&mut self, mark: Mark, expansion: &Expansion, derives: &[Mark]) {
|
2016-10-06 03:04:30 -05:00
|
|
|
let invocation = self.invocations[&mark];
|
|
|
|
self.collect_def_ids(invocation, expansion);
|
|
|
|
|
|
|
|
self.current_module = invocation.module.get();
|
2016-11-11 04:51:15 -06:00
|
|
|
self.current_module.unresolved_invocations.borrow_mut().remove(&mark);
|
2017-02-02 01:01:15 -06:00
|
|
|
self.current_module.unresolved_invocations.borrow_mut().extend(derives);
|
|
|
|
for &derive in derives {
|
|
|
|
self.invocations.insert(derive, invocation);
|
|
|
|
}
|
2016-10-06 03:04:30 -05:00
|
|
|
let mut visitor = BuildReducedGraphVisitor {
|
|
|
|
resolver: self,
|
|
|
|
legacy_scope: LegacyScope::Invocation(invocation),
|
2016-10-10 22:42:06 -05:00
|
|
|
expansion: mark,
|
2016-10-06 03:04:30 -05:00
|
|
|
};
|
|
|
|
expansion.visit_with(&mut visitor);
|
2016-11-10 04:29:36 -06:00
|
|
|
self.current_module.unresolved_invocations.borrow_mut().remove(&mark);
|
2016-10-06 03:04:30 -05:00
|
|
|
invocation.expansion.set(visitor.legacy_scope);
|
2016-09-07 18:21:59 -05:00
|
|
|
}
|
|
|
|
|
2017-03-04 23:15:58 -06:00
|
|
|
fn add_builtin(&mut self, ident: ast::Ident, ext: Rc<SyntaxExtension>) {
|
2016-10-28 01:52:45 -05:00
|
|
|
let def_id = DefId {
|
|
|
|
krate: BUILTIN_MACROS_CRATE,
|
|
|
|
index: DefIndex::new(self.macro_map.len()),
|
|
|
|
};
|
2017-02-23 03:42:33 -06:00
|
|
|
let kind = ext.kind();
|
2016-10-28 01:52:45 -05:00
|
|
|
self.macro_map.insert(def_id, ext);
|
2016-11-07 16:08:26 -06:00
|
|
|
let binding = self.arenas.alloc_name_binding(NameBinding {
|
2017-02-23 03:42:33 -06:00
|
|
|
kind: NameBindingKind::Def(Def::Macro(def_id, kind)),
|
2016-11-07 16:08:26 -06:00
|
|
|
span: DUMMY_SP,
|
2016-12-20 02:32:15 -06:00
|
|
|
vis: ty::Visibility::Invisible,
|
2016-11-07 16:23:26 -06:00
|
|
|
expansion: Mark::root(),
|
2016-11-07 16:08:26 -06:00
|
|
|
});
|
|
|
|
self.builtin_macros.insert(ident.name, binding);
|
2016-09-07 18:21:59 -05:00
|
|
|
}
|
|
|
|
|
2016-11-10 04:11:25 -06:00
|
|
|
fn resolve_imports(&mut self) {
|
|
|
|
ImportResolver { resolver: self }.resolve_imports()
|
|
|
|
}
|
|
|
|
|
2017-02-02 01:01:15 -06:00
|
|
|
// Resolves attribute and derive legacy macros from `#![plugin(..)]`.
|
|
|
|
fn find_legacy_attr_invoc(&mut self, attrs: &mut Vec<ast::Attribute>)
|
|
|
|
-> Option<ast::Attribute> {
|
2016-09-07 18:21:59 -05:00
|
|
|
for i in 0..attrs.len() {
|
2017-02-22 13:15:12 -06:00
|
|
|
if self.session.plugin_attributes.borrow().iter()
|
|
|
|
.any(|&(ref attr_nm, _)| attrs[i].name() == &**attr_nm) {
|
|
|
|
attr::mark_known(&attrs[i]);
|
|
|
|
}
|
|
|
|
|
2016-11-14 22:34:52 -06:00
|
|
|
match self.builtin_macros.get(&attrs[i].name()).cloned() {
|
2016-11-27 04:27:41 -06:00
|
|
|
Some(binding) => match *binding.get_macro(self) {
|
2016-09-06 00:57:58 -05:00
|
|
|
MultiModifier(..) | MultiDecorator(..) | SyntaxExtension::AttrProcMacro(..) => {
|
|
|
|
return Some(attrs.remove(i))
|
|
|
|
}
|
2016-09-07 18:21:59 -05:00
|
|
|
_ => {}
|
|
|
|
},
|
|
|
|
None => {}
|
|
|
|
}
|
|
|
|
}
|
2017-02-01 18:33:42 -06:00
|
|
|
|
|
|
|
// Check for legacy derives
|
|
|
|
for i in 0..attrs.len() {
|
|
|
|
if attrs[i].name() == "derive" {
|
|
|
|
let mut traits = match attrs[i].meta_item_list() {
|
|
|
|
Some(traits) if !traits.is_empty() => traits.to_owned(),
|
|
|
|
_ => continue,
|
|
|
|
};
|
|
|
|
|
|
|
|
for j in 0..traits.len() {
|
|
|
|
let legacy_name = Symbol::intern(&match traits[j].word() {
|
|
|
|
Some(..) => format!("derive_{}", traits[j].name().unwrap()),
|
|
|
|
None => continue,
|
|
|
|
});
|
|
|
|
if !self.builtin_macros.contains_key(&legacy_name) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
let span = traits.remove(j).span;
|
|
|
|
self.gate_legacy_custom_derive(legacy_name, span);
|
|
|
|
if traits.is_empty() {
|
|
|
|
attrs.remove(i);
|
|
|
|
} else {
|
|
|
|
attrs[i].value = ast::MetaItem {
|
|
|
|
name: attrs[i].name(),
|
2017-02-02 01:01:15 -06:00
|
|
|
span: attrs[i].span,
|
2017-02-01 18:33:42 -06:00
|
|
|
node: ast::MetaItemKind::List(traits),
|
|
|
|
};
|
|
|
|
}
|
|
|
|
return Some(ast::Attribute {
|
|
|
|
value: ast::MetaItem {
|
|
|
|
name: legacy_name,
|
|
|
|
span: span,
|
|
|
|
node: ast::MetaItemKind::Word,
|
|
|
|
},
|
|
|
|
id: attr::mk_attr_id(),
|
|
|
|
style: ast::AttrStyle::Outer,
|
|
|
|
is_sugared_doc: false,
|
|
|
|
span: span,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-07 18:21:59 -05:00
|
|
|
None
|
|
|
|
}
|
|
|
|
|
2017-02-06 05:44:38 -06:00
|
|
|
fn resolve_macro(&mut self, scope: Mark, path: &ast::Path, kind: MacroKind,
|
|
|
|
force: bool) -> Result<Rc<SyntaxExtension>, Determinacy> {
|
2016-12-04 21:51:11 -06:00
|
|
|
let ast::Path { ref segments, span } = *path;
|
2016-12-10 00:45:58 -06:00
|
|
|
if segments.iter().any(|segment| segment.parameters.is_some()) {
|
2016-11-27 04:58:46 -06:00
|
|
|
let kind =
|
2016-12-10 00:45:58 -06:00
|
|
|
if segments.last().unwrap().parameters.is_some() { "macro" } else { "module" };
|
2016-11-27 04:58:46 -06:00
|
|
|
let msg = format!("type parameters are not allowed on {}s", kind);
|
|
|
|
self.session.span_err(path.span, &msg);
|
2016-10-14 18:08:53 -05:00
|
|
|
return Err(Determinacy::Determined);
|
|
|
|
}
|
2016-09-07 18:21:59 -05:00
|
|
|
|
2016-11-27 04:58:46 -06:00
|
|
|
let path: Vec<_> = segments.iter().map(|seg| seg.identifier).collect();
|
2016-10-07 19:30:24 -05:00
|
|
|
let invocation = self.invocations[&scope];
|
2016-11-10 04:29:36 -06:00
|
|
|
self.current_module = invocation.module.get();
|
2016-11-27 04:58:46 -06:00
|
|
|
|
2016-12-04 21:51:11 -06:00
|
|
|
if path.len() > 1 {
|
2016-11-27 04:58:46 -06:00
|
|
|
if !self.use_extern_macros {
|
|
|
|
let msg = "non-ident macro paths are experimental";
|
|
|
|
let feature = "use_extern_macros";
|
|
|
|
emit_feature_err(&self.session.parse_sess, feature, span, GateIssue::Language, msg);
|
2017-02-06 05:44:38 -06:00
|
|
|
self.found_unresolved_macro = true;
|
2016-11-27 04:58:46 -06:00
|
|
|
return Err(Determinacy::Determined);
|
|
|
|
}
|
|
|
|
|
2016-12-04 21:51:11 -06:00
|
|
|
let ext = match self.resolve_path(&path, Some(MacroNS), None) {
|
2017-02-18 13:11:42 -06:00
|
|
|
PathResult::NonModule(path_res) => match path_res.base_def() {
|
2016-12-12 04:09:22 -06:00
|
|
|
Def::Err => Err(Determinacy::Determined),
|
|
|
|
def @ _ => Ok(self.get_macro(def)),
|
|
|
|
},
|
2016-11-27 04:58:46 -06:00
|
|
|
PathResult::Module(..) => unreachable!(),
|
|
|
|
PathResult::Indeterminate if !force => return Err(Determinacy::Undetermined),
|
2017-02-06 05:44:38 -06:00
|
|
|
_ => {
|
|
|
|
self.found_unresolved_macro = true;
|
|
|
|
Err(Determinacy::Determined)
|
|
|
|
},
|
2016-11-27 04:58:46 -06:00
|
|
|
};
|
|
|
|
self.current_module.macro_resolutions.borrow_mut()
|
2016-12-04 21:51:11 -06:00
|
|
|
.push((path.into_boxed_slice(), span));
|
2016-11-27 04:58:46 -06:00
|
|
|
return ext;
|
|
|
|
}
|
|
|
|
|
|
|
|
let name = path[0].name;
|
|
|
|
let result = match self.resolve_legacy_scope(&invocation.legacy_scope, name, false) {
|
|
|
|
Some(MacroBinding::Legacy(binding)) => Ok(binding.ext.clone()),
|
|
|
|
Some(MacroBinding::Modern(binding)) => Ok(binding.get_macro(self)),
|
2016-11-28 20:07:12 -06:00
|
|
|
None => match self.resolve_lexical_macro_path_segment(path[0], MacroNS, None) {
|
2016-11-27 04:58:46 -06:00
|
|
|
Ok(binding) => Ok(binding.get_macro(self)),
|
2017-02-06 05:44:38 -06:00
|
|
|
Err(Determinacy::Undetermined) if !force =>
|
|
|
|
return Err(Determinacy::Undetermined),
|
|
|
|
Err(_) => {
|
|
|
|
self.found_unresolved_macro = true;
|
|
|
|
Err(Determinacy::Determined)
|
|
|
|
}
|
2016-11-10 04:29:36 -06:00
|
|
|
},
|
|
|
|
};
|
|
|
|
|
2017-02-06 05:44:38 -06:00
|
|
|
self.current_module.legacy_macro_resolutions.borrow_mut()
|
|
|
|
.push((scope, path[0], span, kind));
|
2017-02-01 04:33:09 -06:00
|
|
|
|
2017-02-06 05:44:38 -06:00
|
|
|
result
|
2017-02-01 04:33:09 -06:00
|
|
|
}
|
2016-09-25 22:17:05 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> Resolver<'a> {
|
2016-11-27 04:58:46 -06:00
|
|
|
// Resolve the initial segment of a non-global macro path (e.g. `foo` in `foo::bar!();`)
|
|
|
|
pub fn resolve_lexical_macro_path_segment(&mut self,
|
2016-11-28 20:07:12 -06:00
|
|
|
ident: Ident,
|
2016-11-27 04:58:46 -06:00
|
|
|
ns: Namespace,
|
|
|
|
record_used: Option<Span>)
|
|
|
|
-> Result<&'a NameBinding<'a>, Determinacy> {
|
2016-11-10 04:29:36 -06:00
|
|
|
let mut module = self.current_module;
|
2016-11-27 04:58:46 -06:00
|
|
|
let mut potential_expanded_shadower: Option<&NameBinding> = None;
|
2016-11-10 04:29:36 -06:00
|
|
|
loop {
|
|
|
|
// Since expanded macros may not shadow the lexical scope (enforced below),
|
|
|
|
// we can ignore unresolved invocations (indicated by the penultimate argument).
|
2016-11-28 20:07:12 -06:00
|
|
|
match self.resolve_ident_in_module(module, ident, ns, true, record_used) {
|
2016-11-26 06:21:47 -06:00
|
|
|
Ok(binding) => {
|
2016-11-10 04:29:36 -06:00
|
|
|
let span = match record_used {
|
|
|
|
Some(span) => span,
|
2016-11-27 04:58:46 -06:00
|
|
|
None => return Ok(binding),
|
2016-11-10 04:29:36 -06:00
|
|
|
};
|
2016-11-27 04:58:46 -06:00
|
|
|
match potential_expanded_shadower {
|
|
|
|
Some(shadower) if shadower.def() != binding.def() => {
|
2016-11-28 20:07:12 -06:00
|
|
|
let name = ident.name;
|
2016-11-27 04:58:46 -06:00
|
|
|
self.ambiguity_errors.push(AmbiguityError {
|
|
|
|
span: span, name: name, b1: shadower, b2: binding, lexical: true,
|
2016-12-09 05:08:39 -06:00
|
|
|
legacy: false,
|
2016-11-27 04:58:46 -06:00
|
|
|
});
|
|
|
|
return Ok(shadower);
|
|
|
|
}
|
|
|
|
_ if binding.expansion == Mark::root() => return Ok(binding),
|
|
|
|
_ => potential_expanded_shadower = Some(binding),
|
2016-11-10 04:29:36 -06:00
|
|
|
}
|
|
|
|
},
|
2016-11-27 04:58:46 -06:00
|
|
|
Err(Determinacy::Undetermined) => return Err(Determinacy::Undetermined),
|
2016-11-26 06:21:47 -06:00
|
|
|
Err(Determinacy::Determined) => {}
|
2016-11-10 04:29:36 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
match module.kind {
|
|
|
|
ModuleKind::Block(..) => module = module.parent.unwrap(),
|
2016-11-27 04:58:46 -06:00
|
|
|
ModuleKind::Def(..) => return match potential_expanded_shadower {
|
|
|
|
Some(binding) => Ok(binding),
|
|
|
|
None if record_used.is_some() => Err(Determinacy::Determined),
|
|
|
|
None => Err(Determinacy::Undetermined),
|
|
|
|
},
|
2016-11-10 04:29:36 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-17 02:16:32 -06:00
|
|
|
pub fn resolve_legacy_scope(&mut self,
|
2016-11-22 19:51:37 -06:00
|
|
|
mut scope: &'a Cell<LegacyScope<'a>>,
|
2016-11-17 02:16:32 -06:00
|
|
|
name: Name,
|
|
|
|
record_used: bool)
|
|
|
|
-> Option<MacroBinding<'a>> {
|
2016-10-31 17:17:15 -05:00
|
|
|
let mut possible_time_travel = None;
|
2016-10-06 03:04:30 -05:00
|
|
|
let mut relative_depth: u32 = 0;
|
2016-11-10 04:29:36 -06:00
|
|
|
let mut binding = None;
|
2016-09-07 18:21:59 -05:00
|
|
|
loop {
|
2016-11-22 19:51:37 -06:00
|
|
|
match scope.get() {
|
2016-10-06 03:04:30 -05:00
|
|
|
LegacyScope::Empty => break,
|
|
|
|
LegacyScope::Expansion(invocation) => {
|
2016-11-22 19:51:37 -06:00
|
|
|
match invocation.expansion.get() {
|
|
|
|
LegacyScope::Invocation(_) => scope.set(invocation.legacy_scope.get()),
|
|
|
|
LegacyScope::Empty => {
|
|
|
|
if possible_time_travel.is_none() {
|
|
|
|
possible_time_travel = Some(scope);
|
|
|
|
}
|
|
|
|
scope = &invocation.legacy_scope;
|
|
|
|
}
|
|
|
|
_ => {
|
|
|
|
relative_depth += 1;
|
|
|
|
scope = &invocation.expansion;
|
2016-10-31 17:17:15 -05:00
|
|
|
}
|
2016-10-01 20:41:19 -05:00
|
|
|
}
|
2016-10-06 03:04:30 -05:00
|
|
|
}
|
|
|
|
LegacyScope::Invocation(invocation) => {
|
2016-10-10 22:48:54 -05:00
|
|
|
relative_depth = relative_depth.saturating_sub(1);
|
2016-11-22 19:51:37 -06:00
|
|
|
scope = &invocation.legacy_scope;
|
2016-10-10 22:48:54 -05:00
|
|
|
}
|
2016-11-10 04:29:36 -06:00
|
|
|
LegacyScope::Binding(potential_binding) => {
|
|
|
|
if potential_binding.name == name {
|
|
|
|
if (!self.use_extern_macros || record_used) && relative_depth > 0 {
|
|
|
|
self.disallowed_shadowing.push(potential_binding);
|
2016-10-06 03:04:30 -05:00
|
|
|
}
|
2016-11-10 04:29:36 -06:00
|
|
|
binding = Some(potential_binding);
|
|
|
|
break
|
2016-10-01 20:41:19 -05:00
|
|
|
}
|
2016-11-22 19:51:37 -06:00
|
|
|
scope = &potential_binding.parent;
|
2016-10-01 20:41:19 -05:00
|
|
|
}
|
2016-10-06 03:04:30 -05:00
|
|
|
};
|
2016-09-07 18:21:59 -05:00
|
|
|
}
|
2016-10-06 03:04:30 -05:00
|
|
|
|
2017-01-14 02:58:50 -06:00
|
|
|
let binding = if let Some(binding) = binding {
|
|
|
|
MacroBinding::Legacy(binding)
|
|
|
|
} else if let Some(binding) = self.builtin_macros.get(&name).cloned() {
|
|
|
|
if !self.use_extern_macros {
|
|
|
|
self.record_use(Ident::with_empty_ctxt(name), MacroNS, binding, DUMMY_SP);
|
|
|
|
}
|
|
|
|
MacroBinding::Modern(binding)
|
|
|
|
} else {
|
|
|
|
return None;
|
2016-11-10 04:29:36 -06:00
|
|
|
};
|
|
|
|
|
|
|
|
if !self.use_extern_macros {
|
|
|
|
if let Some(scope) = possible_time_travel {
|
|
|
|
// Check for disallowed shadowing later
|
|
|
|
self.lexical_macro_resolutions.push((name, scope));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Some(binding)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn finalize_current_module_macro_resolutions(&mut self) {
|
|
|
|
let module = self.current_module;
|
2016-12-04 21:51:11 -06:00
|
|
|
for &(ref path, span) in module.macro_resolutions.borrow().iter() {
|
|
|
|
match self.resolve_path(path, Some(MacroNS), Some(span)) {
|
2016-11-27 04:58:46 -06:00
|
|
|
PathResult::NonModule(_) => {},
|
|
|
|
PathResult::Failed(msg, _) => {
|
|
|
|
resolve_error(self, span, ResolutionError::FailedToResolve(&msg));
|
|
|
|
}
|
|
|
|
_ => unreachable!(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-06 05:44:38 -06:00
|
|
|
for &(mark, ident, span, kind) in module.legacy_macro_resolutions.borrow().iter() {
|
2016-11-22 19:51:37 -06:00
|
|
|
let legacy_scope = &self.invocations[&mark].legacy_scope;
|
2016-11-28 20:07:12 -06:00
|
|
|
let legacy_resolution = self.resolve_legacy_scope(legacy_scope, ident.name, true);
|
|
|
|
let resolution = self.resolve_lexical_macro_path_segment(ident, MacroNS, Some(span));
|
2017-02-06 05:44:38 -06:00
|
|
|
match (legacy_resolution, resolution) {
|
|
|
|
(Some(legacy_resolution), Ok(resolution)) => {
|
|
|
|
let (legacy_span, participle) = match legacy_resolution {
|
|
|
|
MacroBinding::Modern(binding)
|
|
|
|
if binding.def() == resolution.def() => continue,
|
|
|
|
MacroBinding::Modern(binding) => (binding.span, "imported"),
|
|
|
|
MacroBinding::Legacy(binding) => (binding.span, "defined"),
|
|
|
|
};
|
|
|
|
let msg1 = format!("`{}` could refer to the macro {} here", ident, participle);
|
|
|
|
let msg2 = format!("`{}` could also refer to the macro imported here", ident);
|
|
|
|
self.session.struct_span_err(span, &format!("`{}` is ambiguous", ident))
|
|
|
|
.span_note(legacy_span, &msg1)
|
|
|
|
.span_note(resolution.span, &msg2)
|
|
|
|
.emit();
|
|
|
|
},
|
2017-01-09 03:31:14 -06:00
|
|
|
(Some(MacroBinding::Modern(binding)), Err(_)) => {
|
2017-01-14 02:58:50 -06:00
|
|
|
self.record_use(ident, MacroNS, binding, span);
|
2017-01-09 03:31:14 -06:00
|
|
|
self.err_if_macro_use_proc_macro(ident.name, span, binding);
|
|
|
|
},
|
2017-02-06 05:44:38 -06:00
|
|
|
(None, Err(_)) => {
|
|
|
|
let msg = match kind {
|
|
|
|
MacroKind::Bang =>
|
|
|
|
format!("cannot find macro `{}!` in this scope", ident),
|
|
|
|
MacroKind::Attr =>
|
|
|
|
format!("cannot find attribute macro `{}` in this scope", ident),
|
|
|
|
MacroKind::Derive =>
|
|
|
|
format!("cannot find derive macro `{}` in this scope", ident),
|
|
|
|
};
|
|
|
|
let mut err = self.session.struct_span_err(span, &msg);
|
|
|
|
self.suggest_macro_name(&ident.name.as_str(), kind, &mut err);
|
|
|
|
err.emit();
|
|
|
|
},
|
|
|
|
_ => {},
|
2016-11-10 04:29:36 -06:00
|
|
|
};
|
2016-10-31 17:17:15 -05:00
|
|
|
}
|
2016-09-07 18:21:59 -05:00
|
|
|
}
|
2016-09-21 01:25:09 -05:00
|
|
|
|
2017-02-06 05:44:38 -06:00
|
|
|
fn suggest_macro_name(&mut self, name: &str, kind: MacroKind,
|
|
|
|
err: &mut DiagnosticBuilder<'a>) {
|
2017-02-23 03:48:20 -06:00
|
|
|
// First check if this is a locally-defined bang macro.
|
|
|
|
let suggestion = if let MacroKind::Bang = kind {
|
|
|
|
find_best_match_for_name(self.macro_names.iter(), name, None)
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
// Then check builtin macros.
|
|
|
|
}.or_else(|| {
|
|
|
|
// FIXME: get_macro needs an &mut Resolver, can we do it without cloning?
|
|
|
|
let builtin_macros = self.builtin_macros.clone();
|
|
|
|
let names = builtin_macros.iter().filter_map(|(name, binding)| {
|
|
|
|
if binding.get_macro(self).kind() == kind {
|
|
|
|
Some(name)
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
});
|
|
|
|
find_best_match_for_name(names, name, None)
|
|
|
|
// Then check modules.
|
|
|
|
}).or_else(|| {
|
|
|
|
if !self.use_extern_macros {
|
|
|
|
return None;
|
2017-02-06 05:44:38 -06:00
|
|
|
}
|
2017-02-23 03:48:20 -06:00
|
|
|
let is_macro = |def| {
|
|
|
|
if let Def::Macro(_, def_kind) = def {
|
|
|
|
def_kind == kind
|
|
|
|
} else {
|
|
|
|
false
|
|
|
|
}
|
|
|
|
};
|
|
|
|
let ident = Ident::from_str(name);
|
|
|
|
self.lookup_typo_candidate(&vec![ident], MacroNS, is_macro)
|
|
|
|
.as_ref().map(|s| Symbol::intern(s))
|
|
|
|
});
|
|
|
|
|
2017-02-06 05:44:38 -06:00
|
|
|
if let Some(suggestion) = suggestion {
|
2016-09-07 18:21:59 -05:00
|
|
|
if suggestion != name {
|
2017-02-06 05:44:38 -06:00
|
|
|
if let MacroKind::Bang = kind {
|
|
|
|
err.help(&format!("did you mean `{}!`?", suggestion));
|
|
|
|
} else {
|
|
|
|
err.help(&format!("did you mean `{}`?", suggestion));
|
|
|
|
}
|
2016-09-07 18:21:59 -05:00
|
|
|
} else {
|
|
|
|
err.help(&format!("have you added the `#[macro_use]` on the module/import?"));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-09-21 01:25:09 -05:00
|
|
|
|
2016-10-06 03:04:30 -05:00
|
|
|
fn collect_def_ids(&mut self, invocation: &'a InvocationData<'a>, expansion: &Expansion) {
|
2016-10-03 18:48:19 -05:00
|
|
|
let Resolver { ref mut invocations, arenas, graph_root, .. } = *self;
|
2017-03-05 02:14:21 -06:00
|
|
|
let InvocationData { def_index, const_expr, .. } = *invocation;
|
2016-09-16 03:50:34 -05:00
|
|
|
|
2016-09-23 16:13:59 -05:00
|
|
|
let visit_macro_invoc = &mut |invoc: map::MacroInvocationData| {
|
2016-10-03 18:48:19 -05:00
|
|
|
invocations.entry(invoc.mark).or_insert_with(|| {
|
|
|
|
arenas.alloc_invocation_data(InvocationData {
|
2016-09-16 03:50:34 -05:00
|
|
|
def_index: invoc.def_index,
|
2017-03-05 02:14:21 -06:00
|
|
|
const_expr: invoc.const_expr,
|
2016-09-16 03:50:34 -05:00
|
|
|
module: Cell::new(graph_root),
|
2016-10-06 03:04:30 -05:00
|
|
|
expansion: Cell::new(LegacyScope::Empty),
|
|
|
|
legacy_scope: Cell::new(LegacyScope::Empty),
|
2016-09-16 03:50:34 -05:00
|
|
|
})
|
2016-09-14 04:55:20 -05:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2016-09-16 01:45:03 -05:00
|
|
|
let mut def_collector = DefCollector::new(&mut self.definitions);
|
2016-09-14 04:55:20 -05:00
|
|
|
def_collector.visit_macro_invoc = Some(visit_macro_invoc);
|
2016-09-28 21:23:19 -05:00
|
|
|
def_collector.with_parent(def_index, |def_collector| {
|
2017-03-05 02:14:21 -06:00
|
|
|
if const_expr {
|
2016-09-28 21:23:19 -05:00
|
|
|
if let Expansion::Expr(ref expr) = *expansion {
|
2017-03-05 02:14:21 -06:00
|
|
|
def_collector.visit_const_expr(expr);
|
2016-09-28 21:23:19 -05:00
|
|
|
}
|
|
|
|
}
|
2016-09-23 16:13:59 -05:00
|
|
|
expansion.visit_with(def_collector)
|
|
|
|
});
|
2016-09-14 04:55:20 -05:00
|
|
|
}
|
2016-12-01 05:20:04 -06:00
|
|
|
|
|
|
|
pub fn define_macro(&mut self, item: &ast::Item, legacy_scope: &mut LegacyScope<'a>) {
|
|
|
|
if item.ident.name == "macro_rules" {
|
|
|
|
self.session.span_err(item.span, "user-defined macros may not be named `macro_rules`");
|
|
|
|
}
|
|
|
|
|
2017-03-04 23:15:58 -06:00
|
|
|
let invocation = self.arenas.alloc_invocation_data(InvocationData {
|
|
|
|
module: Cell::new(self.current_module),
|
|
|
|
// FIXME(jseyfried) the following are irrelevant
|
|
|
|
def_index: CRATE_DEF_INDEX, const_integer: false,
|
|
|
|
legacy_scope: Cell::new(LegacyScope::Empty), expansion: Cell::new(LegacyScope::Empty),
|
|
|
|
});
|
|
|
|
if let ast::ItemKind::MacroDef(_, mark) = item.node {
|
|
|
|
self.invocations.insert(mark, invocation);
|
|
|
|
}
|
2016-12-01 05:20:04 -06:00
|
|
|
|
|
|
|
*legacy_scope = LegacyScope::Binding(self.arenas.alloc_legacy_binding(LegacyBinding {
|
|
|
|
parent: Cell::new(*legacy_scope),
|
2017-03-04 23:15:58 -06:00
|
|
|
name: item.ident.name,
|
|
|
|
ext: Rc::new(macro_rules::compile(&self.session.parse_sess, item)),
|
|
|
|
span: item.span,
|
2016-12-01 05:20:04 -06:00
|
|
|
}));
|
2017-03-04 23:15:58 -06:00
|
|
|
self.macro_names.insert(item.ident.name);
|
2016-12-01 05:20:04 -06:00
|
|
|
|
2017-03-04 23:15:58 -06:00
|
|
|
if attr::contains_name(&item.attrs, "macro_export") {
|
2016-12-01 05:20:04 -06:00
|
|
|
self.macro_exports.push(Export {
|
2017-03-04 23:15:58 -06:00
|
|
|
name: item.ident.name,
|
|
|
|
def: Def::Macro(self.definitions.local_def_id(item.id), MacroKind::Bang),
|
2016-12-01 05:20:04 -06:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
2017-01-09 03:31:14 -06:00
|
|
|
|
|
|
|
/// Error if `ext` is a Macros 1.1 procedural macro being imported by `#[macro_use]`
|
|
|
|
fn err_if_macro_use_proc_macro(&mut self, name: Name, use_span: Span,
|
|
|
|
binding: &NameBinding<'a>) {
|
|
|
|
use self::SyntaxExtension::*;
|
|
|
|
|
|
|
|
let krate = binding.def().def_id().krate;
|
|
|
|
|
|
|
|
// Plugin-based syntax extensions are exempt from this check
|
|
|
|
if krate == BUILTIN_MACROS_CRATE { return; }
|
|
|
|
|
|
|
|
let ext = binding.get_macro(self);
|
|
|
|
|
|
|
|
match *ext {
|
|
|
|
// If `ext` is a procedural macro, check if we've already warned about it
|
|
|
|
AttrProcMacro(_) | ProcMacro(_) => if !self.warned_proc_macros.insert(name) { return; },
|
|
|
|
_ => return,
|
|
|
|
}
|
|
|
|
|
|
|
|
let warn_msg = match *ext {
|
|
|
|
AttrProcMacro(_) => "attribute procedural macros cannot be \
|
|
|
|
imported with `#[macro_use]`",
|
|
|
|
ProcMacro(_) => "procedural macros cannot be imported with `#[macro_use]`",
|
|
|
|
_ => return,
|
|
|
|
};
|
|
|
|
|
|
|
|
let crate_name = self.session.cstore.crate_name(krate);
|
|
|
|
|
|
|
|
self.session.struct_span_err(use_span, warn_msg)
|
|
|
|
.help(&format!("instead, import the procedural macro like any other item: \
|
|
|
|
`use {}::{};`", crate_name, name))
|
|
|
|
.emit();
|
|
|
|
}
|
2017-02-01 18:33:42 -06:00
|
|
|
|
|
|
|
fn gate_legacy_custom_derive(&mut self, name: Symbol, span: Span) {
|
|
|
|
if !self.session.features.borrow().custom_derive {
|
|
|
|
let sess = &self.session.parse_sess;
|
|
|
|
let explain = feature_gate::EXPLAIN_CUSTOM_DERIVE;
|
|
|
|
emit_feature_err(sess, "custom_derive", span, GateIssue::Language, explain);
|
|
|
|
} else if !self.is_whitelisted_legacy_custom_derive(name) {
|
|
|
|
self.session.span_warn(span, feature_gate::EXPLAIN_DEPR_CUSTOM_DERIVE);
|
|
|
|
}
|
|
|
|
}
|
2016-09-07 18:21:59 -05:00
|
|
|
}
|