rust/compiler/rustc_resolve/src/macros.rs

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

1185 lines
48 KiB
Rust
Raw Normal View History

//! A bunch of methods and structures more or less related to resolving macros and
//! interface provided by `Resolver` to macro expander.
use std::cell::Cell;
use std::mem;
use std::sync::Arc;
use rustc_ast::expand::StrippedCfgItem;
use rustc_ast::{self as ast, Crate, NodeId, attr};
use rustc_ast_pretty::pprust;
use rustc_attr_parsing::StabilityLevel;
use rustc_data_structures::intern::Interned;
2024-03-24 15:11:27 +00:00
use rustc_errors::{Applicability, StashKey};
use rustc_expand::base::{
DeriveResolution, Indeterminate, ResolverExpand, SyntaxExtension, SyntaxExtensionKind,
};
use rustc_expand::compile_declarative_macro;
use rustc_expand::expand::{
AstFragment, AstFragmentKind, Invocation, InvocationKind, SupportsMacroExpansion,
};
use rustc_hir::def::{self, DefKind, Namespace, NonMacroAttrKind};
use rustc_hir::def_id::{CrateNum, DefId, LocalDefId};
2020-03-29 17:19:48 +02:00
use rustc_middle::middle::stability;
use rustc_middle::ty::{RegisteredTools, TyCtxt, Visibility};
use rustc_session::lint::BuiltinLintDiag;
use rustc_session::lint::builtin::{
LEGACY_DERIVE_HELPERS, OUT_OF_SCOPE_MACRO_CALLS, UNKNOWN_OR_MALFORMED_DIAGNOSTIC_ATTRIBUTES,
UNUSED_MACRO_RULES, UNUSED_MACROS,
};
use rustc_session::parse::feature_err;
use rustc_span::edit_distance::edit_distance;
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, AstPass, ExpnData, ExpnKind, LocalExpnId, MacroKind};
use rustc_span::{DUMMY_SP, Ident, Span, Symbol, kw, sym};
use crate::Namespace::*;
use crate::errors::{
self, AddAsNonDerive, CannotDetermineMacroResolution, CannotFindIdentInThisScope,
MacroExpectedFound, RemoveSurroundingDerive,
};
2024-08-06 19:27:15 +08:00
use crate::imports::Import;
use crate::{
BindingKey, DeriveData, Determinacy, Finalize, InvocationParent, MacroData, ModuleKind,
ModuleOrUniformRoot, NameBinding, NameBindingKind, ParentScope, PathResult, ResolutionError,
Resolver, ScopeSet, Segment, ToNameBinding, Used,
};
type Res = def::Res<NodeId>;
2019-04-03 09:07:45 +02:00
/// Binding produced by a `macro_rules` item.
/// Not modularized, can shadow previous `macro_rules` bindings, etc.
#[derive(Debug)]
pub(crate) struct MacroRulesBinding<'ra> {
pub(crate) binding: NameBinding<'ra>,
/// `macro_rules` scope into which the `macro_rules` item was planted.
pub(crate) parent_macro_rules_scope: MacroRulesScopeRef<'ra>,
pub(crate) ident: Ident,
}
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]`.
/// 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.
#[derive(Copy, Clone, Debug)]
pub(crate) enum MacroRulesScope<'ra> {
/// 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.
Binding(&'ra MacroRulesBinding<'ra>),
2019-02-08 14:53:55 +01:00
/// The scope introduced by a macro invocation that can potentially
/// create a `macro_rules!` macro definition.
2021-06-25 20:43:04 +02:00
Invocation(LocalExpnId),
}
/// `macro_rules!` scopes are always kept by reference and inside a cell.
/// The reason is that we update scopes with value `MacroRulesScope::Invocation(invoc_id)`
/// in-place after `invoc_id` gets expanded.
/// This helps to avoid uncontrollable growth of `macro_rules!` scope chains,
/// which usually grow linearly with the number of macro invocations
/// in a module (including derives) and hurt performance.
pub(crate) type MacroRulesScopeRef<'ra> = Interned<'ra, Cell<MacroRulesScope<'ra>>>;
/// 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.
pub(crate) fn sub_namespace_match(
candidate: Option<MacroKind>,
requirement: Option<MacroKind>,
) -> bool {
#[derive(PartialEq)]
enum SubNS {
Bang,
AttrLike,
}
let sub_ns = |kind| match kind {
MacroKind::Bang => SubNS::Bang,
MacroKind::Attr | MacroKind::Derive => SubNS::AttrLike,
};
let candidate = candidate.map(sub_ns);
let requirement = requirement.map(sub_ns);
// "No specific sub-namespace" means "matches anything" for both requirements and candidates.
candidate.is_none() || requirement.is_none() || candidate == requirement
}
// We don't want to format a path using pretty-printing,
// `format!("{}", path)`, because that tries to insert
// line-breaks and is slow.
fn fast_print_path(path: &ast::Path) -> Symbol {
if let [segment] = path.segments.as_slice() {
segment.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())
}
}
Symbol::intern(&path_str)
}
}
2023-03-06 10:56:23 +00:00
pub(crate) fn registered_tools(tcx: TyCtxt<'_>, (): ()) -> RegisteredTools {
let mut registered_tools = RegisteredTools::default();
let (_, pre_configured_attrs) = &*tcx.crate_for_resolver(()).borrow();
for attr in attr::filter_by_name(pre_configured_attrs, sym::register_tool) {
2024-10-07 08:49:47 +09:00
for meta_item_inner in attr.meta_item_list().unwrap_or_default() {
match meta_item_inner.ident() {
Some(ident) => {
if let Some(old_ident) = registered_tools.replace(ident) {
2024-03-24 21:53:18 +00:00
tcx.dcx().emit_err(errors::ToolWasAlreadyRegistered {
span: ident.span,
tool: ident,
old_ident_span: old_ident.span,
});
2019-12-22 17:42:04 -05:00
}
}
None => {
2024-03-24 21:53:18 +00:00
tcx.dcx().emit_err(errors::ToolOnlyAcceptsIdentifiers {
2024-10-07 08:49:47 +09:00
span: meta_item_inner.span(),
2024-03-24 21:53:18 +00:00
tool: sym::register_tool,
});
}
}
}
}
// We implicitly add `rustfmt`, `clippy`, `diagnostic`, `miri` and `rust_analyzer` to known
// tools, but it's not an error to register them explicitly.
let predefined_tools =
[sym::clippy, sym::rustfmt, sym::diagnostic, sym::miri, sym::rust_analyzer];
registered_tools.extend(predefined_tools.iter().cloned().map(Ident::with_dummy_span));
registered_tools
}
impl<'ra, 'tcx> ResolverExpand for Resolver<'ra, 'tcx> {
fn next_node_id(&mut self) -> NodeId {
self.next_node_id()
}
2021-05-02 21:19:28 +02:00
fn invocation_parent(&self, id: LocalExpnId) -> LocalDefId {
Fix anon const def-creation when macros are involved Ever since #125915, some `ast::AnonConst`s turn into `hir::ConstArgKind::Path`s, which don't have associated `DefId`s. To deal with the fact that we don't have resolution information in `DefCollector`, we decided to implement a process where if the anon const *appeared* to be trivial (i.e., `N` or `{ N }`), we would avoid creating a def for it in `DefCollector`. If later, in AST lowering, we realized it turned out to be a unit struct literal, or we were lowering it to something that didn't use `hir::ConstArg`, we'd create its def there. However, let's say we have a macro `m!()` that expands to a reference to a free constant `FOO`. If we use `m!()` in the body of an anon const (e.g., `Foo<{ m!() }>`), then in def collection, it appears to be a nontrivial anon const and we create a def. But the macro expands to something that looks like a trivial const arg, but is not, so in AST lowering we "fix" the mistake we assumed def collection made and create a def for it. This causes a duplicate definition ICE. The ideal long-term fix for this is a bit unclear. One option is to delay def creation for all expression-like nodes until AST lowering (see #128844 for an incomplete attempt at this). This would avoid issues like this one that are caused by hacky workarounds. However, this approach has some downsides as well, and the best approach is yet to be determined. In the meantime, this PR fixes the bug by delaying def creation for anon consts whose bodies are macro invocations until after we expand the macro and know what is inside it. This is accomplished by adding information to create the anon const's def to the data in `Resolver.invocation_parents`.
2024-08-15 14:36:10 -07:00
self.invocation_parents[&id].parent_def
2021-05-02 21:19:28 +02: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::Empty => name,
_ => kw::Crate,
}
});
}
2021-06-25 20:43:04 +02:00
fn visit_ast_fragment_with_placeholders(
&mut self,
expansion: LocalExpnId,
fragment: &AstFragment,
) {
// Integrate the new AST fragment into all the definition and module structures.
// 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] };
let output_macro_rules_scope = self.build_reduced_graph(fragment, parent_scope);
self.output_macro_rules_scopes.insert(expansion, output_macro_rules_scope);
parent_scope.module.unexpanded_invocations.borrow_mut().remove(&expansion);
2024-03-15 14:21:03 +03:00
if let Some(unexpanded_invocations) =
self.impl_unexpanded_invocations.get_mut(&self.invocation_parent(expansion))
{
unexpanded_invocations.remove(&expansion);
}
}
2021-01-10 14:36:30 +03:00
fn register_builtin_macro(&mut self, name: Symbol, ext: SyntaxExtensionKind) {
if self.builtin_macros.insert(name, ext).is_some() {
self.dcx().bug(format!("built-in macro `{name}` was already registered"));
}
}
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.
fn expansion_for_ast_pass(
&mut self,
call_site: Span,
pass: AstPass,
features: &[Symbol],
parent_module_id: Option<NodeId>,
2021-06-25 20:43:04 +02:00
) -> LocalExpnId {
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.tcx.sess.edition(),
2021-06-22 19:20:25 +02:00
features.into(),
None,
parent_module,
2021-06-22 19:20:25 +02:00
),
self.create_stable_hashing_context(),
);
let parent_scope =
parent_module.map_or(self.empty_module, |def_id| self.expect_module(def_id));
self.ast_transform_scopes.insert(expn_id, parent_scope);
2021-06-28 19:29:55 +02:00
expn_id
}
2016-11-10 10:11:25 +00:00
fn resolve_imports(&mut self) {
self.resolve_imports()
2016-11-10 10:11:25 +00:00
}
fn resolve_macro_invocation(
&mut self,
invoc: &Invocation,
2021-06-25 20:43:04 +02:00
eager_expansion_root: LocalExpnId,
force: bool,
) -> Result<Arc<SyntaxExtension>, Indeterminate> {
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
}
};
2024-03-15 14:21:03 +03:00
let (mut derives, mut inner_attr, mut deleg_impl) = (&[][..], false, None);
let (path, kind) = match invoc.kind {
InvocationKind::Attr { ref attr, derives: ref attr_derives, .. } => {
derives = self.arenas.alloc_ast_paths(attr_derives);
inner_attr = attr.style == ast::AttrStyle::Inner;
(&attr.get_normal_item().path, MacroKind::Attr)
}
InvocationKind::Bang { ref mac, .. } => (&mac.path, MacroKind::Bang),
InvocationKind::Derive { ref path, .. } => (path, MacroKind::Derive),
InvocationKind::GlobDelegation { ref item } => {
let ast::AssocItemKind::DelegationMac(deleg) = &item.kind else { unreachable!() };
deleg_impl = Some(self.invocation_parent(invoc_id));
// It is sufficient to consider glob delegation a bang macro for now.
(&deleg.prefix, MacroKind::Bang)
}
};
// Derives are not included when `invocations` are collected, so we have to add them here.
let parent_scope = &ParentScope { derives, ..parent_scope };
let supports_macro_expansion = invoc.fragment_kind.supports_macro_expansion();
let node_id = invoc.expansion_data.lint_node_id;
// This is a heuristic, but it's good enough for the lint.
let looks_like_invoc_in_mod_inert_attr = self
.invocation_parents
.get(&invoc_id)
.or_else(|| self.invocation_parents.get(&eager_expansion_root))
Fix anon const def-creation when macros are involved Ever since #125915, some `ast::AnonConst`s turn into `hir::ConstArgKind::Path`s, which don't have associated `DefId`s. To deal with the fact that we don't have resolution information in `DefCollector`, we decided to implement a process where if the anon const *appeared* to be trivial (i.e., `N` or `{ N }`), we would avoid creating a def for it in `DefCollector`. If later, in AST lowering, we realized it turned out to be a unit struct literal, or we were lowering it to something that didn't use `hir::ConstArg`, we'd create its def there. However, let's say we have a macro `m!()` that expands to a reference to a free constant `FOO`. If we use `m!()` in the body of an anon const (e.g., `Foo<{ m!() }>`), then in def collection, it appears to be a nontrivial anon const and we create a def. But the macro expands to something that looks like a trivial const arg, but is not, so in AST lowering we "fix" the mistake we assumed def collection made and create a def for it. This causes a duplicate definition ICE. The ideal long-term fix for this is a bit unclear. One option is to delay def creation for all expression-like nodes until AST lowering (see #128844 for an incomplete attempt at this). This would avoid issues like this one that are caused by hacky workarounds. However, this approach has some downsides as well, and the best approach is yet to be determined. In the meantime, this PR fixes the bug by delaying def creation for anon consts whose bodies are macro invocations until after we expand the macro and know what is inside it. This is accomplished by adding information to create the anon const's def to the data in `Resolver.invocation_parents`.
2024-08-15 14:36:10 -07:00
.filter(|&&InvocationParent { parent_def: mod_def_id, in_attr, .. }| {
in_attr
&& invoc.fragment_kind == AstFragmentKind::Expr
&& self.tcx.def_kind(mod_def_id) == DefKind::Mod
})
Fix anon const def-creation when macros are involved Ever since #125915, some `ast::AnonConst`s turn into `hir::ConstArgKind::Path`s, which don't have associated `DefId`s. To deal with the fact that we don't have resolution information in `DefCollector`, we decided to implement a process where if the anon const *appeared* to be trivial (i.e., `N` or `{ N }`), we would avoid creating a def for it in `DefCollector`. If later, in AST lowering, we realized it turned out to be a unit struct literal, or we were lowering it to something that didn't use `hir::ConstArg`, we'd create its def there. However, let's say we have a macro `m!()` that expands to a reference to a free constant `FOO`. If we use `m!()` in the body of an anon const (e.g., `Foo<{ m!() }>`), then in def collection, it appears to be a nontrivial anon const and we create a def. But the macro expands to something that looks like a trivial const arg, but is not, so in AST lowering we "fix" the mistake we assumed def collection made and create a def for it. This causes a duplicate definition ICE. The ideal long-term fix for this is a bit unclear. One option is to delay def creation for all expression-like nodes until AST lowering (see #128844 for an incomplete attempt at this). This would avoid issues like this one that are caused by hacky workarounds. However, this approach has some downsides as well, and the best approach is yet to be determined. In the meantime, this PR fixes the bug by delaying def creation for anon consts whose bodies are macro invocations until after we expand the macro and know what is inside it. This is accomplished by adding information to create the anon const's def to the data in `Resolver.invocation_parents`.
2024-08-15 14:36:10 -07:00
.map(|&InvocationParent { parent_def: mod_def_id, .. }| mod_def_id);
let (ext, res) = self.smart_resolve_macro_path(
path,
kind,
supports_macro_expansion,
inner_attr,
parent_scope,
node_id,
force,
2024-03-15 14:21:03 +03:00
deleg_impl,
looks_like_invoc_in_mod_inert_attr,
)?;
let span = invoc.span();
2024-03-15 14:21:03 +03:00
let def_id = if deleg_impl.is_some() { None } else { 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),
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(),
);
Ok(ext)
}
2022-04-17 04:01:17 +02:00
fn record_macro_rule_usage(&mut self, id: NodeId, rule_i: usize) {
let did = self.local_def_id(id);
if let Some(rules) = self.unused_macro_rules.get_mut(&did) {
rules.remove(&rule_i);
}
2022-04-17 04:01:17 +02:00
}
fn check_unused_macros(&mut self) {
2025-03-14 17:18:41 +03:00
#[allow(rustc::potential_query_instability)] // FIXME
for (_, &(node_id, ident)) in self.unused_macros.iter() {
self.lint_buffer.buffer_lint(
UNUSED_MACROS,
node_id,
ident.span,
BuiltinLintDiag::UnusedMacroDefinition(ident.name),
);
2017-05-11 10:26:07 +02:00
}
for (&def_id, unused_arms) in self.unused_macro_rules.iter() {
2025-03-14 17:18:41 +03:00
for (&arm_i, &(ident, rule_span)) in unused_arms.to_sorted_stable_ord() {
if self.unused_macros.contains_key(&def_id) {
// We already lint the entire macro as unused
continue;
}
let node_id = self.def_id_to_node_id[def_id];
self.lint_buffer.buffer_lint(
UNUSED_MACRO_RULES,
node_id,
rule_span,
BuiltinLintDiag::MacroRuleNeverUsed(arm_i, ident.name),
);
2022-04-17 04:01:17 +02:00
}
}
}
2021-06-25 20:43:04 +02:00
fn has_derive_copy(&self, expn_id: LocalExpnId) -> bool {
self.containers_deriving_copy.contains(&expn_id)
}
fn resolve_derives(
&mut self,
2021-06-25 20:43:04 +02:00
expn_id: LocalExpnId,
force: bool,
derive_paths: &dyn Fn() -> Vec<DeriveResolution>,
) -> 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`.
// 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,
});
let parent_scope = self.invocation_parent_scopes[&expn_id];
for (i, resolution) in entry.resolutions.iter_mut().enumerate() {
if resolution.exts.is_none() {
resolution.exts = Some(
match self.resolve_macro_path(
&resolution.path,
Some(MacroKind::Derive),
&parent_scope,
true,
force,
2024-08-06 19:27:15 +08:00
None,
) {
Ok((Some(ext), _)) => {
if !ext.helper_attrs.is_empty() {
let last_seg = resolution.path.segments.last().unwrap();
let span = last_seg.ident.span.normalize_to_macros_2_0();
entry.helper_attrs.extend(
ext.helper_attrs
.iter()
.map(|name| (i, Ident::new(*name, span))),
);
}
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);
}
},
);
}
}
// Sort helpers in a stable way independent from the derive resolution order.
entry.helper_attrs.sort_by_key(|(i, _)| *i);
let helper_attrs = entry
.helper_attrs
.iter()
.map(|(_, ident)| {
let res = Res::NonMacroAttr(NonMacroAttrKind::DeriveHelper);
let binding = (res, Visibility::<DefId>::Public, ident.span, expn_id)
.to_name_binding(self.arenas);
(*ident, binding)
})
.collect();
self.helper_attrs.insert(expn_id, helper_attrs);
// 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)]`.
if entry.has_derive_copy || self.has_derive_copy(parent_scope.expansion) {
self.containers_deriving_copy.insert(expn_id);
}
assert!(self.derive_data.is_empty());
self.derive_data = derive_data;
Ok(())
}
fn take_derive_resolutions(&mut self, expn_id: LocalExpnId) -> Option<Vec<DeriveResolution>> {
self.derive_data.remove(&expn_id).map(|data| data.resolutions)
}
// 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> {
self.path_accessible(expn_id, path, &[TypeNS, ValueNS, MacroNS])
}
fn macro_accessible(
&mut self,
expn_id: LocalExpnId,
path: &ast::Path,
) -> Result<bool, Indeterminate> {
self.path_accessible(expn_id, path, &[MacroNS])
}
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.cstore().get_proc_macro_quoted_span_untracked(krate, id, self.tcx.sess)
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
}
2021-07-16 22:22:08 +02:00
fn declare_proc_macro(&mut self, id: NodeId) {
self.proc_macros.push(id)
}
fn append_stripped_cfg_item(&mut self, parent_node: NodeId, name: Ident, cfg: ast::MetaItem) {
self.stripped_cfg_items.push(StrippedCfgItem { parent_module: parent_node, name, cfg });
}
fn registered_tools(&self) -> &RegisteredTools {
self.registered_tools
}
2024-03-15 14:21:03 +03:00
fn register_glob_delegation(&mut self, invoc_id: LocalExpnId) {
self.glob_delegation_invoc_ids.insert(invoc_id);
}
fn glob_delegation_suffixes(
&mut self,
trait_def_id: DefId,
impl_def_id: LocalDefId,
) -> Result<Vec<(Ident, Option<Ident>)>, Indeterminate> {
let target_trait = self.expect_module(trait_def_id);
if !target_trait.unexpanded_invocations.borrow().is_empty() {
return Err(Indeterminate);
}
// FIXME: Instead of waiting try generating all trait methods, and pruning
// the shadowed ones a bit later, e.g. when all macro expansion completes.
// Pros: expansion will be stuck less (but only in exotic cases), the implementation may be
// less hacky.
// Cons: More code is generated just to be deleted later, deleting already created `DefId`s
// may be nontrivial.
if let Some(unexpanded_invocations) = self.impl_unexpanded_invocations.get(&impl_def_id)
&& !unexpanded_invocations.is_empty()
{
return Err(Indeterminate);
}
let mut idents = Vec::new();
target_trait.for_each_child(self, |this, ident, ns, _binding| {
// FIXME: Adjust hygiene for idents from globs, like for glob imports.
if let Some(overriding_keys) = this.impl_binding_keys.get(&impl_def_id)
&& overriding_keys.contains(&BindingKey::new(ident.normalize_to_macros_2_0(), ns))
{
// The name is overridden, do not produce it from the glob delegation.
} else {
idents.push((ident, None));
}
});
Ok(idents)
}
}
impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
/// Resolve macro path with error reporting and recovery.
/// Uses dummy syntax extensions for unresolved macros or macros with unexpected resolutions
/// for better error recovery.
fn smart_resolve_macro_path(
&mut self,
path: &ast::Path,
kind: MacroKind,
supports_macro_expansion: SupportsMacroExpansion,
inner_attr: bool,
parent_scope: &ParentScope<'ra>,
node_id: NodeId,
force: bool,
2024-03-15 14:21:03 +03:00
deleg_impl: Option<LocalDefId>,
invoc_in_mod_inert_attr: Option<LocalDefId>,
) -> Result<(Arc<SyntaxExtension>, Res), Indeterminate> {
2024-03-15 14:21:03 +03:00
let (ext, res) = match self.resolve_macro_or_delegation_path(
path,
Some(kind),
parent_scope,
true,
force,
deleg_impl,
invoc_in_mod_inert_attr.map(|def_id| (def_id, node_id)),
2024-08-06 19:27:15 +08:00
None,
2024-03-15 14:21: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),
};
2024-03-15 14:21:03 +03:00
// Everything below is irrelevant to glob delegation, take a shortcut.
if deleg_impl.is_some() {
if !matches!(res, Res::Err | Res::Def(DefKind::Trait, _)) {
self.dcx().emit_err(MacroExpectedFound {
span: path.span,
expected: "trait",
article: "a",
found: res.descr(),
macro_path: &pprust::path_to_string(path),
remove_surrounding_derive: None,
add_as_non_derive: None,
});
return Ok((self.dummy_ext(kind), Res::Err));
}
return Ok((ext, res));
}
// Report errors for the resolved macro.
for segment in &path.segments {
if let Some(args) = &segment.args {
2024-03-24 21:53:18 +00:00
self.dcx().emit_err(errors::GenericArgumentsInMacroPath { span: args.span() });
}
if kind == MacroKind::Attr && segment.ident.as_str().starts_with("rustc") {
2024-03-24 21:53:18 +00:00
self.dcx().emit_err(errors::AttributesStartingWithRustcAreReserved {
span: segment.ident.span,
});
}
}
match res {
Res::Def(DefKind::Macro(_), def_id) => {
if let Some(def_id) = def_id.as_local() {
self.unused_macros.remove(&def_id);
if self.proc_macro_stubs.contains(&def_id) {
self.dcx().emit_err(errors::ProcMacroSameCrate {
span: path.span,
is_test: self.tcx.sess.is_test_crate(),
});
}
}
}
Res::NonMacroAttr(..) | Res::Err => {}
_ => panic!("expected `DefKind::Macro` or `Res::NonMacroAttr`"),
};
self.check_stability_and_deprecation(&ext, path, node_id);
let unexpected_res = if ext.macro_kind() != kind {
Some((kind.article(), kind.descr_expected()))
} 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
}
}
}
} else {
None
};
if let Some((article, expected)) = unexpected_res {
let path_str = pprust::path_to_string(path);
let mut err = MacroExpectedFound {
span: path.span,
expected,
2024-03-24 21:53:18 +00:00
article,
found: res.descr(),
macro_path: &path_str,
remove_surrounding_derive: None,
add_as_non_derive: None,
};
// Suggest moving the macro out of the derive() if the macro isn't Derive
if !path.span.from_expansion()
&& kind == MacroKind::Derive
&& ext.macro_kind() != MacroKind::Derive
{
err.remove_surrounding_derive = Some(RemoveSurroundingDerive { span: path.span });
err.add_as_non_derive = Some(AddAsNonDerive { macro_path: &path_str });
}
2024-03-24 21:53:18 +00:00
self.dcx().emit_err(err);
return Ok((self.dummy_ext(kind), Res::Err));
}
// We are trying to avoid reporting this error if other related errors were reported.
if res != Res::Err && inner_attr && !self.tcx.features().custom_inner_attributes() {
let is_macro = match res {
Res::Def(..) => true,
Res::NonMacroAttr(..) => false,
_ => unreachable!(),
};
let msg = if is_macro {
"inner macro attributes are unstable"
} else {
"custom inner attributes are unstable"
};
feature_err(&self.tcx.sess, sym::custom_inner_attributes, path.span, msg).emit();
}
if res == Res::NonMacroAttr(NonMacroAttrKind::Tool)
&& let [namespace, attribute, ..] = &*path.segments
&& namespace.ident.name == sym::diagnostic
&& !(attribute.ident.name == sym::on_unimplemented
Stabilize `#[diagnostic::do_not_recommend]` This commit seeks to stabilize the `#[diagnostic::do_not_recommend]` attribute. This attribute was first proposed as `#[do_not_recommend`] attribute in RFC 2397 (https://github.com/rust-lang/rfcs/pull/2397). It gives the crate authors the ability to not suggest to the compiler to not show certain traits in it's error messages. With the presence of the `#[diagnostic]` tool attribute namespace it was decided to move the attribute there, as that lowers the amount of guarantees the compiler needs to give about the exact way this influences error messages. It turns the attribute into a hint which can be ignored. In addition to the original proposed functionality this attribute now also hides the marked trait in help messages ("This trait is implemented by: "). The attribute does not accept any argument and can only be placed on trait implementations. If it is placed somewhere else a lint warning is emitted and the attribute is otherwise ignored. If an argument is detected a lint warning is emitted and the argument is ignored. This follows the rules outlined by the diagnostic namespace. This attribute allows crates like diesel to improve their error messages drastically. The most common example here is the following error message: ``` error[E0277]: the trait bound `&str: Expression` is not satisfied --> /home/weiznich/Documents/rust/rust/tests/ui/diagnostic_namespace/do_not_recommend.rs:53:15 | LL | SelectInt.check("bar"); | ^^^^^ the trait `Expression` is not implemented for `&str`, which is required by `&str: AsExpression<Integer>` | = help: the following other types implement trait `Expression`: Bound<T> SelectInt note: required for `&str` to implement `AsExpression<Integer>` --> /home/weiznich/Documents/rust/rust/tests/ui/diagnostic_namespace/do_not_recommend.rs:26:13 | LL | impl<T, ST> AsExpression<ST> for T | ^^^^^^^^^^^^^^^^ ^ LL | where LL | T: Expression<SqlType = ST>, | ------------------------ unsatisfied trait bound introduced here ``` By applying the new attribute to the wild card trait implementation of `AsExpression` for `T: Expression` the error message becomes: ``` error[E0277]: the trait bound `&str: AsExpression<Integer>` is not satisfied --> $DIR/as_expression.rs:55:15 | LL | SelectInt.check("bar"); | ^^^^^ the trait `AsExpression<Integer>` is not implemented for `&str` | = help: the trait `AsExpression<Text>` is implemented for `&str` = help: for that trait implementation, expected `Text`, found `Integer` ``` which makes it much easier for users to understand that they are facing a type mismatch. Other explored example usages included * This standard library error message: https://github.com/rust-lang/rust/pull/128008 * That bevy derived example: https://github.com/rust-lang/rust/blob/e1f306899514ea80abc1d1c9f6a57762afb304a3/tests/ui/diagnostic_namespace/do_not_recommend/supress_suggestions_in_help.rs (No more tuple pyramids) Fixes #51992
2024-10-23 08:09:15 +02:00
|| attribute.ident.name == sym::do_not_recommend)
{
let distance =
edit_distance(attribute.ident.name.as_str(), sym::on_unimplemented.as_str(), 5);
let typo_name = distance.map(|_| sym::on_unimplemented);
self.tcx.sess.psess.buffer_lint(
UNKNOWN_OR_MALFORMED_DIAGNOSTIC_ATTRIBUTES,
attribute.span(),
node_id,
BuiltinLintDiag::UnknownDiagnosticAttribute { span: attribute.span(), typo_name },
);
}
Ok((ext, res))
}
pub(crate) fn resolve_macro_path(
&mut self,
path: &ast::Path,
kind: Option<MacroKind>,
parent_scope: &ParentScope<'ra>,
trace: bool,
force: bool,
ignore_import: Option<Import<'ra>>,
) -> Result<(Option<Arc<SyntaxExtension>>, Res), Determinacy> {
2024-08-06 19:27:15 +08:00
self.resolve_macro_or_delegation_path(
path,
kind,
parent_scope,
trace,
force,
None,
None,
ignore_import,
)
2024-03-15 14:21:03 +03:00
}
fn resolve_macro_or_delegation_path(
&mut self,
ast_path: &ast::Path,
2024-03-15 14:21:03 +03:00
kind: Option<MacroKind>,
parent_scope: &ParentScope<'ra>,
2024-03-15 14:21:03 +03:00
trace: bool,
force: bool,
deleg_impl: Option<LocalDefId>,
invoc_in_mod_inert_attr: Option<(LocalDefId, NodeId)>,
ignore_import: Option<Import<'ra>>,
) -> Result<(Option<Arc<SyntaxExtension>>, Res), Determinacy> {
let path_span = ast_path.span;
let mut path = Segment::from_path(ast_path);
2016-11-27 10:58:46 +00:00
// Possibly apply the macro helper hack
2024-03-15 14:21:03 +03:00
if deleg_impl.is_none()
&& kind == Some(MacroKind::Bang)
&& let [segment] = path.as_slice()
&& segment.ident.span.ctxt().outer_expn_data().local_inner_macros
{
let root = Ident::new(kw::DollarCrate, segment.ident.span);
path.insert(0, Segment::from_ident(root));
}
2024-03-15 14:21:03 +03:00
let res = if deleg_impl.is_some() || path.len() > 1 {
let ns = if deleg_impl.is_some() { TypeNS } else { MacroNS };
2024-08-06 19:27:15 +08:00
let res = match self.maybe_resolve_path(&path, Some(ns), parent_scope, ignore_import) {
PathResult::NonModule(path_res) if let Some(res) = path_res.full_res() => Ok(res),
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),
2024-03-15 14:21:03 +03:00
PathResult::Module(ModuleOrUniformRoot::Module(module)) => {
Ok(module.res().unwrap())
}
PathResult::Module(..) => unreachable!(),
2016-11-27 10:58:46 +00:00
};
if trace {
let kind = kind.expect("macro kind must be specified if tracing is enabled");
self.multi_segment_macro_resolutions.push((
path,
path_span,
kind,
*parent_scope,
res.ok(),
2024-03-15 14:21:03 +03:00
ns,
));
}
2016-11-27 10:58:46 +00:00
self.prohibit_imported_non_macro_attrs(None, res.ok(), path_span);
res
} else {
let scope_set = kind.map_or(ScopeSet::All(MacroNS), ScopeSet::Macro);
let binding = self.early_resolve_ident_in_lexical_scope(
path[0].ident,
scope_set,
parent_scope,
None,
force,
None,
2024-08-06 19:27:15 +08:00
None,
);
if let Err(Determinacy::Undetermined) = binding {
return Err(Determinacy::Undetermined);
}
2016-11-10 10:29:36 +00:00
if trace {
let kind = kind.expect("macro kind must be specified if tracing is enabled");
self.single_segment_macro_resolutions.push((
path[0].ident,
kind,
*parent_scope,
binding.ok(),
));
}
let res = binding.map(|binding| binding.res());
self.prohibit_imported_non_macro_attrs(binding.ok(), res.ok(), path_span);
self.report_out_of_scope_macro_calls(
ast_path,
parent_scope,
invoc_in_mod_inert_attr,
binding.ok(),
);
res
};
2024-03-15 14:21:03 +03:00
let res = res?;
let ext = match deleg_impl {
Some(impl_def_id) => match res {
def::Res::Def(DefKind::Trait, def_id) => {
let edition = self.tcx.sess.edition();
Some(Arc::new(SyntaxExtension::glob_delegation(def_id, impl_def_id, edition)))
2024-03-15 14:21:03 +03:00
}
_ => None,
},
None => self.get_macro(res).map(|macro_data| Arc::clone(&macro_data.ext)),
2024-03-15 14:21:03 +03:00
};
Ok((ext, res))
}
2016-09-26 03:17:05 +00:00
pub(crate) fn finalize_macro_resolutions(&mut self, krate: &Crate) {
let check_consistency = |this: &mut Self,
path: &[Segment],
span,
kind: MacroKind,
initial_res: Option<Res>,
res: Res| {
if let Some(initial_res) = initial_res {
if res != initial_res {
// 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 errors, so this is a bug.
this.dcx().span_delayed_bug(span, "inconsistent resolution for a macro");
}
} else if this.tcx.dcx().has_errors().is_none() && this.privacy_errors.is_empty() {
// 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 no other error is reported elsewhere.
let err = this.dcx().create_err(CannotDetermineMacroResolution {
span,
kind: kind.descr(),
path: Segment::names_to_string(path),
});
err.stash(span, StashKey::UndeterminedMacroResolution);
}
};
let macro_resolutions = mem::take(&mut self.multi_segment_macro_resolutions);
2024-03-15 14:21:03 +03:00
for (mut path, path_span, kind, parent_scope, initial_res, ns) in macro_resolutions {
// FIXME: Path resolution will ICE if segment IDs present.
for seg in &mut path {
seg.id = None;
}
match self.resolve_path(
&path,
2024-03-15 14:21:03 +03:00
Some(ns),
&parent_scope,
Some(Finalize::new(ast::CRATE_NODE_ID, path_span)),
None,
2024-08-06 19:27:15 +08:00
None,
) {
PathResult::NonModule(path_res) if let Some(res) = path_res.full_res() => {
check_consistency(self, &path, path_span, kind, initial_res, res)
}
2024-03-15 14:21:03 +03:00
// This may be a trait for glob delegation expansions.
PathResult::Module(ModuleOrUniformRoot::Module(module)) => check_consistency(
self,
&path,
path_span,
kind,
initial_res,
module.res().unwrap(),
),
path_res @ (PathResult::NonModule(..) | PathResult::Failed { .. }) => {
let mut suggestion = None;
let (span, label, module, segment) =
if let PathResult::Failed { span, label, module, segment_name, .. } =
path_res
{
// try to suggest if it's not a macro, maybe a function
2022-10-18 01:58:22 +08:00
if let PathResult::NonModule(partial_res) =
2024-08-06 19:27:15 +08:00
self.maybe_resolve_path(&path, Some(ValueNS), &parent_scope, None)
2022-10-18 01:58:22 +08:00
&& partial_res.unresolved_segments() == 0
{
let sm = self.tcx.sess.source_map();
2022-10-18 01:58:22 +08:00
let exclamation_span = sm.next_point(span);
suggestion = Some((
vec![(exclamation_span, "".to_string())],
format!(
"{} is not a macro, but a {}, try to remove `!`",
Segment::names_to_string(&path),
partial_res.base_res().descr()
),
2022-10-18 01:58:22 +08:00
Applicability::MaybeIncorrect,
));
}
(span, label, module, segment_name)
} else {
(
path_span,
format!(
"partially resolved path in {} {}",
kind.article(),
kind.descr()
2019-12-22 17:42:04 -05:00
),
None,
path.last().map(|segment| segment.ident.name).unwrap(),
2019-12-22 17:42:04 -05:00
)
};
self.report_error(
span,
ResolutionError::FailedToResolve {
segment: Some(segment),
label,
suggestion,
module,
2019-01-16 15:30:41 -05:00
},
);
2016-11-27 10:58:46 +00:00
}
PathResult::Module(..) | PathResult::Indeterminate => unreachable!(),
2016-11-27 10:58:46 +00:00
}
}
let macro_resolutions = mem::take(&mut self.single_segment_macro_resolutions);
for (ident, kind, parent_scope, initial_binding) in macro_resolutions {
match self.early_resolve_ident_in_lexical_scope(
ident,
ScopeSet::Macro(kind),
&parent_scope,
Some(Finalize::new(ast::CRATE_NODE_ID, ident.span)),
true,
None,
2024-08-06 19:27:15 +08:00
None,
) {
Ok(binding) => {
let initial_res = initial_binding.map(|initial_binding| {
self.record_use(ident, initial_binding, Used::Other);
initial_binding.res()
});
let res = binding.res();
2018-11-18 14:41:06 +03:00
let seg = Segment::from_ident(ident);
check_consistency(self, &[seg], ident.span, kind, initial_res, res);
if res == Res::NonMacroAttr(NonMacroAttrKind::DeriveHelperCompat) {
let node_id = self
.invocation_parents
.get(&parent_scope.expansion)
Fix anon const def-creation when macros are involved Ever since #125915, some `ast::AnonConst`s turn into `hir::ConstArgKind::Path`s, which don't have associated `DefId`s. To deal with the fact that we don't have resolution information in `DefCollector`, we decided to implement a process where if the anon const *appeared* to be trivial (i.e., `N` or `{ N }`), we would avoid creating a def for it in `DefCollector`. If later, in AST lowering, we realized it turned out to be a unit struct literal, or we were lowering it to something that didn't use `hir::ConstArg`, we'd create its def there. However, let's say we have a macro `m!()` that expands to a reference to a free constant `FOO`. If we use `m!()` in the body of an anon const (e.g., `Foo<{ m!() }>`), then in def collection, it appears to be a nontrivial anon const and we create a def. But the macro expands to something that looks like a trivial const arg, but is not, so in AST lowering we "fix" the mistake we assumed def collection made and create a def for it. This causes a duplicate definition ICE. The ideal long-term fix for this is a bit unclear. One option is to delay def creation for all expression-like nodes until AST lowering (see #128844 for an incomplete attempt at this). This would avoid issues like this one that are caused by hacky workarounds. However, this approach has some downsides as well, and the best approach is yet to be determined. In the meantime, this PR fixes the bug by delaying def creation for anon consts whose bodies are macro invocations until after we expand the macro and know what is inside it. This is accomplished by adding information to create the anon const's def to the data in `Resolver.invocation_parents`.
2024-08-15 14:36:10 -07:00
.map_or(ast::CRATE_NODE_ID, |parent| {
self.def_id_to_node_id[parent.parent_def]
});
self.lint_buffer.buffer_lint(
LEGACY_DERIVE_HELPERS,
node_id,
ident.span,
BuiltinLintDiag::LegacyDeriveHelpers(binding.span),
);
}
}
Err(..) => {
let expected = kind.descr_expected();
let mut err = self.dcx().create_err(CannotFindIdentInThisScope {
span: ident.span,
expected,
ident,
});
self.unresolved_macro_suggestions(&mut err, kind, &parent_scope, ident, krate);
err.emit();
}
}
2016-10-31 22:17:15 +00:00
}
let builtin_attrs = mem::take(&mut self.builtin_attrs);
for (ident, parent_scope) in builtin_attrs {
let _ = self.early_resolve_ident_in_lexical_scope(
ident,
ScopeSet::Macro(MacroKind::Attr),
&parent_scope,
Some(Finalize::new(ast::CRATE_NODE_ID, ident.span)),
true,
None,
2024-08-06 19:27:15 +08:00
None,
);
}
}
fn check_stability_and_deprecation(
&mut self,
ext: &SyntaxExtension,
path: &ast::Path,
node_id: NodeId,
) {
let span = path.span;
if let Some(stability) = &ext.stability {
if let StabilityLevel::Unstable { reason, issue, is_soft, implied_by } = stability.level
{
let feature = stability.feature;
let is_allowed =
|feature| self.tcx.features().enabled(feature) || span.allows_unstable(feature);
let allowed_by_implication = implied_by.is_some_and(|feature| is_allowed(feature));
if !is_allowed(feature) && !allowed_by_implication {
let lint_buffer = &mut self.lint_buffer;
let soft_handler = |lint, span, msg: String| {
lint_buffer.buffer_lint(
lint,
node_id,
span,
2024-04-15 18:07:22 +00:00
BuiltinLintDiag::UnstableFeature(
// FIXME make this translatable
msg.into(),
),
)
};
stability::report_unstable(
self.tcx.sess,
feature,
reason.to_opt_reason(),
issue,
None,
is_soft,
span,
soft_handler,
stability::UnstableKind::Regular,
);
}
}
}
if let Some(depr) = &ext.deprecation {
let path = pprust::path_to_string(path);
2024-04-21 13:24:25 +00:00
stability::early_report_macro_deprecation(
&mut self.lint_buffer,
2024-04-21 13:24:25 +00:00
depr,
span,
node_id,
2024-04-21 13:24:25 +00:00
path,
);
}
}
fn prohibit_imported_non_macro_attrs(
&self,
binding: Option<NameBinding<'ra>>,
res: Option<Res>,
span: Span,
) {
if let Some(Res::NonMacroAttr(kind)) = res {
if kind != NonMacroAttrKind::Tool && binding.is_none_or(|b| b.is_import()) {
2024-03-24 21:53:18 +00:00
let binding_span = binding.map(|binding| binding.span);
self.dcx().emit_err(errors::CannotUseThroughAnImport {
span,
article: kind.article(),
descr: kind.descr(),
binding_span,
});
}
}
}
fn report_out_of_scope_macro_calls(
&mut self,
path: &ast::Path,
parent_scope: &ParentScope<'ra>,
invoc_in_mod_inert_attr: Option<(LocalDefId, NodeId)>,
binding: Option<NameBinding<'ra>>,
) {
if let Some((mod_def_id, node_id)) = invoc_in_mod_inert_attr
&& let Some(binding) = binding
// This is a `macro_rules` itself, not some import.
&& let NameBindingKind::Res(res) = binding.kind
&& let Res::Def(DefKind::Macro(MacroKind::Bang), def_id) = res
// And the `macro_rules` is defined inside the attribute's module,
// so it cannot be in scope unless imported.
&& self.tcx.is_descendant_of(def_id, mod_def_id.to_def_id())
{
// Try to resolve our ident ignoring `macro_rules` scopes.
// If such resolution is successful and gives the same result
// (e.g. if the macro is re-imported), then silence the lint.
let no_macro_rules = self.arenas.alloc_macro_rules_scope(MacroRulesScope::Empty);
let fallback_binding = self.early_resolve_ident_in_lexical_scope(
path.segments[0].ident,
ScopeSet::Macro(MacroKind::Bang),
&ParentScope { macro_rules: no_macro_rules, ..*parent_scope },
None,
false,
None,
2024-08-06 19:27:15 +08:00
None,
);
if fallback_binding.ok().and_then(|b| b.res().opt_def_id()) != Some(def_id) {
2025-02-19 18:52:53 +00:00
let location = match parent_scope.module.kind {
ModuleKind::Def(_, _, name) if name == kw::Empty => {
"the crate root".to_string()
}
ModuleKind::Def(kind, def_id, name) => {
format!("{} `{name}`", kind.descr(def_id))
}
ModuleKind::Block => "this scope".to_string(),
};
self.tcx.sess.psess.buffer_lint(
OUT_OF_SCOPE_MACRO_CALLS,
path.span,
node_id,
BuiltinLintDiag::OutOfScopeMacroCalls {
span: path.span,
path: pprust::path_to_string(path),
2025-02-19 18:52:53 +00:00
location,
},
);
}
}
}
pub(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 {
2022-06-15 00:31:21 +09:00
let macro_kind = self.get_macro(res).map(|macro_data| macro_data.ext.macro_kind());
if macro_kind.is_some() && sub_namespace_match(macro_kind, Some(MacroKind::Attr)) {
2024-03-24 21:53:18 +00:00
self.dcx()
.emit_err(errors::NameReservedInAttributeNamespace { span: ident.span, ident });
}
}
}
2022-04-17 04:01:17 +02:00
/// Compile the macro into a `SyntaxExtension` and its rule spans.
///
/// Possibly replace its expander to a pre-defined one for built-in macros.
pub(crate) fn compile_macro(
&mut self,
macro_def: &ast::MacroDef,
ident: Ident,
attrs: &[rustc_hir::Attribute],
span: Span,
node_id: NodeId,
edition: Edition,
) -> MacroData {
let (mut ext, mut rule_spans) = compile_declarative_macro(
self.tcx.sess,
self.tcx.features(),
macro_def,
ident,
attrs,
span,
node_id,
edition,
);
if let Some(builtin_name) = ext.builtin_name {
// The macro was marked with `#[rustc_builtin_macro]`.
if let Some(builtin_ext_kind) = self.builtin_macros.get(&builtin_name) {
// The macro is a built-in, replace its expander function
// while still taking everything else from the source code.
ext.kind = builtin_ext_kind.clone();
rule_spans = Vec::new();
} else {
self.dcx().emit_err(errors::CannotFindBuiltinMacroWithName { span, ident });
}
}
MacroData { ext: Arc::new(ext), rule_spans, macro_rules: macro_def.macro_rules }
}
fn path_accessible(
&mut self,
expn_id: LocalExpnId,
path: &ast::Path,
namespaces: &[Namespace],
) -> Result<bool, Indeterminate> {
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 namespaces {
2024-08-06 19:27:15 +08:00
match self.maybe_resolve_path(path, Some(*ns), &parent_scope, None) {
PathResult::Module(ModuleOrUniformRoot::Module(_)) => return Ok(true),
PathResult::NonModule(partial_res) if partial_res.unresolved_segments() == 0 => {
return Ok(true);
}
PathResult::NonModule(..) |
// HACK(Urgau): This shouldn't be necessary
PathResult::Failed { is_error_from_last_segment: false, .. } => {
self.dcx()
.emit_err(errors::CfgAccessibleUnsure { span });
// If we get a partially resolved NonModule in one namespace, we should get the
// same result in any other namespaces, so we can return early.
return Ok(false);
}
PathResult::Indeterminate => indeterminate = true,
// We can only be sure that a path doesn't exist after having tested all the
// possibilities, only at that time we can return false.
PathResult::Failed { .. } => {}
PathResult::Module(_) => panic!("unexpected path resolution"),
}
}
if indeterminate {
return Err(Indeterminate);
}
Ok(false)
}
}