2019-08-13 02:46:42 +03:00
|
|
|
//! A bunch of methods and structures more or less related to resolving macros and
|
|
|
|
//! interface provided by `Resolver` to macro expander.
|
|
|
|
|
2019-12-29 19:42:23 +03:00
|
|
|
use crate::imports::ImportResolver;
|
2019-02-07 02:15:23 +09:00
|
|
|
use crate::Namespace::*;
|
2022-03-26 20:59:09 +01:00
|
|
|
use crate::{BuiltinMacroState, Determinacy};
|
|
|
|
use crate::{DeriveData, Finalize, ParentScope, ResolutionError, Resolver, ScopeSet};
|
|
|
|
use crate::{ModuleKind, ModuleOrUniformRoot, NameBinding, PathResult, Segment};
|
2021-02-21 16:32:38 +03:00
|
|
|
use rustc_ast::{self as ast, Inline, ItemKind, ModKind, NodeId};
|
2020-06-27 23:51:28 +03:00
|
|
|
use rustc_ast_lowering::ResolverAstLowering;
|
2020-01-11 17:02:46 +01:00
|
|
|
use rustc_ast_pretty::pprust;
|
2020-07-30 11:27:50 +10:00
|
|
|
use rustc_attr::StabilityLevel;
|
2019-12-24 05:02:53 +01:00
|
|
|
use rustc_data_structures::fx::FxHashSet;
|
2022-02-04 14:26:29 +11:00
|
|
|
use rustc_data_structures::intern::Interned;
|
2020-11-19 01:49:20 +03:00
|
|
|
use rustc_data_structures::sync::Lrc;
|
2020-08-31 00:04:01 -04:00
|
|
|
use rustc_errors::struct_span_err;
|
2021-03-08 15:05:03 +03:00
|
|
|
use rustc_expand::base::{Annotatable, DeriveResolutions, Indeterminate, ResolverExpand};
|
|
|
|
use rustc_expand::base::{SyntaxExtension, SyntaxExtensionKind};
|
2019-12-29 17:23:55 +03:00
|
|
|
use rustc_expand::compile_declarative_macro;
|
2021-03-01 16:02:09 -05:00
|
|
|
use rustc_expand::expand::{AstFragment, Invocation, InvocationKind, SupportsMacroExpansion};
|
2020-01-05 02:37:57 +01:00
|
|
|
use rustc_hir::def::{self, DefKind, NonMacroAttrKind};
|
2021-06-28 19:29:55 +02:00
|
|
|
use rustc_hir::def_id::{CrateNum, LocalDefId};
|
2020-03-29 17:19:48 +02:00
|
|
|
use rustc_middle::middle::stability;
|
2022-03-26 20:59:09 +01:00
|
|
|
use rustc_middle::ty::RegisteredTools;
|
|
|
|
use rustc_session::lint::builtin::{LEGACY_DERIVE_HELPERS, SOFT_UNSTABLE, UNUSED_MACROS};
|
2020-11-14 14:47:14 +03:00
|
|
|
use rustc_session::lint::BuiltinLintDiagnostics;
|
2020-11-19 01:49:20 +03:00
|
|
|
use rustc_session::parse::feature_err;
|
2020-03-11 12:49:08 +01:00
|
|
|
use rustc_session::Session;
|
2020-01-01 19:40:49 +01:00
|
|
|
use rustc_span::edition::Edition;
|
2021-06-25 20:43:04 +02:00
|
|
|
use rustc_span::hygiene::{self, ExpnData, ExpnKind, LocalExpnId};
|
2020-11-19 01:49:20 +03:00
|
|
|
use rustc_span::hygiene::{AstPass, MacroKind};
|
2020-04-19 13:00:18 +02:00
|
|
|
use rustc_span::symbol::{kw, sym, Ident, Symbol};
|
2019-12-31 20:15:40 +03:00
|
|
|
use rustc_span::{Span, DUMMY_SP};
|
2020-11-06 16:11:21 +03:00
|
|
|
use std::cell::Cell;
|
2022-03-26 20:59:09 +01:00
|
|
|
use std::mem;
|
2017-03-01 08:44:05 +00:00
|
|
|
|
2019-08-17 20:49:00 +03:00
|
|
|
type Res = def::Res<NodeId>;
|
2019-04-03 09:07:45 +02:00
|
|
|
|
2018-08-31 22:53:08 +03:00
|
|
|
/// Binding produced by a `macro_rules` item.
|
2020-03-14 01:06:36 +03:00
|
|
|
/// Not modularized, can shadow previous `macro_rules` bindings, etc.
|
2018-10-22 01:28:59 +03:00
|
|
|
#[derive(Debug)]
|
2020-03-14 01:06:36 +03:00
|
|
|
pub struct MacroRulesBinding<'a> {
|
2019-07-12 02:29:28 +03:00
|
|
|
crate binding: &'a NameBinding<'a>,
|
2020-03-14 01:06:36 +03:00
|
|
|
/// `macro_rules` scope into which the `macro_rules` item was planted.
|
2020-11-06 16:11:21 +03:00
|
|
|
crate parent_macro_rules_scope: MacroRulesScopeRef<'a>,
|
2019-07-12 02:29:28 +03:00
|
|
|
crate ident: Ident,
|
2018-08-29 04:48:02 +03:00
|
|
|
}
|
|
|
|
|
2019-02-08 14:53:55 +01: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]`.
|
2020-03-14 01:06:36 +03:00
|
|
|
/// Some macro invocations need to introduce `macro_rules` scopes too because they
|
2019-02-08 14:53:55 +01:00
|
|
|
/// can potentially expand into macro definitions.
|
2018-10-22 01:28:59 +03:00
|
|
|
#[derive(Copy, Clone, Debug)]
|
2020-03-14 01:06:36 +03:00
|
|
|
pub enum MacroRulesScope<'a> {
|
2018-08-31 22:53:08 +03:00
|
|
|
/// Empty "root" scope at the crate start containing no names.
|
2016-10-06 08:04:30 +00:00
|
|
|
Empty,
|
2019-02-08 14:53:55 +01:00
|
|
|
/// The scope introduced by a `macro_rules!` macro definition.
|
2020-03-14 01:06:36 +03:00
|
|
|
Binding(&'a MacroRulesBinding<'a>),
|
2019-02-08 14:53:55 +01:00
|
|
|
/// The scope introduced by a macro invocation that can potentially
|
2018-08-31 22:53:08 +03:00
|
|
|
/// create a `macro_rules!` macro definition.
|
2021-06-25 20:43:04 +02:00
|
|
|
Invocation(LocalExpnId),
|
2018-08-18 02:38:51 +03:00
|
|
|
}
|
|
|
|
|
2020-11-06 16:11:21 +03:00
|
|
|
/// `macro_rules!` scopes are always kept by reference and inside a cell.
|
2020-11-14 00:05:05 +03:00
|
|
|
/// The reason is that we update scopes with value `MacroRulesScope::Invocation(invoc_id)`
|
|
|
|
/// in-place after `invoc_id` gets expanded.
|
2020-11-06 16:11:21 +03:00
|
|
|
/// This helps to avoid uncontrollable growth of `macro_rules!` scope chains,
|
2022-03-30 15:14:15 -04:00
|
|
|
/// which usually grow linearly with the number of macro invocations
|
2020-11-06 16:11:21 +03:00
|
|
|
/// in a module (including derives) and hurt performance.
|
2022-02-04 14:26:29 +11:00
|
|
|
pub(crate) type MacroRulesScopeRef<'a> = Interned<'a, Cell<MacroRulesScope<'a>>>;
|
2020-11-06 16:11:21 +03:00
|
|
|
|
2022-03-26 20:59:09 +01: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.
|
|
|
|
crate fn sub_namespace_match(candidate: Option<MacroKind>, requirement: Option<MacroKind>) -> bool {
|
2018-09-08 22:19:53 +03:00
|
|
|
#[derive(PartialEq)]
|
|
|
|
enum SubNS {
|
|
|
|
Bang,
|
|
|
|
AttrLike,
|
|
|
|
}
|
|
|
|
let sub_ns = |kind| match kind {
|
2019-06-18 22:23:13 +03:00
|
|
|
MacroKind::Bang => SubNS::Bang,
|
|
|
|
MacroKind::Attr | MacroKind::Derive => SubNS::AttrLike,
|
2018-09-08 22:19:53 +03:00
|
|
|
};
|
2019-06-18 22:23:13 +03:00
|
|
|
let candidate = candidate.map(sub_ns);
|
|
|
|
let requirement = requirement.map(sub_ns);
|
2018-09-08 22:19:53 +03:00
|
|
|
// "No specific sub-namespace" means "matches anything" for both requirements and candidates.
|
2018-11-08 00:39:07 +03:00
|
|
|
candidate.is_none() || requirement.is_none() || candidate == requirement
|
2018-09-11 00:28:35 +03:00
|
|
|
}
|
|
|
|
|
2019-06-18 10:48:44 +03: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 15:58:56 +03:00
|
|
|
fn fast_print_path(path: &ast::Path) -> Symbol {
|
|
|
|
if path.segments.len() == 1 {
|
2020-03-20 15:03:11 +01:00
|
|
|
path.segments[0].ident.name
|
2019-06-30 15:58:56 +03:00
|
|
|
} 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 {
|
2021-12-15 16:13:11 +11:00
|
|
|
path_str.push_str(segment.ident.as_str())
|
2019-06-30 15:58:56 +03:00
|
|
|
}
|
2019-06-18 10:48:44 +03:00
|
|
|
}
|
2019-06-30 15:58:56 +03:00
|
|
|
Symbol::intern(&path_str)
|
2019-06-18 10:48:44 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-04 16:47:03 +03:00
|
|
|
/// The code common between processing `#![register_tool]` and `#![register_attr]`.
|
2019-11-03 20:28:20 +03:00
|
|
|
fn registered_idents(
|
|
|
|
sess: &Session,
|
|
|
|
attrs: &[ast::Attribute],
|
|
|
|
attr_name: Symbol,
|
|
|
|
descr: &str,
|
|
|
|
) -> FxHashSet<Ident> {
|
|
|
|
let mut registered = FxHashSet::default();
|
2020-07-30 11:27:50 +10:00
|
|
|
for attr in sess.filter_by_name(attrs, attr_name) {
|
2019-11-03 20:28:20 +03:00
|
|
|
for nested_meta in attr.meta_item_list().unwrap_or_default() {
|
|
|
|
match nested_meta.ident() {
|
|
|
|
Some(ident) => {
|
|
|
|
if let Some(old_ident) = registered.replace(ident) {
|
|
|
|
let msg = format!("{} `{}` was already registered", descr, ident);
|
|
|
|
sess.struct_span_err(ident.span, &msg)
|
|
|
|
.span_label(old_ident.span, "already registered here")
|
|
|
|
.emit();
|
2019-12-22 17:42:04 -05:00
|
|
|
}
|
2019-11-03 20:28:20 +03:00
|
|
|
}
|
|
|
|
None => {
|
|
|
|
let msg = format!("`{}` only accepts identifiers", attr_name);
|
|
|
|
let span = nested_meta.span();
|
|
|
|
sess.struct_span_err(span, &msg).span_label(span, "not an identifier").emit();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
registered
|
|
|
|
}
|
|
|
|
|
|
|
|
crate fn registered_attrs_and_tools(
|
|
|
|
sess: &Session,
|
|
|
|
attrs: &[ast::Attribute],
|
|
|
|
) -> (FxHashSet<Ident>, FxHashSet<Ident>) {
|
|
|
|
let registered_attrs = registered_idents(sess, attrs, sym::register_attr, "attribute");
|
|
|
|
let mut registered_tools = registered_idents(sess, attrs, sym::register_tool, "tool");
|
|
|
|
// We implicitly add `rustfmt` and `clippy` to known tools,
|
|
|
|
// but it's not an error to register them explicitly.
|
|
|
|
let predefined_tools = [sym::clippy, sym::rustfmt];
|
|
|
|
registered_tools.extend(predefined_tools.iter().cloned().map(Ident::with_dummy_span));
|
|
|
|
(registered_attrs, registered_tools)
|
|
|
|
}
|
|
|
|
|
2021-02-21 16:32:38 +03:00
|
|
|
// Some feature gates for inner attributes are reported as lints for backward compatibility.
|
|
|
|
fn soft_custom_inner_attributes_gate(path: &ast::Path, invoc: &Invocation) -> bool {
|
|
|
|
match &path.segments[..] {
|
|
|
|
// `#![test]`
|
|
|
|
[seg] if seg.ident.name == sym::test => return true,
|
|
|
|
// `#![rustfmt::skip]` on out-of-line modules
|
|
|
|
[seg1, seg2] if seg1.ident.name == sym::rustfmt && seg2.ident.name == sym::skip => {
|
|
|
|
if let InvocationKind::Attr { item, .. } = &invoc.kind {
|
|
|
|
if let Annotatable::Item(item) = item {
|
|
|
|
if let ItemKind::Mod(_, ModKind::Loaded(_, Inline::No, _)) = item.kind {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_ => {}
|
|
|
|
}
|
|
|
|
false
|
|
|
|
}
|
|
|
|
|
2020-06-27 23:51:28 +03:00
|
|
|
impl<'a> ResolverExpand for Resolver<'a> {
|
2019-08-17 20:49:00 +03:00
|
|
|
fn next_node_id(&mut self) -> NodeId {
|
2019-11-03 17:38:02 -05:00
|
|
|
self.next_node_id()
|
2016-09-05 00:10:27 +00:00
|
|
|
}
|
|
|
|
|
2021-05-02 21:19:28 +02:00
|
|
|
fn invocation_parent(&self, id: LocalExpnId) -> LocalDefId {
|
|
|
|
self.invocation_parents[&id].0
|
|
|
|
}
|
|
|
|
|
2019-07-05 03:09:24 +03: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 {
|
2020-12-29 20:28:08 -05:00
|
|
|
ModuleKind::Def(.., name) if name != kw::Empty => name,
|
2019-07-05 03:09:24 +03:00
|
|
|
_ => kw::Crate,
|
2018-12-28 00:31:28 +03:00
|
|
|
}
|
2019-07-05 03:09:24 +03:00
|
|
|
});
|
2018-12-28 00:31:28 +03:00
|
|
|
}
|
|
|
|
|
2021-06-25 20:43:04 +02:00
|
|
|
fn visit_ast_fragment_with_placeholders(
|
|
|
|
&mut self,
|
|
|
|
expansion: LocalExpnId,
|
|
|
|
fragment: &AstFragment,
|
|
|
|
) {
|
2019-08-17 19:32:52 +03:00
|
|
|
// Integrate the new AST fragment into all the definition and module structures.
|
2019-08-13 03:34:46 +03:00
|
|
|
// We are inside the `expansion` now, but other parent scope components are still the same.
|
|
|
|
let parent_scope = ParentScope { expansion, ..self.invocation_parent_scopes[&expansion] };
|
2020-03-14 01:06:36 +03:00
|
|
|
let output_macro_rules_scope = self.build_reduced_graph(fragment, parent_scope);
|
|
|
|
self.output_macro_rules_scopes.insert(expansion, output_macro_rules_scope);
|
2019-08-17 19:32:52 +03:00
|
|
|
|
2019-08-17 20:49:00 +03:00
|
|
|
parent_scope.module.unexpanded_invocations.borrow_mut().remove(&expansion);
|
2016-09-07 23:21:59 +00:00
|
|
|
}
|
|
|
|
|
2021-01-10 14:36:30 +03:00
|
|
|
fn register_builtin_macro(&mut self, name: Symbol, ext: SyntaxExtensionKind) {
|
|
|
|
if self.builtin_macros.insert(name, BuiltinMacroState::NotYetSeen(ext)).is_some() {
|
2018-09-04 01:14:58 +03:00
|
|
|
self.session
|
2021-01-10 14:36:30 +03:00
|
|
|
.diagnostic()
|
|
|
|
.bug(&format!("built-in macro `{}` was already registered", name));
|
2018-09-04 01:14:58 +03:00
|
|
|
}
|
2016-09-07 23:21:59 +00:00
|
|
|
}
|
|
|
|
|
2019-09-05 15:05:58 +01:00
|
|
|
// Create a new Expansion with a definition site of the provided module, or
|
|
|
|
// a fake empty `#[no_implicit_prelude]` module if no module is provided.
|
2019-08-28 12:41:29 +03:00
|
|
|
fn expansion_for_ast_pass(
|
2019-08-25 20:58:03 +01:00
|
|
|
&mut self,
|
2019-08-28 12:41:29 +03:00
|
|
|
call_site: Span,
|
2019-08-25 20:58:03 +01:00
|
|
|
pass: AstPass,
|
|
|
|
features: &[Symbol],
|
|
|
|
parent_module_id: Option<NodeId>,
|
2021-06-25 20:43:04 +02:00
|
|
|
) -> LocalExpnId {
|
2021-09-12 01:59:05 +03:00
|
|
|
let parent_module =
|
|
|
|
parent_module_id.map(|module_id| self.local_def_id(module_id).to_def_id());
|
2021-06-25 20:43:04 +02:00
|
|
|
let expn_id = LocalExpnId::fresh(
|
2021-06-22 19:20:25 +02:00
|
|
|
ExpnData::allow_unstable(
|
|
|
|
ExpnKind::AstPass(pass),
|
|
|
|
call_site,
|
|
|
|
self.session.edition(),
|
|
|
|
features.into(),
|
|
|
|
None,
|
2021-09-12 01:59:05 +03:00
|
|
|
parent_module,
|
2021-06-22 19:20:25 +02:00
|
|
|
),
|
|
|
|
self.create_stable_hashing_context(),
|
|
|
|
);
|
2019-08-28 12:41:29 +03:00
|
|
|
|
2021-09-12 01:59:05 +03:00
|
|
|
let parent_scope =
|
2021-09-12 02:06:27 +03:00
|
|
|
parent_module.map_or(self.empty_module, |def_id| self.expect_module(def_id));
|
2019-08-25 20:58:03 +01:00
|
|
|
self.ast_transform_scopes.insert(expn_id, parent_scope);
|
2021-06-28 19:29:55 +02:00
|
|
|
|
2019-08-28 12:41:29 +03:00
|
|
|
expn_id
|
2019-08-25 20:58:03 +01:00
|
|
|
}
|
|
|
|
|
2016-11-10 10:11:25 +00:00
|
|
|
fn resolve_imports(&mut self) {
|
2019-08-08 14:06:42 +03:00
|
|
|
ImportResolver { r: self }.resolve_imports()
|
2016-11-10 10:11:25 +00:00
|
|
|
}
|
|
|
|
|
2019-08-20 00:24:28 +03:00
|
|
|
fn resolve_macro_invocation(
|
|
|
|
&mut self,
|
|
|
|
invoc: &Invocation,
|
2021-06-25 20:43:04 +02:00
|
|
|
eager_expansion_root: LocalExpnId,
|
2019-08-20 00:24:28 +03:00
|
|
|
force: bool,
|
2020-11-14 14:47:14 +03:00
|
|
|
) -> Result<Lrc<SyntaxExtension>, Indeterminate> {
|
2019-08-20 00:24:28 +03:00
|
|
|
let invoc_id = invoc.expansion_data.id;
|
|
|
|
let parent_scope = match self.invocation_parent_scopes.get(&invoc_id) {
|
|
|
|
Some(parent_scope) => *parent_scope,
|
|
|
|
None => {
|
|
|
|
// If there's no entry in the table, then we are resolving an eagerly expanded
|
|
|
|
// macro, which should inherit its parent scope from its eager expansion root -
|
|
|
|
// the macro that requested this eager expansion.
|
|
|
|
let parent_scope = *self
|
|
|
|
.invocation_parent_scopes
|
|
|
|
.get(&eager_expansion_root)
|
|
|
|
.expect("non-eager expansion without a parent scope");
|
|
|
|
self.invocation_parent_scopes.insert(invoc_id, parent_scope);
|
|
|
|
parent_scope
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2020-11-14 14:47:14 +03:00
|
|
|
let (path, kind, inner_attr, derives) = match invoc.kind {
|
|
|
|
InvocationKind::Attr { ref attr, ref derives, .. } => (
|
2019-10-24 06:33:12 +11:00
|
|
|
&attr.get_normal_item().path,
|
2019-10-24 06:40:35 +11:00
|
|
|
MacroKind::Attr,
|
2020-11-19 01:49:20 +03:00
|
|
|
attr.style == ast::AttrStyle::Inner,
|
2019-10-24 06:40:35 +11:00
|
|
|
self.arenas.alloc_ast_paths(derives),
|
2018-08-13 02:57:19 +03:00
|
|
|
),
|
2020-11-14 14:47:14 +03:00
|
|
|
InvocationKind::Bang { ref mac, .. } => (&mac.path, MacroKind::Bang, false, &[][..]),
|
|
|
|
InvocationKind::Derive { ref path, .. } => (path, MacroKind::Derive, false, &[][..]),
|
2017-03-01 23:48:16 +00:00
|
|
|
};
|
2018-08-11 16:40:08 +03:00
|
|
|
|
2019-08-12 23:39:49 +03:00
|
|
|
// Derives are not included when `invocations` are collected, so we have to add them here.
|
2019-08-13 01:39:10 +03:00
|
|
|
let parent_scope = &ParentScope { derives, ..parent_scope };
|
2021-03-01 16:02:09 -05:00
|
|
|
let supports_macro_expansion = invoc.fragment_kind.supports_macro_expansion();
|
2021-07-14 18:24:12 -05:00
|
|
|
let node_id = invoc.expansion_data.lint_node_id;
|
2020-11-19 01:49:20 +03:00
|
|
|
let (ext, res) = self.smart_resolve_macro_path(
|
|
|
|
path,
|
|
|
|
kind,
|
2021-03-01 16:02:09 -05:00
|
|
|
supports_macro_expansion,
|
2020-11-19 01:49:20 +03:00
|
|
|
inner_attr,
|
|
|
|
parent_scope,
|
|
|
|
node_id,
|
|
|
|
force,
|
2021-02-21 16:32:38 +03:00
|
|
|
soft_custom_inner_attributes_gate(path, invoc),
|
2020-11-19 01:49:20 +03:00
|
|
|
)?;
|
2018-08-15 03:51:12 +03:00
|
|
|
|
2019-06-20 22:00:47 +03:00
|
|
|
let span = invoc.span();
|
2021-09-12 01:47:46 +03:00
|
|
|
let def_id = res.opt_def_id();
|
2021-06-22 19:20:25 +02:00
|
|
|
invoc_id.set_expn_data(
|
|
|
|
ext.expn_data(
|
|
|
|
parent_scope.expansion,
|
|
|
|
span,
|
|
|
|
fast_print_path(path),
|
2021-09-12 01:47:46 +03:00
|
|
|
def_id,
|
|
|
|
def_id.map(|def_id| self.macro_def_scope(def_id).nearest_parent_mod()),
|
2021-06-22 19:20:25 +02:00
|
|
|
),
|
|
|
|
self.create_stable_hashing_context(),
|
|
|
|
);
|
2019-06-21 01:59:30 +03:00
|
|
|
|
2020-11-14 14:47:14 +03:00
|
|
|
Ok(ext)
|
2017-03-01 23:48:16 +00:00
|
|
|
}
|
|
|
|
|
2019-10-25 09:15:33 -04:00
|
|
|
fn check_unused_macros(&mut self) {
|
2021-11-10 12:00:46 +01:00
|
|
|
for (_, &(node_id, ident)) in self.unused_macros.iter() {
|
|
|
|
self.lint_buffer.buffer_lint(
|
|
|
|
UNUSED_MACROS,
|
|
|
|
node_id,
|
|
|
|
ident.span,
|
|
|
|
&format!("unused macro definition: `{}`", ident.as_str()),
|
|
|
|
);
|
2017-05-11 10:26:07 +02:00
|
|
|
}
|
2017-03-01 23:48:16 +00:00
|
|
|
}
|
2019-08-03 04:22:44 +03:00
|
|
|
|
2021-06-25 20:43:04 +02:00
|
|
|
fn has_derive_copy(&self, expn_id: LocalExpnId) -> bool {
|
2019-11-04 10:09:58 +01:00
|
|
|
self.containers_deriving_copy.contains(&expn_id)
|
2019-08-03 04:22:44 +03:00
|
|
|
}
|
|
|
|
|
2020-11-14 14:47:14 +03:00
|
|
|
fn resolve_derives(
|
|
|
|
&mut self,
|
2021-06-25 20:43:04 +02:00
|
|
|
expn_id: LocalExpnId,
|
2020-11-14 14:47:14 +03:00
|
|
|
force: bool,
|
2021-03-08 15:05:03 +03:00
|
|
|
derive_paths: &dyn Fn() -> DeriveResolutions,
|
2020-11-14 14:47:14 +03:00
|
|
|
) -> Result<(), Indeterminate> {
|
|
|
|
// Block expansion of the container until we resolve all derives in it.
|
|
|
|
// This is required for two reasons:
|
|
|
|
// - Derive helper attributes are in scope for the item to which the `#[derive]`
|
|
|
|
// is applied, so they have to be produced by the container's expansion rather
|
|
|
|
// than by individual derives.
|
|
|
|
// - Derives in the container need to know whether one of them is a built-in `Copy`.
|
2021-03-08 15:05:03 +03:00
|
|
|
// Temporarily take the data to avoid borrow checker conflicts.
|
|
|
|
let mut derive_data = mem::take(&mut self.derive_data);
|
|
|
|
let entry = derive_data.entry(expn_id).or_insert_with(|| DeriveData {
|
|
|
|
resolutions: derive_paths(),
|
|
|
|
helper_attrs: Vec::new(),
|
|
|
|
has_derive_copy: false,
|
|
|
|
});
|
2020-11-14 14:47:14 +03:00
|
|
|
let parent_scope = self.invocation_parent_scopes[&expn_id];
|
2021-06-20 18:10:02 +03:00
|
|
|
for (i, (path, _, opt_ext)) in entry.resolutions.iter_mut().enumerate() {
|
2021-03-08 15:05:03 +03:00
|
|
|
if opt_ext.is_none() {
|
|
|
|
*opt_ext = Some(
|
|
|
|
match self.resolve_macro_path(
|
|
|
|
&path,
|
|
|
|
Some(MacroKind::Derive),
|
|
|
|
&parent_scope,
|
|
|
|
true,
|
|
|
|
force,
|
|
|
|
) {
|
|
|
|
Ok((Some(ext), _)) => {
|
|
|
|
if !ext.helper_attrs.is_empty() {
|
|
|
|
let last_seg = path.segments.last().unwrap();
|
|
|
|
let span = last_seg.ident.span.normalize_to_macros_2_0();
|
|
|
|
entry.helper_attrs.extend(
|
2021-04-04 17:51:31 +03:00
|
|
|
ext.helper_attrs
|
|
|
|
.iter()
|
|
|
|
.map(|name| (i, Ident::new(*name, span))),
|
2021-03-08 15:05:03 +03:00
|
|
|
);
|
|
|
|
}
|
|
|
|
entry.has_derive_copy |= ext.builtin_name == Some(sym::Copy);
|
|
|
|
ext
|
|
|
|
}
|
|
|
|
Ok(_) | Err(Determinacy::Determined) => self.dummy_ext(MacroKind::Derive),
|
|
|
|
Err(Determinacy::Undetermined) => {
|
|
|
|
assert!(self.derive_data.is_empty());
|
|
|
|
self.derive_data = derive_data;
|
|
|
|
return Err(Indeterminate);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
);
|
|
|
|
}
|
2020-11-14 14:47:14 +03:00
|
|
|
}
|
2021-04-04 17:51:31 +03:00
|
|
|
// Sort helpers in a stable way independent from the derive resolution order.
|
|
|
|
entry.helper_attrs.sort_by_key(|(i, _)| *i);
|
|
|
|
self.helper_attrs
|
|
|
|
.insert(expn_id, entry.helper_attrs.iter().map(|(_, ident)| *ident).collect());
|
2020-11-14 14:47:14 +03:00
|
|
|
// Mark this derive as having `Copy` either if it has `Copy` itself or if its parent derive
|
|
|
|
// has `Copy`, to support cases like `#[derive(Clone, Copy)] #[derive(Debug)]`.
|
2021-03-08 15:05:03 +03:00
|
|
|
if entry.has_derive_copy || self.has_derive_copy(parent_scope.expansion) {
|
2020-11-14 14:47:14 +03:00
|
|
|
self.containers_deriving_copy.insert(expn_id);
|
|
|
|
}
|
2021-03-08 15:05:03 +03:00
|
|
|
assert!(self.derive_data.is_empty());
|
|
|
|
self.derive_data = derive_data;
|
2020-11-14 14:47:14 +03:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2021-06-25 20:43:04 +02:00
|
|
|
fn take_derive_resolutions(&mut self, expn_id: LocalExpnId) -> Option<DeriveResolutions> {
|
2021-03-08 15:05:03 +03:00
|
|
|
self.derive_data.remove(&expn_id).map(|data| data.resolutions)
|
2020-11-14 14:47:14 +03:00
|
|
|
}
|
|
|
|
|
2020-03-10 00:56:20 +03:00
|
|
|
// The function that implements the resolution logic of `#[cfg_accessible(path)]`.
|
|
|
|
// Returns true if the path can certainly be resolved in one of three namespaces,
|
|
|
|
// returns false if the path certainly cannot be resolved in any of the three namespaces.
|
|
|
|
// Returns `Indeterminate` if we cannot give a certain answer yet.
|
2021-06-25 20:43:04 +02:00
|
|
|
fn cfg_accessible(
|
|
|
|
&mut self,
|
|
|
|
expn_id: LocalExpnId,
|
|
|
|
path: &ast::Path,
|
|
|
|
) -> Result<bool, Indeterminate> {
|
2020-03-10 00:56:20 +03:00
|
|
|
let span = path.span;
|
|
|
|
let path = &Segment::from_path(path);
|
|
|
|
let parent_scope = self.invocation_parent_scopes[&expn_id];
|
|
|
|
|
|
|
|
let mut indeterminate = false;
|
|
|
|
for ns in [TypeNS, ValueNS, MacroNS].iter().copied() {
|
2022-04-08 22:50:56 +02:00
|
|
|
match self.maybe_resolve_path(path, Some(ns), &parent_scope) {
|
2020-03-10 00:56:20 +03:00
|
|
|
PathResult::Module(ModuleOrUniformRoot::Module(_)) => return Ok(true),
|
|
|
|
PathResult::NonModule(partial_res) if partial_res.unresolved_segments() == 0 => {
|
|
|
|
return Ok(true);
|
|
|
|
}
|
|
|
|
PathResult::Indeterminate => indeterminate = true,
|
|
|
|
// FIXME: `resolve_path` is not ready to report partially resolved paths
|
|
|
|
// correctly, so we just report an error if the path was reported as unresolved.
|
|
|
|
// This needs to be fixed for `cfg_accessible` to be useful.
|
|
|
|
PathResult::NonModule(..) | PathResult::Failed { .. } => {}
|
|
|
|
PathResult::Module(_) => panic!("unexpected path resolution"),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if indeterminate {
|
|
|
|
return Err(Indeterminate);
|
|
|
|
}
|
|
|
|
|
|
|
|
self.session
|
|
|
|
.struct_span_err(span, "not sure whether the path is accessible or not")
|
|
|
|
.span_note(span, "`cfg_accessible` is not fully implemented")
|
|
|
|
.emit();
|
|
|
|
Ok(false)
|
|
|
|
}
|
Implement span quoting for proc-macros
This PR implements span quoting, allowing proc-macros to produce spans
pointing *into their own crate*. This is used by the unstable
`proc_macro::quote!` macro, allowing us to get error messages like this:
```
error[E0412]: cannot find type `MissingType` in this scope
--> $DIR/auxiliary/span-from-proc-macro.rs:37:20
|
LL | pub fn error_from_attribute(_args: TokenStream, _input: TokenStream) -> TokenStream {
| ----------------------------------------------------------------------------------- in this expansion of procedural macro `#[error_from_attribute]`
...
LL | field: MissingType
| ^^^^^^^^^^^ not found in this scope
|
::: $DIR/span-from-proc-macro.rs:8:1
|
LL | #[error_from_attribute]
| ----------------------- in this macro invocation
```
Here, `MissingType` occurs inside the implementation of the proc-macro
`#[error_from_attribute]`. Previosuly, this would always result in a
span pointing at `#[error_from_attribute]`
This will make many proc-macro-related error message much more useful -
when a proc-macro generates code containing an error, users will get an
error message pointing directly at that code (within the macro
definition), instead of always getting a span pointing at the macro
invocation site.
This is implemented as follows:
* When a proc-macro crate is being *compiled*, it causes the `quote!`
macro to get run. This saves all of the sapns in the input to `quote!`
into the metadata of *the proc-macro-crate* (which we are currently
compiling). The `quote!` macro then expands to a call to
`proc_macro::Span::recover_proc_macro_span(id)`, where `id` is an
opaque identifier for the span in the crate metadata.
* When the same proc-macro crate is *run* (e.g. it is loaded from disk
and invoked by some consumer crate), the call to
`proc_macro::Span::recover_proc_macro_span` causes us to load the span
from the proc-macro crate's metadata. The proc-macro then produces a
`TokenStream` containing a `Span` pointing into the proc-macro crate
itself.
The recursive nature of 'quote!' can be difficult to understand at
first. The file `src/test/ui/proc-macro/quote-debug.stdout` shows
the output of the `quote!` macro, which should make this eaier to
understand.
This PR also supports custom quoting spans in custom quote macros (e.g.
the `quote` crate). All span quoting goes through the
`proc_macro::quote_span` method, which can be called by a custom quote
macro to perform span quoting. An example of this usage is provided in
`src/test/ui/proc-macro/auxiliary/custom-quote.rs`
Custom quoting currently has a few limitations:
In order to quote a span, we need to generate a call to
`proc_macro::Span::recover_proc_macro_span`. However, proc-macros
support renaming the `proc_macro` crate, so we can't simply hardcode
this path. Previously, the `quote_span` method used the path
`crate::Span` - however, this only works when it is called by the
builtin `quote!` macro in the same crate. To support being called from
arbitrary crates, we need access to the name of the `proc_macro` crate
to generate a path. This PR adds an additional argument to `quote_span`
to specify the name of the `proc_macro` crate. Howver, this feels kind
of hacky, and we may want to change this before stabilizing anything
quote-related.
Additionally, using `quote_span` currently requires enabling the
`proc_macro_internals` feature. The builtin `quote!` macro
has an `#[allow_internal_unstable]` attribute, but this won't work for
custom quote implementations. This will likely require some additional
tricks to apply `allow_internal_unstable` to the span of
`proc_macro::Span::recover_proc_macro_span`.
2020-08-02 19:52:16 -04:00
|
|
|
|
|
|
|
fn get_proc_macro_quoted_span(&self, krate: CrateNum, id: usize) -> Span {
|
|
|
|
self.crate_loader.cstore().get_proc_macro_quoted_span_untracked(krate, id, self.session)
|
|
|
|
}
|
2021-07-16 22:22:08 +02:00
|
|
|
|
|
|
|
fn declare_proc_macro(&mut self, id: NodeId) {
|
|
|
|
self.proc_macros.push(id)
|
|
|
|
}
|
2021-09-29 01:17:54 +03:00
|
|
|
|
|
|
|
fn registered_tools(&self) -> &RegisteredTools {
|
|
|
|
&self.registered_tools
|
|
|
|
}
|
2017-03-01 23:48:16 +00:00
|
|
|
}
|
|
|
|
|
2018-12-10 05:37:10 +01:00
|
|
|
impl<'a> Resolver<'a> {
|
2019-07-03 23:59:03 +03:00
|
|
|
/// Resolve macro path with error reporting and recovery.
|
2020-11-19 01:49:20 +03:00
|
|
|
/// Uses dummy syntax extensions for unresolved macros or macros with unexpected resolutions
|
|
|
|
/// for better error recovery.
|
2019-07-03 23:59:03 +03:00
|
|
|
fn smart_resolve_macro_path(
|
2018-09-13 01:41:07 +03:00
|
|
|
&mut self,
|
|
|
|
path: &ast::Path,
|
|
|
|
kind: MacroKind,
|
2021-03-01 16:02:09 -05:00
|
|
|
supports_macro_expansion: SupportsMacroExpansion,
|
2020-11-19 01:49:20 +03:00
|
|
|
inner_attr: bool,
|
2018-09-13 01:41:07 +03:00
|
|
|
parent_scope: &ParentScope<'a>,
|
2020-06-09 20:59:43 +03:00
|
|
|
node_id: NodeId,
|
2018-09-13 01:41:07 +03:00
|
|
|
force: bool,
|
2021-02-21 16:32:38 +03:00
|
|
|
soft_custom_inner_attributes_gate: bool,
|
2019-07-03 23:59:03 +03:00
|
|
|
) -> Result<(Lrc<SyntaxExtension>, Res), Indeterminate> {
|
2019-07-16 00:10:34 +03:00
|
|
|
let (ext, res) = match self.resolve_macro_path(path, Some(kind), parent_scope, true, force)
|
|
|
|
{
|
2019-07-03 23:59:03 +03:00
|
|
|
Ok((Some(ext), res)) => (ext, res),
|
|
|
|
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 03:51:12 +03:00
|
|
|
|
2020-03-24 20:41:16 +03:00
|
|
|
// Report errors for the resolved macro.
|
2019-07-03 23:59:03 +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-08-15 03:51:12 +03:00
|
|
|
}
|
2020-03-24 20:41:16 +03:00
|
|
|
if kind == MacroKind::Attr && segment.ident.as_str().starts_with("rustc") {
|
|
|
|
self.session.span_err(
|
|
|
|
segment.ident.span,
|
|
|
|
"attributes starting with `rustc` are reserved for use by the `rustc` compiler",
|
|
|
|
);
|
2019-07-03 11:44:57 +03:00
|
|
|
}
|
2019-07-03 23:59:03 +03:00
|
|
|
}
|
2018-08-15 03:51:12 +03:00
|
|
|
|
2019-04-20 19:36:05 +03:00
|
|
|
match res {
|
2019-06-18 22:23:13 +03:00
|
|
|
Res::Def(DefKind::Macro(_), def_id) => {
|
2020-05-24 23:39:39 +01:00
|
|
|
if let Some(def_id) = def_id.as_local() {
|
|
|
|
self.unused_macros.remove(&def_id);
|
|
|
|
if self.proc_macro_stubs.contains(&def_id) {
|
2019-07-03 01:44:04 +03:00
|
|
|
self.session.span_err(
|
|
|
|
path.span,
|
|
|
|
"can't use a procedural macro from the same crate that defines it",
|
|
|
|
);
|
|
|
|
}
|
2019-06-20 11:52:31 +03:00
|
|
|
}
|
2019-06-18 22:23:13 +03:00
|
|
|
}
|
2019-07-12 01:00:20 +03:00
|
|
|
Res::NonMacroAttr(..) | Res::Err => {}
|
2019-04-20 19:36:05 +03:00
|
|
|
_ => panic!("expected `DefKind::Macro` or `Res::NonMacroAttr`"),
|
2019-07-03 11:44:57 +03:00
|
|
|
};
|
2018-08-15 03:51:12 +03:00
|
|
|
|
2020-06-09 20:59:43 +03:00
|
|
|
self.check_stability_and_deprecation(&ext, path, node_id);
|
2019-07-03 23:59:03 +03:00
|
|
|
|
2020-11-19 01:49:20 +03:00
|
|
|
let unexpected_res = if ext.macro_kind() != kind {
|
|
|
|
Some((kind.article(), kind.descr_expected()))
|
2021-03-01 16:02:09 -05:00
|
|
|
} else if matches!(res, Res::Def(..)) {
|
|
|
|
match supports_macro_expansion {
|
|
|
|
SupportsMacroExpansion::No => Some(("a", "non-macro attribute")),
|
|
|
|
SupportsMacroExpansion::Yes { supports_inner_attrs } => {
|
|
|
|
if inner_attr && !supports_inner_attrs {
|
|
|
|
Some(("a", "non-macro inner attribute"))
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-11-19 01:49:20 +03:00
|
|
|
} else {
|
|
|
|
None
|
|
|
|
};
|
|
|
|
if let Some((article, expected)) = unexpected_res {
|
2019-10-08 22:17:46 +02:00
|
|
|
let path_str = pprust::path_to_string(path);
|
|
|
|
let msg = format!("expected {}, found {} `{}`", expected, res.descr(), path_str);
|
2019-07-03 12:47:24 +03:00
|
|
|
self.session
|
|
|
|
.struct_span_err(path.span, &msg)
|
2020-11-19 01:49:20 +03:00
|
|
|
.span_label(path.span, format!("not {} {}", article, expected))
|
2019-07-03 12:47:24 +03:00
|
|
|
.emit();
|
2020-11-19 01:49:20 +03:00
|
|
|
return Ok((self.dummy_ext(kind), Res::Err));
|
|
|
|
}
|
|
|
|
|
|
|
|
// We are trying to avoid reporting this error if other related errors were reported.
|
2020-11-12 23:42:42 +03:00
|
|
|
if res != Res::Err
|
|
|
|
&& inner_attr
|
2020-11-19 01:49:20 +03:00
|
|
|
&& !self.session.features_untracked().custom_inner_attributes
|
|
|
|
{
|
2020-11-12 23:42:42 +03:00
|
|
|
let msg = match res {
|
|
|
|
Res::Def(..) => "inner macro attributes are unstable",
|
|
|
|
Res::NonMacroAttr(..) => "custom inner attributes are unstable",
|
|
|
|
_ => unreachable!(),
|
|
|
|
};
|
2021-02-21 16:32:38 +03:00
|
|
|
if soft_custom_inner_attributes_gate {
|
2020-11-12 23:42:42 +03:00
|
|
|
self.session.parse_sess.buffer_lint(SOFT_UNSTABLE, path.span, node_id, msg);
|
|
|
|
} else {
|
|
|
|
feature_err(&self.session.parse_sess, sym::custom_inner_attributes, path.span, msg)
|
|
|
|
.emit();
|
|
|
|
}
|
2020-11-19 01:49:20 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
Ok((ext, res))
|
2017-07-25 00:33:15 +03:00
|
|
|
}
|
2016-09-07 23:21:59 +00:00
|
|
|
|
2019-07-03 23:59:03 +03:00
|
|
|
pub fn resolve_macro_path(
|
2018-09-13 01:41:07 +03:00
|
|
|
&mut self,
|
|
|
|
path: &ast::Path,
|
2019-07-16 00:10:34 +03:00
|
|
|
kind: Option<MacroKind>,
|
2018-09-13 01:41:07 +03:00
|
|
|
parent_scope: &ParentScope<'a>,
|
2018-11-14 02:20:59 +03:00
|
|
|
trace: bool,
|
2018-09-13 01:41:07 +03:00
|
|
|
force: bool,
|
2019-07-03 23:59:03 +03:00
|
|
|
) -> Result<(Option<Lrc<SyntaxExtension>>, Res), Determinacy> {
|
2018-10-27 20:23:54 +03:00
|
|
|
let path_span = path.span;
|
2018-09-12 15:21:50 +12:00
|
|
|
let mut path = Segment::from_path(path);
|
2016-11-27 10:58:46 +00:00
|
|
|
|
2018-06-11 14:21:36 +03:00
|
|
|
// Possibly apply the macro helper hack
|
2019-07-16 00:10:34 +03:00
|
|
|
if kind == Some(MacroKind::Bang)
|
|
|
|
&& path.len() == 1
|
2019-08-13 23:56:42 +03:00
|
|
|
&& path[0].ident.span.ctxt().outer_expn_data().local_inner_macros
|
|
|
|
{
|
2019-05-11 17:41:37 +03:00
|
|
|
let root = Ident::new(kw::DollarCrate, path[0].ident.span);
|
2018-09-12 15:21:50 +12:00
|
|
|
path.insert(0, Segment::from_ident(root));
|
2018-06-11 14:21:36 +03:00
|
|
|
}
|
|
|
|
|
2019-07-03 23:59:03 +03:00
|
|
|
let res = if path.len() > 1 {
|
2022-04-08 22:50:56 +02:00
|
|
|
let res = match self.maybe_resolve_path(&path, Some(MacroNS), parent_scope) {
|
2018-11-10 18:58:37 +03:00
|
|
|
PathResult::NonModule(path_res) if path_res.unresolved_segments() == 0 => {
|
2019-04-20 19:36:05 +03:00
|
|
|
Ok(path_res.base_res())
|
2018-11-10 18:58:37 +03:00
|
|
|
}
|
2016-11-27 10:58:46 +00:00
|
|
|
PathResult::Indeterminate if !force => return Err(Determinacy::Undetermined),
|
2019-01-16 15:30:41 -05:00
|
|
|
PathResult::NonModule(..)
|
|
|
|
| PathResult::Indeterminate
|
|
|
|
| PathResult::Failed { .. } => Err(Determinacy::Determined),
|
2018-11-10 18:58:37 +03:00
|
|
|
PathResult::Module(..) => unreachable!(),
|
2016-11-27 10:58:46 +00:00
|
|
|
};
|
2018-09-18 03:19:26 +03:00
|
|
|
|
2018-11-14 02:20:59 +03:00
|
|
|
if trace {
|
2019-07-16 00:10:34 +03:00
|
|
|
let kind = kind.expect("macro kind must be specified if tracing is enabled");
|
2019-08-12 21:52:37 +03:00
|
|
|
self.multi_segment_macro_resolutions.push((
|
2019-08-13 01:39:10 +03:00
|
|
|
path,
|
|
|
|
path_span,
|
|
|
|
kind,
|
|
|
|
*parent_scope,
|
|
|
|
res.ok(),
|
|
|
|
));
|
2018-11-14 02:20:59 +03:00
|
|
|
}
|
2016-11-27 10:58:46 +00:00
|
|
|
|
2019-04-20 19:36:05 +03:00
|
|
|
self.prohibit_imported_non_macro_attrs(None, res.ok(), path_span);
|
|
|
|
res
|
2017-03-11 10:58:19 +00:00
|
|
|
} else {
|
2019-08-10 01:40:05 +03:00
|
|
|
let scope_set = kind.map_or(ScopeSet::All(MacroNS, false), ScopeSet::Macro);
|
2018-09-19 01:01:09 +03:00
|
|
|
let binding = self.early_resolve_ident_in_lexical_scope(
|
2019-07-16 00:10:34 +03:00
|
|
|
path[0].ident,
|
|
|
|
scope_set,
|
|
|
|
parent_scope,
|
2022-03-24 00:32:00 +03:00
|
|
|
None,
|
2019-07-16 00:10:34 +03:00
|
|
|
force,
|
2022-04-08 22:50:56 +02:00
|
|
|
None,
|
2018-09-19 01:01:09 +03:00
|
|
|
);
|
2018-12-16 20:23:27 +03:00
|
|
|
if let Err(Determinacy::Undetermined) = binding {
|
|
|
|
return Err(Determinacy::Undetermined);
|
2018-09-19 01:01:09 +03:00
|
|
|
}
|
2016-11-10 10:29:36 +00:00
|
|
|
|
2018-11-14 02:20:59 +03:00
|
|
|
if trace {
|
2019-07-16 00:10:34 +03:00
|
|
|
let kind = kind.expect("macro kind must be specified if tracing is enabled");
|
2019-08-12 21:52:37 +03:00
|
|
|
self.single_segment_macro_resolutions.push((
|
2019-08-13 01:39:10 +03:00
|
|
|
path[0].ident,
|
|
|
|
kind,
|
|
|
|
*parent_scope,
|
|
|
|
binding.ok(),
|
|
|
|
));
|
2018-11-14 02:20:59 +03:00
|
|
|
}
|
2017-02-01 21:03:09 +10:30
|
|
|
|
2019-04-20 19:36:05 +03:00
|
|
|
let res = binding.map(|binding| binding.res());
|
|
|
|
self.prohibit_imported_non_macro_attrs(binding.ok(), res.ok(), path_span);
|
|
|
|
res
|
2019-07-03 23:59:03 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
res.map(|res| (self.get_macro(res), res))
|
2017-02-01 21:03:09 +10:30
|
|
|
}
|
2016-09-26 03:17:05 +00:00
|
|
|
|
2019-08-12 21:52:37 +03:00
|
|
|
crate fn finalize_macro_resolutions(&mut self) {
|
2018-12-16 20:23:27 +03:00
|
|
|
let check_consistency = |this: &mut Self,
|
|
|
|
path: &[Segment],
|
|
|
|
span,
|
|
|
|
kind: MacroKind,
|
2019-04-20 19:36:05 +03:00
|
|
|
initial_res: Option<Res>,
|
|
|
|
res: Res| {
|
|
|
|
if let Some(initial_res) = initial_res {
|
2020-10-24 21:17:34 +03:00
|
|
|
if res != initial_res {
|
2018-11-10 18:58:37 +03:00
|
|
|
// Make sure compilation does not succeed if preferred macro resolution
|
|
|
|
// has changed after the macro had been expanded. In theory all such
|
2020-10-24 21:17:34 +03:00
|
|
|
// situations should be reported as errors, so this is a bug.
|
|
|
|
this.session.delay_span_bug(span, "inconsistent resolution for a macro");
|
2018-11-10 18:58:37 +03: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 14:41:06 +03:00
|
|
|
kind.descr(),
|
|
|
|
Segment::names_to_string(path)
|
|
|
|
);
|
2018-11-10 18:58:37 +03:00
|
|
|
let msg_note = "import resolution is stuck, try simplifying macro imports";
|
|
|
|
this.session.struct_span_err(span, &msg).note(msg_note).emit();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2019-08-12 21:52:37 +03:00
|
|
|
let macro_resolutions = mem::take(&mut self.multi_segment_macro_resolutions);
|
2019-04-20 19:36:05 +03:00
|
|
|
for (mut path, path_span, kind, parent_scope, initial_res) in macro_resolutions {
|
2018-10-27 20:23:54 +03:00
|
|
|
// FIXME: Path resolution will ICE if segment IDs present.
|
|
|
|
for seg in &mut path {
|
|
|
|
seg.id = None;
|
|
|
|
}
|
2019-08-05 21:18:50 +03:00
|
|
|
match self.resolve_path(
|
|
|
|
&path,
|
|
|
|
Some(MacroNS),
|
|
|
|
&parent_scope,
|
2022-04-30 16:26:36 +03:00
|
|
|
Some(Finalize::new(ast::CRATE_NODE_ID, path_span)),
|
2022-04-08 22:50:56 +02:00
|
|
|
None,
|
2019-08-05 21:18:50 +03:00
|
|
|
) {
|
2018-11-10 18:58:37 +03:00
|
|
|
PathResult::NonModule(path_res) if path_res.unresolved_segments() == 0 => {
|
2019-04-20 19:36:05 +03:00
|
|
|
let res = path_res.base_res();
|
|
|
|
check_consistency(self, &path, path_span, kind, initial_res, res);
|
2018-11-10 18:58:37 +03:00
|
|
|
}
|
2019-01-16 15:30:41 -05: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 18:58:37 +03:00
|
|
|
} else {
|
|
|
|
(
|
|
|
|
path_span,
|
|
|
|
format!(
|
|
|
|
"partially resolved path in {} {}",
|
|
|
|
kind.article(),
|
|
|
|
kind.descr()
|
2019-12-22 17:42:04 -05:00
|
|
|
),
|
|
|
|
)
|
2018-11-10 18:58:37 +03:00
|
|
|
};
|
2019-08-08 23:32:58 +03:00
|
|
|
self.report_error(
|
|
|
|
span,
|
|
|
|
ResolutionError::FailedToResolve { label, suggestion: None },
|
2019-01-16 15:30:41 -05:00
|
|
|
);
|
2016-11-27 10:58:46 +00:00
|
|
|
}
|
2018-11-10 18:58:37 +03:00
|
|
|
PathResult::Module(..) | PathResult::Indeterminate => unreachable!(),
|
2016-11-27 10:58:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-12 21:52:37 +03:00
|
|
|
let macro_resolutions = mem::take(&mut self.single_segment_macro_resolutions);
|
2018-11-10 18:58:37 +03:00
|
|
|
for (ident, kind, parent_scope, initial_binding) in macro_resolutions {
|
2018-11-24 19:14:05 +03:00
|
|
|
match self.early_resolve_ident_in_lexical_scope(
|
|
|
|
ident,
|
|
|
|
ScopeSet::Macro(kind),
|
2018-11-10 18:58:37 +03:00
|
|
|
&parent_scope,
|
2022-04-30 16:57:18 +03:00
|
|
|
Some(Finalize::new(ast::CRATE_NODE_ID, ident.span)),
|
2018-11-10 18:58:37 +03:00
|
|
|
true,
|
2022-04-08 22:50:56 +02:00
|
|
|
None,
|
2018-11-10 18:58:37 +03:00
|
|
|
) {
|
2018-09-18 03:19:26 +03:00
|
|
|
Ok(binding) => {
|
2019-04-20 19:36:05 +03:00
|
|
|
let initial_res = initial_binding.map(|initial_binding| {
|
2021-08-22 16:50:59 +02:00
|
|
|
self.record_use(ident, initial_binding, false);
|
2019-04-20 19:36:05 +03:00
|
|
|
initial_binding.res()
|
2018-11-10 18:58:37 +03:00
|
|
|
});
|
2019-04-20 19:36:05 +03:00
|
|
|
let res = binding.res();
|
2018-11-18 14:41:06 +03:00
|
|
|
let seg = Segment::from_ident(ident);
|
2019-04-20 19:36:05 +03:00
|
|
|
check_consistency(self, &[seg], ident.span, kind, initial_res, res);
|
2020-11-14 14:47:14 +03:00
|
|
|
if res == Res::NonMacroAttr(NonMacroAttrKind::DeriveHelperCompat) {
|
2021-07-14 18:24:12 -05:00
|
|
|
let node_id = self
|
|
|
|
.invocation_parents
|
|
|
|
.get(&parent_scope.expansion)
|
|
|
|
.map_or(ast::CRATE_NODE_ID, |id| self.def_id_to_node_id[id.0]);
|
2020-11-14 14:47:14 +03:00
|
|
|
self.lint_buffer.buffer_lint_with_diagnostic(
|
|
|
|
LEGACY_DERIVE_HELPERS,
|
2021-07-14 18:24:12 -05:00
|
|
|
node_id,
|
2020-11-14 14:47:14 +03:00
|
|
|
ident.span,
|
|
|
|
"derive helper attribute is used before it is introduced",
|
|
|
|
BuiltinLintDiagnostics::LegacyDeriveHelpers(binding.span),
|
|
|
|
);
|
|
|
|
}
|
2018-05-28 22:13:59 +03:00
|
|
|
}
|
2018-09-18 03:19:26 +03:00
|
|
|
Err(..) => {
|
2019-09-15 12:55:18 +03:00
|
|
|
let expected = kind.descr_expected();
|
2019-09-15 13:12:14 +03:00
|
|
|
let msg = format!("cannot find {} `{}` in this scope", expected, ident);
|
2018-09-18 03:19:26 +03:00
|
|
|
let mut err = self.session.struct_span_err(ident.span, &msg);
|
2019-07-12 02:29:28 +03:00
|
|
|
self.unresolved_macro_suggestions(&mut err, kind, &parent_scope, ident);
|
2017-02-06 22:14:38 +10:30
|
|
|
err.emit();
|
2018-05-28 22:13:59 +03:00
|
|
|
}
|
2018-09-18 03:19:26 +03:00
|
|
|
}
|
2016-10-31 22:17:15 +00:00
|
|
|
}
|
2018-09-03 00:04:54 +03:00
|
|
|
|
2019-08-12 21:52:37 +03:00
|
|
|
let builtin_attrs = mem::take(&mut self.builtin_attrs);
|
2018-09-13 01:41:07 +03:00
|
|
|
for (ident, parent_scope) in builtin_attrs {
|
2018-11-05 01:00:31 +03:00
|
|
|
let _ = self.early_resolve_ident_in_lexical_scope(
|
2018-11-24 19:14:05 +03:00
|
|
|
ident,
|
|
|
|
ScopeSet::Macro(MacroKind::Attr),
|
|
|
|
&parent_scope,
|
2022-04-30 16:57:18 +03:00
|
|
|
Some(Finalize::new(ast::CRATE_NODE_ID, ident.span)),
|
2018-11-24 19:14:05 +03:00
|
|
|
true,
|
2022-04-08 22:50:56 +02:00
|
|
|
None,
|
2018-09-18 03:19:26 +03:00
|
|
|
);
|
2018-09-03 00:04:54 +03:00
|
|
|
}
|
2016-09-07 23:21:59 +00:00
|
|
|
}
|
2016-09-21 06:25:09 +00:00
|
|
|
|
2020-06-09 20:59:43 +03:00
|
|
|
fn check_stability_and_deprecation(
|
|
|
|
&mut self,
|
|
|
|
ext: &SyntaxExtension,
|
|
|
|
path: &ast::Path,
|
|
|
|
node_id: NodeId,
|
|
|
|
) {
|
2019-07-03 23:59:03 +03:00
|
|
|
let span = path.span;
|
2019-06-22 02:44:45 +03:00
|
|
|
if let Some(stability) = &ext.stability {
|
2019-09-01 14:54:57 +03:00
|
|
|
if let StabilityLevel::Unstable { reason, issue, is_soft } = stability.level {
|
2019-06-22 16:18:05 +03:00
|
|
|
let feature = stability.feature;
|
|
|
|
if !self.active_features.contains(&feature) && !span.allows_unstable(feature) {
|
2019-10-25 09:15:33 -04:00
|
|
|
let lint_buffer = &mut self.lint_buffer;
|
|
|
|
let soft_handler =
|
|
|
|
|lint, span, msg: &_| lint_buffer.buffer_lint(lint, node_id, span, msg);
|
2019-10-10 23:37:41 +03:00
|
|
|
stability::report_unstable(
|
|
|
|
self.session,
|
|
|
|
feature,
|
|
|
|
reason,
|
|
|
|
issue,
|
2021-11-13 16:39:54 +09:00
|
|
|
None,
|
2019-10-10 23:37:41 +03:00
|
|
|
is_soft,
|
|
|
|
span,
|
|
|
|
soft_handler,
|
|
|
|
);
|
2019-06-22 02:44:45 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if let Some(depr) = &ext.deprecation {
|
2019-10-08 22:17:46 +02:00
|
|
|
let path = pprust::path_to_string(&path);
|
2021-09-30 15:15:10 +04:00
|
|
|
let (message, lint) = stability::deprecation_message_and_lint(depr, "macro", &path);
|
2020-07-20 10:44:43 -04:00
|
|
|
stability::early_report_deprecation(
|
|
|
|
&mut self.lint_buffer,
|
|
|
|
&message,
|
|
|
|
depr.suggestion,
|
|
|
|
lint,
|
|
|
|
span,
|
2020-11-12 22:29:52 +03:00
|
|
|
node_id,
|
2020-07-20 10:44:43 -04:00
|
|
|
);
|
2019-06-22 02:44:45 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-12 04:11:46 +03:00
|
|
|
fn prohibit_imported_non_macro_attrs(
|
|
|
|
&self,
|
|
|
|
binding: Option<&'a NameBinding<'a>>,
|
2019-04-20 19:36:05 +03:00
|
|
|
res: Option<Res>,
|
|
|
|
span: Span,
|
|
|
|
) {
|
|
|
|
if let Some(Res::NonMacroAttr(kind)) = res {
|
2018-12-12 04:11:46 +03:00
|
|
|
if kind != NonMacroAttrKind::Tool && binding.map_or(true, |b| b.is_import()) {
|
2019-11-04 16:47:03 +03:00
|
|
|
let msg =
|
|
|
|
format!("cannot use {} {} through an import", kind.article(), kind.descr());
|
2018-12-12 04:11:46 +03:00
|
|
|
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-30 01:24:34 +03: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`.
|
2020-11-14 14:47:14 +03:00
|
|
|
if ident.name == sym::cfg || ident.name == sym::cfg_attr {
|
2019-07-03 23:59:03 +03:00
|
|
|
let macro_kind = self.get_macro(res).map(|ext| ext.macro_kind());
|
2019-06-30 01:24:34 +03: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),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-22 23:06:56 +03:00
|
|
|
/// Compile the macro into a `SyntaxExtension` and possibly replace
|
|
|
|
/// its expander to a pre-defined one for built-in macros.
|
2019-08-20 23:18:55 +03:00
|
|
|
crate fn compile_macro(&mut self, item: &ast::Item, edition: Edition) -> SyntaxExtension {
|
2019-09-22 17:42:17 +03:00
|
|
|
let mut result = compile_declarative_macro(
|
2020-07-30 11:27:50 +10:00
|
|
|
&self.session,
|
2019-06-20 11:52:31 +03:00
|
|
|
self.session.features_untracked(),
|
|
|
|
item,
|
|
|
|
edition,
|
|
|
|
);
|
|
|
|
|
2021-01-09 16:48:58 +01:00
|
|
|
if let Some(builtin_name) = result.builtin_name {
|
2019-06-20 11:52:31 +03:00
|
|
|
// The macro was marked with `#[rustc_builtin_macro]`.
|
2021-01-09 16:48:58 +01:00
|
|
|
if let Some(builtin_macro) = self.builtin_macros.get_mut(&builtin_name) {
|
2019-11-22 23:06:56 +03:00
|
|
|
// The macro is a built-in, replace its expander function
|
|
|
|
// while still taking everything else from the source code.
|
2020-08-31 00:04:01 -04:00
|
|
|
// If we already loaded this builtin macro, give a better error message than 'no such builtin macro'.
|
|
|
|
match mem::replace(builtin_macro, BuiltinMacroState::AlreadySeen(item.span)) {
|
2021-12-11 19:52:23 +08:00
|
|
|
BuiltinMacroState::NotYetSeen(ext) => {
|
|
|
|
result.kind = ext;
|
|
|
|
if item.id != ast::DUMMY_NODE_ID {
|
|
|
|
self.builtin_macro_kinds
|
|
|
|
.insert(self.local_def_id(item.id), result.macro_kind());
|
|
|
|
}
|
|
|
|
}
|
2020-08-31 00:04:01 -04:00
|
|
|
BuiltinMacroState::AlreadySeen(span) => {
|
|
|
|
struct_span_err!(
|
|
|
|
self.session,
|
|
|
|
item.span,
|
|
|
|
E0773,
|
|
|
|
"attempted to define built-in macro more than once"
|
|
|
|
)
|
|
|
|
.span_note(span, "previously defined here")
|
|
|
|
.emit();
|
|
|
|
}
|
|
|
|
}
|
2019-06-20 11:52:31 +03:00
|
|
|
} else {
|
|
|
|
let msg = format!("cannot find a built-in macro with name `{}`", item.ident);
|
|
|
|
self.session.span_err(item.span, &msg);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-20 23:18:55 +03:00
|
|
|
result
|
2019-06-20 11:52:31 +03:00
|
|
|
}
|
2016-09-07 23:21:59 +00:00
|
|
|
}
|