rust/src/librustc_resolve/macros.rs

929 lines
40 KiB
Rust
Raw Normal View History

// 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.
use {AmbiguityError, CrateLint, Resolver, ResolutionError, resolve_error};
use {Module, ModuleKind, NameBinding, NameBindingKind, PathResult};
2016-11-10 10:29:36 +00:00
use Namespace::{self, MacroNS};
use build_reduced_graph::BuildReducedGraphVisitor;
2016-11-10 10:11:25 +00:00
use resolve_imports::ImportResolver;
use rustc::hir::def_id::{DefId, BUILTIN_MACROS_CRATE, CRATE_DEF_INDEX, DefIndex,
DefIndexAddressSpace};
2016-10-28 06:52:45 +00:00
use rustc::hir::def::{Def, Export};
use rustc::hir::map::{self, DefCollector};
2017-05-11 08:26:07 +00:00
use rustc::{ty, lint};
use syntax::ast::{self, Name, Ident};
use syntax::attr::{self, HasAttrs};
use syntax::errors::DiagnosticBuilder;
use syntax::ext::base::{self, Annotatable, Determinacy, MultiModifier, MultiDecorator};
use syntax::ext::base::{MacroKind, SyntaxExtension, Resolver as SyntaxResolver};
use syntax::ext::expand::{self, AstFragment, AstFragmentKind, Invocation, InvocationKind};
use syntax::ext::hygiene::{self, Mark};
use syntax::ext::placeholders::placeholder;
use syntax::ext::tt::macro_rules;
use syntax::feature_gate::{self, emit_feature_err, GateIssue};
2016-12-31 07:25:59 +00:00
use syntax::fold::{self, Folder};
2017-03-08 23:13:35 +00:00
use syntax::parse::parser::PathStyle;
use syntax::parse::token::{self, Token};
use syntax::ptr::P;
use syntax::symbol::{Symbol, keywords};
2017-03-08 23:13:35 +00:00
use syntax::tokenstream::{TokenStream, TokenTree, Delimited};
use syntax::util::lev_distance::find_best_match_for_name;
use syntax_pos::{Span, DUMMY_SP};
use std::cell::Cell;
use std::mem;
2018-02-27 16:11:14 +00:00
use rustc_data_structures::sync::Lrc;
#[derive(Clone)]
pub struct InvocationData<'a> {
2016-09-16 08:50:34 +00:00
pub module: Cell<Module<'a>>,
2016-10-16 03:39:52 +00:00
pub def_index: DefIndex,
2016-10-06 08:04:30 +00: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>>,
}
impl<'a> InvocationData<'a> {
pub fn root(graph_root: Module<'a>) -> Self {
InvocationData {
2016-09-16 08:50:34 +00:00
module: Cell::new(graph_root),
def_index: CRATE_DEF_INDEX,
2016-10-06 08:04:30 +00:00
legacy_scope: Cell::new(LegacyScope::Empty),
expansion: Cell::new(LegacyScope::Empty),
}
}
}
2016-10-06 08:04:30 +00: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> {
pub parent: Cell<LegacyScope<'a>>,
2017-03-22 08:39:51 +00:00
pub ident: Ident,
def_id: DefId,
pub span: Span,
2016-10-06 08:04:30 +00:00
}
pub struct ProcMacError {
crate_name: Symbol,
name: Symbol,
module: ast::NodeId,
use_span: Span,
warn_msg: &'static str,
}
#[derive(Copy, Clone)]
2016-11-10 10:29:36 +00:00
pub enum MacroBinding<'a> {
Legacy(&'a LegacyBinding<'a>),
2017-03-16 01:39:47 +00:00
Global(&'a NameBinding<'a>),
2016-11-10 10:29:36 +00:00
Modern(&'a NameBinding<'a>),
}
impl<'a> MacroBinding<'a> {
pub fn span(self) -> Span {
match self {
MacroBinding::Legacy(binding) => binding.span,
2017-03-16 01:39:47 +00:00
MacroBinding::Global(binding) | MacroBinding::Modern(binding) => binding.span,
}
}
pub fn binding(self) -> &'a NameBinding<'a> {
match self {
2017-03-16 01:39:47 +00:00
MacroBinding::Global(binding) | MacroBinding::Modern(binding) => binding,
MacroBinding::Legacy(_) => panic!("unexpected MacroBinding::Legacy"),
}
}
pub fn def_ignoring_ambiguity(self) -> Def {
match self {
MacroBinding::Legacy(binding) => Def::Macro(binding.def_id, MacroKind::Bang),
MacroBinding::Global(binding) | MacroBinding::Modern(binding) =>
binding.def_ignoring_ambiguity(),
}
}
}
impl<'a> base::Resolver for Resolver<'a> {
fn next_node_id(&mut self) -> ast::NodeId {
self.session.next_node_id()
}
fn get_module_scope(&mut self, id: ast::NodeId) -> Mark {
2017-03-22 08:39:51 +00:00
let mark = Mark::fresh(Mark::root());
let module = self.module_map[&self.definitions.local_def_id(id)];
self.invocations.insert(mark, self.arenas.alloc_invocation_data(InvocationData {
2016-09-16 08:50:34 +00:00
module: Cell::new(module),
def_index: module.def_id().unwrap().index,
2016-10-06 08:04:30 +00:00
legacy_scope: Cell::new(LegacyScope::Empty),
expansion: Cell::new(LegacyScope::Empty),
2016-09-16 08:50:34 +00:00
}));
mark
}
fn eliminate_crate_var(&mut self, item: P<ast::Item>) -> P<ast::Item> {
2017-05-10 11:19:29 +00:00
struct EliminateCrateVar<'b, 'a: 'b>(&'b mut Resolver<'a>, Span);
impl<'a, 'b> Folder for EliminateCrateVar<'a, 'b> {
fn fold_path(&mut self, path: ast::Path) -> ast::Path {
match self.fold_qpath(None, path) {
(None, path) => path,
_ => unreachable!(),
}
}
fn fold_qpath(&mut self, mut qself: Option<ast::QSelf>, mut path: ast::Path)
-> (Option<ast::QSelf>, ast::Path) {
qself = qself.map(|ast::QSelf { ty, path_span, position }| {
ast::QSelf {
ty: self.fold_ty(ty),
path_span: self.new_span(path_span),
position,
}
});
2018-06-24 16:12:00 +00:00
if path.segments[0].ident.name == keywords::DollarCrate.name() {
let module = self.0.resolve_crate_root(path.segments[0].ident);
path.segments[0].ident.name = keywords::CrateRoot.name();
if !module.is_local() {
2018-03-19 00:54:56 +00:00
let span = path.segments[0].ident.span;
path.segments.insert(1, match module.kind {
ModuleKind::Def(_, name) => ast::PathSegment::from_ident(
2018-03-19 00:54:56 +00:00
ast::Ident::with_empty_ctxt(name).with_span_pos(span)
),
_ => unreachable!(),
});
if let Some(qself) = &mut qself {
qself.position += 1;
}
}
}
(qself, path)
}
fn fold_mac(&mut self, mac: ast::Mac) -> ast::Mac {
fold::noop_fold_mac(mac, self)
}
}
2017-05-10 11:19:29 +00:00
EliminateCrateVar(self, item.span).fold_item(item).expect_one("")
}
fn is_whitelisted_legacy_custom_derive(&self, name: Name) -> bool {
self.whitelisted_legacy_custom_derives.contains(&name)
}
fn visit_ast_fragment_with_placeholders(&mut self, mark: Mark, fragment: &AstFragment,
derives: &[Mark]) {
2016-10-06 08:04:30 +00:00
let invocation = self.invocations[&mark];
self.collect_def_ids(mark, invocation, fragment);
2016-10-06 08:04:30 +00:00
self.current_module = invocation.module.get();
self.current_module.unresolved_invocations.borrow_mut().remove(&mark);
self.current_module.unresolved_invocations.borrow_mut().extend(derives);
for &derive in derives {
self.invocations.insert(derive, invocation);
}
2016-10-06 08:04:30 +00:00
let mut visitor = BuildReducedGraphVisitor {
resolver: self,
legacy_scope: LegacyScope::Invocation(invocation),
expansion: mark,
2016-10-06 08:04:30 +00:00
};
fragment.visit_with(&mut visitor);
2016-10-06 08:04:30 +00:00
invocation.expansion.set(visitor.legacy_scope);
}
2018-02-27 16:11:14 +00:00
fn add_builtin(&mut self, ident: ast::Ident, ext: Lrc<SyntaxExtension>) {
2016-10-28 06:52:45 +00:00
let def_id = DefId {
krate: BUILTIN_MACROS_CRATE,
index: DefIndex::from_array_index(self.macro_map.len(),
DefIndexAddressSpace::Low),
2016-10-28 06:52:45 +00:00
};
2017-02-23 09:42:33 +00:00
let kind = ext.kind();
2016-10-28 06:52:45 +00:00
self.macro_map.insert(def_id, ext);
let binding = self.arenas.alloc_name_binding(NameBinding {
2017-02-23 09:42:33 +00:00
kind: NameBindingKind::Def(Def::Macro(def_id, kind)),
span: DUMMY_SP,
vis: ty::Visibility::Invisible,
expansion: Mark::root(),
});
2017-03-16 01:39:47 +00:00
self.global_macros.insert(ident.name, binding);
}
2016-11-10 10:11:25 +00:00
fn resolve_imports(&mut self) {
ImportResolver { resolver: self }.resolve_imports()
}
// Resolves attribute and derive legacy macros from `#![plugin(..)]`.
fn find_legacy_attr_invoc(&mut self, attrs: &mut Vec<ast::Attribute>, allow_derive: bool)
-> Option<ast::Attribute> {
for i in 0..attrs.len() {
let name = attrs[i].name();
if self.session.plugin_attributes.borrow().iter()
.any(|&(ref attr_nm, _)| name == &**attr_nm) {
attr::mark_known(&attrs[i]);
}
2017-03-16 01:39:47 +00:00
match self.global_macros.get(&name).cloned() {
2016-11-27 10:27:41 +00:00
Some(binding) => match *binding.get_macro(self) {
2016-09-06 05:57:58 +00:00
MultiModifier(..) | MultiDecorator(..) | SyntaxExtension::AttrProcMacro(..) => {
return Some(attrs.remove(i))
}
_ => {}
},
None => {}
}
}
if !allow_derive { return None }
// Check for legacy derives
for i in 0..attrs.len() {
let name = attrs[i].name();
if name == "derive" {
let result = attrs[i].parse_list(&self.session.parse_sess, |parser| {
parser.parse_path_allowing_meta(PathStyle::Mod)
});
2017-03-08 23:13:35 +00:00
let mut traits = match result {
Ok(traits) => traits,
Err(mut e) => {
e.cancel();
continue
}
};
for j in 0..traits.len() {
2017-03-08 23:13:35 +00:00
if traits[j].segments.len() > 1 {
continue
}
let trait_name = traits[j].segments[0].ident.name;
2017-03-08 23:13:35 +00:00
let legacy_name = Symbol::intern(&format!("derive_{}", trait_name));
2017-03-16 01:39:47 +00:00
if !self.global_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 {
2017-03-08 23:13:35 +00:00
let mut tokens = Vec::new();
for (j, path) in traits.iter().enumerate() {
if j > 0 {
2017-03-08 23:13:35 +00:00
tokens.push(TokenTree::Token(attrs[i].span, Token::Comma).into());
}
for (k, segment) in path.segments.iter().enumerate() {
if k > 0 {
2017-03-08 23:13:35 +00:00
tokens.push(TokenTree::Token(path.span, Token::ModSep).into());
}
let tok = Token::from_ast_ident(segment.ident);
2017-03-08 23:13:35 +00:00
tokens.push(TokenTree::Token(path.span, tok).into());
}
}
attrs[i].tokens = TokenTree::Delimited(attrs[i].span, Delimited {
delim: token::Paren,
tts: TokenStream::concat(tokens).into(),
}).into();
}
return Some(ast::Attribute {
2018-03-19 00:54:56 +00:00
path: ast::Path::from_ident(Ident::new(legacy_name, span)),
tokens: TokenStream::empty(),
id: attr::mk_attr_id(),
style: ast::AttrStyle::Outer,
is_sugared_doc: false,
span,
});
}
}
}
None
}
fn resolve_invoc(&mut self, invoc: &mut Invocation, scope: Mark, force: bool)
2018-02-27 16:11:14 +00:00
-> Result<Option<Lrc<SyntaxExtension>>, Determinacy> {
let def = match invoc.kind {
InvocationKind::Attr { attr: None, .. } => return Ok(None),
2017-06-04 05:10:14 +00:00
_ => self.resolve_invoc_to_def(invoc, scope, force)?,
};
let def_id = def.def_id();
2017-03-24 23:03:15 +00:00
self.macro_defs.insert(invoc.expansion_data.mark, def_id);
2017-03-24 23:03:15 +00:00
let normal_module_def_id =
self.macro_def_scope(invoc.expansion_data.mark).normal_ancestor_id;
self.definitions.add_parent_module_of_macro_def(invoc.expansion_data.mark,
normal_module_def_id);
2017-03-24 23:03:15 +00:00
self.unused_macros.remove(&def_id);
2017-03-22 08:39:51 +00:00
let ext = self.get_macro(def);
invoc.expansion_data.mark.set_default_transparency(ext.default_transparency());
invoc.expansion_data.mark.set_is_builtin(def_id.krate == BUILTIN_MACROS_CRATE);
2017-03-22 08:39:51 +00:00
Ok(Some(ext))
}
fn resolve_macro(&mut self, scope: Mark, path: &ast::Path, kind: MacroKind, force: bool)
2018-02-27 16:11:14 +00:00
-> Result<Lrc<SyntaxExtension>, Determinacy> {
2017-05-11 08:26:07 +00:00
self.resolve_macro_to_def(scope, path, kind, force).map(|def| {
2017-05-15 05:35:19 +00:00
self.unused_macros.remove(&def.def_id());
2017-05-11 08:26:07 +00:00
self.get_macro(def)
})
}
fn check_unused_macros(&self) {
2017-05-15 05:35:19 +00:00
for did in self.unused_macros.iter() {
let id_span = match *self.macro_map[did] {
SyntaxExtension::NormalTT { def_info, .. } |
SyntaxExtension::DeclMacro { def_info, .. } => def_info,
2017-05-15 05:35:19 +00:00
_ => None,
};
if let Some((id, span)) = id_span {
2017-05-11 08:26:07 +00:00
let lint = lint::builtin::UNUSED_MACROS;
rustc: Rearchitect lints to be emitted more eagerly In preparation for incremental compilation this commit refactors the lint handling infrastructure in the compiler to be more "eager" and overall more incremental-friendly. Many passes of the compiler can emit lints at various points but before this commit all lints were buffered in a table to be emitted at the very end of compilation. This commit changes these lints to be emitted immediately during compilation using pre-calculated lint level-related data structures. Linting today is split into two phases, one set of "early" lints run on the `syntax::ast` and a "late" set of lints run on the HIR. This commit moves the "early" lints to running as late as possible in compilation, just before HIR lowering. This notably means that we're catching resolve-related lints just before HIR lowering. The early linting remains a pass very similar to how it was before, maintaining context of the current lint level as it walks the tree. Post-HIR, however, linting is structured as a method on the `TyCtxt` which transitively executes a query to calculate lint levels. Each request to lint on a `TyCtxt` will query the entire crate's 'lint level data structure' and then go from there about whether the lint should be emitted or not. The query depends on the entire HIR crate but should be very quick to calculate (just a quick walk of the HIR) and the red-green system should notice that the lint level data structure rarely changes, and should hopefully preserve incrementality. Overall this resulted in a pretty big change to the test suite now that lints are emitted much earlier in compilation (on-demand vs only at the end). This in turn necessitated the addition of many `#![allow(warnings)]` directives throughout the compile-fail test suite and a number of updates to the UI test suite.
2017-07-27 04:51:09 +00:00
let msg = "unused macro definition";
self.session.buffer_lint(lint, id, span, msg);
2017-05-11 08:26:07 +00:00
} else {
bug!("attempted to create unused macro error, but span not available");
}
}
}
}
impl<'a> Resolver<'a> {
fn resolve_invoc_to_def(&mut self, invoc: &mut Invocation, scope: Mark, force: bool)
-> Result<Def, Determinacy> {
let (attr, traits, item) = match invoc.kind {
InvocationKind::Attr { ref mut attr, ref traits, ref mut item } => (attr, traits, item),
InvocationKind::Bang { ref mac, .. } => {
return self.resolve_macro_to_def(scope, &mac.node.path, MacroKind::Bang, force);
}
2017-03-08 23:13:35 +00:00
InvocationKind::Derive { ref path, .. } => {
return self.resolve_macro_to_def(scope, path, MacroKind::Derive, force);
}
};
let path = attr.as_ref().unwrap().path.clone();
let mut determinacy = Determinacy::Determined;
match self.resolve_macro_to_def(scope, &path, MacroKind::Attr, force) {
Ok(def) => return Ok(def),
Err(Determinacy::Undetermined) => determinacy = Determinacy::Undetermined,
Err(Determinacy::Determined) if force => return Err(Determinacy::Determined),
Err(Determinacy::Determined) => {}
}
// Ok at this point we've determined that the `attr` above doesn't
// actually resolve at this time, so we may want to report an error.
// It could be the case, though, that `attr` won't ever resolve! If
// there's a custom derive that could be used it might declare `attr` as
// a custom attribute accepted by the derive. In this case we don't want
// to report this particular invocation as unresolved, but rather we'd
// want to move on to the next invocation.
//
// This loop here looks through all of the derive annotations in scope
// and tries to resolve them. If they themselves successfully resolve
// *and* the resolve mentions that this attribute's name is a registered
// custom attribute then we flag this attribute as known and update
// `invoc` above to point to the next invocation.
//
// By then returning `Undetermined` we should continue resolution to
// resolve the next attribute.
let attr_name = match path.segments.len() {
1 => path.segments[0].ident.name,
_ => return Err(determinacy),
};
2017-03-08 23:13:35 +00:00
for path in traits {
match self.resolve_macro(scope, path, MacroKind::Derive, force) {
Ok(ext) => if let SyntaxExtension::ProcMacroDerive(_, ref inert_attrs, _) = *ext {
if inert_attrs.contains(&attr_name) {
// FIXME(jseyfried) Avoid `mem::replace` here.
let dummy_item = placeholder(AstFragmentKind::Items, ast::DUMMY_NODE_ID)
.make_items().pop().unwrap();
let dummy_item = Annotatable::Item(dummy_item);
*item = mem::replace(item, dummy_item).map_attrs(|mut attrs| {
let inert_attr = attr.take().unwrap();
attr::mark_known(&inert_attr);
if self.proc_macro_enabled {
*attr = expand::find_attr_invoc(&mut attrs);
}
attrs.push(inert_attr);
attrs
});
return Err(Determinacy::Undetermined)
}
},
Err(Determinacy::Undetermined) => determinacy = Determinacy::Undetermined,
Err(Determinacy::Determined) => {}
}
}
Err(determinacy)
}
fn resolve_macro_to_def(&mut self, scope: Mark, path: &ast::Path, kind: MacroKind, force: bool)
-> Result<Def, Determinacy> {
if kind != MacroKind::Bang && path.segments.len() > 1 {
rustc: Tweak custom attribute capabilities This commit starts to lay some groundwork for the stabilization of custom attribute invocations and general procedural macros. It applies a number of changes discussed on [internals] as well as a [recent issue][issue], namely: * The path used to specify a custom attribute must be of length one and cannot be a global path. This'll help future-proof us against any ambiguities and give us more time to settle the precise syntax. In the meantime though a bare identifier can be used and imported to invoke a custom attribute macro. A new feature gate, `proc_macro_path_invoc`, was added to gate multi-segment paths and absolute paths. * The set of items which can be annotated by a custom procedural attribute has been restricted. Statements, expressions, and modules are disallowed behind two new feature gates: `proc_macro_expr` and `proc_macro_mod`. * The input to procedural macro attributes has been restricted and adjusted. Today an invocation like `#[foo(bar)]` will receive `(bar)` as the input token stream, but after this PR it will only receive `bar` (the delimiters were removed). Invocations like `#[foo]` are still allowed and will be invoked in the same way as `#[foo()]`. This is a **breaking change** for all nightly users as the syntax coming in to procedural macros will be tweaked slightly. * Procedural macros (`foo!()` style) can only be expanded to item-like items by default. A separate feature gate, `proc_macro_non_items`, is required to expand to items like expressions, statements, etc. Closes #50038 [internals]: https://internals.rust-lang.org/t/help-stabilize-a-subset-of-macros-2-0/7252 [issue]: https://github.com/rust-lang/rust/issues/50038
2018-04-20 14:50:39 +00:00
if !self.session.features_untracked().proc_macro_path_invoc {
emit_feature_err(
&self.session.parse_sess,
"proc_macro_path_invoc",
path.span,
GateIssue::Language,
"paths of length greater than one in macro invocations are \
currently unstable",
);
}
}
let def = self.resolve_macro_to_def_inner(scope, path, kind, force);
if def != Err(Determinacy::Undetermined) {
// Do not report duplicated errors on every undetermined resolution.
2018-02-23 17:48:54 +00:00
path.segments.iter().find(|segment| segment.args.is_some()).map(|segment| {
self.session.span_err(segment.args.as_ref().unwrap().span(),
"generic arguments in macro path");
});
}
def
}
2018-01-06 08:31:54 +00:00
pub fn resolve_macro_to_def_inner(&mut self, scope: Mark, path: &ast::Path,
kind: MacroKind, force: bool)
-> Result<Def, Determinacy> {
let ast::Path { ref segments, span } = *path;
let mut path: Vec<_> = segments.iter().map(|seg| seg.ident).collect();
2016-10-08 00:30:24 +00:00
let invocation = self.invocations[&scope];
let module = invocation.module.get();
self.current_module = if module.is_trait() { module.parent.unwrap() } else { module };
2016-11-27 10:58:46 +00:00
// Possibly apply the macro helper hack
if self.use_extern_macros && kind == MacroKind::Bang && path.len() == 1 &&
path[0].span.ctxt().outer().expn_info().map_or(false, |info| info.local_inner_macros) {
let root = Ident::new(keywords::DollarCrate.name(), path[0].span);
path.insert(0, root);
}
if path.len() > 1 {
2017-03-08 23:13:35 +00:00
if !self.use_extern_macros && self.gated_errors.insert(span) {
2016-11-27 10:58:46 +00:00
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);
self.found_unresolved_macro = true;
2016-11-27 10:58:46 +00:00
return Err(Determinacy::Determined);
}
let def = match self.resolve_path(&path, Some(MacroNS), false, span, CrateLint::No) {
PathResult::NonModule(path_res) => match path_res.base_def() {
Def::Err => Err(Determinacy::Determined),
def @ _ => {
if path_res.unresolved_segments() > 0 {
self.found_unresolved_macro = true;
self.session.span_err(span, "fail to resolve non-ident macro path");
Err(Determinacy::Determined)
} else {
Ok(def)
}
}
},
2016-11-27 10:58:46 +00:00
PathResult::Module(..) => unreachable!(),
PathResult::Indeterminate if !force => return Err(Determinacy::Undetermined),
_ => {
self.found_unresolved_macro = true;
Err(Determinacy::Determined)
},
2016-11-27 10:58:46 +00:00
};
self.current_module.nearest_item_scope().macro_resolutions.borrow_mut()
.push((path.into_boxed_slice(), span));
return def;
2016-11-27 10:58:46 +00:00
}
2018-03-18 13:47:09 +00:00
let legacy_resolution = self.resolve_legacy_scope(&invocation.legacy_scope, path[0], false);
let result = if let Some(MacroBinding::Legacy(binding)) = legacy_resolution {
Ok(Def::Macro(binding.def_id, MacroKind::Bang))
} else {
2018-03-18 13:47:09 +00:00
match self.resolve_lexical_macro_path_segment(path[0], MacroNS, false, span) {
Ok(binding) => Ok(binding.binding().def_ignoring_ambiguity()),
Err(Determinacy::Undetermined) if !force => return Err(Determinacy::Undetermined),
Err(_) => {
self.found_unresolved_macro = true;
Err(Determinacy::Determined)
}
}
2016-11-10 10:29:36 +00:00
};
self.current_module.nearest_item_scope().legacy_macro_resolutions.borrow_mut()
.push((scope, path[0], kind, result.ok()));
result
}
2016-09-26 03:17:05 +00:00
2016-11-27 10:58:46 +00: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,
2017-03-22 08:39:51 +00:00
mut ident: Ident,
2016-11-27 10:58:46 +00:00
ns: Namespace,
record_used: bool,
path_span: Span)
-> Result<MacroBinding<'a>, Determinacy> {
2017-03-22 08:39:51 +00:00
ident = ident.modern();
let mut module = Some(self.current_module);
let mut potential_illegal_shadower = Err(Determinacy::Determined);
let determinacy =
if record_used { Determinacy::Determined } else { Determinacy::Undetermined };
2016-11-10 10:29:36 +00:00
loop {
2017-03-22 08:39:51 +00:00
let orig_current_module = self.current_module;
let result = if let Some(module) = module {
2017-03-22 08:39:51 +00:00
self.current_module = module; // Lexical resolutions can never be a privacy error.
// Since expanded macros may not shadow the lexical scope and
2017-03-16 01:39:47 +00:00
// globs may not shadow global macros (both enforced below),
// we resolve with restricted shadowing (indicated by the penultimate argument).
2017-03-22 08:39:51 +00:00
self.resolve_ident_in_module_unadjusted(
module, ident, ns, true, record_used, path_span,
).map(MacroBinding::Modern)
} else {
2017-03-16 01:39:47 +00:00
self.global_macros.get(&ident.name).cloned().ok_or(determinacy)
.map(MacroBinding::Global)
};
2017-03-22 08:39:51 +00:00
self.current_module = orig_current_module;
match result.map(MacroBinding::binding) {
2016-11-26 12:21:47 +00:00
Ok(binding) => {
if !record_used {
return result;
}
if let Ok(MacroBinding::Modern(shadower)) = potential_illegal_shadower {
if shadower.def() != binding.def() {
let name = ident.name;
2016-11-27 10:58:46 +00:00
self.ambiguity_errors.push(AmbiguityError {
span: path_span,
name,
b1: shadower,
b2: binding,
lexical: true,
2016-11-27 10:58:46 +00:00
});
return potential_illegal_shadower;
2016-11-27 10:58:46 +00:00
}
}
if binding.expansion != Mark::root() ||
(binding.is_glob_import() && module.unwrap().def().is_some()) {
potential_illegal_shadower = result;
} else {
return result;
2016-11-10 10:29:36 +00:00
}
},
2016-11-27 10:58:46 +00:00
Err(Determinacy::Undetermined) => return Err(Determinacy::Undetermined),
2016-11-26 12:21:47 +00:00
Err(Determinacy::Determined) => {}
2016-11-10 10:29:36 +00:00
}
module = match module {
Some(module) => self.hygienic_lexical_parent(module, &mut ident.span),
None => return potential_illegal_shadower,
2016-11-10 10:29:36 +00:00
}
}
}
2016-11-17 08:16:32 +00:00
pub fn resolve_legacy_scope(&mut self,
mut scope: &'a Cell<LegacyScope<'a>>,
2017-03-22 08:39:51 +00:00
ident: Ident,
2016-11-17 08:16:32 +00:00
record_used: bool)
-> Option<MacroBinding<'a>> {
2017-03-22 08:39:51 +00:00
let ident = ident.modern();
2016-10-31 22:17:15 +00:00
let mut possible_time_travel = None;
2016-10-06 08:04:30 +00:00
let mut relative_depth: u32 = 0;
2016-11-10 10:29:36 +00:00
let mut binding = None;
loop {
match scope.get() {
2016-10-06 08:04:30 +00:00
LegacyScope::Empty => break,
LegacyScope::Expansion(invocation) => {
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 22:17:15 +00:00
}
}
2016-10-06 08:04:30 +00:00
}
LegacyScope::Invocation(invocation) => {
2016-10-11 03:48:54 +00:00
relative_depth = relative_depth.saturating_sub(1);
scope = &invocation.legacy_scope;
2016-10-11 03:48:54 +00:00
}
2016-11-10 10:29:36 +00:00
LegacyScope::Binding(potential_binding) => {
2017-03-22 08:39:51 +00:00
if potential_binding.ident == ident {
2016-11-10 10:29:36 +00:00
if (!self.use_extern_macros || record_used) && relative_depth > 0 {
self.disallowed_shadowing.push(potential_binding);
2016-10-06 08:04:30 +00:00
}
2016-11-10 10:29:36 +00:00
binding = Some(potential_binding);
break
}
scope = &potential_binding.parent;
}
2016-10-06 08:04:30 +00:00
};
}
2016-10-06 08:04:30 +00:00
2017-01-14 08:58:50 +00:00
let binding = if let Some(binding) = binding {
MacroBinding::Legacy(binding)
2017-03-22 08:39:51 +00:00
} else if let Some(binding) = self.global_macros.get(&ident.name).cloned() {
2017-01-14 08:58:50 +00:00
if !self.use_extern_macros {
2017-03-22 08:39:51 +00:00
self.record_use(ident, MacroNS, binding, DUMMY_SP);
2017-01-14 08:58:50 +00:00
}
2017-03-16 01:39:47 +00:00
MacroBinding::Global(binding)
2017-01-14 08:58:50 +00:00
} else {
return None;
2016-11-10 10:29:36 +00:00
};
if !self.use_extern_macros {
if let Some(scope) = possible_time_travel {
// Check for disallowed shadowing later
2017-03-22 08:39:51 +00:00
self.lexical_macro_resolutions.push((ident, scope));
2016-11-10 10:29:36 +00:00
}
}
Some(binding)
}
pub fn finalize_current_module_macro_resolutions(&mut self) {
let module = self.current_module;
for &(ref path, span) in module.macro_resolutions.borrow().iter() {
match self.resolve_path(&path, Some(MacroNS), true, span, CrateLint::No) {
2016-11-27 10:58:46 +00:00
PathResult::NonModule(_) => {},
PathResult::Failed(span, msg, _) => {
2016-11-27 10:58:46 +00:00
resolve_error(self, span, ResolutionError::FailedToResolve(&msg));
}
_ => unreachable!(),
}
}
for &(mark, ident, kind, def) in module.legacy_macro_resolutions.borrow().iter() {
let span = ident.span;
let legacy_scope = &self.invocations[&mark].legacy_scope;
2017-03-22 08:39:51 +00:00
let legacy_resolution = self.resolve_legacy_scope(legacy_scope, ident, true);
let resolution = self.resolve_lexical_macro_path_segment(ident, MacroNS, true, span);
let check_consistency = |this: &Self, binding: MacroBinding| {
if let Some(def) = def {
if this.ambiguity_errors.is_empty() && this.disallowed_shadowing.is_empty() &&
binding.def_ignoring_ambiguity() != def {
// Make sure compilation does not succeed if preferred macro resolution
// has changed after the macro had been expanded. In theory all such
// situations should be reported as ambiguity errors, so this is span-bug.
span_bug!(span, "inconsistent resolution for a macro");
}
} else {
// It's possible that the macro was unresolved (indeterminate) and silently
// expanded into a dummy fragment for recovery during expansion.
// Now, post-expansion, the resolution may succeed, but we can't change the
// past and need to report an error.
let msg =
format!("cannot determine resolution for the {} `{}`", kind.descr(), ident);
let msg_note = "import resolution is stuck, try simplifying macro imports";
this.session.struct_span_err(span, &msg).note(msg_note).emit();
}
};
match (legacy_resolution, resolution) {
(Some(MacroBinding::Legacy(legacy_binding)), Ok(MacroBinding::Modern(binding))) => {
let msg1 = format!("`{}` could refer to the macro defined here", ident);
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_binding.span, &msg1)
.span_note(binding.span, &msg2)
.emit();
},
(None, Err(_)) => {
assert!(def.is_none());
let bang = if kind == MacroKind::Bang { "!" } else { "" };
let msg =
format!("cannot find {} `{}{}` in this scope", kind.descr(), ident, bang);
let mut err = self.session.struct_span_err(span, &msg);
2018-05-26 12:12:38 +00:00
self.suggest_macro_name(&ident.as_str(), kind, &mut err, span);
err.emit();
},
(Some(MacroBinding::Modern(_)), _) | (_, Ok(MacroBinding::Legacy(_))) => {
span_bug!(span, "impossible macro resolution result");
}
// OK, unambiguous resolution
(Some(binding), Err(_)) | (None, Ok(binding)) |
// OK, legacy wins over global even if their definitions are different
(Some(binding @ MacroBinding::Legacy(_)), Ok(MacroBinding::Global(_))) |
// OK, modern wins over global even if their definitions are different
(Some(MacroBinding::Global(_)), Ok(binding @ MacroBinding::Modern(_))) => {
check_consistency(self, binding);
}
(Some(MacroBinding::Global(binding1)), Ok(MacroBinding::Global(binding2))) => {
if binding1.def() != binding2.def() {
span_bug!(span, "mismatch between same global macro resolutions");
}
check_consistency(self, MacroBinding::Global(binding1));
self.record_use(ident, MacroNS, binding1, span);
self.err_if_macro_use_proc_macro(ident.name, span, binding1);
},
2016-11-10 10:29:36 +00:00
};
2016-10-31 22:17:15 +00:00
}
}
fn suggest_macro_name(&mut self, name: &str, kind: MacroKind,
err: &mut DiagnosticBuilder<'a>, span: Span) {
// First check if this is a locally-defined bang macro.
let suggestion = if let MacroKind::Bang = kind {
2017-03-22 08:39:51 +00:00
find_best_match_for_name(self.macro_names.iter().map(|ident| &ident.name), name, None)
} else {
None
2017-03-16 01:39:47 +00:00
// Then check global macros.
}.or_else(|| {
// FIXME: get_macro needs an &mut Resolver, can we do it without cloning?
2017-03-16 01:39:47 +00:00
let global_macros = self.global_macros.clone();
let names = global_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;
}
let is_macro = |def| {
if let Def::Macro(_, def_kind) = def {
def_kind == kind
} else {
false
}
};
2018-03-18 13:47:09 +00:00
let ident = Ident::new(Symbol::intern(name), span);
self.lookup_typo_candidate(&vec![ident], MacroNS, is_macro, span)
});
if let Some(suggestion) = suggestion {
if suggestion != name {
if let MacroKind::Bang = kind {
err.span_suggestion(span, "you could try the macro", suggestion.to_string());
} else {
2017-05-16 13:12:24 +00:00
err.span_suggestion(span, "try", suggestion.to_string());
}
} else {
err.help("have you added the `#[macro_use]` on the module/import?");
}
}
}
2017-03-24 23:03:15 +00:00
fn collect_def_ids(&mut self,
mark: Mark,
invocation: &'a InvocationData<'a>,
fragment: &AstFragment) {
let Resolver { ref mut invocations, arenas, graph_root, .. } = *self;
let InvocationData { def_index, .. } = *invocation;
2016-09-16 08:50:34 +00:00
let visit_macro_invoc = &mut |invoc: map::MacroInvocationData| {
invocations.entry(invoc.mark).or_insert_with(|| {
arenas.alloc_invocation_data(InvocationData {
2016-09-16 08:50:34 +00:00
def_index: invoc.def_index,
module: Cell::new(graph_root),
2016-10-06 08:04:30 +00:00
expansion: Cell::new(LegacyScope::Empty),
legacy_scope: Cell::new(LegacyScope::Empty),
2016-09-16 08:50:34 +00:00
})
});
};
2017-03-24 23:03:15 +00:00
let mut def_collector = DefCollector::new(&mut self.definitions, mark);
def_collector.visit_macro_invoc = Some(visit_macro_invoc);
def_collector.with_parent(def_index, |def_collector| {
fragment.visit_with(def_collector)
});
}
pub fn define_macro(&mut self,
item: &ast::Item,
expansion: Mark,
legacy_scope: &mut LegacyScope<'a>) {
self.local_macro_def_scopes.insert(item.id, self.current_module);
let ident = item.ident;
if ident.name == "macro_rules" {
self.session.span_err(item.span, "user-defined macros may not be named `macro_rules`");
}
let def_id = self.definitions.local_def_id(item.id);
2018-02-27 16:11:14 +00:00
let ext = Lrc::new(macro_rules::compile(&self.session.parse_sess,
2018-02-14 15:11:02 +00:00
&self.session.features_untracked(),
item, hygiene::default_edition()));
self.macro_map.insert(def_id, ext);
let def = match item.node { ast::ItemKind::MacroDef(ref def) => def, _ => unreachable!() };
if def.legacy {
2017-03-22 08:39:51 +00:00
let ident = ident.modern();
self.macro_names.insert(ident);
*legacy_scope = LegacyScope::Binding(self.arenas.alloc_legacy_binding(LegacyBinding {
2017-03-22 08:39:51 +00:00
parent: Cell::new(*legacy_scope), ident: ident, def_id: def_id, span: item.span,
}));
let def = Def::Macro(def_id, MacroKind::Bang);
self.all_macros.insert(ident.name, def);
if attr::contains_name(&item.attrs, "macro_export") {
self.macro_exports.push(Export {
ident: ident.modern(),
def: def,
vis: ty::Visibility::Public,
span: item.span,
});
} else {
self.unused_macros.insert(def_id);
}
2017-05-11 08:26:07 +00:00
} else {
let module = self.current_module;
let def = Def::Macro(def_id, MacroKind::Bang);
let vis = self.resolve_visibility(&item.vis);
if vis != ty::Visibility::Public {
self.unused_macros.insert(def_id);
}
self.define(module, ident, MacroNS, (def, vis, item.span, expansion));
}
}
/// 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>) {
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
SyntaxExtension::AttrProcMacro(..) | SyntaxExtension::ProcMacro { .. } =>
if !self.warned_proc_macros.insert(name) { return; },
_ => return,
}
let warn_msg = match *ext {
SyntaxExtension::AttrProcMacro(..) =>
"attribute procedural macros cannot be imported with `#[macro_use]`",
SyntaxExtension::ProcMacro { .. } =>
"procedural macros cannot be imported with `#[macro_use]`",
_ => return,
};
let def_id = self.current_module.normal_ancestor_id;
let node_id = self.definitions.as_local_node_id(def_id).unwrap();
self.proc_mac_errors.push(ProcMacError {
crate_name: self.cstore.crate_name_untracked(krate),
name,
module: node_id,
use_span,
warn_msg,
});
}
pub fn report_proc_macro_import(&mut self, krate: &ast::Crate) {
for err in self.proc_mac_errors.drain(..) {
let (span, found_use) = ::UsePlacementFinder::check(krate, err.module);
if let Some(span) = span {
let found_use = if found_use { "" } else { "\n" };
self.session.struct_span_err(err.use_span, err.warn_msg)
.span_suggestion(
span,
"instead, import the procedural macro like any other item",
format!("use {}::{};{}", err.crate_name, err.name, found_use),
).emit();
} else {
self.session.struct_span_err(err.use_span, err.warn_msg)
.help(&format!("instead, import the procedural macro like any other item: \
`use {}::{};`", err.crate_name, err.name))
.emit();
}
}
}
fn gate_legacy_custom_derive(&mut self, name: Symbol, span: Span) {
2018-02-14 15:11:02 +00:00
if !self.session.features_untracked().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);
}
}
}