2019-07-03 08:44:57 +00:00
|
|
|
use crate::{AmbiguityError, AmbiguityKind, AmbiguityErrorMisc, Determinacy};
|
2019-07-11 18:45:43 +00:00
|
|
|
use crate::{CrateLint, Resolver, ResolutionError, Scope, ScopeSet, ParentScope, Weak};
|
2019-02-06 17:15:23 +00:00
|
|
|
use crate::{Module, ModuleKind, NameBinding, NameBindingKind, PathResult, Segment, ToNameBinding};
|
|
|
|
use crate::{is_known_tool, resolve_error};
|
|
|
|
use crate::ModuleOrUniformRoot;
|
|
|
|
use crate::Namespace::*;
|
|
|
|
use crate::build_reduced_graph::{BuildReducedGraphVisitor, IsMacroExport};
|
|
|
|
use crate::resolve_imports::ImportResolver;
|
2019-05-08 19:07:12 +00:00
|
|
|
use rustc::hir::def_id::{CrateNum, DefId, DefIndex, CRATE_DEF_INDEX};
|
2019-04-20 15:26:26 +00:00
|
|
|
use rustc::hir::def::{self, DefKind, NonMacroAttrKind};
|
2019-07-02 10:47:28 +00:00
|
|
|
use rustc::hir::map::DefCollector;
|
2019-06-21 21:18:09 +00:00
|
|
|
use rustc::middle::stability;
|
2019-06-20 08:52:31 +00:00
|
|
|
use rustc::{ty, lint, span_bug};
|
2019-07-02 22:44:04 +00:00
|
|
|
use syntax::ast::{self, Ident, ItemKind};
|
2019-06-21 08:50:30 +00:00
|
|
|
use syntax::attr::{self, StabilityLevel};
|
2019-07-03 08:44:57 +00:00
|
|
|
use syntax::ext::base::{self, Indeterminate};
|
2019-01-26 13:29:34 +00:00
|
|
|
use syntax::ext::base::{MacroKind, SyntaxExtension};
|
2018-09-18 22:46:18 +00:00
|
|
|
use syntax::ext::expand::{AstFragment, Invocation, InvocationKind};
|
2019-07-06 18:02:45 +00:00
|
|
|
use syntax::ext::hygiene::{self, Mark, ExpnInfo, ExpnKind};
|
2016-09-21 06:25:09 +00:00
|
|
|
use syntax::ext::tt::macro_rules;
|
2019-07-11 18:13:11 +00:00
|
|
|
use syntax::feature_gate::{emit_feature_err, is_builtin_attr_name};
|
|
|
|
use syntax::feature_gate::GateIssue;
|
2019-05-11 14:41:37 +00:00
|
|
|
use syntax::symbol::{Symbol, kw, sym};
|
2016-11-07 22:08:26 +00:00
|
|
|
use syntax_pos::{Span, DUMMY_SP};
|
2016-09-07 23:21:59 +00:00
|
|
|
|
2017-03-01 08:44:05 +00:00
|
|
|
use std::cell::Cell;
|
2018-11-25 13:08:43 +00:00
|
|
|
use std::{mem, ptr};
|
2018-02-27 16:11:14 +00:00
|
|
|
use rustc_data_structures::sync::Lrc;
|
2017-03-01 08:44:05 +00:00
|
|
|
|
2019-04-20 16:36:05 +00:00
|
|
|
type Res = def::Res<ast::NodeId>;
|
2019-04-03 07:07:45 +00:00
|
|
|
|
2019-07-02 10:47:28 +00:00
|
|
|
// FIXME: Merge this with `ParentScope`.
|
2018-10-21 22:28:59 +00:00
|
|
|
#[derive(Clone, Debug)]
|
2016-10-03 23:48:19 +00:00
|
|
|
pub struct InvocationData<'a> {
|
2019-02-08 13:53:55 +00:00
|
|
|
/// The module in which the macro was invoked.
|
2019-07-02 10:47:28 +00:00
|
|
|
crate module: Module<'a>,
|
2019-02-08 13:53:55 +00:00
|
|
|
/// The legacy scope in which the macro was invoked.
|
2018-08-31 19:53:08 +00:00
|
|
|
/// The invocation path is resolved in this scope.
|
2019-07-02 10:47:28 +00:00
|
|
|
crate parent_legacy_scope: LegacyScope<'a>,
|
2019-02-08 13:53:55 +00:00
|
|
|
/// The legacy scope *produced* by expanding this macro invocation,
|
2018-08-31 19:53:08 +00:00
|
|
|
/// includes all the macro_rules items, other invocations, etc generated by it.
|
2018-11-07 21:39:07 +00:00
|
|
|
/// `None` if the macro is not expanded yet.
|
|
|
|
crate output_legacy_scope: Cell<Option<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 {
|
2019-07-02 10:47:28 +00:00
|
|
|
module: graph_root,
|
|
|
|
parent_legacy_scope: LegacyScope::Empty,
|
|
|
|
output_legacy_scope: Cell::new(None),
|
2016-09-14 09:55:20 +00:00
|
|
|
}
|
|
|
|
}
|
2016-09-07 23:21:59 +00:00
|
|
|
}
|
|
|
|
|
2018-08-31 19:53:08 +00:00
|
|
|
/// Binding produced by a `macro_rules` item.
|
|
|
|
/// Not modularized, can shadow previous legacy bindings, etc.
|
2018-10-21 22:28:59 +00:00
|
|
|
#[derive(Debug)]
|
2018-08-29 01:48:02 +00:00
|
|
|
pub struct LegacyBinding<'a> {
|
|
|
|
binding: &'a NameBinding<'a>,
|
2018-08-31 19:53:08 +00:00
|
|
|
/// Legacy scope into which the `macro_rules` item was planted.
|
|
|
|
parent_legacy_scope: LegacyScope<'a>,
|
2018-08-29 01:48:02 +00:00
|
|
|
ident: Ident,
|
|
|
|
}
|
|
|
|
|
2019-02-08 13:53:55 +00:00
|
|
|
/// The scope introduced by a `macro_rules!` macro.
|
|
|
|
/// This starts at the macro's definition and ends at the end of the macro's parent
|
|
|
|
/// module (named or unnamed), or even further if it escapes with `#[macro_use]`.
|
2018-08-31 19:53:08 +00:00
|
|
|
/// Some macro invocations need to introduce legacy scopes too because they
|
2019-02-08 13:53:55 +00:00
|
|
|
/// can potentially expand into macro definitions.
|
2018-10-21 22:28:59 +00:00
|
|
|
#[derive(Copy, Clone, Debug)]
|
2016-10-06 08:04:30 +00:00
|
|
|
pub enum LegacyScope<'a> {
|
2018-08-31 19:53:08 +00:00
|
|
|
/// Empty "root" scope at the crate start containing no names.
|
2016-10-06 08:04:30 +00:00
|
|
|
Empty,
|
2019-02-08 13:53:55 +00:00
|
|
|
/// The scope introduced by a `macro_rules!` macro definition.
|
2016-10-06 08:04:30 +00:00
|
|
|
Binding(&'a LegacyBinding<'a>),
|
2019-02-08 13:53:55 +00:00
|
|
|
/// The scope introduced by a macro invocation that can potentially
|
2018-08-31 19:53:08 +00:00
|
|
|
/// create a `macro_rules!` macro definition.
|
|
|
|
Invocation(&'a InvocationData<'a>),
|
2018-08-17 23:38:51 +00:00
|
|
|
}
|
|
|
|
|
2018-09-08 19:19:53 +00:00
|
|
|
// Macro namespace is separated into two sub-namespaces, one for bang macros and
|
|
|
|
// one for attribute-like macros (attributes, derives).
|
|
|
|
// We ignore resolutions from one sub-namespace when searching names in scope for another.
|
2018-11-07 21:39:07 +00:00
|
|
|
fn sub_namespace_match(candidate: Option<MacroKind>, requirement: Option<MacroKind>) -> bool {
|
2018-09-08 19:19:53 +00:00
|
|
|
#[derive(PartialEq)]
|
|
|
|
enum SubNS { Bang, AttrLike }
|
|
|
|
let sub_ns = |kind| match kind {
|
2019-06-18 19:23:13 +00:00
|
|
|
MacroKind::Bang => SubNS::Bang,
|
|
|
|
MacroKind::Attr | MacroKind::Derive => SubNS::AttrLike,
|
2018-09-08 19:19:53 +00:00
|
|
|
};
|
2019-06-18 19:23:13 +00:00
|
|
|
let candidate = candidate.map(sub_ns);
|
|
|
|
let requirement = requirement.map(sub_ns);
|
2018-09-08 19:19:53 +00:00
|
|
|
// "No specific sub-namespace" means "matches anything" for both requirements and candidates.
|
2018-11-07 21:39:07 +00:00
|
|
|
candidate.is_none() || requirement.is_none() || candidate == requirement
|
2018-09-10 21:28:35 +00:00
|
|
|
}
|
|
|
|
|
2019-06-18 07:48:44 +00:00
|
|
|
// We don't want to format a path using pretty-printing,
|
|
|
|
// `format!("{}", path)`, because that tries to insert
|
|
|
|
// line-breaks and is slow.
|
2019-06-30 12:58:56 +00:00
|
|
|
fn fast_print_path(path: &ast::Path) -> Symbol {
|
|
|
|
if path.segments.len() == 1 {
|
|
|
|
return path.segments[0].ident.name
|
|
|
|
} else {
|
|
|
|
let mut path_str = String::with_capacity(64);
|
|
|
|
for (i, segment) in path.segments.iter().enumerate() {
|
|
|
|
if i != 0 {
|
|
|
|
path_str.push_str("::");
|
|
|
|
}
|
|
|
|
if segment.ident.name != kw::PathRoot {
|
|
|
|
path_str.push_str(&segment.ident.as_str())
|
|
|
|
}
|
2019-06-18 07:48:44 +00:00
|
|
|
}
|
2019-06-30 12:58:56 +00:00
|
|
|
Symbol::intern(&path_str)
|
2019-06-18 07:48:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-02 22:44:04 +00:00
|
|
|
fn proc_macro_stub(item: &ast::Item) -> Option<(MacroKind, Ident, Span)> {
|
|
|
|
if attr::contains_name(&item.attrs, sym::proc_macro) {
|
|
|
|
return Some((MacroKind::Bang, item.ident, item.span));
|
|
|
|
} else if attr::contains_name(&item.attrs, sym::proc_macro_attribute) {
|
|
|
|
return Some((MacroKind::Attr, item.ident, item.span));
|
|
|
|
} else if let Some(attr) = attr::find_by_name(&item.attrs, sym::proc_macro_derive) {
|
|
|
|
if let Some(nested_meta) = attr.meta_item_list().and_then(|list| list.get(0).cloned()) {
|
|
|
|
if let Some(ident) = nested_meta.ident() {
|
|
|
|
return Some((MacroKind::Derive, ident, ident.span));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
None
|
|
|
|
}
|
|
|
|
|
2018-12-10 04:37:10 +00:00
|
|
|
impl<'a> base::Resolver for Resolver<'a> {
|
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 {
|
2019-07-06 18:02:45 +00:00
|
|
|
let span = DUMMY_SP.fresh_expansion(Mark::root(), ExpnInfo::default(
|
|
|
|
ExpnKind::Macro(MacroKind::Attr, sym::test_case), DUMMY_SP, self.session.edition()
|
|
|
|
));
|
|
|
|
let mark = span.ctxt().outer();
|
2016-12-20 08:32:15 +00:00
|
|
|
let module = self.module_map[&self.definitions.local_def_id(id)];
|
2019-07-02 10:47:28 +00:00
|
|
|
self.definitions.set_invocation_parent(mark, module.def_id().unwrap().index);
|
2016-10-03 23:48:19 +00:00
|
|
|
self.invocations.insert(mark, self.arenas.alloc_invocation_data(InvocationData {
|
2019-07-02 10:47:28 +00:00
|
|
|
module,
|
|
|
|
parent_legacy_scope: LegacyScope::Empty,
|
|
|
|
output_legacy_scope: Cell::new(None),
|
2016-09-16 08:50:34 +00:00
|
|
|
}));
|
2016-09-23 07:23:01 +00:00
|
|
|
mark
|
|
|
|
}
|
|
|
|
|
2019-07-05 00:09:24 +00:00
|
|
|
fn resolve_dollar_crates(&mut self) {
|
|
|
|
hygiene::update_dollar_crate_names(|ctxt| {
|
|
|
|
let ident = Ident::new(kw::DollarCrate, DUMMY_SP.with_ctxt(ctxt));
|
|
|
|
match self.resolve_crate_root(ident).kind {
|
|
|
|
ModuleKind::Def(.., name) if name != kw::Invalid => name,
|
|
|
|
_ => kw::Crate,
|
2018-12-27 21:31:28 +00:00
|
|
|
}
|
2019-07-05 00:09:24 +00:00
|
|
|
});
|
2018-12-27 21:31:28 +00:00
|
|
|
}
|
|
|
|
|
2018-06-19 23:08:08 +00:00
|
|
|
fn visit_ast_fragment_with_placeholders(&mut self, mark: Mark, fragment: &AstFragment,
|
|
|
|
derives: &[Mark]) {
|
2019-07-02 10:47:28 +00:00
|
|
|
fragment.visit_with(&mut DefCollector::new(&mut self.definitions, mark));
|
2016-10-06 08:04:30 +00:00
|
|
|
|
2019-07-02 10:47:28 +00:00
|
|
|
let invocation = self.invocations[&mark];
|
|
|
|
self.current_module = invocation.module;
|
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);
|
2019-07-02 10:47:28 +00:00
|
|
|
let parent_def = self.definitions.invocation_parent(mark);
|
|
|
|
for &derive_invoc_id in derives {
|
|
|
|
self.definitions.set_invocation_parent(derive_invoc_id, parent_def);
|
|
|
|
}
|
2018-10-17 09:36:19 +00:00
|
|
|
self.invocations.extend(derives.iter().map(|&derive| (derive, invocation)));
|
2016-10-06 08:04:30 +00:00
|
|
|
let mut visitor = BuildReducedGraphVisitor {
|
|
|
|
resolver: self,
|
2019-07-02 10:47:28 +00:00
|
|
|
current_legacy_scope: invocation.parent_legacy_scope,
|
2016-10-11 03:42:06 +00:00
|
|
|
expansion: mark,
|
2016-10-06 08:04:30 +00:00
|
|
|
};
|
2018-06-19 23:08:08 +00:00
|
|
|
fragment.visit_with(&mut visitor);
|
2018-11-07 21:39:07 +00:00
|
|
|
invocation.output_legacy_scope.set(Some(visitor.current_legacy_scope));
|
2016-09-07 23:21:59 +00:00
|
|
|
}
|
|
|
|
|
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 {
|
2018-09-11 09:18:58 +00:00
|
|
|
krate: CrateNum::BuiltinMacros,
|
2019-05-18 11:19:33 +00:00
|
|
|
index: DefIndex::from(self.macro_map.len()),
|
2016-10-28 06:52:45 +00:00
|
|
|
};
|
2019-06-16 15:58:39 +00:00
|
|
|
let kind = ext.macro_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 {
|
2019-04-20 16:36:05 +00:00
|
|
|
kind: NameBindingKind::Res(Res::Def(DefKind::Macro(kind), def_id), false),
|
2018-12-29 15:15:29 +00:00
|
|
|
ambiguity: None,
|
2016-11-07 22:08:26 +00:00
|
|
|
span: DUMMY_SP,
|
2018-11-11 17:28:56 +00:00
|
|
|
vis: ty::Visibility::Public,
|
2016-11-07 22:23:26 +00:00
|
|
|
expansion: Mark::root(),
|
2016-11-07 22:08:26 +00:00
|
|
|
});
|
2018-09-03 22:14:58 +00:00
|
|
|
if self.builtin_macros.insert(ident.name, binding).is_some() {
|
|
|
|
self.session.span_err(ident.span,
|
|
|
|
&format!("built-in macro `{}` was already defined", ident));
|
|
|
|
}
|
2016-09-07 23:21:59 +00:00
|
|
|
}
|
|
|
|
|
2016-11-10 10:11:25 +00:00
|
|
|
fn resolve_imports(&mut self) {
|
|
|
|
ImportResolver { resolver: self }.resolve_imports()
|
|
|
|
}
|
|
|
|
|
2018-09-02 01:57:56 +00:00
|
|
|
fn resolve_macro_invocation(&mut self, invoc: &Invocation, invoc_id: Mark, force: bool)
|
2019-07-03 08:44:57 +00:00
|
|
|
-> Result<Option<Lrc<SyntaxExtension>>, Indeterminate> {
|
2018-09-18 22:46:18 +00:00
|
|
|
let (path, kind, derives_in_scope, after_derive) = match invoc.kind {
|
2019-07-07 22:00:43 +00:00
|
|
|
InvocationKind::Attr { ref attr, ref derives, after_derive, .. } =>
|
|
|
|
(&attr.path, MacroKind::Attr, derives.clone(), after_derive),
|
2018-08-12 23:57:19 +00:00
|
|
|
InvocationKind::Bang { ref mac, .. } =>
|
2018-09-18 22:46:18 +00:00
|
|
|
(&mac.node.path, MacroKind::Bang, Vec::new(), false),
|
2018-08-12 23:57:19 +00:00
|
|
|
InvocationKind::Derive { ref path, .. } =>
|
2018-09-18 22:46:18 +00:00
|
|
|
(path, MacroKind::Derive, Vec::new(), false),
|
2019-07-07 22:00:43 +00:00
|
|
|
InvocationKind::DeriveContainer { .. } =>
|
|
|
|
return Ok(None),
|
2017-03-01 23:48:16 +00:00
|
|
|
};
|
2018-08-11 13:40:08 +00:00
|
|
|
|
2018-09-12 22:41:07 +00:00
|
|
|
let parent_scope = self.invoc_parent_scope(invoc_id, derives_in_scope);
|
2019-07-03 20:59:03 +00:00
|
|
|
let (ext, res) = self.smart_resolve_macro_path(path, kind, &parent_scope, true, force)?;
|
2018-08-15 00:51:12 +00:00
|
|
|
|
2019-06-20 19:00:47 +00:00
|
|
|
let span = invoc.span();
|
2019-07-03 20:59:03 +00:00
|
|
|
invoc.expansion_data.mark.set_expn_info(ext.expn_info(span, fast_print_path(path)));
|
2019-06-20 22:59:30 +00:00
|
|
|
|
2019-06-18 07:48:44 +00:00
|
|
|
if let Res::Def(_, def_id) = res {
|
2018-09-18 22:46:18 +00:00
|
|
|
if after_derive {
|
2019-06-20 19:00:47 +00:00
|
|
|
self.session.span_err(span, "macro attributes must be placed before `#[derive]`");
|
|
|
|
}
|
2018-08-15 00:51:12 +00:00
|
|
|
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);
|
2018-07-12 10:24:59 +00:00
|
|
|
}
|
2018-08-15 00:51:12 +00:00
|
|
|
|
2017-03-22 08:39:51 +00:00
|
|
|
Ok(Some(ext))
|
2017-03-01 23:48:16 +00:00
|
|
|
}
|
|
|
|
|
2017-05-11 08:26:07 +00:00
|
|
|
fn check_unused_macros(&self) {
|
2019-06-20 08:52:31 +00:00
|
|
|
for (&node_id, &span) in self.unused_macros.iter() {
|
|
|
|
self.session.buffer_lint(
|
|
|
|
lint::builtin::UNUSED_MACROS, node_id, span, "unused macro definition"
|
|
|
|
);
|
2017-05-11 08:26:07 +00:00
|
|
|
}
|
2017-03-01 23:48:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-10 04:37:10 +00:00
|
|
|
impl<'a> Resolver<'a> {
|
2018-11-08 22:29:07 +00:00
|
|
|
pub fn dummy_parent_scope(&self) -> ParentScope<'a> {
|
2018-09-12 22:41:07 +00:00
|
|
|
self.invoc_parent_scope(Mark::root(), Vec::new())
|
|
|
|
}
|
|
|
|
|
2018-11-08 22:29:07 +00:00
|
|
|
fn invoc_parent_scope(&self, invoc_id: Mark, derives: Vec<ast::Path>) -> ParentScope<'a> {
|
2018-09-12 22:41:07 +00:00
|
|
|
let invoc = self.invocations[&invoc_id];
|
|
|
|
ParentScope {
|
2019-07-02 10:47:28 +00:00
|
|
|
module: invoc.module.nearest_item_scope(),
|
2018-09-12 22:41:07 +00:00
|
|
|
expansion: invoc_id.parent(),
|
2019-07-02 10:47:28 +00:00
|
|
|
legacy: invoc.parent_legacy_scope,
|
2018-09-12 22:41:07 +00:00
|
|
|
derives,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-03 20:59:03 +00:00
|
|
|
/// Resolve macro path with error reporting and recovery.
|
|
|
|
fn smart_resolve_macro_path(
|
2018-09-12 22:41:07 +00:00
|
|
|
&mut self,
|
|
|
|
path: &ast::Path,
|
|
|
|
kind: MacroKind,
|
|
|
|
parent_scope: &ParentScope<'a>,
|
2018-11-13 23:20:59 +00:00
|
|
|
trace: bool,
|
2018-09-12 22:41:07 +00:00
|
|
|
force: bool,
|
2019-07-03 20:59:03 +00:00
|
|
|
) -> Result<(Lrc<SyntaxExtension>, Res), Indeterminate> {
|
|
|
|
let (ext, res) = match self.resolve_macro_path(path, kind, parent_scope, trace, force) {
|
|
|
|
Ok((Some(ext), res)) => (ext, res),
|
|
|
|
// Use dummy syntax extensions for unresolved macros for better recovery.
|
|
|
|
Ok((None, res)) => (self.dummy_ext(kind), res),
|
|
|
|
Err(Determinacy::Determined) => (self.dummy_ext(kind), Res::Err),
|
|
|
|
Err(Determinacy::Undetermined) => return Err(Indeterminate),
|
|
|
|
};
|
2018-08-15 00:51:12 +00:00
|
|
|
|
|
|
|
// Report errors and enforce feature gates for the resolved macro.
|
2019-06-30 10:00:45 +00:00
|
|
|
let features = self.session.features_untracked();
|
2019-07-03 20:59:03 +00:00
|
|
|
for segment in &path.segments {
|
|
|
|
if let Some(args) = &segment.args {
|
|
|
|
self.session.span_err(args.span(), "generic arguments in macro path");
|
2018-08-15 00:51:12 +00:00
|
|
|
}
|
2019-07-03 20:59:03 +00:00
|
|
|
if kind == MacroKind::Attr && !features.rustc_attrs &&
|
|
|
|
segment.ident.as_str().starts_with("rustc") {
|
|
|
|
let msg =
|
|
|
|
"attributes starting with `rustc` are reserved for use by the `rustc` compiler";
|
|
|
|
emit_feature_err(
|
|
|
|
&self.session.parse_sess,
|
|
|
|
sym::rustc_attrs,
|
|
|
|
segment.ident.span,
|
|
|
|
GateIssue::Language,
|
|
|
|
msg,
|
|
|
|
);
|
2019-07-03 08:44:57 +00:00
|
|
|
}
|
2019-07-03 20:59:03 +00:00
|
|
|
}
|
2018-08-15 00:51:12 +00:00
|
|
|
|
2019-04-20 16:36:05 +00:00
|
|
|
match res {
|
2019-06-18 19:23:13 +00:00
|
|
|
Res::Def(DefKind::Macro(_), def_id) => {
|
2019-06-20 08:52:31 +00:00
|
|
|
if let Some(node_id) = self.definitions.as_local_node_id(def_id) {
|
|
|
|
self.unused_macros.remove(&node_id);
|
2019-07-02 22:44:04 +00:00
|
|
|
if self.proc_macro_stubs.contains(&node_id) {
|
|
|
|
self.session.span_err(
|
|
|
|
path.span,
|
|
|
|
"can't use a procedural macro from the same crate that defines it",
|
|
|
|
);
|
|
|
|
}
|
2019-06-20 08:52:31 +00:00
|
|
|
}
|
2019-06-18 19:23:13 +00:00
|
|
|
}
|
2019-04-20 16:36:05 +00:00
|
|
|
Res::NonMacroAttr(attr_kind) => {
|
2019-07-03 08:44:57 +00:00
|
|
|
if attr_kind == NonMacroAttrKind::Custom {
|
|
|
|
assert!(path.segments.len() == 1);
|
|
|
|
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);
|
|
|
|
self.report_unknown_attribute(
|
|
|
|
path.span,
|
|
|
|
&path.segments[0].ident.as_str(),
|
|
|
|
&msg,
|
|
|
|
sym::custom_attribute,
|
|
|
|
);
|
2018-08-15 00:51:12 +00:00
|
|
|
}
|
|
|
|
}
|
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
|
|
|
}
|
2019-07-03 20:59:03 +00:00
|
|
|
Res::Err => {}
|
2019-04-20 16:36:05 +00:00
|
|
|
_ => panic!("expected `DefKind::Macro` or `Res::NonMacroAttr`"),
|
2019-07-03 08:44:57 +00:00
|
|
|
};
|
2018-08-15 00:51:12 +00:00
|
|
|
|
2019-07-03 20:59:03 +00:00
|
|
|
self.check_stability_and_deprecation(&ext, path);
|
|
|
|
|
2019-07-03 09:47:24 +00:00
|
|
|
Ok(if ext.macro_kind() != kind {
|
|
|
|
let expected = if kind == MacroKind::Attr { "attribute" } else { kind.descr() };
|
|
|
|
let msg = format!("expected {}, found {} `{}`", expected, res.descr(), path);
|
|
|
|
self.session.struct_span_err(path.span, &msg)
|
|
|
|
.span_label(path.span, format!("not {} {}", kind.article(), expected))
|
|
|
|
.emit();
|
2019-07-03 20:59:03 +00:00
|
|
|
// Use dummy syntax extensions for unexpected macro kinds for better recovery.
|
|
|
|
(self.dummy_ext(kind), Res::Err)
|
2019-07-03 09:47:24 +00:00
|
|
|
} else {
|
2019-07-03 20:59:03 +00:00
|
|
|
(ext, res)
|
2019-07-03 09:47:24 +00:00
|
|
|
})
|
2017-07-24 21:33:15 +00:00
|
|
|
}
|
2016-09-07 23:21:59 +00:00
|
|
|
|
2019-07-03 20:59:03 +00:00
|
|
|
pub fn resolve_macro_path(
|
2018-09-12 22:41:07 +00:00
|
|
|
&mut self,
|
|
|
|
path: &ast::Path,
|
|
|
|
kind: MacroKind,
|
|
|
|
parent_scope: &ParentScope<'a>,
|
2018-11-13 23:20:59 +00:00
|
|
|
trace: bool,
|
2018-09-12 22:41:07 +00:00
|
|
|
force: bool,
|
2019-07-03 20:59:03 +00:00
|
|
|
) -> Result<(Option<Lrc<SyntaxExtension>>, Res), Determinacy> {
|
2018-10-27 17:23:54 +00:00
|
|
|
let path_span = path.span;
|
2018-09-12 03:21:50 +00:00
|
|
|
let mut path = Segment::from_path(path);
|
2016-11-27 10:58:46 +00:00
|
|
|
|
2018-06-11 11:21:36 +00:00
|
|
|
// Possibly apply the macro helper hack
|
2018-05-14 00:22:52 +00:00
|
|
|
if kind == MacroKind::Bang && path.len() == 1 &&
|
2019-05-27 03:52:11 +00:00
|
|
|
path[0].ident.span.ctxt().outer_expn_info()
|
2018-09-12 03:21:50 +00:00
|
|
|
.map_or(false, |info| info.local_inner_macros) {
|
2019-05-11 14:41:37 +00:00
|
|
|
let root = Ident::new(kw::DollarCrate, path[0].ident.span);
|
2018-09-12 03:21:50 +00:00
|
|
|
path.insert(0, Segment::from_ident(root));
|
2018-06-11 11:21:36 +00:00
|
|
|
}
|
|
|
|
|
2019-07-03 20:59:03 +00:00
|
|
|
let res = if path.len() > 1 {
|
2019-04-20 16:36:05 +00:00
|
|
|
let res = match self.resolve_path(&path, Some(MacroNS), parent_scope,
|
2018-10-27 17:23:54 +00:00
|
|
|
false, path_span, CrateLint::No) {
|
2018-11-10 15:58:37 +00:00
|
|
|
PathResult::NonModule(path_res) if path_res.unresolved_segments() == 0 => {
|
2019-04-20 16:36:05 +00:00
|
|
|
Ok(path_res.base_res())
|
2018-11-10 15:58:37 +00:00
|
|
|
}
|
2016-11-27 10:58:46 +00:00
|
|
|
PathResult::Indeterminate if !force => return Err(Determinacy::Undetermined),
|
2019-01-16 20:30:41 +00:00
|
|
|
PathResult::NonModule(..)
|
|
|
|
| PathResult::Indeterminate
|
|
|
|
| PathResult::Failed { .. } => Err(Determinacy::Determined),
|
2018-11-10 15:58:37 +00:00
|
|
|
PathResult::Module(..) => unreachable!(),
|
2016-11-27 10:58:46 +00:00
|
|
|
};
|
2018-09-18 00:19:26 +00:00
|
|
|
|
2018-11-13 23:20:59 +00:00
|
|
|
if trace {
|
|
|
|
parent_scope.module.multi_segment_macro_resolutions.borrow_mut()
|
2019-04-20 16:36:05 +00:00
|
|
|
.push((path, path_span, kind, parent_scope.clone(), res.ok()));
|
2018-11-13 23:20:59 +00:00
|
|
|
}
|
2016-11-27 10:58:46 +00:00
|
|
|
|
2019-04-20 16:36:05 +00:00
|
|
|
self.prohibit_imported_non_macro_attrs(None, res.ok(), path_span);
|
|
|
|
res
|
2017-03-11 10:58:19 +00:00
|
|
|
} else {
|
2018-09-18 22:01:09 +00:00
|
|
|
let binding = self.early_resolve_ident_in_lexical_scope(
|
2018-11-24 16:14:05 +00:00
|
|
|
path[0].ident, ScopeSet::Macro(kind), parent_scope, false, force, path_span
|
2018-09-18 22:01:09 +00:00
|
|
|
);
|
2018-12-16 17:23:27 +00:00
|
|
|
if let Err(Determinacy::Undetermined) = binding {
|
|
|
|
return Err(Determinacy::Undetermined);
|
2018-09-18 22:01:09 +00:00
|
|
|
}
|
2016-11-10 10:29:36 +00:00
|
|
|
|
2018-11-13 23:20:59 +00:00
|
|
|
if trace {
|
|
|
|
parent_scope.module.single_segment_macro_resolutions.borrow_mut()
|
|
|
|
.push((path[0].ident, kind, parent_scope.clone(), binding.ok()));
|
|
|
|
}
|
2017-02-01 10:33:09 +00:00
|
|
|
|
2019-04-20 16:36:05 +00:00
|
|
|
let res = binding.map(|binding| binding.res());
|
|
|
|
self.prohibit_imported_non_macro_attrs(binding.ok(), res.ok(), path_span);
|
|
|
|
res
|
2019-07-03 20:59:03 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
res.map(|res| (self.get_macro(res), res))
|
2017-02-01 10:33:09 +00:00
|
|
|
}
|
2016-09-26 03:17:05 +00:00
|
|
|
|
2018-09-18 00:19:26 +00:00
|
|
|
// Resolve an identifier in lexical scope.
|
2018-07-22 23:52:51 +00:00
|
|
|
// 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-11-27 02:59:49 +00:00
|
|
|
// The function is used for resolving initial segments of macro paths (e.g., `foo` in
|
2018-11-03 19:02:36 +00:00
|
|
|
// `foo::bar!(); or `foo!();`) and also for import paths on 2018 edition.
|
2018-09-18 00:19:26 +00:00
|
|
|
crate fn early_resolve_ident_in_lexical_scope(
|
2018-08-17 23:38:51 +00:00
|
|
|
&mut self,
|
2018-11-24 12:07:03 +00:00
|
|
|
orig_ident: Ident,
|
2018-11-24 16:14:05 +00:00
|
|
|
scope_set: ScopeSet,
|
2018-09-12 22:41:07 +00:00
|
|
|
parent_scope: &ParentScope<'a>,
|
2018-08-17 23:38:51 +00:00
|
|
|
record_used: bool,
|
|
|
|
force: bool,
|
2018-08-28 01:07:31 +00:00
|
|
|
path_span: Span,
|
2018-09-18 00:19:26 +00:00
|
|
|
) -> Result<&'a NameBinding<'a>, Determinacy> {
|
2018-07-22 23:52:51 +00: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.
|
2018-09-16 22:56:15 +00:00
|
|
|
// 2. "Closed set" below means new names cannot appear after the current resolution attempt.
|
2018-07-22 23:52:51 +00:00
|
|
|
// 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).
|
2018-09-16 22:56:15 +00:00
|
|
|
// (Value NS)
|
|
|
|
// 1. FIXME: Ribs (local variables), 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. Standard library prelude (de-facto closed, controlled).
|
2018-07-22 23:52:51 +00:00
|
|
|
// (Macro NS)
|
2018-09-26 10:11:34 +00:00
|
|
|
// 1-3. Derive helpers (open, not controlled). All ambiguities with other names
|
|
|
|
// are currently reported as errors. They should be higher in priority than preludes
|
|
|
|
// and probably even names in modules according to the "general principles" above. They
|
|
|
|
// also should be subject to restricted shadowing because are effectively produced by
|
|
|
|
// derives (you need to resolve the derive first to add helpers into scope), but they
|
|
|
|
// should be available before the derive is expanded for compatibility.
|
|
|
|
// It's mess in general, so we are being conservative for now.
|
|
|
|
// 1-3. `macro_rules` (open, not controlled), loop through legacy scopes. Have higher
|
2018-09-18 00:19:26 +00:00
|
|
|
// priority than prelude macros, but create ambiguities with macros in modules.
|
2018-09-26 10:11:34 +00:00
|
|
|
// 1-3. Names in modules (both normal `mod`ules and blocks), loop through hygienic parents
|
2018-09-18 00:19:26 +00:00
|
|
|
// (open, not controlled). Have higher priority than prelude macros, but create
|
|
|
|
// ambiguities with `macro_rules`.
|
2018-09-26 10:11:34 +00:00
|
|
|
// 4. `macro_use` prelude (open, the open part is from macro expansions, not controlled).
|
|
|
|
// 4a. User-defined prelude from macro-use
|
2018-07-22 23:52:51 +00:00
|
|
|
// (open, the open part is from macro expansions, not controlled).
|
2018-09-26 10:11:34 +00:00
|
|
|
// 4b. Standard library prelude is currently implemented as `macro-use` (closed, controlled)
|
2018-09-18 00:19:26 +00:00
|
|
|
// 5. Language prelude: builtin macros (closed, controlled, except for legacy plugins).
|
|
|
|
// 6. Language prelude: builtin attributes (closed, controlled).
|
2018-09-26 10:11:34 +00:00
|
|
|
// 4-6. Legacy plugin helpers (open, not controlled). Similar to derive helpers,
|
2018-09-18 00:19:26 +00:00
|
|
|
// but introduced by legacy plugins using `register_attribute`. Priority is somewhere
|
|
|
|
// in prelude, not sure where exactly (creates ambiguities with any other prelude names).
|
|
|
|
|
2019-02-06 17:15:23 +00:00
|
|
|
bitflags::bitflags! {
|
2018-09-18 00:19:26 +00:00
|
|
|
struct Flags: u8 {
|
2018-11-25 13:08:43 +00:00
|
|
|
const MACRO_RULES = 1 << 0;
|
|
|
|
const MODULE = 1 << 1;
|
|
|
|
const PRELUDE = 1 << 2;
|
|
|
|
const MISC_SUGGEST_CRATE = 1 << 3;
|
|
|
|
const MISC_SUGGEST_SELF = 1 << 4;
|
|
|
|
const MISC_FROM_PRELUDE = 1 << 5;
|
2018-09-18 00:19:26 +00:00
|
|
|
}
|
|
|
|
}
|
2018-07-22 23:52:51 +00:00
|
|
|
|
2018-08-04 02:17:51 +00:00
|
|
|
assert!(force || !record_used); // `record_used` implies `force`
|
2018-11-24 12:07:03 +00:00
|
|
|
let mut ident = orig_ident.modern();
|
2018-07-22 23:52:51 +00:00
|
|
|
|
2018-11-08 22:29:07 +00:00
|
|
|
// Make sure `self`, `super` etc produce an error when passed to here.
|
|
|
|
if ident.is_path_segment_keyword() {
|
|
|
|
return Err(Determinacy::Determined);
|
|
|
|
}
|
|
|
|
|
2018-08-28 00:27:41 +00:00
|
|
|
// This is *the* result, resolution from the scope closest to the resolved identifier.
|
|
|
|
// However, sometimes this result is "weak" because it comes from a glob import or
|
|
|
|
// a macro expansion, and in this case it cannot shadow names from outer scopes, e.g.
|
|
|
|
// mod m { ... } // solution in outer scope
|
2018-07-22 23:52:51 +00:00
|
|
|
// {
|
2018-08-28 00:27:41 +00:00
|
|
|
// use prefix::*; // imports another `m` - innermost solution
|
|
|
|
// // weak, cannot shadow the outer `m`, need to report ambiguity error
|
2018-07-22 23:52:51 +00:00
|
|
|
// m::mac!();
|
|
|
|
// }
|
2018-08-28 00:27:41 +00:00
|
|
|
// So we have to save the innermost solution and continue searching in outer scopes
|
|
|
|
// to detect potential ambiguities.
|
2019-02-06 17:15:23 +00:00
|
|
|
let mut innermost_result: Option<(&NameBinding<'_>, Flags)> = None;
|
2018-07-22 23:52:51 +00:00
|
|
|
|
|
|
|
// Go through all the scopes and try to resolve the name.
|
2018-11-24 16:14:05 +00:00
|
|
|
let rust_2015 = orig_ident.span.rust_2015();
|
2018-11-24 21:25:03 +00:00
|
|
|
let (ns, macro_kind, is_import, is_absolute_path) = match scope_set {
|
|
|
|
ScopeSet::Import(ns) => (ns, None, true, false),
|
|
|
|
ScopeSet::AbsolutePath(ns) => (ns, None, false, true),
|
|
|
|
ScopeSet::Macro(macro_kind) => (MacroNS, Some(macro_kind), false, false),
|
|
|
|
ScopeSet::Module => (TypeNS, None, false, false),
|
2018-11-24 16:14:05 +00:00
|
|
|
};
|
2018-11-24 12:07:03 +00:00
|
|
|
let mut where_to_resolve = match ns {
|
2019-07-11 18:45:43 +00:00
|
|
|
_ if is_absolute_path => Scope::CrateRoot,
|
|
|
|
TypeNS | ValueNS => Scope::Module(parent_scope.module),
|
|
|
|
MacroNS => Scope::DeriveHelpers,
|
2018-11-07 21:39:07 +00:00
|
|
|
};
|
2018-09-12 22:41:07 +00:00
|
|
|
let mut use_prelude = !parent_scope.module.no_implicit_prelude;
|
2018-11-07 21:39:07 +00:00
|
|
|
let mut determinacy = Determinacy::Determined;
|
2016-11-10 10:29:36 +00:00
|
|
|
loop {
|
2018-07-22 23:52:51 +00:00
|
|
|
let result = match where_to_resolve {
|
2019-07-11 18:45:43 +00:00
|
|
|
Scope::DeriveHelpers => {
|
2018-09-26 10:11:34 +00:00
|
|
|
let mut result = Err(Determinacy::Determined);
|
|
|
|
for derive in &parent_scope.derives {
|
|
|
|
let parent_scope = ParentScope { derives: Vec::new(), ..*parent_scope };
|
2019-07-03 20:59:03 +00:00
|
|
|
match self.resolve_macro_path(derive, MacroKind::Derive,
|
|
|
|
&parent_scope, true, force) {
|
|
|
|
Ok((Some(ext), _)) => if ext.helper_attrs.contains(&ident.name) {
|
2019-06-16 15:58:39 +00:00
|
|
|
let binding = (Res::NonMacroAttr(NonMacroAttrKind::DeriveHelper),
|
|
|
|
ty::Visibility::Public, derive.span, Mark::root())
|
|
|
|
.to_name_binding(self.arenas);
|
|
|
|
result = Ok((binding, Flags::empty()));
|
|
|
|
break;
|
2018-09-26 10:11:34 +00:00
|
|
|
}
|
2019-07-03 20:59:03 +00:00
|
|
|
Ok(_) | Err(Determinacy::Determined) => {}
|
|
|
|
Err(Determinacy::Undetermined) =>
|
|
|
|
result = Err(Determinacy::Undetermined),
|
2018-09-26 10:11:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
result
|
|
|
|
}
|
2019-07-11 18:45:43 +00:00
|
|
|
Scope::MacroRules(legacy_scope) => match legacy_scope {
|
2018-09-18 00:19:26 +00:00
|
|
|
LegacyScope::Binding(legacy_binding) if ident == legacy_binding.ident =>
|
2018-11-04 22:11:59 +00:00
|
|
|
Ok((legacy_binding.binding, Flags::MACRO_RULES)),
|
2018-11-07 21:39:07 +00:00
|
|
|
LegacyScope::Invocation(invoc) if invoc.output_legacy_scope.get().is_none() =>
|
|
|
|
Err(Determinacy::Undetermined),
|
2018-09-18 00:19:26 +00:00
|
|
|
_ => Err(Determinacy::Determined),
|
|
|
|
}
|
2019-07-11 18:45:43 +00:00
|
|
|
Scope::CrateRoot => {
|
2019-05-11 14:41:37 +00:00
|
|
|
let root_ident = Ident::new(kw::PathRoot, orig_ident.span);
|
2018-11-24 12:07:03 +00:00
|
|
|
let root_module = self.resolve_crate_root(root_ident);
|
|
|
|
let binding = self.resolve_ident_in_module_ext(
|
|
|
|
ModuleOrUniformRoot::Module(root_module),
|
|
|
|
orig_ident,
|
|
|
|
ns,
|
|
|
|
None,
|
|
|
|
record_used,
|
|
|
|
path_span,
|
|
|
|
);
|
|
|
|
match binding {
|
2018-11-25 13:08:43 +00:00
|
|
|
Ok(binding) => Ok((binding, Flags::MODULE | Flags::MISC_SUGGEST_CRATE)),
|
2018-11-24 12:07:03 +00:00
|
|
|
Err((Determinacy::Undetermined, Weak::No)) =>
|
|
|
|
return Err(Determinacy::determined(force)),
|
|
|
|
Err((Determinacy::Undetermined, Weak::Yes)) =>
|
|
|
|
Err(Determinacy::Undetermined),
|
|
|
|
Err((Determinacy::Determined, _)) => Err(Determinacy::Determined),
|
|
|
|
}
|
|
|
|
}
|
2019-07-11 18:45:43 +00:00
|
|
|
Scope::Module(module) => {
|
2018-07-22 23:52:51 +00:00
|
|
|
let orig_current_module = mem::replace(&mut self.current_module, module);
|
2018-11-07 21:39:07 +00:00
|
|
|
let binding = self.resolve_ident_in_module_unadjusted_ext(
|
2018-08-09 13:29:22 +00:00
|
|
|
ModuleOrUniformRoot::Module(module),
|
|
|
|
ident,
|
|
|
|
ns,
|
2018-11-08 22:29:07 +00:00
|
|
|
None,
|
2018-08-09 13:29:22 +00:00
|
|
|
true,
|
|
|
|
record_used,
|
|
|
|
path_span,
|
2018-07-22 23:52:51 +00:00
|
|
|
);
|
|
|
|
self.current_module = orig_current_module;
|
2018-11-07 21:39:07 +00:00
|
|
|
match binding {
|
|
|
|
Ok(binding) => {
|
2018-11-25 13:08:43 +00:00
|
|
|
let misc_flags = if ptr::eq(module, self.graph_root) {
|
|
|
|
Flags::MISC_SUGGEST_CRATE
|
|
|
|
} else if module.is_normal() {
|
2018-11-07 21:39:07 +00:00
|
|
|
Flags::MISC_SUGGEST_SELF
|
|
|
|
} else {
|
|
|
|
Flags::empty()
|
|
|
|
};
|
|
|
|
Ok((binding, Flags::MODULE | misc_flags))
|
|
|
|
}
|
2018-11-17 17:13:25 +00:00
|
|
|
Err((Determinacy::Undetermined, Weak::No)) =>
|
2018-11-07 21:39:07 +00:00
|
|
|
return Err(Determinacy::determined(force)),
|
2018-11-17 17:13:25 +00:00
|
|
|
Err((Determinacy::Undetermined, Weak::Yes)) =>
|
|
|
|
Err(Determinacy::Undetermined),
|
|
|
|
Err((Determinacy::Determined, _)) => Err(Determinacy::Determined),
|
2018-11-07 21:39:07 +00:00
|
|
|
}
|
2018-07-22 23:52:51 +00:00
|
|
|
}
|
2019-07-11 18:45:43 +00:00
|
|
|
Scope::MacroUsePrelude => {
|
2018-11-18 00:25:59 +00:00
|
|
|
if use_prelude || rust_2015 {
|
2018-11-07 21:39:07 +00:00
|
|
|
match self.macro_use_prelude.get(&ident.name).cloned() {
|
|
|
|
Some(binding) =>
|
|
|
|
Ok((binding, Flags::PRELUDE | Flags::MISC_FROM_PRELUDE)),
|
|
|
|
None => Err(Determinacy::determined(
|
|
|
|
self.graph_root.unresolved_invocations.borrow().is_empty()
|
|
|
|
))
|
2018-11-02 21:07:56 +00:00
|
|
|
}
|
2018-11-07 21:39:07 +00:00
|
|
|
} else {
|
|
|
|
Err(Determinacy::Determined)
|
2018-09-03 22:14:58 +00:00
|
|
|
}
|
|
|
|
}
|
2019-07-11 18:45:43 +00:00
|
|
|
Scope::BuiltinMacros => {
|
2018-09-03 22:14:58 +00:00
|
|
|
match self.builtin_macros.get(&ident.name).cloned() {
|
2018-11-04 22:11:59 +00:00
|
|
|
Some(binding) => Ok((binding, Flags::PRELUDE)),
|
2018-07-22 23:52:51 +00:00
|
|
|
None => Err(Determinacy::Determined),
|
|
|
|
}
|
|
|
|
}
|
2019-07-11 18:45:43 +00:00
|
|
|
Scope::BuiltinAttrs => {
|
2018-09-08 19:19:53 +00:00
|
|
|
if is_builtin_attr_name(ident.name) {
|
2019-04-20 16:36:05 +00:00
|
|
|
let binding = (Res::NonMacroAttr(NonMacroAttrKind::Builtin),
|
2018-11-04 22:11:59 +00:00
|
|
|
ty::Visibility::Public, DUMMY_SP, Mark::root())
|
2018-08-02 23:05:00 +00:00
|
|
|
.to_name_binding(self.arenas);
|
2018-11-04 22:11:59 +00:00
|
|
|
Ok((binding, Flags::PRELUDE))
|
2018-07-22 23:52:51 +00:00
|
|
|
} else {
|
|
|
|
Err(Determinacy::Determined)
|
|
|
|
}
|
|
|
|
}
|
2019-07-11 18:45:43 +00:00
|
|
|
Scope::LegacyPluginHelpers => {
|
2018-11-18 00:25:59 +00:00
|
|
|
if (use_prelude || rust_2015) &&
|
2018-11-02 21:07:56 +00:00
|
|
|
self.session.plugin_attributes.borrow().iter()
|
2019-05-08 03:21:18 +00:00
|
|
|
.any(|(name, _)| ident.name == *name) {
|
2019-04-20 16:36:05 +00:00
|
|
|
let binding = (Res::NonMacroAttr(NonMacroAttrKind::LegacyPluginHelper),
|
2018-11-04 22:11:59 +00:00
|
|
|
ty::Visibility::Public, DUMMY_SP, Mark::root())
|
2018-09-15 20:46:54 +00:00
|
|
|
.to_name_binding(self.arenas);
|
2018-11-04 22:11:59 +00:00
|
|
|
Ok((binding, Flags::PRELUDE))
|
2018-09-15 20:46:54 +00:00
|
|
|
} else {
|
|
|
|
Err(Determinacy::Determined)
|
|
|
|
}
|
|
|
|
}
|
2019-07-11 18:45:43 +00:00
|
|
|
Scope::ExternPrelude => {
|
2018-11-24 21:25:03 +00:00
|
|
|
if use_prelude || is_absolute_path {
|
2018-11-17 18:08:00 +00:00
|
|
|
match self.extern_prelude_get(ident, !record_used) {
|
2018-11-07 21:39:07 +00:00
|
|
|
Some(binding) => Ok((binding, Flags::PRELUDE)),
|
|
|
|
None => Err(Determinacy::determined(
|
|
|
|
self.graph_root.unresolved_invocations.borrow().is_empty()
|
|
|
|
)),
|
2018-09-28 22:31:54 +00:00
|
|
|
}
|
2018-11-07 21:39:07 +00:00
|
|
|
} else {
|
|
|
|
Err(Determinacy::Determined)
|
2018-07-22 23:52:51 +00:00
|
|
|
}
|
|
|
|
}
|
2019-07-11 18:45:43 +00:00
|
|
|
Scope::ToolPrelude => {
|
2018-07-22 23:52:51 +00:00
|
|
|
if use_prelude && is_known_tool(ident.name) {
|
2019-04-20 16:36:05 +00:00
|
|
|
let binding = (Res::ToolMod, ty::Visibility::Public,
|
2018-11-04 22:11:59 +00:00
|
|
|
DUMMY_SP, Mark::root()).to_name_binding(self.arenas);
|
|
|
|
Ok((binding, Flags::PRELUDE))
|
2018-07-22 23:52:51 +00:00
|
|
|
} else {
|
|
|
|
Err(Determinacy::Determined)
|
|
|
|
}
|
|
|
|
}
|
2019-07-11 18:45:43 +00:00
|
|
|
Scope::StdLibPrelude => {
|
2018-07-22 23:52:51 +00:00
|
|
|
let mut result = Err(Determinacy::Determined);
|
|
|
|
if use_prelude {
|
|
|
|
if let Some(prelude) = self.prelude {
|
2018-08-09 13:29:22 +00:00
|
|
|
if let Ok(binding) = self.resolve_ident_in_module_unadjusted(
|
|
|
|
ModuleOrUniformRoot::Module(prelude),
|
|
|
|
ident,
|
|
|
|
ns,
|
|
|
|
false,
|
|
|
|
path_span,
|
|
|
|
) {
|
2018-11-04 22:11:59 +00:00
|
|
|
result = Ok((binding, Flags::PRELUDE | Flags::MISC_FROM_PRELUDE));
|
2018-07-22 23:52:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
result
|
|
|
|
}
|
2019-07-11 18:45:43 +00:00
|
|
|
Scope::BuiltinTypes => {
|
2018-09-18 00:19:26 +00:00
|
|
|
match self.primitive_type_table.primitive_types.get(&ident.name).cloned() {
|
|
|
|
Some(prim_ty) => {
|
2019-04-20 16:36:05 +00:00
|
|
|
let binding = (Res::PrimTy(prim_ty), ty::Visibility::Public,
|
2018-11-04 22:11:59 +00:00
|
|
|
DUMMY_SP, Mark::root()).to_name_binding(self.arenas);
|
|
|
|
Ok((binding, Flags::PRELUDE))
|
2018-09-18 00:19:26 +00:00
|
|
|
}
|
|
|
|
None => Err(Determinacy::Determined)
|
2018-07-22 23:52:51 +00:00
|
|
|
}
|
|
|
|
}
|
2017-03-11 10:58:19 +00:00
|
|
|
};
|
|
|
|
|
2018-07-22 23:52:51 +00:00
|
|
|
match result {
|
2018-11-07 21:39:07 +00:00
|
|
|
Ok((binding, flags)) if sub_namespace_match(binding.macro_kind(), macro_kind) => {
|
2017-05-12 09:21:11 +00:00
|
|
|
if !record_used {
|
2018-09-18 00:19:26 +00:00
|
|
|
return Ok(binding);
|
2017-05-12 09:21:11 +00:00
|
|
|
}
|
2018-07-22 23:52:51 +00:00
|
|
|
|
2018-11-04 22:11:59 +00:00
|
|
|
if let Some((innermost_binding, innermost_flags)) = innermost_result {
|
2018-08-28 00:27:41 +00:00
|
|
|
// Found another solution, if the first one was "weak", report an error.
|
2019-04-20 16:36:05 +00:00
|
|
|
let (res, innermost_res) = (binding.res(), innermost_binding.res());
|
|
|
|
if res != innermost_res {
|
|
|
|
let builtin = Res::NonMacroAttr(NonMacroAttrKind::Builtin);
|
|
|
|
let derive_helper = Res::NonMacroAttr(NonMacroAttrKind::DeriveHelper);
|
2018-11-04 22:11:59 +00:00
|
|
|
let legacy_helper =
|
2019-04-20 16:36:05 +00:00
|
|
|
Res::NonMacroAttr(NonMacroAttrKind::LegacyPluginHelper);
|
2018-11-04 22:11:59 +00:00
|
|
|
|
|
|
|
let ambiguity_error_kind = if is_import {
|
|
|
|
Some(AmbiguityKind::Import)
|
2019-04-20 16:36:05 +00:00
|
|
|
} else if innermost_res == builtin || res == builtin {
|
2018-11-04 22:11:59 +00:00
|
|
|
Some(AmbiguityKind::BuiltinAttr)
|
2019-04-20 16:36:05 +00:00
|
|
|
} else if innermost_res == derive_helper || res == derive_helper {
|
2018-11-04 22:11:59 +00:00
|
|
|
Some(AmbiguityKind::DeriveHelper)
|
2019-04-20 16:36:05 +00:00
|
|
|
} else if innermost_res == legacy_helper &&
|
2018-11-04 22:11:59 +00:00
|
|
|
flags.contains(Flags::PRELUDE) ||
|
2019-04-20 16:36:05 +00:00
|
|
|
res == legacy_helper &&
|
2018-11-04 22:11:59 +00:00
|
|
|
innermost_flags.contains(Flags::PRELUDE) {
|
|
|
|
Some(AmbiguityKind::LegacyHelperVsPrelude)
|
|
|
|
} else if innermost_flags.contains(Flags::MACRO_RULES) &&
|
|
|
|
flags.contains(Flags::MODULE) &&
|
|
|
|
!self.disambiguate_legacy_vs_modern(innermost_binding,
|
2018-11-24 12:07:03 +00:00
|
|
|
binding) ||
|
|
|
|
flags.contains(Flags::MACRO_RULES) &&
|
|
|
|
innermost_flags.contains(Flags::MODULE) &&
|
|
|
|
!self.disambiguate_legacy_vs_modern(binding,
|
|
|
|
innermost_binding) {
|
2018-11-04 22:11:59 +00:00
|
|
|
Some(AmbiguityKind::LegacyVsModern)
|
|
|
|
} else if innermost_binding.is_glob_import() {
|
|
|
|
Some(AmbiguityKind::GlobVsOuter)
|
|
|
|
} else if innermost_binding.may_appear_after(parent_scope.expansion,
|
|
|
|
binding) {
|
|
|
|
Some(AmbiguityKind::MoreExpandedVsOuter)
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
};
|
|
|
|
if let Some(kind) = ambiguity_error_kind {
|
2018-11-25 13:08:43 +00:00
|
|
|
let misc = |f: Flags| if f.contains(Flags::MISC_SUGGEST_CRATE) {
|
|
|
|
AmbiguityErrorMisc::SuggestCrate
|
|
|
|
} else if f.contains(Flags::MISC_SUGGEST_SELF) {
|
2018-11-04 22:11:59 +00:00
|
|
|
AmbiguityErrorMisc::SuggestSelf
|
|
|
|
} else if f.contains(Flags::MISC_FROM_PRELUDE) {
|
|
|
|
AmbiguityErrorMisc::FromPrelude
|
|
|
|
} else {
|
|
|
|
AmbiguityErrorMisc::None
|
|
|
|
};
|
|
|
|
self.ambiguity_errors.push(AmbiguityError {
|
|
|
|
kind,
|
2018-11-25 13:08:43 +00:00
|
|
|
ident: orig_ident,
|
2018-11-04 22:11:59 +00:00
|
|
|
b1: innermost_binding,
|
|
|
|
b2: binding,
|
|
|
|
misc1: misc(innermost_flags),
|
|
|
|
misc2: misc(flags),
|
|
|
|
});
|
|
|
|
return Ok(innermost_binding);
|
|
|
|
}
|
2016-11-27 10:58:46 +00:00
|
|
|
}
|
2018-08-28 00:27:41 +00:00
|
|
|
} else {
|
|
|
|
// Found the first solution.
|
2018-11-04 22:11:59 +00:00
|
|
|
innermost_result = Some((binding, flags));
|
2017-03-11 10:58:19 +00:00
|
|
|
}
|
2018-07-22 23:52:51 +00:00
|
|
|
}
|
2018-11-07 21:39:07 +00:00
|
|
|
Ok(..) | Err(Determinacy::Determined) => {}
|
|
|
|
Err(Determinacy::Undetermined) => determinacy = Determinacy::Undetermined
|
2016-11-10 10:29:36 +00:00
|
|
|
}
|
2018-11-07 21:39:07 +00:00
|
|
|
|
|
|
|
where_to_resolve = match where_to_resolve {
|
2019-07-11 18:45:43 +00:00
|
|
|
Scope::DeriveHelpers =>
|
|
|
|
Scope::MacroRules(parent_scope.legacy),
|
|
|
|
Scope::MacroRules(legacy_scope) => match legacy_scope {
|
|
|
|
LegacyScope::Binding(binding) => Scope::MacroRules(
|
2018-11-07 21:39:07 +00:00
|
|
|
binding.parent_legacy_scope
|
|
|
|
),
|
2019-07-11 18:45:43 +00:00
|
|
|
LegacyScope::Invocation(invoc) => Scope::MacroRules(
|
2019-07-02 10:47:28 +00:00
|
|
|
invoc.output_legacy_scope.get().unwrap_or(invoc.parent_legacy_scope)
|
2018-11-07 21:39:07 +00:00
|
|
|
),
|
2019-07-11 18:45:43 +00:00
|
|
|
LegacyScope::Empty => Scope::Module(parent_scope.module),
|
2018-11-07 21:39:07 +00:00
|
|
|
}
|
2019-07-11 18:45:43 +00:00
|
|
|
Scope::CrateRoot => match ns {
|
2018-11-24 21:25:03 +00:00
|
|
|
TypeNS => {
|
|
|
|
ident.span.adjust(Mark::root());
|
2019-07-11 18:45:43 +00:00
|
|
|
Scope::ExternPrelude
|
2018-11-24 21:25:03 +00:00
|
|
|
}
|
|
|
|
ValueNS | MacroNS => break,
|
|
|
|
}
|
2019-07-11 18:45:43 +00:00
|
|
|
Scope::Module(module) => {
|
2018-11-07 21:39:07 +00:00
|
|
|
match self.hygienic_lexical_parent(module, &mut ident.span) {
|
2019-07-11 18:45:43 +00:00
|
|
|
Some(parent_module) => Scope::Module(parent_module),
|
2018-11-07 21:39:07 +00:00
|
|
|
None => {
|
2019-07-06 16:45:23 +00:00
|
|
|
ident.span.adjust(Mark::root());
|
2018-11-07 21:39:07 +00:00
|
|
|
use_prelude = !module.no_implicit_prelude;
|
|
|
|
match ns {
|
2019-07-11 18:45:43 +00:00
|
|
|
TypeNS => Scope::ExternPrelude,
|
|
|
|
ValueNS => Scope::StdLibPrelude,
|
|
|
|
MacroNS => Scope::MacroUsePrelude,
|
2018-11-07 21:39:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-07-11 18:45:43 +00:00
|
|
|
Scope::MacroUsePrelude => Scope::StdLibPrelude,
|
|
|
|
Scope::BuiltinMacros => Scope::BuiltinAttrs,
|
|
|
|
Scope::BuiltinAttrs => Scope::LegacyPluginHelpers,
|
|
|
|
Scope::LegacyPluginHelpers => break, // nowhere else to search
|
|
|
|
Scope::ExternPrelude if is_absolute_path => break,
|
|
|
|
Scope::ExternPrelude => Scope::ToolPrelude,
|
|
|
|
Scope::ToolPrelude => Scope::StdLibPrelude,
|
|
|
|
Scope::StdLibPrelude => match ns {
|
|
|
|
TypeNS => Scope::BuiltinTypes,
|
2018-11-07 21:39:07 +00:00
|
|
|
ValueNS => break, // nowhere else to search
|
2019-07-11 18:45:43 +00:00
|
|
|
MacroNS => Scope::BuiltinMacros,
|
2018-11-07 21:39:07 +00:00
|
|
|
}
|
2019-07-11 18:45:43 +00:00
|
|
|
Scope::BuiltinTypes => break, // nowhere else to search
|
2018-11-07 21:39:07 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
continue;
|
2018-07-22 23:52:51 +00:00
|
|
|
}
|
2016-11-10 10:29:36 +00:00
|
|
|
|
2018-08-28 00:27:41 +00:00
|
|
|
// The first found solution was the only one, return it.
|
2019-01-16 23:17:22 +00:00
|
|
|
if let Some((binding, _)) = innermost_result {
|
2018-09-18 00:19:26 +00:00
|
|
|
return Ok(binding);
|
2016-11-10 10:29:36 +00:00
|
|
|
}
|
2018-07-22 23:52:51 +00:00
|
|
|
|
2018-11-07 21:39:07 +00:00
|
|
|
let determinacy = Determinacy::determined(determinacy == Determinacy::Determined || force);
|
2018-11-03 19:02:36 +00:00
|
|
|
if determinacy == Determinacy::Determined && macro_kind == Some(MacroKind::Attr) {
|
2018-08-04 02:17:51 +00:00
|
|
|
// For single-segment attributes interpret determinate "no resolution" as a custom
|
2018-09-10 21:28:35 +00:00
|
|
|
// attribute. (Lexical resolution implies the first segment and attr kind should imply
|
2018-08-04 02:17:51 +00:00
|
|
|
// the last segment, so we are certainly working with a single-segment attribute here.)
|
|
|
|
assert!(ns == MacroNS);
|
2019-04-20 16:36:05 +00:00
|
|
|
let binding = (Res::NonMacroAttr(NonMacroAttrKind::Custom),
|
2018-08-03 21:25:45 +00:00
|
|
|
ty::Visibility::Public, ident.span, Mark::root())
|
|
|
|
.to_name_binding(self.arenas);
|
2018-09-18 00:19:26 +00:00
|
|
|
Ok(binding)
|
2018-08-03 21:25:45 +00:00
|
|
|
} else {
|
|
|
|
Err(determinacy)
|
|
|
|
}
|
2016-11-10 10:29:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn finalize_current_module_macro_resolutions(&mut self) {
|
|
|
|
let module = self.current_module;
|
2018-10-27 17:23:54 +00:00
|
|
|
|
2018-12-16 17:23:27 +00:00
|
|
|
let check_consistency = |this: &mut Self, path: &[Segment], span, kind: MacroKind,
|
2019-04-20 16:36:05 +00:00
|
|
|
initial_res: Option<Res>, res: Res| {
|
|
|
|
if let Some(initial_res) = initial_res {
|
|
|
|
if res != initial_res && res != Res::Err && this.ambiguity_errors.is_empty() {
|
2018-11-10 15:58:37 +00: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 a bug.
|
2019-04-20 16:36:05 +00:00
|
|
|
if initial_res == Res::NonMacroAttr(NonMacroAttrKind::Custom) {
|
2018-12-16 17:23:27 +00:00
|
|
|
// Yeah, legacy custom attributes are implemented using forced resolution
|
|
|
|
// (which is a best effort error recovery tool, basically), so we can't
|
|
|
|
// promise their resolution won't change later.
|
|
|
|
let msg = format!("inconsistent resolution for a macro: first {}, then {}",
|
2019-05-04 12:22:00 +00:00
|
|
|
initial_res.descr(), res.descr());
|
2018-12-16 17:23:27 +00:00
|
|
|
this.session.span_err(span, &msg);
|
|
|
|
} else {
|
|
|
|
span_bug!(span, "inconsistent resolution for a macro");
|
|
|
|
}
|
2018-11-10 15:58:37 +00:00
|
|
|
}
|
|
|
|
} 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.
|
|
|
|
// However, non-speculative `resolve_path` can successfully return private items
|
|
|
|
// even if speculative `resolve_path` returned nothing previously, so we skip this
|
|
|
|
// less informative error if the privacy error is reported elsewhere.
|
|
|
|
if this.privacy_errors.is_empty() {
|
|
|
|
let msg = format!("cannot determine resolution for the {} `{}`",
|
2018-11-18 11:41:06 +00:00
|
|
|
kind.descr(), Segment::names_to_string(path));
|
2018-11-10 15:58:37 +00:00
|
|
|
let msg_note = "import resolution is stuck, try simplifying macro imports";
|
|
|
|
this.session.struct_span_err(span, &msg).note(msg_note).emit();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2018-10-27 17:23:54 +00:00
|
|
|
let macro_resolutions =
|
2019-06-30 18:30:01 +00:00
|
|
|
mem::take(&mut *module.multi_segment_macro_resolutions.borrow_mut());
|
2019-04-20 16:36:05 +00:00
|
|
|
for (mut path, path_span, kind, parent_scope, initial_res) in macro_resolutions {
|
2018-10-27 17:23:54 +00:00
|
|
|
// FIXME: Path resolution will ICE if segment IDs present.
|
|
|
|
for seg in &mut path { seg.id = None; }
|
2018-11-03 19:02:36 +00:00
|
|
|
match self.resolve_path(&path, Some(MacroNS), &parent_scope,
|
2018-10-27 17:23:54 +00:00
|
|
|
true, path_span, CrateLint::No) {
|
2018-11-10 15:58:37 +00:00
|
|
|
PathResult::NonModule(path_res) if path_res.unresolved_segments() == 0 => {
|
2019-04-20 16:36:05 +00:00
|
|
|
let res = path_res.base_res();
|
|
|
|
check_consistency(self, &path, path_span, kind, initial_res, res);
|
2018-11-10 15:58:37 +00:00
|
|
|
}
|
2019-01-16 20:30:41 +00:00
|
|
|
path_res @ PathResult::NonModule(..) | path_res @ PathResult::Failed { .. } => {
|
|
|
|
let (span, label) = if let PathResult::Failed { span, label, .. } = path_res {
|
|
|
|
(span, label)
|
2018-11-10 15:58:37 +00:00
|
|
|
} else {
|
|
|
|
(path_span, format!("partially resolved path in {} {}",
|
|
|
|
kind.article(), kind.descr()))
|
|
|
|
};
|
2019-01-16 20:30:41 +00:00
|
|
|
resolve_error(self, span, ResolutionError::FailedToResolve {
|
|
|
|
label,
|
|
|
|
suggestion: None
|
|
|
|
});
|
2016-11-27 10:58:46 +00:00
|
|
|
}
|
2018-11-10 15:58:37 +00:00
|
|
|
PathResult::Module(..) | PathResult::Indeterminate => unreachable!(),
|
2016-11-27 10:58:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-10 15:58:37 +00:00
|
|
|
let macro_resolutions =
|
2019-06-30 18:30:01 +00:00
|
|
|
mem::take(&mut *module.single_segment_macro_resolutions.borrow_mut());
|
2018-11-10 15:58:37 +00:00
|
|
|
for (ident, kind, parent_scope, initial_binding) in macro_resolutions {
|
2018-11-24 16:14:05 +00:00
|
|
|
match self.early_resolve_ident_in_lexical_scope(ident, ScopeSet::Macro(kind),
|
2018-11-10 15:58:37 +00:00
|
|
|
&parent_scope, true, true, ident.span) {
|
2018-09-18 00:19:26 +00:00
|
|
|
Ok(binding) => {
|
2019-04-20 16:36:05 +00:00
|
|
|
let initial_res = initial_binding.map(|initial_binding| {
|
2018-11-13 23:17:40 +00:00
|
|
|
self.record_use(ident, MacroNS, initial_binding, false);
|
2019-04-20 16:36:05 +00:00
|
|
|
initial_binding.res()
|
2018-11-10 15:58:37 +00:00
|
|
|
});
|
2019-04-20 16:36:05 +00:00
|
|
|
let res = binding.res();
|
2018-11-18 11:41:06 +00:00
|
|
|
let seg = Segment::from_ident(ident);
|
2019-04-20 16:36:05 +00:00
|
|
|
check_consistency(self, &[seg], ident.span, kind, initial_res, res);
|
2018-05-28 19:13:59 +00:00
|
|
|
}
|
2018-09-18 00:19:26 +00:00
|
|
|
Err(..) => {
|
2018-09-18 22:01:09 +00:00
|
|
|
assert!(initial_binding.is_none());
|
2018-05-28 19:13:59 +00:00
|
|
|
let bang = if kind == MacroKind::Bang { "!" } else { "" };
|
|
|
|
let msg =
|
|
|
|
format!("cannot find {} `{}{}` in this scope", kind.descr(), ident, bang);
|
2018-09-18 00:19:26 +00:00
|
|
|
let mut err = self.session.struct_span_err(ident.span, &msg);
|
2019-05-07 06:03:44 +00:00
|
|
|
self.suggest_macro_name(ident.name, kind, &mut err, ident.span);
|
2017-02-06 11:44:38 +00:00
|
|
|
err.emit();
|
2018-05-28 19:13:59 +00:00
|
|
|
}
|
2018-09-18 00:19:26 +00:00
|
|
|
}
|
2016-10-31 22:17:15 +00:00
|
|
|
}
|
2018-09-02 21:04:54 +00:00
|
|
|
|
2019-06-30 18:30:01 +00:00
|
|
|
let builtin_attrs = mem::take(&mut *module.builtin_attrs.borrow_mut());
|
2018-09-12 22:41:07 +00:00
|
|
|
for (ident, parent_scope) in builtin_attrs {
|
2018-11-04 22:00:31 +00:00
|
|
|
let _ = self.early_resolve_ident_in_lexical_scope(
|
2018-11-24 16:14:05 +00:00
|
|
|
ident, ScopeSet::Macro(MacroKind::Attr), &parent_scope, true, true, ident.span
|
2018-09-18 00:19:26 +00:00
|
|
|
);
|
2018-09-02 21:04:54 +00:00
|
|
|
}
|
2016-09-07 23:21:59 +00:00
|
|
|
}
|
2016-09-21 06:25:09 +00:00
|
|
|
|
2019-07-03 20:59:03 +00:00
|
|
|
fn check_stability_and_deprecation(&self, ext: &SyntaxExtension, path: &ast::Path) {
|
|
|
|
let span = path.span;
|
2019-06-21 23:44:45 +00:00
|
|
|
if let Some(stability) = &ext.stability {
|
|
|
|
if let StabilityLevel::Unstable { reason, issue } = stability.level {
|
2019-06-22 13:18:05 +00:00
|
|
|
let feature = stability.feature;
|
|
|
|
if !self.active_features.contains(&feature) && !span.allows_unstable(feature) {
|
2019-06-21 23:44:45 +00:00
|
|
|
stability::report_unstable(self.session, feature, reason, issue, span);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if let Some(depr) = &stability.rustc_depr {
|
2019-07-03 20:59:03 +00:00
|
|
|
let (message, lint) = stability::rustc_deprecation_message(depr, &path.to_string());
|
2019-06-21 23:44:45 +00:00
|
|
|
stability::early_report_deprecation(
|
|
|
|
self.session, &message, depr.suggestion, lint, span
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if let Some(depr) = &ext.deprecation {
|
2019-07-03 20:59:03 +00:00
|
|
|
let (message, lint) = stability::deprecation_message(depr, &path.to_string());
|
2019-06-21 23:44:45 +00:00
|
|
|
stability::early_report_deprecation(self.session, &message, None, lint, span);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-12 01:11:46 +00:00
|
|
|
fn prohibit_imported_non_macro_attrs(&self, binding: Option<&'a NameBinding<'a>>,
|
2019-04-20 16:36:05 +00:00
|
|
|
res: Option<Res>, span: Span) {
|
|
|
|
if let Some(Res::NonMacroAttr(kind)) = res {
|
2018-12-12 01:11:46 +00:00
|
|
|
if kind != NonMacroAttrKind::Tool && binding.map_or(true, |b| b.is_import()) {
|
|
|
|
let msg = format!("cannot use a {} through an import", kind.descr());
|
|
|
|
let mut err = self.session.struct_span_err(span, &msg);
|
|
|
|
if let Some(binding) = binding {
|
|
|
|
err.span_note(binding.span, &format!("the {} imported here", kind.descr()));
|
|
|
|
}
|
|
|
|
err.emit();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-29 22:24:34 +00:00
|
|
|
crate fn check_reserved_macro_name(&mut self, ident: Ident, res: Res) {
|
|
|
|
// Reserve some names that are not quite covered by the general check
|
|
|
|
// performed on `Resolver::builtin_attrs`.
|
|
|
|
if ident.name == sym::cfg || ident.name == sym::cfg_attr || ident.name == sym::derive {
|
2019-07-03 20:59:03 +00:00
|
|
|
let macro_kind = self.get_macro(res).map(|ext| ext.macro_kind());
|
2019-06-29 22:24:34 +00:00
|
|
|
if macro_kind.is_some() && sub_namespace_match(macro_kind, Some(MacroKind::Attr)) {
|
|
|
|
self.session.span_err(
|
|
|
|
ident.span, &format!("name `{}` is reserved in attribute namespace", ident)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-18 01:55:51 +00:00
|
|
|
pub fn define_macro(&mut self,
|
|
|
|
item: &ast::Item,
|
|
|
|
expansion: Mark,
|
2018-08-29 01:48:02 +00:00
|
|
|
current_legacy_scope: &mut LegacyScope<'a>) {
|
2019-07-02 22:44:04 +00:00
|
|
|
let (ext, ident, span, is_legacy) = match &item.node {
|
|
|
|
ItemKind::MacroDef(def) => {
|
|
|
|
let ext = Lrc::new(macro_rules::compile(
|
|
|
|
&self.session.parse_sess,
|
|
|
|
&self.session.features_untracked(),
|
|
|
|
item,
|
|
|
|
self.session.edition(),
|
|
|
|
));
|
|
|
|
(ext, item.ident, item.span, def.legacy)
|
|
|
|
}
|
|
|
|
ItemKind::Fn(..) => match proc_macro_stub(item) {
|
|
|
|
Some((macro_kind, ident, span)) => {
|
|
|
|
self.proc_macro_stubs.insert(item.id);
|
|
|
|
(self.dummy_ext(macro_kind), ident, span, false)
|
|
|
|
}
|
|
|
|
None => return,
|
|
|
|
}
|
|
|
|
_ => unreachable!(),
|
|
|
|
};
|
2016-12-01 11:20:04 +00:00
|
|
|
|
2017-03-01 23:48:16 +00:00
|
|
|
let def_id = self.definitions.local_def_id(item.id);
|
2019-07-02 22:44:04 +00:00
|
|
|
let res = Res::Def(DefKind::Macro(ext.macro_kind()), def_id);
|
2017-03-01 23:48:16 +00:00
|
|
|
self.macro_map.insert(def_id, ext);
|
2019-07-02 22:44:04 +00:00
|
|
|
self.local_macro_def_scopes.insert(item.id, self.current_module);
|
2016-12-01 11:20:04 +00:00
|
|
|
|
2019-07-02 22:44:04 +00:00
|
|
|
if is_legacy {
|
2017-03-22 08:39:51 +00:00
|
|
|
let ident = ident.modern();
|
|
|
|
self.macro_names.insert(ident);
|
2019-05-08 03:21:18 +00:00
|
|
|
let is_macro_export = attr::contains_name(&item.attrs, sym::macro_export);
|
2018-12-09 18:49:21 +00:00
|
|
|
let vis = if is_macro_export {
|
|
|
|
ty::Visibility::Public
|
|
|
|
} else {
|
|
|
|
ty::Visibility::Restricted(DefId::local(CRATE_DEF_INDEX))
|
|
|
|
};
|
2019-07-02 22:44:04 +00:00
|
|
|
let binding = (res, vis, span, expansion).to_name_binding(self.arenas);
|
2018-09-27 01:49:40 +00:00
|
|
|
self.set_binding_parent_module(binding, self.current_module);
|
2018-08-29 01:48:02 +00:00
|
|
|
let legacy_binding = self.arenas.alloc_legacy_binding(LegacyBinding {
|
2018-08-31 19:53:08 +00:00
|
|
|
parent_legacy_scope: *current_legacy_scope, binding, ident
|
2018-08-29 01:48:02 +00:00
|
|
|
});
|
|
|
|
*current_legacy_scope = LegacyScope::Binding(legacy_binding);
|
2019-04-20 16:36:05 +00:00
|
|
|
self.all_macros.insert(ident.name, res);
|
2018-12-09 18:49:21 +00:00
|
|
|
if is_macro_export {
|
2018-05-14 00:22:52 +00:00
|
|
|
let module = self.graph_root;
|
|
|
|
self.define(module, ident, MacroNS,
|
2019-07-02 22:44:04 +00:00
|
|
|
(res, vis, span, expansion, IsMacroExport));
|
2017-03-18 01:55:51 +00:00
|
|
|
} else {
|
2019-06-29 22:24:34 +00:00
|
|
|
self.check_reserved_macro_name(ident, res);
|
2019-07-02 22:44:04 +00:00
|
|
|
self.unused_macros.insert(item.id, span);
|
2017-03-18 01:55:51 +00:00
|
|
|
}
|
2017-05-11 08:26:07 +00:00
|
|
|
} else {
|
2017-03-18 01:55:51 +00:00
|
|
|
let module = self.current_module;
|
|
|
|
let vis = self.resolve_visibility(&item.vis);
|
2017-05-31 15:03:41 +00:00
|
|
|
if vis != ty::Visibility::Public {
|
2019-07-02 22:44:04 +00:00
|
|
|
self.unused_macros.insert(item.id, span);
|
2017-05-31 15:03:41 +00:00
|
|
|
}
|
2019-07-02 22:44:04 +00:00
|
|
|
self.define(module, ident, MacroNS, (res, vis, span, expansion));
|
2016-12-01 11:20:04 +00:00
|
|
|
}
|
|
|
|
}
|
2016-09-07 23:21:59 +00:00
|
|
|
}
|