2016-09-07 23:21:59 +00: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.
|
|
|
|
|
2018-07-23 02:52:51 +03:00
|
|
|
use {AmbiguityError, CrateLint, Resolver, ResolutionError, is_known_tool, resolve_error};
|
|
|
|
use {Module, ModuleKind, NameBinding, NameBindingKind, PathResult, ToNameBinding};
|
2018-08-09 16:29:22 +03:00
|
|
|
use ModuleOrUniformRoot;
|
2018-07-23 02:52:51 +03:00
|
|
|
use Namespace::{self, TypeNS, MacroNS};
|
2018-07-29 14:51:17 +03:00
|
|
|
use build_reduced_graph::{BuildReducedGraphVisitor, IsMacroExport};
|
2016-11-10 10:11:25 +00:00
|
|
|
use resolve_imports::ImportResolver;
|
2018-01-02 06:36:12 -05:00
|
|
|
use rustc::hir::def_id::{DefId, BUILTIN_MACROS_CRATE, CRATE_DEF_INDEX, DefIndex,
|
|
|
|
DefIndexAddressSpace};
|
2018-05-14 03:22:52 +03:00
|
|
|
use rustc::hir::def::{Def, NonMacroAttrKind};
|
2016-09-23 21:13:59 +00:00
|
|
|
use rustc::hir::map::{self, DefCollector};
|
2017-05-11 10:26:07 +02:00
|
|
|
use rustc::{ty, lint};
|
2018-07-31 15:23:31 -06:00
|
|
|
use rustc::middle::cstore::CrateStore;
|
2016-11-29 02:07:12 +00:00
|
|
|
use syntax::ast::{self, Name, Ident};
|
2018-08-03 02:30:03 +03:00
|
|
|
use syntax::attr;
|
2016-09-07 23:21:59 +00:00
|
|
|
use syntax::errors::DiagnosticBuilder;
|
2018-08-03 02:30:03 +03:00
|
|
|
use syntax::ext::base::{self, Determinacy, MultiModifier, MultiDecorator};
|
2017-03-01 08:44:05 +00:00
|
|
|
use syntax::ext::base::{MacroKind, SyntaxExtension, Resolver as SyntaxResolver};
|
2018-08-03 02:30:03 +03:00
|
|
|
use syntax::ext::expand::{AstFragment, Invocation, InvocationKind};
|
2018-06-30 19:53:46 +03:00
|
|
|
use syntax::ext::hygiene::{self, Mark};
|
2016-09-21 06:25:09 +00:00
|
|
|
use syntax::ext::tt::macro_rules;
|
2018-07-23 02:52:51 +03:00
|
|
|
use syntax::feature_gate::{self, feature_err, emit_feature_err, is_builtin_attr_name, GateIssue};
|
2018-08-11 16:40:08 +03:00
|
|
|
use syntax::feature_gate::EXPLAIN_DERIVE_UNDERSCORE;
|
2016-12-31 17:55:59 +10:30
|
|
|
use syntax::fold::{self, Folder};
|
2017-03-08 23:13:35 +00:00
|
|
|
use syntax::parse::parser::PathStyle;
|
|
|
|
use syntax::parse::token::{self, Token};
|
2016-11-08 03:16:54 +00:00
|
|
|
use syntax::ptr::P;
|
2017-02-02 00:33:42 +00:00
|
|
|
use syntax::symbol::{Symbol, keywords};
|
2017-03-08 23:13:35 +00:00
|
|
|
use syntax::tokenstream::{TokenStream, TokenTree, Delimited};
|
2016-09-07 23:21:59 +00:00
|
|
|
use syntax::util::lev_distance::find_best_match_for_name;
|
2016-11-07 22:08:26 +00:00
|
|
|
use syntax_pos::{Span, DUMMY_SP};
|
2018-08-19 15:01:33 -04:00
|
|
|
use errors::Applicability;
|
2016-09-07 23:21:59 +00:00
|
|
|
|
2017-03-01 08:44:05 +00:00
|
|
|
use std::cell::Cell;
|
|
|
|
use std::mem;
|
2018-02-27 17:11:14 +01:00
|
|
|
use rustc_data_structures::sync::Lrc;
|
2018-08-13 22:15:16 +03:00
|
|
|
use rustc_data_structures::small_vec::ExpectOne;
|
2017-03-01 08:44:05 +00:00
|
|
|
|
2018-08-18 02:38:51 +03:00
|
|
|
crate struct FromPrelude(bool);
|
|
|
|
|
2016-09-14 09:55:20 +00:00
|
|
|
#[derive(Clone)]
|
2016-10-03 23:48:19 +00:00
|
|
|
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>>,
|
2016-09-14 09:55:20 +00:00
|
|
|
}
|
|
|
|
|
2016-10-03 23:48:19 +00:00
|
|
|
impl<'a> InvocationData<'a> {
|
2016-09-14 21:03:09 +00:00
|
|
|
pub fn root(graph_root: Module<'a>) -> Self {
|
2016-10-03 23:48:19 +00:00
|
|
|
InvocationData {
|
2016-09-16 08:50:34 +00:00
|
|
|
module: Cell::new(graph_root),
|
2016-09-14 09:55:20 +00:00
|
|
|
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-09-14 09:55:20 +00:00
|
|
|
}
|
|
|
|
}
|
2016-09-07 23:21:59 +00:00
|
|
|
}
|
|
|
|
|
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>),
|
|
|
|
}
|
|
|
|
|
2018-08-28 00:56:11 +03:00
|
|
|
// Binding produced by a `macro_rules` item.
|
|
|
|
// Not modularized, can shadow previous legacy bindings, etc.
|
2016-10-06 08:04:30 +00:00
|
|
|
pub struct LegacyBinding<'a> {
|
2018-08-28 01:41:07 +03:00
|
|
|
binding: &'a NameBinding<'a>,
|
|
|
|
parent: Cell<LegacyScope<'a>>,
|
|
|
|
ident: Ident,
|
2018-08-18 02:38:51 +03:00
|
|
|
}
|
|
|
|
|
2017-11-16 15:03:25 +01:00
|
|
|
pub struct ProcMacError {
|
|
|
|
crate_name: Symbol,
|
|
|
|
name: Symbol,
|
|
|
|
module: ast::NodeId,
|
|
|
|
use_span: Span,
|
|
|
|
warn_msg: &'static str,
|
|
|
|
}
|
|
|
|
|
2018-07-31 15:23:31 -06:00
|
|
|
impl<'a, 'crateloader: 'a> base::Resolver for Resolver<'a, 'crateloader> {
|
2016-09-05 00:10:27 +00:00
|
|
|
fn next_node_id(&mut self) -> ast::NodeId {
|
|
|
|
self.session.next_node_id()
|
|
|
|
}
|
|
|
|
|
2016-09-23 07:23:01 +00:00
|
|
|
fn get_module_scope(&mut self, id: ast::NodeId) -> Mark {
|
2017-03-22 08:39:51 +00:00
|
|
|
let mark = Mark::fresh(Mark::root());
|
2016-12-20 08:32:15 +00:00
|
|
|
let module = self.module_map[&self.definitions.local_def_id(id)];
|
2016-10-03 23:48:19 +00:00
|
|
|
self.invocations.insert(mark, self.arenas.alloc_invocation_data(InvocationData {
|
2016-09-16 08:50:34 +00:00
|
|
|
module: Cell::new(module),
|
2016-09-23 07:23:01 +00:00
|
|
|
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
|
|
|
}));
|
2016-09-23 07:23:01 +00:00
|
|
|
mark
|
|
|
|
}
|
|
|
|
|
2016-11-08 03:16:54 +00:00
|
|
|
fn eliminate_crate_var(&mut self, item: P<ast::Item>) -> P<ast::Item> {
|
2018-07-31 15:23:31 -06:00
|
|
|
struct EliminateCrateVar<'b, 'a: 'b, 'crateloader: 'a>(
|
|
|
|
&'b mut Resolver<'a, 'crateloader>, Span
|
|
|
|
);
|
2016-11-08 03:16:54 +00:00
|
|
|
|
2018-07-31 15:23:31 -06:00
|
|
|
impl<'a, 'b, 'crateloader> Folder for EliminateCrateVar<'a, 'b, 'crateloader> {
|
2018-06-11 19:44:48 -04:00
|
|
|
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 19:12:00 +03:00
|
|
|
if path.segments[0].ident.name == keywords::DollarCrate.name() {
|
|
|
|
let module = self.0.resolve_crate_root(path.segments[0].ident);
|
2018-03-18 03:53:41 +03:00
|
|
|
path.segments[0].ident.name = keywords::CrateRoot.name();
|
2016-12-05 03:51:11 +00:00
|
|
|
if !module.is_local() {
|
2018-03-19 03:54:56 +03:00
|
|
|
let span = path.segments[0].ident.span;
|
2016-12-05 03:51:11 +00:00
|
|
|
path.segments.insert(1, match module.kind {
|
2017-03-08 20:30:06 +03:00
|
|
|
ModuleKind::Def(_, name) => ast::PathSegment::from_ident(
|
2018-03-19 03:54:56 +03:00
|
|
|
ast::Ident::with_empty_ctxt(name).with_span_pos(span)
|
2017-03-08 20:30:06 +03:00
|
|
|
),
|
2016-11-08 03:16:54 +00:00
|
|
|
_ => unreachable!(),
|
2018-06-11 19:44:48 -04:00
|
|
|
});
|
|
|
|
if let Some(qself) = &mut qself {
|
|
|
|
qself.position += 1;
|
|
|
|
}
|
2016-11-08 03:16:54 +00:00
|
|
|
}
|
|
|
|
}
|
2018-06-11 19:44:48 -04:00
|
|
|
(qself, path)
|
2016-11-08 03:16:54 +00:00
|
|
|
}
|
2016-12-31 16:02:06 +10:30
|
|
|
|
|
|
|
fn fold_mac(&mut self, mac: ast::Mac) -> ast::Mac {
|
|
|
|
fold::noop_fold_mac(mac, self)
|
|
|
|
}
|
2016-11-08 03:16:54 +00:00
|
|
|
}
|
|
|
|
|
2017-05-10 13:19:29 +02:00
|
|
|
EliminateCrateVar(self, item.span).fold_item(item).expect_one("")
|
2016-11-08 03:16:54 +00:00
|
|
|
}
|
|
|
|
|
2016-12-22 06:03:19 +00:00
|
|
|
fn is_whitelisted_legacy_custom_derive(&self, name: Name) -> bool {
|
|
|
|
self.whitelisted_legacy_custom_derives.contains(&name)
|
|
|
|
}
|
|
|
|
|
2018-06-20 02:08:08 +03:00
|
|
|
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];
|
2018-06-20 02:08:08 +03:00
|
|
|
self.collect_def_ids(mark, invocation, fragment);
|
2016-10-06 08:04:30 +00:00
|
|
|
|
|
|
|
self.current_module = invocation.module.get();
|
2016-11-11 10:51:15 +00:00
|
|
|
self.current_module.unresolved_invocations.borrow_mut().remove(&mark);
|
2017-02-02 07:01:15 +00:00
|
|
|
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),
|
2016-10-11 03:42:06 +00:00
|
|
|
expansion: mark,
|
2016-10-06 08:04:30 +00:00
|
|
|
};
|
2018-06-20 02:08:08 +03:00
|
|
|
fragment.visit_with(&mut visitor);
|
2016-10-06 08:04:30 +00:00
|
|
|
invocation.expansion.set(visitor.legacy_scope);
|
2016-09-07 23:21:59 +00:00
|
|
|
}
|
|
|
|
|
2018-02-27 17:11:14 +01: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,
|
2018-01-02 06:36:12 -05:00
|
|
|
index: DefIndex::from_array_index(self.macro_map.len(),
|
|
|
|
DefIndexAddressSpace::Low),
|
2016-10-28 06:52:45 +00:00
|
|
|
};
|
2017-02-23 20:12:33 +10:30
|
|
|
let kind = ext.kind();
|
2016-10-28 06:52:45 +00:00
|
|
|
self.macro_map.insert(def_id, ext);
|
2016-11-07 22:08:26 +00:00
|
|
|
let binding = self.arenas.alloc_name_binding(NameBinding {
|
2018-07-29 14:51:17 +03:00
|
|
|
kind: NameBindingKind::Def(Def::Macro(def_id, kind), false),
|
2016-11-07 22:08:26 +00:00
|
|
|
span: DUMMY_SP,
|
2016-12-20 08:32:15 +00:00
|
|
|
vis: ty::Visibility::Invisible,
|
2016-11-07 22:23:26 +00:00
|
|
|
expansion: Mark::root(),
|
2016-11-07 22:08:26 +00:00
|
|
|
});
|
2018-07-16 21:36:13 +03:00
|
|
|
self.macro_prelude.insert(ident.name, binding);
|
2016-09-07 23:21:59 +00:00
|
|
|
}
|
|
|
|
|
2018-08-30 13:39:32 -07:00
|
|
|
fn add_unshadowable_attr(&mut self, ident: ast::Ident, ext: Lrc<SyntaxExtension>) {
|
|
|
|
let def_id = DefId {
|
|
|
|
krate: BUILTIN_MACROS_CRATE,
|
|
|
|
index: DefIndex::from_array_index(self.macro_map.len(),
|
|
|
|
DefIndexAddressSpace::Low),
|
|
|
|
};
|
|
|
|
let kind = ext.kind();
|
|
|
|
self.macro_map.insert(def_id, ext);
|
|
|
|
let binding = self.arenas.alloc_name_binding(NameBinding {
|
|
|
|
kind: NameBindingKind::Def(Def::Macro(def_id, kind), false),
|
|
|
|
span: DUMMY_SP,
|
|
|
|
vis: ty::Visibility::Invisible,
|
|
|
|
expansion: Mark::root(),
|
|
|
|
});
|
|
|
|
self.unshadowable_attrs.insert(ident.name, binding);
|
|
|
|
}
|
|
|
|
|
2016-11-10 10:11:25 +00:00
|
|
|
fn resolve_imports(&mut self) {
|
|
|
|
ImportResolver { resolver: self }.resolve_imports()
|
|
|
|
}
|
|
|
|
|
2017-02-02 07:01:15 +00:00
|
|
|
// Resolves attribute and derive legacy macros from `#![plugin(..)]`.
|
2018-04-17 23:19:21 -07:00
|
|
|
fn find_legacy_attr_invoc(&mut self, attrs: &mut Vec<ast::Attribute>, allow_derive: bool)
|
2017-02-02 07:01:15 +00:00
|
|
|
-> Option<ast::Attribute> {
|
2016-09-07 23:21:59 +00:00
|
|
|
for i in 0..attrs.len() {
|
2018-01-30 14:53:01 +09:00
|
|
|
let name = attrs[i].name();
|
2017-03-03 09:23:59 +00:00
|
|
|
|
2017-02-22 11:15:12 -08:00
|
|
|
if self.session.plugin_attributes.borrow().iter()
|
2017-03-03 09:23:59 +00:00
|
|
|
.any(|&(ref attr_nm, _)| name == &**attr_nm) {
|
2017-02-22 11:15:12 -08:00
|
|
|
attr::mark_known(&attrs[i]);
|
|
|
|
}
|
|
|
|
|
2018-07-16 21:36:13 +03:00
|
|
|
match self.macro_prelude.get(&name).cloned() {
|
2016-11-27 10:27:41 +00:00
|
|
|
Some(binding) => match *binding.get_macro(self) {
|
2016-09-06 17:57:58 +12:00
|
|
|
MultiModifier(..) | MultiDecorator(..) | SyntaxExtension::AttrProcMacro(..) => {
|
|
|
|
return Some(attrs.remove(i))
|
|
|
|
}
|
2016-09-07 23:21:59 +00:00
|
|
|
_ => {}
|
|
|
|
},
|
|
|
|
None => {}
|
|
|
|
}
|
|
|
|
}
|
2017-02-02 00:33:42 +00:00
|
|
|
|
2018-04-17 23:19:21 -07:00
|
|
|
if !allow_derive { return None }
|
|
|
|
|
2017-02-02 00:33:42 +00:00
|
|
|
// Check for legacy derives
|
|
|
|
for i in 0..attrs.len() {
|
2018-01-30 14:53:01 +09:00
|
|
|
let name = attrs[i].name();
|
2017-03-03 09:23:59 +00:00
|
|
|
|
|
|
|
if name == "derive" {
|
2017-04-03 22:23:32 +00:00
|
|
|
let result = attrs[i].parse_list(&self.session.parse_sess, |parser| {
|
2018-01-30 14:53:01 +09:00
|
|
|
parser.parse_path_allowing_meta(PathStyle::Mod)
|
2017-04-03 22:23:32 +00:00
|
|
|
});
|
|
|
|
|
2017-03-08 23:13:35 +00:00
|
|
|
let mut traits = match result {
|
|
|
|
Ok(traits) => traits,
|
|
|
|
Err(mut e) => {
|
|
|
|
e.cancel();
|
|
|
|
continue
|
|
|
|
}
|
2017-02-02 00:33:42 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
for j in 0..traits.len() {
|
2017-03-08 23:13:35 +00:00
|
|
|
if traits[j].segments.len() > 1 {
|
|
|
|
continue
|
|
|
|
}
|
2018-03-18 03:53:41 +03:00
|
|
|
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));
|
2018-07-16 21:36:13 +03:00
|
|
|
if !self.macro_prelude.contains_key(&legacy_name) {
|
2017-02-02 00:33:42 +00:00
|
|
|
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();
|
2017-03-20 05:03:10 +00:00
|
|
|
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());
|
|
|
|
}
|
2017-03-20 05:03:10 +00:00
|
|
|
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());
|
|
|
|
}
|
2018-03-18 03:53:41 +03:00
|
|
|
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();
|
2017-02-02 00:33:42 +00:00
|
|
|
}
|
|
|
|
return Some(ast::Attribute {
|
2018-03-19 03:54:56 +03:00
|
|
|
path: ast::Path::from_ident(Ident::new(legacy_name, span)),
|
2017-03-03 09:23:59 +00:00
|
|
|
tokens: TokenStream::empty(),
|
2017-02-02 00:33:42 +00:00
|
|
|
id: attr::mk_attr_id(),
|
|
|
|
style: ast::AttrStyle::Outer,
|
|
|
|
is_sugared_doc: false,
|
2017-08-06 22:54:09 -07:00
|
|
|
span,
|
2017-02-02 00:33:42 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-07 23:21:59 +00:00
|
|
|
None
|
|
|
|
}
|
|
|
|
|
2018-08-15 03:51:12 +03:00
|
|
|
fn resolve_macro_invocation(&mut self, invoc: &Invocation, scope: Mark, force: bool)
|
|
|
|
-> Result<Option<Lrc<SyntaxExtension>>, Determinacy> {
|
|
|
|
let (path, kind, derives_in_scope) = match invoc.kind {
|
2018-08-13 02:57:19 +03:00
|
|
|
InvocationKind::Attr { attr: None, .. } =>
|
|
|
|
return Ok(None),
|
|
|
|
InvocationKind::Attr { attr: Some(ref attr), ref traits, .. } =>
|
|
|
|
(&attr.path, MacroKind::Attr, &traits[..]),
|
|
|
|
InvocationKind::Bang { ref mac, .. } =>
|
|
|
|
(&mac.node.path, MacroKind::Bang, &[][..]),
|
|
|
|
InvocationKind::Derive { ref path, .. } =>
|
|
|
|
(path, MacroKind::Derive, &[][..]),
|
2017-03-01 23:48:16 +00:00
|
|
|
};
|
2018-08-11 16:40:08 +03:00
|
|
|
|
2018-08-15 03:51:12 +03:00
|
|
|
let (def, ext) = self.resolve_macro_to_def(path, kind, scope, derives_in_scope, force)?;
|
|
|
|
|
|
|
|
if let Def::Macro(def_id, _) = def {
|
|
|
|
self.macro_defs.insert(invoc.expansion_data.mark, def_id);
|
|
|
|
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);
|
|
|
|
invoc.expansion_data.mark.set_default_transparency(ext.default_transparency());
|
|
|
|
invoc.expansion_data.mark.set_is_builtin(def_id.krate == BUILTIN_MACROS_CRATE);
|
2018-07-12 13:24:59 +03:00
|
|
|
}
|
2018-08-15 03:51:12 +03:00
|
|
|
|
2017-03-22 08:39:51 +00:00
|
|
|
Ok(Some(ext))
|
2017-03-01 23:48:16 +00:00
|
|
|
}
|
|
|
|
|
2018-08-15 03:51:12 +03:00
|
|
|
fn resolve_macro_path(&mut self, path: &ast::Path, kind: MacroKind, scope: Mark,
|
|
|
|
derives_in_scope: &[ast::Path], force: bool)
|
|
|
|
-> Result<Lrc<SyntaxExtension>, Determinacy> {
|
|
|
|
Ok(self.resolve_macro_to_def(path, kind, scope, derives_in_scope, force)?.1)
|
2017-05-11 10:26:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
fn check_unused_macros(&self) {
|
2017-05-15 07:35:19 +02:00
|
|
|
for did in self.unused_macros.iter() {
|
2017-05-12 08:10:52 +02:00
|
|
|
let id_span = match *self.macro_map[did] {
|
2018-06-24 19:24:51 +03:00
|
|
|
SyntaxExtension::NormalTT { def_info, .. } |
|
|
|
|
SyntaxExtension::DeclMacro { def_info, .. } => def_info,
|
2017-05-15 07:35:19 +02:00
|
|
|
_ => None,
|
|
|
|
};
|
2017-05-12 08:10:52 +02:00
|
|
|
if let Some((id, span)) = id_span {
|
2017-05-11 10:26:07 +02:00
|
|
|
let lint = lint::builtin::UNUSED_MACROS;
|
2017-07-26 21:51:09 -07:00
|
|
|
let msg = "unused macro definition";
|
|
|
|
self.session.buffer_lint(lint, id, span, msg);
|
2017-05-11 10:26:07 +02:00
|
|
|
} else {
|
|
|
|
bug!("attempted to create unused macro error, but span not available");
|
|
|
|
}
|
|
|
|
}
|
2017-03-01 23:48:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-31 15:23:31 -06:00
|
|
|
impl<'a, 'cl> Resolver<'a, 'cl> {
|
2018-08-15 03:51:12 +03:00
|
|
|
fn resolve_macro_to_def(&mut self, path: &ast::Path, kind: MacroKind, scope: Mark,
|
2018-08-13 02:57:19 +03:00
|
|
|
derives_in_scope: &[ast::Path], force: bool)
|
2018-08-15 03:51:12 +03:00
|
|
|
-> Result<(Def, Lrc<SyntaxExtension>), Determinacy> {
|
|
|
|
let def = self.resolve_macro_to_def_inner(path, kind, scope, derives_in_scope, force);
|
|
|
|
|
|
|
|
// Report errors and enforce feature gates for the resolved macro.
|
2018-07-23 02:52:51 +03:00
|
|
|
if def != Err(Determinacy::Undetermined) {
|
|
|
|
// Do not report duplicated errors on every undetermined resolution.
|
2018-08-15 03:51:12 +03:00
|
|
|
for segment in &path.segments {
|
|
|
|
if let Some(args) = &segment.args {
|
|
|
|
self.session.span_err(args.span(), "generic arguments in macro path");
|
|
|
|
}
|
|
|
|
}
|
2018-07-23 02:52:51 +03:00
|
|
|
}
|
2018-08-15 03:51:12 +03:00
|
|
|
|
|
|
|
let def = def?;
|
|
|
|
|
|
|
|
match def {
|
|
|
|
Def::Macro(def_id, macro_kind) => {
|
|
|
|
self.unused_macros.remove(&def_id);
|
|
|
|
if macro_kind == MacroKind::ProcMacroStub {
|
|
|
|
let msg = "can't use a procedural macro from the same crate that defines it";
|
|
|
|
self.session.span_err(path.span, msg);
|
|
|
|
return Err(Determinacy::Determined);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Def::NonMacroAttr(attr_kind) => {
|
|
|
|
if kind == MacroKind::Attr {
|
|
|
|
let features = self.session.features_untracked();
|
|
|
|
if attr_kind == NonMacroAttrKind::Custom {
|
|
|
|
assert!(path.segments.len() == 1);
|
|
|
|
let name = path.segments[0].ident.name.as_str();
|
|
|
|
if name.starts_with("rustc_") {
|
|
|
|
if !features.rustc_attrs {
|
|
|
|
let msg = "unless otherwise specified, attributes with the prefix \
|
|
|
|
`rustc_` are reserved for internal compiler diagnostics";
|
|
|
|
feature_err(&self.session.parse_sess, "rustc_attrs", path.span,
|
|
|
|
GateIssue::Language, &msg).emit();
|
|
|
|
}
|
|
|
|
} else if name.starts_with("derive_") {
|
|
|
|
if !features.custom_derive {
|
|
|
|
feature_err(&self.session.parse_sess, "custom_derive", path.span,
|
|
|
|
GateIssue::Language, EXPLAIN_DERIVE_UNDERSCORE).emit();
|
|
|
|
}
|
|
|
|
} else if !features.custom_attribute {
|
|
|
|
let msg = format!("The attribute `{}` is currently unknown to the \
|
|
|
|
compiler and may have meaning added to it in the \
|
|
|
|
future", path);
|
|
|
|
feature_err(&self.session.parse_sess, "custom_attribute", path.span,
|
|
|
|
GateIssue::Language, &msg).emit();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Not only attributes, but anything in macro namespace can result in
|
|
|
|
// `Def::NonMacroAttr` definition (e.g. `inline!()`), so we must report
|
|
|
|
// an error for those cases.
|
|
|
|
let msg = format!("expected a macro, found {}", def.kind_name());
|
|
|
|
self.session.span_err(path.span, &msg);
|
|
|
|
return Err(Determinacy::Determined);
|
|
|
|
}
|
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 07:50:39 -07:00
|
|
|
}
|
2018-08-15 03:51:12 +03:00
|
|
|
_ => panic!("expected `Def::Macro` or `Def::NonMacroAttr`"),
|
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 07:50:39 -07:00
|
|
|
}
|
2018-08-15 03:51:12 +03:00
|
|
|
|
|
|
|
Ok((def, self.get_macro(def)))
|
2017-07-25 00:33:15 +03:00
|
|
|
}
|
2016-09-07 23:21:59 +00:00
|
|
|
|
2018-08-15 03:51:12 +03:00
|
|
|
pub fn resolve_macro_to_def_inner(&mut self, path: &ast::Path, kind: MacroKind, scope: Mark,
|
2018-08-13 02:57:19 +03:00
|
|
|
derives_in_scope: &[ast::Path], force: bool)
|
|
|
|
-> Result<Def, Determinacy> {
|
2017-07-25 00:33:15 +03:00
|
|
|
let ast::Path { ref segments, span } = *path;
|
2018-06-11 14:21:36 +03:00
|
|
|
let mut path: Vec<_> = segments.iter().map(|seg| seg.ident).collect();
|
2016-10-08 00:30:24 +00:00
|
|
|
let invocation = self.invocations[&scope];
|
2017-08-25 13:33:15 -07:00
|
|
|
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
|
|
|
|
2018-06-11 14:21:36 +03:00
|
|
|
// Possibly apply the macro helper hack
|
2018-05-14 03:22:52 +03:00
|
|
|
if kind == MacroKind::Bang && path.len() == 1 &&
|
2018-06-11 14:21:36 +03:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2016-12-05 03:51:11 +00:00
|
|
|
if path.len() > 1 {
|
2018-08-09 16:29:22 +03:00
|
|
|
let res = self.resolve_path(None, &path, Some(MacroNS), false, span, CrateLint::No);
|
|
|
|
let def = match res {
|
2017-02-18 22:11:42 +03:00
|
|
|
PathResult::NonModule(path_res) => match path_res.base_def() {
|
2016-12-12 10:09:22 +00:00
|
|
|
Def::Err => Err(Determinacy::Determined),
|
2017-12-28 00:46:27 +09:00
|
|
|
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-12-12 10:09:22 +00:00
|
|
|
},
|
2016-11-27 10:58:46 +00:00
|
|
|
PathResult::Module(..) => unreachable!(),
|
|
|
|
PathResult::Indeterminate if !force => return Err(Determinacy::Undetermined),
|
2017-02-06 22:14:38 +10:30
|
|
|
_ => {
|
|
|
|
self.found_unresolved_macro = true;
|
|
|
|
Err(Determinacy::Determined)
|
|
|
|
},
|
2016-11-27 10:58:46 +00:00
|
|
|
};
|
2017-03-27 05:22:18 +00:00
|
|
|
self.current_module.nearest_item_scope().macro_resolutions.borrow_mut()
|
2016-12-05 03:51:11 +00:00
|
|
|
.push((path.into_boxed_slice(), span));
|
2017-03-01 23:48:16 +00:00
|
|
|
return def;
|
2016-11-27 10:58:46 +00:00
|
|
|
}
|
|
|
|
|
2018-09-02 09:03:24 -07:00
|
|
|
if kind == MacroKind::Attr {
|
2018-08-30 13:39:32 -07:00
|
|
|
if let Some(ext) = self.unshadowable_attrs.get(&path[0].name) {
|
|
|
|
return Ok(ext.def());
|
|
|
|
}
|
2018-07-20 18:04:02 -07:00
|
|
|
}
|
|
|
|
|
2018-03-18 16:47:09 +03:00
|
|
|
let legacy_resolution = self.resolve_legacy_scope(&invocation.legacy_scope, path[0], false);
|
2018-08-28 02:20:59 +03:00
|
|
|
let result = if let Some(legacy_binding) = legacy_resolution {
|
2018-08-18 02:38:51 +03:00
|
|
|
Ok(legacy_binding.def())
|
2017-03-11 10:58:19 +00:00
|
|
|
} else {
|
2018-08-04 00:25:45 +03:00
|
|
|
match self.resolve_lexical_macro_path_segment(path[0], MacroNS, false, force,
|
|
|
|
kind == MacroKind::Attr, span) {
|
2018-08-18 02:38:51 +03:00
|
|
|
Ok((binding, _)) => Ok(binding.def_ignoring_ambiguity()),
|
2018-08-04 00:25:45 +03:00
|
|
|
Err(Determinacy::Undetermined) => return Err(Determinacy::Undetermined),
|
|
|
|
Err(Determinacy::Determined) => {
|
2017-02-06 22:14:38 +10:30
|
|
|
self.found_unresolved_macro = true;
|
|
|
|
Err(Determinacy::Determined)
|
|
|
|
}
|
2017-03-11 10:58:19 +00:00
|
|
|
}
|
2016-11-10 10:29:36 +00:00
|
|
|
};
|
|
|
|
|
2017-03-27 05:22:18 +00:00
|
|
|
self.current_module.nearest_item_scope().legacy_macro_resolutions.borrow_mut()
|
2018-05-28 22:13:59 +03:00
|
|
|
.push((scope, path[0], kind, result.ok()));
|
2017-02-01 21:03:09 +10:30
|
|
|
|
2018-08-13 02:57:19 +03:00
|
|
|
if let Ok(Def::NonMacroAttr(NonMacroAttrKind::Custom)) = result {} else {
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
// At this point we've found that the `attr` is determinately unresolved and thus can be
|
|
|
|
// interpreted as a custom attribute. Normally custom attributes are feature gated, but
|
|
|
|
// it may be a custom attribute whitelisted by a derive macro and they do not require
|
|
|
|
// a feature gate.
|
|
|
|
//
|
|
|
|
// So here we look through all of the derive annotations in scope and try to resolve them.
|
|
|
|
// If they themselves successfully resolve *and* one of the resolved derive macros
|
|
|
|
// whitelists this attribute's name, then this is a registered attribute and we can convert
|
|
|
|
// it from a "generic custom attrite" into a "known derive helper attribute".
|
|
|
|
assert!(kind == MacroKind::Attr);
|
|
|
|
enum ConvertToDeriveHelper { Yes, No, DontKnow }
|
|
|
|
let mut convert_to_derive_helper = ConvertToDeriveHelper::No;
|
|
|
|
for derive in derives_in_scope {
|
2018-08-15 03:51:12 +03:00
|
|
|
match self.resolve_macro_path(derive, MacroKind::Derive, scope, &[], force) {
|
2018-08-13 02:57:19 +03:00
|
|
|
Ok(ext) => if let SyntaxExtension::ProcMacroDerive(_, ref inert_attrs, _) = *ext {
|
|
|
|
if inert_attrs.contains(&path[0].name) {
|
|
|
|
convert_to_derive_helper = ConvertToDeriveHelper::Yes;
|
|
|
|
break
|
|
|
|
}
|
|
|
|
},
|
|
|
|
Err(Determinacy::Undetermined) =>
|
|
|
|
convert_to_derive_helper = ConvertToDeriveHelper::DontKnow,
|
|
|
|
Err(Determinacy::Determined) => {}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
match convert_to_derive_helper {
|
|
|
|
ConvertToDeriveHelper::Yes => Ok(Def::NonMacroAttr(NonMacroAttrKind::DeriveHelper)),
|
|
|
|
ConvertToDeriveHelper::No => result,
|
|
|
|
ConvertToDeriveHelper::DontKnow => Err(Determinacy::determined(force)),
|
|
|
|
}
|
2017-02-01 21:03:09 +10:30
|
|
|
}
|
2016-09-26 03:17:05 +00:00
|
|
|
|
2018-07-23 02:52:51 +03:00
|
|
|
// Resolve the initial segment of a non-global macro path
|
|
|
|
// (e.g. `foo` in `foo::bar!(); or `foo!();`).
|
|
|
|
// This is a variation of `fn resolve_ident_in_lexical_scope` that can be run during
|
|
|
|
// expansion and import resolution (perhaps they can be merged in the future).
|
2018-08-18 02:38:51 +03:00
|
|
|
crate fn resolve_lexical_macro_path_segment(
|
|
|
|
&mut self,
|
|
|
|
mut ident: Ident,
|
|
|
|
ns: Namespace,
|
|
|
|
record_used: bool,
|
|
|
|
force: bool,
|
|
|
|
is_attr: bool,
|
|
|
|
path_span: Span
|
|
|
|
) -> Result<(&'a NameBinding<'a>, FromPrelude), Determinacy> {
|
2018-07-23 02:52:51 +03:00
|
|
|
// General principles:
|
|
|
|
// 1. Not controlled (user-defined) names should have higher priority than controlled names
|
|
|
|
// built into the language or standard library. This way we can add new names into the
|
|
|
|
// language or standard library without breaking user code.
|
|
|
|
// 2. "Closed set" below means new names can appear after the current resolution attempt.
|
|
|
|
// Places to search (in order of decreasing priority):
|
|
|
|
// (Type NS)
|
|
|
|
// 1. FIXME: Ribs (type parameters), there's no necessary infrastructure yet
|
|
|
|
// (open set, not controlled).
|
|
|
|
// 2. Names in modules (both normal `mod`ules and blocks), loop through hygienic parents
|
|
|
|
// (open, not controlled).
|
|
|
|
// 3. Extern prelude (closed, not controlled).
|
|
|
|
// 4. Tool modules (closed, controlled right now, but not in the future).
|
|
|
|
// 5. Standard library prelude (de-facto closed, controlled).
|
|
|
|
// 6. Language prelude (closed, controlled).
|
|
|
|
// (Macro NS)
|
|
|
|
// 1. Names in modules (both normal `mod`ules and blocks), loop through hygienic parents
|
|
|
|
// (open, not controlled).
|
|
|
|
// 2. Macro prelude (language, standard library, user-defined legacy plugins lumped into
|
|
|
|
// one set) (open, the open part is from macro expansions, not controlled).
|
|
|
|
// 2a. User-defined prelude from macro-use
|
|
|
|
// (open, the open part is from macro expansions, not controlled).
|
|
|
|
// 2b. Standard library prelude, currently just a macro-use (closed, controlled)
|
|
|
|
// 2c. Language prelude, perhaps including builtin attributes
|
|
|
|
// (closed, controlled, except for legacy plugins).
|
|
|
|
// 3. Builtin attributes (closed, controlled).
|
|
|
|
|
|
|
|
assert!(ns == TypeNS || ns == MacroNS);
|
2018-08-04 05:17:51 +03:00
|
|
|
assert!(force || !record_used); // `record_used` implies `force`
|
2017-03-22 08:39:51 +00:00
|
|
|
ident = ident.modern();
|
2018-07-23 02:52:51 +03:00
|
|
|
|
|
|
|
// Names from inner scope that can't shadow names from outer scopes, e.g.
|
|
|
|
// mod m { ... }
|
|
|
|
// {
|
|
|
|
// use prefix::*; // if this imports another `m`, then it can't shadow the outer `m`
|
|
|
|
// // and we have and ambiguity error
|
|
|
|
// m::mac!();
|
|
|
|
// }
|
|
|
|
// This includes names from globs and from macro expansions.
|
2018-08-18 02:38:51 +03:00
|
|
|
let mut potentially_ambiguous_result: Option<(&NameBinding, FromPrelude)> = None;
|
2018-07-23 02:52:51 +03:00
|
|
|
|
|
|
|
enum WhereToResolve<'a> {
|
|
|
|
Module(Module<'a>),
|
|
|
|
MacroPrelude,
|
|
|
|
BuiltinAttrs,
|
|
|
|
ExternPrelude,
|
|
|
|
ToolPrelude,
|
|
|
|
StdLibPrelude,
|
|
|
|
PrimitiveTypes,
|
|
|
|
}
|
|
|
|
|
|
|
|
// Go through all the scopes and try to resolve the name.
|
|
|
|
let mut where_to_resolve = WhereToResolve::Module(self.current_module);
|
|
|
|
let mut use_prelude = !self.current_module.no_implicit_prelude;
|
2016-11-10 10:29:36 +00:00
|
|
|
loop {
|
2018-07-23 02:52:51 +03:00
|
|
|
let result = match where_to_resolve {
|
|
|
|
WhereToResolve::Module(module) => {
|
|
|
|
let orig_current_module = mem::replace(&mut self.current_module, module);
|
|
|
|
let binding = self.resolve_ident_in_module_unadjusted(
|
2018-08-09 16:29:22 +03:00
|
|
|
ModuleOrUniformRoot::Module(module),
|
|
|
|
ident,
|
|
|
|
ns,
|
|
|
|
true,
|
|
|
|
record_used,
|
|
|
|
path_span,
|
2018-07-23 02:52:51 +03:00
|
|
|
);
|
|
|
|
self.current_module = orig_current_module;
|
2018-08-18 02:38:51 +03:00
|
|
|
binding.map(|binding| (binding, FromPrelude(false)))
|
2018-07-23 02:52:51 +03:00
|
|
|
}
|
|
|
|
WhereToResolve::MacroPrelude => {
|
|
|
|
match self.macro_prelude.get(&ident.name).cloned() {
|
2018-08-18 02:38:51 +03:00
|
|
|
Some(binding) => Ok((binding, FromPrelude(true))),
|
2018-07-23 02:52:51 +03:00
|
|
|
None => Err(Determinacy::Determined),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
WhereToResolve::BuiltinAttrs => {
|
2018-08-11 16:58:28 +03:00
|
|
|
// FIXME: Only built-in attributes are not considered as candidates for
|
|
|
|
// non-attributes to fight off regressions on stable channel (#53205).
|
|
|
|
// We need to come up with some more principled approach instead.
|
|
|
|
if is_attr && is_builtin_attr_name(ident.name) {
|
2018-08-03 02:05:00 +03:00
|
|
|
let binding = (Def::NonMacroAttr(NonMacroAttrKind::Builtin),
|
|
|
|
ty::Visibility::Public, ident.span, Mark::root())
|
|
|
|
.to_name_binding(self.arenas);
|
2018-08-18 02:38:51 +03:00
|
|
|
Ok((binding, FromPrelude(true)))
|
2018-07-23 02:52:51 +03:00
|
|
|
} else {
|
|
|
|
Err(Determinacy::Determined)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
WhereToResolve::ExternPrelude => {
|
|
|
|
if use_prelude && self.extern_prelude.contains(&ident.name) {
|
|
|
|
if !self.session.features_untracked().extern_prelude &&
|
|
|
|
!self.ignore_extern_prelude_feature {
|
|
|
|
feature_err(&self.session.parse_sess, "extern_prelude",
|
|
|
|
ident.span, GateIssue::Language,
|
|
|
|
"access to extern crates through prelude is experimental")
|
|
|
|
.emit();
|
|
|
|
}
|
|
|
|
|
|
|
|
let crate_id =
|
|
|
|
self.crate_loader.process_path_extern(ident.name, ident.span);
|
|
|
|
let crate_root =
|
|
|
|
self.get_module(DefId { krate: crate_id, index: CRATE_DEF_INDEX });
|
|
|
|
self.populate_module_if_necessary(crate_root);
|
|
|
|
|
|
|
|
let binding = (crate_root, ty::Visibility::Public,
|
|
|
|
ident.span, Mark::root()).to_name_binding(self.arenas);
|
2018-08-18 02:38:51 +03:00
|
|
|
Ok((binding, FromPrelude(true)))
|
2018-07-23 02:52:51 +03:00
|
|
|
} else {
|
|
|
|
Err(Determinacy::Determined)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
WhereToResolve::ToolPrelude => {
|
|
|
|
if use_prelude && is_known_tool(ident.name) {
|
|
|
|
let binding = (Def::ToolMod, ty::Visibility::Public,
|
|
|
|
ident.span, Mark::root()).to_name_binding(self.arenas);
|
2018-08-18 02:38:51 +03:00
|
|
|
Ok((binding, FromPrelude(true)))
|
2018-07-23 02:52:51 +03:00
|
|
|
} else {
|
|
|
|
Err(Determinacy::Determined)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
WhereToResolve::StdLibPrelude => {
|
|
|
|
let mut result = Err(Determinacy::Determined);
|
|
|
|
if use_prelude {
|
|
|
|
if let Some(prelude) = self.prelude {
|
2018-08-09 16:29:22 +03:00
|
|
|
if let Ok(binding) = self.resolve_ident_in_module_unadjusted(
|
|
|
|
ModuleOrUniformRoot::Module(prelude),
|
|
|
|
ident,
|
|
|
|
ns,
|
|
|
|
false,
|
|
|
|
false,
|
|
|
|
path_span,
|
|
|
|
) {
|
2018-08-18 02:38:51 +03:00
|
|
|
result = Ok((binding, FromPrelude(true)));
|
2018-07-23 02:52:51 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
result
|
|
|
|
}
|
|
|
|
WhereToResolve::PrimitiveTypes => {
|
|
|
|
if let Some(prim_ty) =
|
|
|
|
self.primitive_type_table.primitive_types.get(&ident.name).cloned() {
|
|
|
|
let binding = (Def::PrimTy(prim_ty), ty::Visibility::Public,
|
|
|
|
ident.span, Mark::root()).to_name_binding(self.arenas);
|
2018-08-18 02:38:51 +03:00
|
|
|
Ok((binding, FromPrelude(true)))
|
2018-07-23 02:52:51 +03:00
|
|
|
} else {
|
|
|
|
Err(Determinacy::Determined)
|
|
|
|
}
|
|
|
|
}
|
2017-03-11 10:58:19 +00:00
|
|
|
};
|
|
|
|
|
2018-07-23 02:52:51 +03:00
|
|
|
macro_rules! continue_search { () => {
|
|
|
|
where_to_resolve = match where_to_resolve {
|
|
|
|
WhereToResolve::Module(module) => {
|
|
|
|
match self.hygienic_lexical_parent(module, &mut ident.span) {
|
|
|
|
Some(parent_module) => WhereToResolve::Module(parent_module),
|
|
|
|
None => {
|
|
|
|
use_prelude = !module.no_implicit_prelude;
|
|
|
|
if ns == MacroNS {
|
|
|
|
WhereToResolve::MacroPrelude
|
|
|
|
} else {
|
|
|
|
WhereToResolve::ExternPrelude
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
WhereToResolve::MacroPrelude => WhereToResolve::BuiltinAttrs,
|
|
|
|
WhereToResolve::BuiltinAttrs => break, // nowhere else to search
|
|
|
|
WhereToResolve::ExternPrelude => WhereToResolve::ToolPrelude,
|
|
|
|
WhereToResolve::ToolPrelude => WhereToResolve::StdLibPrelude,
|
|
|
|
WhereToResolve::StdLibPrelude => WhereToResolve::PrimitiveTypes,
|
|
|
|
WhereToResolve::PrimitiveTypes => break, // nowhere else to search
|
|
|
|
};
|
|
|
|
|
|
|
|
continue;
|
|
|
|
}}
|
|
|
|
|
|
|
|
match result {
|
|
|
|
Ok(result) => {
|
2017-05-12 11:21:11 +02:00
|
|
|
if !record_used {
|
2018-07-23 02:52:51 +03:00
|
|
|
return Ok(result);
|
2017-05-12 11:21:11 +02:00
|
|
|
}
|
2018-07-23 02:52:51 +03:00
|
|
|
|
|
|
|
// Found a solution that is ambiguous with a previously found solution.
|
|
|
|
// Push an ambiguity error for later reporting and
|
|
|
|
// return something for better recovery.
|
|
|
|
if let Some(previous_result) = potentially_ambiguous_result {
|
2018-08-18 02:38:51 +03:00
|
|
|
if result.0.def() != previous_result.0.def() {
|
2016-11-27 10:58:46 +00:00
|
|
|
self.ambiguity_errors.push(AmbiguityError {
|
2017-05-12 11:21:11 +02:00
|
|
|
span: path_span,
|
2018-07-23 02:52:51 +03:00
|
|
|
name: ident.name,
|
2018-08-18 02:38:51 +03:00
|
|
|
b1: previous_result.0,
|
|
|
|
b2: result.0,
|
2016-11-27 10:58:46 +00:00
|
|
|
});
|
2018-07-23 02:52:51 +03:00
|
|
|
return Ok(previous_result);
|
2016-11-27 10:58:46 +00:00
|
|
|
}
|
2017-03-11 10:58:19 +00:00
|
|
|
}
|
2018-07-23 02:52:51 +03:00
|
|
|
|
|
|
|
// Found a solution that's not an ambiguity yet, but is "suspicious" and
|
|
|
|
// can participate in ambiguities later on.
|
|
|
|
// Remember it and go search for other solutions in outer scopes.
|
2018-08-18 02:38:51 +03:00
|
|
|
if result.0.is_glob_import() || result.0.expansion != Mark::root() {
|
2018-07-23 02:52:51 +03:00
|
|
|
potentially_ambiguous_result = Some(result);
|
|
|
|
|
|
|
|
continue_search!();
|
2016-11-10 10:29:36 +00:00
|
|
|
}
|
2018-07-23 02:52:51 +03:00
|
|
|
|
|
|
|
// Found a solution that can't be ambiguous, great success.
|
|
|
|
return Ok(result);
|
2016-11-10 10:29:36 +00:00
|
|
|
},
|
2018-07-23 02:52:51 +03:00
|
|
|
Err(Determinacy::Determined) => {
|
|
|
|
continue_search!();
|
|
|
|
}
|
2018-08-04 00:25:45 +03:00
|
|
|
Err(Determinacy::Undetermined) => return Err(Determinacy::determined(force)),
|
2016-11-10 10:29:36 +00:00
|
|
|
}
|
2018-07-23 02:52:51 +03:00
|
|
|
}
|
2016-11-10 10:29:36 +00:00
|
|
|
|
2018-07-23 02:52:51 +03:00
|
|
|
// Previously found potentially ambiguous result turned out to not be ambiguous after all.
|
|
|
|
if let Some(previous_result) = potentially_ambiguous_result {
|
|
|
|
return Ok(previous_result);
|
2016-11-10 10:29:36 +00:00
|
|
|
}
|
2018-07-23 02:52:51 +03:00
|
|
|
|
2018-08-04 00:25:45 +03:00
|
|
|
let determinacy = Determinacy::determined(force);
|
|
|
|
if determinacy == Determinacy::Determined && is_attr {
|
2018-08-04 05:17:51 +03:00
|
|
|
// For single-segment attributes interpret determinate "no resolution" as a custom
|
|
|
|
// attribute. (Lexical resolution implies the first segment and is_attr should imply
|
|
|
|
// the last segment, so we are certainly working with a single-segment attribute here.)
|
|
|
|
assert!(ns == MacroNS);
|
2018-08-04 00:25:45 +03:00
|
|
|
let binding = (Def::NonMacroAttr(NonMacroAttrKind::Custom),
|
|
|
|
ty::Visibility::Public, ident.span, Mark::root())
|
|
|
|
.to_name_binding(self.arenas);
|
2018-08-18 02:38:51 +03:00
|
|
|
Ok((binding, FromPrelude(true)))
|
2018-08-04 00:25:45 +03:00
|
|
|
} else {
|
|
|
|
Err(determinacy)
|
|
|
|
}
|
2016-11-10 10:29:36 +00:00
|
|
|
}
|
|
|
|
|
2018-08-28 01:41:07 +03:00
|
|
|
fn resolve_legacy_scope(&mut self,
|
|
|
|
scope: &'a Cell<LegacyScope<'a>>,
|
|
|
|
ident: Ident,
|
|
|
|
record_used: bool)
|
2018-08-28 02:20:59 +03:00
|
|
|
-> Option<&'a NameBinding<'a>> {
|
2017-03-22 08:39:51 +00:00
|
|
|
let ident = ident.modern();
|
2018-08-28 01:41:07 +03:00
|
|
|
|
|
|
|
// Names from inner scope that can't shadow names from outer scopes, e.g.
|
|
|
|
// macro_rules! mac { ... }
|
|
|
|
// {
|
|
|
|
// define_mac!(); // if this generates another `macro_rules! mac`, then it can't shadow
|
|
|
|
// // the outer `mac` and we have and ambiguity error
|
|
|
|
// mac!();
|
|
|
|
// }
|
2018-08-28 02:20:59 +03:00
|
|
|
let mut potentially_ambiguous_result: Option<&NameBinding> = None;
|
2018-08-28 01:41:07 +03:00
|
|
|
|
|
|
|
// Go through all the scopes and try to resolve the name.
|
|
|
|
let mut where_to_resolve = scope;
|
2016-09-07 23:21:59 +00:00
|
|
|
loop {
|
2018-08-28 01:41:07 +03:00
|
|
|
let result = match where_to_resolve.get() {
|
|
|
|
LegacyScope::Binding(legacy_binding) => if ident == legacy_binding.ident {
|
2018-08-28 02:20:59 +03:00
|
|
|
Some(legacy_binding.binding)
|
2018-08-28 01:41:07 +03:00
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
_ => None,
|
|
|
|
};
|
|
|
|
|
|
|
|
macro_rules! continue_search { () => {
|
|
|
|
where_to_resolve = match where_to_resolve.get() {
|
|
|
|
LegacyScope::Binding(binding) => &binding.parent,
|
2018-08-28 02:20:59 +03:00
|
|
|
LegacyScope::Invocation(invocation) => &invocation.legacy_scope,
|
2018-08-28 01:41:07 +03:00
|
|
|
LegacyScope::Expansion(invocation) => match invocation.expansion.get() {
|
|
|
|
LegacyScope::Empty => &invocation.legacy_scope,
|
2018-08-28 02:20:59 +03:00
|
|
|
LegacyScope::Binding(..) |
|
|
|
|
LegacyScope::Expansion(..) => &invocation.expansion,
|
2018-08-28 01:41:07 +03:00
|
|
|
LegacyScope::Invocation(..) => {
|
|
|
|
where_to_resolve.set(invocation.legacy_scope.get());
|
|
|
|
where_to_resolve
|
2016-10-31 22:17:15 +00:00
|
|
|
}
|
2016-10-02 01:41:19 +00:00
|
|
|
}
|
2018-08-28 01:41:07 +03:00
|
|
|
LegacyScope::Empty => break, // nowhere else to search
|
|
|
|
};
|
|
|
|
|
|
|
|
continue;
|
|
|
|
}}
|
|
|
|
|
|
|
|
match result {
|
|
|
|
Some(result) => {
|
|
|
|
if !record_used {
|
|
|
|
return Some(result);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Found a solution that is ambiguous with a previously found solution.
|
|
|
|
// Push an ambiguity error for later reporting and
|
|
|
|
// return something for better recovery.
|
|
|
|
if let Some(previous_result) = potentially_ambiguous_result {
|
2018-08-28 02:20:59 +03:00
|
|
|
if result.def() != previous_result.def() {
|
2018-08-28 01:41:07 +03:00
|
|
|
self.ambiguity_errors.push(AmbiguityError {
|
|
|
|
span: ident.span,
|
|
|
|
name: ident.name,
|
2018-08-28 02:20:59 +03:00
|
|
|
b1: previous_result,
|
|
|
|
b2: result,
|
2018-08-28 01:41:07 +03:00
|
|
|
});
|
|
|
|
return Some(previous_result);
|
2016-10-06 08:04:30 +00:00
|
|
|
}
|
2016-10-02 01:41:19 +00:00
|
|
|
}
|
2018-08-28 01:41:07 +03:00
|
|
|
|
|
|
|
// Found a solution that's not an ambiguity yet, but is "suspicious" and
|
|
|
|
// can participate in ambiguities later on.
|
|
|
|
// Remember it and go search for other solutions in outer scopes.
|
2018-08-28 02:20:59 +03:00
|
|
|
if result.expansion != Mark::root() {
|
2018-08-28 01:41:07 +03:00
|
|
|
potentially_ambiguous_result = Some(result);
|
|
|
|
|
|
|
|
continue_search!();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Found a solution that can't be ambiguous.
|
|
|
|
return Some(result);
|
2016-10-02 01:41:19 +00:00
|
|
|
}
|
2018-08-28 01:41:07 +03:00
|
|
|
None => {
|
|
|
|
continue_search!();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Previously found potentially ambiguous result turned out to not be ambiguous after all.
|
|
|
|
if let Some(previous_result) = potentially_ambiguous_result {
|
|
|
|
return Some(previous_result);
|
2016-09-07 23:21:59 +00:00
|
|
|
}
|
2016-10-06 08:04:30 +00:00
|
|
|
|
2018-08-18 02:38:51 +03:00
|
|
|
None
|
2016-11-10 10:29:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn finalize_current_module_macro_resolutions(&mut self) {
|
|
|
|
let module = self.current_module;
|
2016-12-05 03:51:11 +00:00
|
|
|
for &(ref path, span) in module.macro_resolutions.borrow().iter() {
|
2018-08-09 16:29:22 +03:00
|
|
|
match self.resolve_path(None, &path, Some(MacroNS), true, span, CrateLint::No) {
|
2016-11-27 10:58:46 +00:00
|
|
|
PathResult::NonModule(_) => {},
|
2017-07-23 15:15:45 -07:00
|
|
|
PathResult::Failed(span, msg, _) => {
|
2016-11-27 10:58:46 +00:00
|
|
|
resolve_error(self, span, ResolutionError::FailedToResolve(&msg));
|
|
|
|
}
|
|
|
|
_ => unreachable!(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-28 22:13:59 +03:00
|
|
|
for &(mark, ident, kind, def) in module.legacy_macro_resolutions.borrow().iter() {
|
|
|
|
let span = ident.span;
|
2016-11-23 01:51:37 +00:00
|
|
|
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);
|
2018-08-04 00:25:45 +03:00
|
|
|
let resolution = self.resolve_lexical_macro_path_segment(ident, MacroNS, true, true,
|
|
|
|
kind == MacroKind::Attr, span);
|
2018-05-28 22:13:59 +03:00
|
|
|
|
2018-08-18 02:38:51 +03:00
|
|
|
let check_consistency = |this: &Self, new_def: Def| {
|
2018-05-28 22:13:59 +03:00
|
|
|
if let Some(def) = def {
|
2018-08-28 01:41:07 +03:00
|
|
|
if this.ambiguity_errors.is_empty() && new_def != def && new_def != Def::Err {
|
2018-05-28 22:13:59 +03:00
|
|
|
// 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();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2017-02-06 22:14:38 +10:30
|
|
|
match (legacy_resolution, resolution) {
|
|
|
|
(None, Err(_)) => {
|
2018-05-28 22:13:59 +03:00
|
|
|
assert!(def.is_none());
|
|
|
|
let bang = if kind == MacroKind::Bang { "!" } else { "" };
|
|
|
|
let msg =
|
|
|
|
format!("cannot find {} `{}{}` in this scope", kind.descr(), ident, bang);
|
2017-02-06 22:14:38 +10:30
|
|
|
let mut err = self.session.struct_span_err(span, &msg);
|
2018-05-26 15:12:38 +03:00
|
|
|
self.suggest_macro_name(&ident.as_str(), kind, &mut err, span);
|
2017-02-06 22:14:38 +10:30
|
|
|
err.emit();
|
|
|
|
},
|
2018-08-28 02:20:59 +03:00
|
|
|
(Some(legacy_binding), Ok((binding, FromPrelude(from_prelude))))
|
|
|
|
if !from_prelude || legacy_binding.expansion != Mark::root() => {
|
2018-08-28 00:56:11 +03:00
|
|
|
if legacy_binding.def_ignoring_ambiguity() != binding.def_ignoring_ambiguity() {
|
|
|
|
self.report_ambiguity_error(ident.name, span, legacy_binding, binding);
|
2018-08-18 02:38:51 +03:00
|
|
|
}
|
|
|
|
},
|
2018-08-28 02:20:59 +03:00
|
|
|
// OK, non-macro-expanded legacy wins over prelude even if defs are different
|
|
|
|
(Some(legacy_binding), Ok(_)) |
|
2018-05-28 22:13:59 +03:00
|
|
|
// OK, unambiguous resolution
|
2018-08-28 02:20:59 +03:00
|
|
|
(Some(legacy_binding), Err(_)) => {
|
2018-08-18 02:38:51 +03:00
|
|
|
check_consistency(self, legacy_binding.def());
|
2018-05-28 22:13:59 +03:00
|
|
|
}
|
2018-08-18 02:38:51 +03:00
|
|
|
// OK, unambiguous resolution
|
|
|
|
(None, Ok((binding, FromPrelude(from_prelude)))) => {
|
|
|
|
check_consistency(self, binding.def_ignoring_ambiguity());
|
|
|
|
if from_prelude {
|
|
|
|
self.record_use(ident, MacroNS, binding, span);
|
|
|
|
self.err_if_macro_use_proc_macro(ident.name, span, binding);
|
2018-05-28 22:13:59 +03:00
|
|
|
}
|
2018-08-18 02:38:51 +03:00
|
|
|
}
|
2016-11-10 10:29:36 +00:00
|
|
|
};
|
2016-10-31 22:17:15 +00:00
|
|
|
}
|
2016-09-07 23:21:59 +00:00
|
|
|
}
|
2016-09-21 06:25:09 +00:00
|
|
|
|
2017-02-06 22:14:38 +10:30
|
|
|
fn suggest_macro_name(&mut self, name: &str, kind: MacroKind,
|
2017-05-12 11:21:11 +02:00
|
|
|
err: &mut DiagnosticBuilder<'a>, span: Span) {
|
2017-02-23 20:18:20 +10:30
|
|
|
// 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)
|
2017-02-23 20:18:20 +10:30
|
|
|
} else {
|
|
|
|
None
|
2017-03-16 01:39:47 +00:00
|
|
|
// Then check global macros.
|
2017-02-23 20:18:20 +10:30
|
|
|
}.or_else(|| {
|
|
|
|
// FIXME: get_macro needs an &mut Resolver, can we do it without cloning?
|
2018-07-16 21:36:13 +03:00
|
|
|
let macro_prelude = self.macro_prelude.clone();
|
|
|
|
let names = macro_prelude.iter().filter_map(|(name, binding)| {
|
2017-02-23 20:18:20 +10:30
|
|
|
if binding.get_macro(self).kind() == kind {
|
|
|
|
Some(name)
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
});
|
|
|
|
find_best_match_for_name(names, name, None)
|
|
|
|
// Then check modules.
|
|
|
|
}).or_else(|| {
|
|
|
|
let is_macro = |def| {
|
|
|
|
if let Def::Macro(_, def_kind) = def {
|
|
|
|
def_kind == kind
|
|
|
|
} else {
|
|
|
|
false
|
|
|
|
}
|
|
|
|
};
|
2018-03-18 16:47:09 +03:00
|
|
|
let ident = Ident::new(Symbol::intern(name), span);
|
2018-07-27 16:50:28 +02:00
|
|
|
self.lookup_typo_candidate(&[ident], MacroNS, is_macro, span)
|
2017-02-23 20:18:20 +10:30
|
|
|
});
|
|
|
|
|
2017-02-06 22:14:38 +10:30
|
|
|
if let Some(suggestion) = suggestion {
|
2016-09-07 23:21:59 +00:00
|
|
|
if suggestion != name {
|
2017-02-06 22:14:38 +10:30
|
|
|
if let MacroKind::Bang = kind {
|
2018-08-19 15:01:33 -04:00
|
|
|
err.span_suggestion_with_applicability(
|
|
|
|
span,
|
|
|
|
"you could try the macro",
|
|
|
|
suggestion.to_string(),
|
|
|
|
Applicability::MaybeIncorrect
|
|
|
|
);
|
2017-02-06 22:14:38 +10:30
|
|
|
} else {
|
2018-08-19 15:01:33 -04:00
|
|
|
err.span_suggestion_with_applicability(
|
|
|
|
span,
|
|
|
|
"try",
|
|
|
|
suggestion.to_string(),
|
|
|
|
Applicability::MaybeIncorrect
|
|
|
|
);
|
2017-02-06 22:14:38 +10:30
|
|
|
}
|
2016-09-07 23:21:59 +00:00
|
|
|
} else {
|
2017-05-04 14:17:23 +02:00
|
|
|
err.help("have you added the `#[macro_use]` on the module/import?");
|
2016-09-07 23:21:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-09-21 06:25:09 +00:00
|
|
|
|
2017-03-24 23:03:15 +00:00
|
|
|
fn collect_def_ids(&mut self,
|
|
|
|
mark: Mark,
|
|
|
|
invocation: &'a InvocationData<'a>,
|
2018-06-20 02:08:08 +03:00
|
|
|
fragment: &AstFragment) {
|
2016-10-03 23:48:19 +00:00
|
|
|
let Resolver { ref mut invocations, arenas, graph_root, .. } = *self;
|
2018-05-17 21:28:50 +03:00
|
|
|
let InvocationData { def_index, .. } = *invocation;
|
2016-09-16 08:50:34 +00:00
|
|
|
|
2016-09-23 21:13:59 +00:00
|
|
|
let visit_macro_invoc = &mut |invoc: map::MacroInvocationData| {
|
2016-10-03 23:48:19 +00:00
|
|
|
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
|
|
|
})
|
2016-09-14 09:55:20 +00:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2017-03-24 23:03:15 +00:00
|
|
|
let mut def_collector = DefCollector::new(&mut self.definitions, mark);
|
2016-09-14 09:55:20 +00:00
|
|
|
def_collector.visit_macro_invoc = Some(visit_macro_invoc);
|
2016-09-29 02:23:19 +00:00
|
|
|
def_collector.with_parent(def_index, |def_collector| {
|
2018-06-20 02:08:08 +03:00
|
|
|
fragment.visit_with(def_collector)
|
2016-09-23 21:13:59 +00:00
|
|
|
});
|
2016-09-14 09:55:20 +00:00
|
|
|
}
|
2016-12-01 11:20:04 +00:00
|
|
|
|
2017-03-18 01:55:51 +00:00
|
|
|
pub fn define_macro(&mut self,
|
|
|
|
item: &ast::Item,
|
|
|
|
expansion: Mark,
|
|
|
|
legacy_scope: &mut LegacyScope<'a>) {
|
2017-03-01 23:48:16 +00:00
|
|
|
self.local_macro_def_scopes.insert(item.id, self.current_module);
|
|
|
|
let ident = item.ident;
|
|
|
|
if ident.name == "macro_rules" {
|
2016-12-01 11:20:04 +00:00
|
|
|
self.session.span_err(item.span, "user-defined macros may not be named `macro_rules`");
|
|
|
|
}
|
|
|
|
|
2017-03-01 23:48:16 +00:00
|
|
|
let def_id = self.definitions.local_def_id(item.id);
|
2018-02-27 17:11:14 +01:00
|
|
|
let ext = Lrc::new(macro_rules::compile(&self.session.parse_sess,
|
2018-02-14 16:11:02 +01:00
|
|
|
&self.session.features_untracked(),
|
2018-05-13 03:51:46 +03:00
|
|
|
item, hygiene::default_edition()));
|
2017-03-01 23:48:16 +00:00
|
|
|
self.macro_map.insert(def_id, ext);
|
2016-12-01 11:20:04 +00:00
|
|
|
|
2017-03-18 01:55:51 +00:00
|
|
|
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);
|
2018-01-06 17:23:33 +05:30
|
|
|
let def = Def::Macro(def_id, MacroKind::Bang);
|
2018-08-28 00:56:11 +03:00
|
|
|
let vis = ty::Visibility::Invisible; // Doesn't matter for legacy bindings
|
|
|
|
let binding = (def, vis, item.span, expansion).to_name_binding(self.arenas);
|
|
|
|
*legacy_scope = LegacyScope::Binding(self.arenas.alloc_legacy_binding(
|
|
|
|
LegacyBinding { parent: Cell::new(*legacy_scope), binding, ident }
|
|
|
|
));
|
2018-01-06 17:23:33 +05:30
|
|
|
self.all_macros.insert(ident.name, def);
|
2017-03-18 01:55:51 +00:00
|
|
|
if attr::contains_name(&item.attrs, "macro_export") {
|
2018-05-14 03:22:52 +03:00
|
|
|
let module = self.graph_root;
|
|
|
|
let vis = ty::Visibility::Public;
|
|
|
|
self.define(module, ident, MacroNS,
|
|
|
|
(def, vis, item.span, expansion, IsMacroExport));
|
2017-03-18 01:55:51 +00:00
|
|
|
} else {
|
|
|
|
self.unused_macros.insert(def_id);
|
|
|
|
}
|
2017-05-11 10:26:07 +02:00
|
|
|
} else {
|
2017-03-18 01:55:51 +00:00
|
|
|
let module = self.current_module;
|
|
|
|
let def = Def::Macro(def_id, MacroKind::Bang);
|
|
|
|
let vis = self.resolve_visibility(&item.vis);
|
2017-05-31 17:03:41 +02:00
|
|
|
if vis != ty::Visibility::Public {
|
|
|
|
self.unused_macros.insert(def_id);
|
|
|
|
}
|
2017-03-18 01:55:51 +00:00
|
|
|
self.define(module, ident, MacroNS, (def, vis, item.span, expansion));
|
2016-12-01 11:20:04 +00:00
|
|
|
}
|
|
|
|
}
|
2017-01-09 01:31:14 -08: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>) {
|
2018-08-18 02:38:51 +03:00
|
|
|
let krate = match binding.def() {
|
|
|
|
Def::NonMacroAttr(..) | Def::Err => return,
|
|
|
|
Def::Macro(def_id, _) => def_id.krate,
|
|
|
|
_ => unreachable!(),
|
|
|
|
};
|
2017-01-09 01:31:14 -08:00
|
|
|
|
|
|
|
// 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
|
2018-06-24 19:24:51 +03:00
|
|
|
SyntaxExtension::AttrProcMacro(..) | SyntaxExtension::ProcMacro { .. } =>
|
2018-05-13 03:51:46 +03:00
|
|
|
if !self.warned_proc_macros.insert(name) { return; },
|
2017-01-09 01:31:14 -08:00
|
|
|
_ => return,
|
|
|
|
}
|
|
|
|
|
|
|
|
let warn_msg = match *ext {
|
2018-06-24 19:24:51 +03:00
|
|
|
SyntaxExtension::AttrProcMacro(..) =>
|
|
|
|
"attribute procedural macros cannot be imported with `#[macro_use]`",
|
|
|
|
SyntaxExtension::ProcMacro { .. } =>
|
|
|
|
"procedural macros cannot be imported with `#[macro_use]`",
|
2017-01-09 01:31:14 -08:00
|
|
|
_ => return,
|
|
|
|
};
|
|
|
|
|
2017-11-16 15:03:25 +01:00
|
|
|
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);
|
2017-01-09 01:31:14 -08:00
|
|
|
|
2017-11-16 15:03:25 +01:00
|
|
|
if let Some(span) = span {
|
|
|
|
let found_use = if found_use { "" } else { "\n" };
|
|
|
|
self.session.struct_span_err(err.use_span, err.warn_msg)
|
2018-08-19 15:01:33 -04:00
|
|
|
.span_suggestion_with_applicability(
|
2017-11-16 15:03:25 +01:00
|
|
|
span,
|
|
|
|
"instead, import the procedural macro like any other item",
|
|
|
|
format!("use {}::{};{}", err.crate_name, err.name, found_use),
|
2018-08-19 15:01:33 -04:00
|
|
|
Applicability::MachineApplicable
|
2017-11-16 15:03:25 +01:00
|
|
|
).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();
|
|
|
|
}
|
|
|
|
}
|
2017-01-09 01:31:14 -08:00
|
|
|
}
|
2017-02-02 00:33:42 +00:00
|
|
|
|
|
|
|
fn gate_legacy_custom_derive(&mut self, name: Symbol, span: Span) {
|
2018-02-14 16:11:02 +01:00
|
|
|
if !self.session.features_untracked().custom_derive {
|
2017-02-02 00:33:42 +00:00
|
|
|
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 23:21:59 +00:00
|
|
|
}
|