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.
|
|
|
|
|
2024-01-27 19:09:55 +08:00
|
|
|
use std::cell::Cell;
|
2024-06-18 16:45:50 +03:00
|
|
|
use std::mem;
|
2025-02-03 06:44:41 +03:00
|
|
|
use std::sync::Arc;
|
2024-07-29 08:13:50 +10:00
|
|
|
|
2023-03-10 22:39:14 +01:00
|
|
|
use rustc_ast::expand::StrippedCfgItem;
|
2024-12-13 18:42:24 +01:00
|
|
|
use rustc_ast::{self as ast, Crate, NodeId, attr};
|
2020-01-11 17:02:46 +01:00
|
|
|
use rustc_ast_pretty::pprust;
|
2024-12-13 14:47:11 +01:00
|
|
|
use rustc_attr_parsing::StabilityLevel;
|
2022-02-04 14:26:29 +11:00
|
|
|
use rustc_data_structures::intern::Interned;
|
2024-03-24 15:11:27 +00:00
|
|
|
use rustc_errors::{Applicability, StashKey};
|
2024-04-25 15:13:53 +10:00
|
|
|
use rustc_expand::base::{
|
2024-12-13 18:42:24 +01:00
|
|
|
DeriveResolution, Indeterminate, ResolverExpand, SyntaxExtension, SyntaxExtensionKind,
|
2021-03-08 15:05:03 +03:00
|
|
|
};
|
2019-12-29 17:23:55 +03:00
|
|
|
use rustc_expand::compile_declarative_macro;
|
2024-06-18 16:45:50 +03:00
|
|
|
use rustc_expand::expand::{
|
|
|
|
AstFragment, AstFragmentKind, Invocation, InvocationKind, SupportsMacroExpansion,
|
|
|
|
};
|
2024-03-12 10:55:17 +08:00
|
|
|
use rustc_hir::def::{self, DefKind, Namespace, NonMacroAttrKind};
|
2023-08-22 19:14:32 +08:00
|
|
|
use rustc_hir::def_id::{CrateNum, DefId, LocalDefId};
|
2020-03-29 17:19:48 +02:00
|
|
|
use rustc_middle::middle::stability;
|
2023-08-22 19:14:32 +08:00
|
|
|
use rustc_middle::ty::{RegisteredTools, TyCtxt, Visibility};
|
2024-02-29 16:40:44 +11:00
|
|
|
use rustc_session::lint::BuiltinLintDiag;
|
2024-06-18 16:45:50 +03:00
|
|
|
use rustc_session::lint::builtin::{
|
2024-12-13 18:42:24 +01:00
|
|
|
LEGACY_DERIVE_HELPERS, OUT_OF_SCOPE_MACRO_CALLS, UNKNOWN_OR_MALFORMED_DIAGNOSTIC_ATTRIBUTES,
|
|
|
|
UNUSED_MACRO_RULES, UNUSED_MACROS,
|
2024-06-18 16:45:50 +03:00
|
|
|
};
|
2020-11-19 01:49:20 +03:00
|
|
|
use rustc_session::parse::feature_err;
|
2024-05-15 00:49:33 +02:00
|
|
|
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};
|
2024-12-13 10:29:23 +11:00
|
|
|
use rustc_span::{DUMMY_SP, Ident, Span, Symbol, kw, sym};
|
2024-07-29 08:13:50 +10:00
|
|
|
|
2020-11-06 16:11:21 +03:00
|
|
|
use crate::Namespace::*;
|
2024-01-27 19:09:55 +08:00
|
|
|
use crate::errors::{
|
2020-11-06 16:11:21 +03:00
|
|
|
self, AddAsNonDerive, CannotDetermineMacroResolution, CannotFindIdentInThisScope,
|
2024-01-27 19:09:55 +08:00
|
|
|
MacroExpectedFound, RemoveSurroundingDerive,
|
2024-07-29 08:13:50 +10:00
|
|
|
};
|
2024-08-06 19:27:15 +08:00
|
|
|
use crate::imports::Import;
|
2022-03-26 20:59:09 +01:00
|
|
|
use crate::{
|
2025-03-17 16:50:55 +01:00
|
|
|
BindingKey, DeriveData, Determinacy, Finalize, InvocationParent, MacroData, ModuleKind,
|
|
|
|
ModuleOrUniformRoot, NameBinding, NameBindingKind, ParentScope, PathResult, ResolutionError,
|
|
|
|
Resolver, ScopeSet, Segment, ToNameBinding, Used,
|
2022-03-26 20:59:09 +01:00
|
|
|
};
|
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)]
|
2024-09-10 16:19:40 +10:00
|
|
|
pub(crate) struct MacroRulesBinding<'ra> {
|
|
|
|
pub(crate) binding: NameBinding<'ra>,
|
2020-03-14 01:06:36 +03:00
|
|
|
/// `macro_rules` scope into which the `macro_rules` item was planted.
|
2024-09-10 16:19:40 +10:00
|
|
|
pub(crate) parent_macro_rules_scope: MacroRulesScopeRef<'ra>,
|
2022-05-20 19:51:09 -04:00
|
|
|
pub(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)]
|
2024-09-10 16:19:40 +10:00
|
|
|
pub(crate) enum MacroRulesScope<'ra> {
|
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.
|
2024-09-10 16:19:40 +10:00
|
|
|
Binding(&'ra MacroRulesBinding<'ra>),
|
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.
|
2024-09-10 16:19:40 +10:00
|
|
|
pub(crate) type MacroRulesScopeRef<'ra> = Interned<'ra, Cell<MacroRulesScope<'ra>>>;
|
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.
|
2022-05-20 19:51:09 -04:00
|
|
|
pub(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 {
|
2024-08-07 12:41:49 +02:00
|
|
|
if let [segment] = path.segments.as_slice() {
|
|
|
|
segment.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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-06 10:56:23 +00:00
|
|
|
pub(crate) fn registered_tools(tcx: TyCtxt<'_>, (): ()) -> RegisteredTools {
|
|
|
|
let mut registered_tools = RegisteredTools::default();
|
2023-03-14 16:53:04 +04:00
|
|
|
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() {
|
2019-11-03 20:28:20 +03:00
|
|
|
Some(ident) => {
|
2022-08-28 21:23:23 +09:00
|
|
|
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
|
|
|
}
|
2019-11-03 20:28:20 +03: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,
|
|
|
|
});
|
2019-11-03 20:28:20 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2024-05-18 10:20:05 +02:00
|
|
|
// 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];
|
2019-11-03 20:28:20 +03:00
|
|
|
registered_tools.extend(predefined_tools.iter().cloned().map(Ident::with_dummy_span));
|
2022-08-28 21:23:23 +09:00
|
|
|
registered_tools
|
2019-11-03 20:28:20 +03:00
|
|
|
}
|
|
|
|
|
2024-09-10 16:19:40 +10:00
|
|
|
impl<'ra, 'tcx> ResolverExpand for Resolver<'ra, 'tcx> {
|
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 {
|
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
|
|
|
}
|
|
|
|
|
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);
|
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);
|
|
|
|
}
|
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) {
|
2025-03-17 16:50:55 +01:00
|
|
|
if self.builtin_macros.insert(name, ext).is_some() {
|
2023-12-18 22:21:37 +11:00
|
|
|
self.dcx().bug(format!("built-in macro `{name}` was already registered"));
|
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,
|
2023-02-20 10:38:48 +00:00
|
|
|
self.tcx.sess.edition(),
|
2021-06-22 19:20:25 +02:00
|
|
|
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) {
|
2023-02-22 21:20:54 +04:00
|
|
|
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,
|
2025-02-03 06:44:41 +03:00
|
|
|
) -> Result<Arc<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
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
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)
|
|
|
|
}
|
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;
|
2024-06-18 16:45:50 +03:00
|
|
|
// 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, .. }| {
|
2024-06-26 13:00:51 +03:00
|
|
|
in_attr
|
|
|
|
&& invoc.fragment_kind == AstFragmentKind::Expr
|
2024-06-18 16:45:50 +03:00
|
|
|
&& self.tcx.def_kind(mod_def_id) == DefKind::Mod
|
2024-06-26 13:00:51 +03:00
|
|
|
})
|
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);
|
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,
|
2024-03-15 14:21:03 +03:00
|
|
|
deleg_impl,
|
2024-06-18 16:45:50 +03:00
|
|
|
looks_like_invoc_in_mod_inert_attr,
|
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();
|
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),
|
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
|
|
|
}
|
|
|
|
|
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);
|
2024-10-17 19:51:18 +02:00
|
|
|
if let Some(rules) = self.unused_macro_rules.get_mut(&did) {
|
|
|
|
rules.remove(&rule_i);
|
|
|
|
}
|
2022-04-17 04:01:17 +02:00
|
|
|
}
|
|
|
|
|
2019-10-25 09:15:33 -04:00
|
|
|
fn check_unused_macros(&mut self) {
|
2025-03-14 17:18:41 +03:00
|
|
|
#[allow(rustc::potential_query_instability)] // FIXME
|
2021-11-10 12:00:46 +01:00
|
|
|
for (_, &(node_id, ident)) in self.unused_macros.iter() {
|
2024-05-20 17:47:54 +00:00
|
|
|
self.lint_buffer.buffer_lint(
|
2021-11-10 12:00:46 +01:00
|
|
|
UNUSED_MACROS,
|
|
|
|
node_id,
|
|
|
|
ident.span,
|
2024-04-14 20:11:14 +00:00
|
|
|
BuiltinLintDiag::UnusedMacroDefinition(ident.name),
|
2021-11-10 12:00:46 +01:00
|
|
|
);
|
2017-05-11 10:26:07 +02:00
|
|
|
}
|
2024-10-17 19:51:18 +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() {
|
2024-10-17 19:51:18 +02:00
|
|
|
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
|
|
|
}
|
|
|
|
}
|
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,
|
2024-04-25 15:13:53 +10:00
|
|
|
derive_paths: &dyn Fn() -> Vec<DeriveResolution>,
|
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];
|
2024-04-25 15:13:53 +10:00
|
|
|
for (i, resolution) in entry.resolutions.iter_mut().enumerate() {
|
|
|
|
if resolution.exts.is_none() {
|
|
|
|
resolution.exts = Some(
|
2021-03-08 15:05:03 +03:00
|
|
|
match self.resolve_macro_path(
|
2024-04-25 15:13:53 +10:00
|
|
|
&resolution.path,
|
2021-03-08 15:05:03 +03:00
|
|
|
Some(MacroKind::Derive),
|
|
|
|
&parent_scope,
|
|
|
|
true,
|
|
|
|
force,
|
2024-08-06 19:27:15 +08:00
|
|
|
None,
|
2021-03-08 15:05:03 +03:00
|
|
|
) {
|
|
|
|
Ok((Some(ext), _)) => {
|
|
|
|
if !ext.helper_attrs.is_empty() {
|
2024-04-25 15:13:53 +10:00
|
|
|
let last_seg = resolution.path.segments.last().unwrap();
|
2021-03-08 15:05:03 +03:00
|
|
|
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);
|
2023-08-22 19:14:32 +08:00
|
|
|
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);
|
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(())
|
|
|
|
}
|
|
|
|
|
2024-04-25 15:13:53 +10:00
|
|
|
fn take_derive_resolutions(&mut self, expn_id: LocalExpnId) -> Option<Vec<DeriveResolution>> {
|
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> {
|
2024-03-12 10:55:17 +08:00
|
|
|
self.path_accessible(expn_id, path, &[TypeNS, ValueNS, MacroNS])
|
|
|
|
}
|
2020-03-10 00:56:20 +03:00
|
|
|
|
2024-03-12 10:55:17 +08:00
|
|
|
fn macro_accessible(
|
|
|
|
&mut self,
|
|
|
|
expn_id: LocalExpnId,
|
|
|
|
path: &ast::Path,
|
|
|
|
) -> Result<bool, Indeterminate> {
|
|
|
|
self.path_accessible(expn_id, path, &[MacroNS])
|
2020-03-10 00:56:20 +03:00
|
|
|
}
|
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 {
|
2023-02-20 10:38:48 +00:00
|
|
|
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)
|
|
|
|
}
|
2021-09-29 01:17:54 +03:00
|
|
|
|
2023-03-10 22:39:14 +01:00
|
|
|
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 });
|
|
|
|
}
|
|
|
|
|
2021-09-29 01:17:54 +03:00
|
|
|
fn registered_tools(&self) -> &RegisteredTools {
|
2023-11-21 20:07:32 +01:00
|
|
|
self.registered_tools
|
2021-09-29 01:17:54 +03:00
|
|
|
}
|
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)
|
|
|
|
}
|
2017-03-01 23:48:16 +00:00
|
|
|
}
|
|
|
|
|
2024-09-10 16:19:40 +10:00
|
|
|
impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
|
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,
|
2024-09-10 16:19:40 +10:00
|
|
|
parent_scope: &ParentScope<'ra>,
|
2020-06-09 20:59:43 +03:00
|
|
|
node_id: NodeId,
|
2018-09-13 01:41:07 +03:00
|
|
|
force: bool,
|
2024-03-15 14:21:03 +03:00
|
|
|
deleg_impl: Option<LocalDefId>,
|
2024-06-18 16:45:50 +03:00
|
|
|
invoc_in_mod_inert_attr: Option<LocalDefId>,
|
2025-02-03 06:44:41 +03:00
|
|
|
) -> 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,
|
2024-06-18 16:45:50 +03:00
|
|
|
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
|
|
|
) {
|
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
|
|
|
|
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));
|
|
|
|
}
|
|
|
|
|
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 {
|
2024-03-24 21:53:18 +00:00
|
|
|
self.dcx().emit_err(errors::GenericArgumentsInMacroPath { span: args.span() });
|
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") {
|
2024-03-24 21:53:18 +00:00
|
|
|
self.dcx().emit_err(errors::AttributesStartingWithRustcAreReserved {
|
|
|
|
span: segment.ident.span,
|
|
|
|
});
|
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) {
|
2023-12-18 22:21:37 +11:00
|
|
|
self.dcx().emit_err(errors::ProcMacroSameCrate {
|
2023-04-12 21:45:21 +01:00
|
|
|
span: path.span,
|
|
|
|
is_test: self.tcx.sess.is_test_crate(),
|
|
|
|
});
|
2019-07-03 01:44:04 +03:00
|
|
|
}
|
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);
|
2023-03-26 15:59:45 +01:00
|
|
|
|
2023-04-07 08:14:48 +01:00
|
|
|
let mut err = MacroExpectedFound {
|
|
|
|
span: path.span,
|
|
|
|
expected,
|
2024-03-24 21:53:18 +00:00
|
|
|
article,
|
2023-04-07 08:14:48 +01:00
|
|
|
found: res.descr(),
|
|
|
|
macro_path: &path_str,
|
2024-03-06 17:08:34 +11:00
|
|
|
remove_surrounding_derive: None,
|
|
|
|
add_as_non_derive: None,
|
2023-04-07 08:14:48 +01:00
|
|
|
};
|
2023-03-26 15:59:45 +01:00
|
|
|
|
2023-04-07 08:14:48 +01:00
|
|
|
// Suggest moving the macro out of the derive() if the macro isn't Derive
|
2023-04-06 18:02:52 +01:00
|
|
|
if !path.span.from_expansion()
|
|
|
|
&& kind == MacroKind::Derive
|
|
|
|
&& ext.macro_kind() != MacroKind::Derive
|
|
|
|
{
|
2023-04-07 08:14:48 +01:00
|
|
|
err.remove_surrounding_derive = Some(RemoveSurroundingDerive { span: path.span });
|
2023-04-07 08:44:19 +01:00
|
|
|
err.add_as_non_derive = Some(AddAsNonDerive { macro_path: &path_str });
|
2023-03-26 15:59:45 +01:00
|
|
|
}
|
|
|
|
|
2024-03-24 21:53:18 +00:00
|
|
|
self.dcx().emit_err(err);
|
2023-04-07 08:14:48 +01:00
|
|
|
|
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.
|
2024-10-09 09:01:57 +02:00
|
|
|
if res != Res::Err && inner_attr && !self.tcx.features().custom_inner_attributes() {
|
2024-04-14 20:11:14 +00:00
|
|
|
let is_macro = match res {
|
|
|
|
Res::Def(..) => true,
|
|
|
|
Res::NonMacroAttr(..) => false,
|
2020-11-12 23:42:42 +03:00
|
|
|
_ => unreachable!(),
|
|
|
|
};
|
2024-12-13 18:42:24 +01:00
|
|
|
let msg = if is_macro {
|
|
|
|
"inner macro attributes are unstable"
|
2020-11-12 23:42:42 +03:00
|
|
|
} else {
|
2024-12-13 18:42:24 +01:00
|
|
|
"custom inner attributes are unstable"
|
|
|
|
};
|
|
|
|
feature_err(&self.tcx.sess, sym::custom_inner_attributes, path.span, msg).emit();
|
2020-11-19 01:49:20 +03:00
|
|
|
}
|
|
|
|
|
2023-04-28 13:04:35 +02:00
|
|
|
if res == Res::NonMacroAttr(NonMacroAttrKind::Tool)
|
2024-05-15 00:49:33 +02:00
|
|
|
&& let [namespace, attribute, ..] = &*path.segments
|
|
|
|
&& namespace.ident.name == sym::diagnostic
|
2024-05-10 14:12:30 +02:00
|
|
|
&& !(attribute.ident.name == sym::on_unimplemented
|
2024-10-23 08:09:15 +02:00
|
|
|
|| attribute.ident.name == sym::do_not_recommend)
|
2023-04-28 13:04:35 +02:00
|
|
|
{
|
2024-05-15 00:49:33 +02:00
|
|
|
let distance =
|
|
|
|
edit_distance(attribute.ident.name.as_str(), sym::on_unimplemented.as_str(), 5);
|
|
|
|
|
2024-04-14 20:11:14 +00:00
|
|
|
let typo_name = distance.map(|_| sym::on_unimplemented);
|
|
|
|
|
2024-05-20 17:47:54 +00:00
|
|
|
self.tcx.sess.psess.buffer_lint(
|
2023-06-02 13:55:46 +02:00
|
|
|
UNKNOWN_OR_MALFORMED_DIAGNOSTIC_ATTRIBUTES,
|
2024-05-15 00:49:33 +02:00
|
|
|
attribute.span(),
|
2023-04-28 13:04:35 +02:00
|
|
|
node_id,
|
2024-04-14 20:11:14 +00:00
|
|
|
BuiltinLintDiag::UnknownDiagnosticAttribute { span: attribute.span(), typo_name },
|
2023-04-28 13:04:35 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2022-02-01 20:30:32 +08:00
|
|
|
pub(crate) 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>,
|
2024-09-10 16:19:40 +10:00
|
|
|
parent_scope: &ParentScope<'ra>,
|
2018-11-14 02:20:59 +03:00
|
|
|
trace: bool,
|
2018-09-13 01:41:07 +03:00
|
|
|
force: bool,
|
2024-09-10 16:19:40 +10:00
|
|
|
ignore_import: Option<Import<'ra>>,
|
2025-02-03 06:44:41 +03:00
|
|
|
) -> 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,
|
2024-06-18 16:45:50 +03:00
|
|
|
ast_path: &ast::Path,
|
2024-03-15 14:21:03 +03:00
|
|
|
kind: Option<MacroKind>,
|
2024-09-10 16:19:40 +10:00
|
|
|
parent_scope: &ParentScope<'ra>,
|
2024-03-15 14:21:03 +03:00
|
|
|
trace: bool,
|
|
|
|
force: bool,
|
|
|
|
deleg_impl: Option<LocalDefId>,
|
2024-06-18 16:45:50 +03:00
|
|
|
invoc_in_mod_inert_attr: Option<(LocalDefId, NodeId)>,
|
2024-09-10 16:19:40 +10:00
|
|
|
ignore_import: Option<Import<'ra>>,
|
2025-02-03 06:44:41 +03:00
|
|
|
) -> Result<(Option<Arc<SyntaxExtension>>, Res), Determinacy> {
|
2024-06-18 16:45:50 +03:00
|
|
|
let path_span = ast_path.span;
|
|
|
|
let mut path = Segment::from_path(ast_path);
|
2016-11-27 10:58:46 +00:00
|
|
|
|
2018-06-11 14:21:36 +03:00
|
|
|
// Possibly apply the macro helper hack
|
2024-03-15 14:21:03 +03:00
|
|
|
if deleg_impl.is_none()
|
|
|
|
&& kind == Some(MacroKind::Bang)
|
2024-08-07 12:41:49 +02:00
|
|
|
&& let [segment] = path.as_slice()
|
|
|
|
&& segment.ident.span.ctxt().outer_expn_data().local_inner_macros
|
2019-08-13 23:56:42 +03:00
|
|
|
{
|
2024-08-07 12:41:49 +02:00
|
|
|
let root = Ident::new(kw::DollarCrate, segment.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
|
|
|
}
|
|
|
|
|
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) {
|
2022-10-10 19:21:35 +04:00
|
|
|
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())
|
|
|
|
}
|
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(),
|
2024-03-15 14:21:03 +03:00
|
|
|
ns,
|
2019-08-13 01:39:10 +03:00
|
|
|
));
|
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 {
|
2023-05-29 23:36:06 +03:00
|
|
|
let scope_set = kind.map_or(ScopeSet::All(MacroNS), 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,
|
2024-08-06 19:27:15 +08: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);
|
2024-06-18 16:45:50 +03:00
|
|
|
self.report_out_of_scope_macro_calls(
|
|
|
|
ast_path,
|
|
|
|
parent_scope,
|
|
|
|
invoc_in_mod_inert_attr,
|
|
|
|
binding.ok(),
|
|
|
|
);
|
2019-04-20 19:36:05 +03:00
|
|
|
res
|
2019-07-03 23:59:03 +03:00
|
|
|
};
|
|
|
|
|
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();
|
2025-02-03 06:44:41 +03:00
|
|
|
Some(Arc::new(SyntaxExtension::glob_delegation(def_id, impl_def_id, edition)))
|
2024-03-15 14:21:03 +03:00
|
|
|
}
|
|
|
|
_ => None,
|
|
|
|
},
|
2025-02-03 06:44:41 +03:00
|
|
|
None => self.get_macro(res).map(|macro_data| Arc::clone(¯o_data.ext)),
|
2024-03-15 14:21:03 +03:00
|
|
|
};
|
|
|
|
Ok((ext, res))
|
2017-02-01 21:03:09 +10:30
|
|
|
}
|
2016-09-26 03:17:05 +00:00
|
|
|
|
2023-06-10 00:06:34 +08:00
|
|
|
pub(crate) fn finalize_macro_resolutions(&mut self, krate: &Crate) {
|
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.
|
2023-12-18 22:21:37 +11:00
|
|
|
this.dcx().span_delayed_bug(span, "inconsistent resolution for a macro");
|
2018-11-10 18:58:37 +03:00
|
|
|
}
|
2024-01-27 19:09:55 +08:00
|
|
|
} else if this.tcx.dcx().has_errors().is_none() && this.privacy_errors.is_empty() {
|
2018-11-10 18:58:37 +03:00
|
|
|
// 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
|
2024-01-27 19:09:55 +08:00
|
|
|
// 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);
|
2018-11-10 18:58:37 +03:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2019-08-12 21:52:37 +03:00
|
|
|
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 {
|
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,
|
2024-03-15 14:21:03 +03:00
|
|
|
Some(ns),
|
2019-08-05 21:18:50 +03:00
|
|
|
&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,
|
2024-08-06 19:27:15 +08:00
|
|
|
None,
|
2019-08-05 21:18:50 +03:00
|
|
|
) {
|
2022-10-10 19:21:35 +04:00
|
|
|
PathResult::NonModule(path_res) if let Some(res) = path_res.full_res() => {
|
|
|
|
check_consistency(self, &path, path_span, kind, initial_res, res)
|
2018-11-10 18:58:37 +03:00
|
|
|
}
|
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(),
|
|
|
|
),
|
2023-03-22 15:38:55 +01:00
|
|
|
path_res @ (PathResult::NonModule(..) | PathResult::Failed { .. }) => {
|
2022-10-17 05:54:13 +08:00
|
|
|
let mut suggestion = None;
|
2024-07-23 00:23:54 +00:00
|
|
|
let (span, label, module, segment) =
|
|
|
|
if let PathResult::Failed { span, label, module, segment_name, .. } =
|
|
|
|
path_res
|
|
|
|
{
|
2022-10-17 05:54:13 +08:00
|
|
|
// 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
|
|
|
|
{
|
2023-02-20 10:38:48 +00:00
|
|
|
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())],
|
2022-10-17 05:54:13 +08:00
|
|
|
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,
|
|
|
|
));
|
2022-10-17 05:54:13 +08:00
|
|
|
}
|
2024-07-23 00:23:54 +00:00
|
|
|
(span, label, module, segment_name)
|
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
|
|
|
),
|
2023-03-10 22:39:14 +01:00
|
|
|
None,
|
2024-07-23 00:23:54 +00:00
|
|
|
path.last().map(|segment| segment.ident.name).unwrap(),
|
2019-12-22 17:42:04 -05:00
|
|
|
)
|
2018-11-10 18:58:37 +03:00
|
|
|
};
|
2023-03-10 22:39:14 +01:00
|
|
|
self.report_error(
|
|
|
|
span,
|
|
|
|
ResolutionError::FailedToResolve {
|
2024-07-23 00:23:54 +00:00
|
|
|
segment: Some(segment),
|
2023-03-10 22:39:14 +01:00
|
|
|
label,
|
|
|
|
suggestion,
|
|
|
|
module,
|
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,
|
2024-08-06 19:27:15 +08: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| {
|
2023-11-10 10:11:24 +08:00
|
|
|
self.record_use(ident, initial_binding, Used::Other);
|
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)
|
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]
|
|
|
|
});
|
2024-05-20 17:47:54 +00:00
|
|
|
self.lint_buffer.buffer_lint(
|
2020-11-14 14:47:14 +03:00
|
|
|
LEGACY_DERIVE_HELPERS,
|
2021-07-14 18:24:12 -05:00
|
|
|
node_id,
|
2020-11-14 14:47:14 +03:00
|
|
|
ident.span,
|
2024-02-29 16:40:44 +11:00
|
|
|
BuiltinLintDiag::LegacyDeriveHelpers(binding.span),
|
2020-11-14 14:47:14 +03:00
|
|
|
);
|
|
|
|
}
|
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();
|
2023-06-18 12:15:17 +01:00
|
|
|
|
2023-12-18 22:21:37 +11:00
|
|
|
let mut err = self.dcx().create_err(CannotFindIdentInThisScope {
|
2023-06-18 12:15:17 +01:00
|
|
|
span: ident.span,
|
|
|
|
expected,
|
|
|
|
ident,
|
|
|
|
});
|
2023-06-10 00:06:34 +08:00
|
|
|
self.unresolved_macro_suggestions(&mut err, kind, &parent_scope, ident, krate);
|
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,
|
2024-08-06 19:27:15 +08: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 {
|
2022-07-13 13:10:37 +01:00
|
|
|
if let StabilityLevel::Unstable { reason, issue, is_soft, implied_by } = stability.level
|
|
|
|
{
|
2019-06-22 16:18:05 +03:00
|
|
|
let feature = stability.feature;
|
2022-07-13 13:10:37 +01:00
|
|
|
|
2024-10-08 14:06:56 +02:00
|
|
|
let is_allowed =
|
|
|
|
|feature| self.tcx.features().enabled(feature) || span.allows_unstable(feature);
|
2023-05-24 14:33:43 +00:00
|
|
|
let allowed_by_implication = implied_by.is_some_and(|feature| is_allowed(feature));
|
2022-07-13 13:10:37 +01:00
|
|
|
if !is_allowed(feature) && !allowed_by_implication {
|
2019-10-25 09:15:33 -04:00
|
|
|
let lint_buffer = &mut self.lint_buffer;
|
2024-04-14 20:11:14 +00:00
|
|
|
let soft_handler = |lint, span, msg: String| {
|
2024-05-20 17:47:54 +00:00
|
|
|
lint_buffer.buffer_lint(
|
2024-04-14 20:11:14 +00:00
|
|
|
lint,
|
|
|
|
node_id,
|
|
|
|
span,
|
2024-04-15 18:07:22 +00:00
|
|
|
BuiltinLintDiag::UnstableFeature(
|
|
|
|
// FIXME make this translatable
|
|
|
|
msg.into(),
|
|
|
|
),
|
2024-04-14 20:11:14 +00:00
|
|
|
)
|
|
|
|
};
|
2019-10-10 23:37:41 +03:00
|
|
|
stability::report_unstable(
|
2023-02-20 10:38:48 +00:00
|
|
|
self.tcx.sess,
|
2019-10-10 23:37:41 +03:00
|
|
|
feature,
|
2022-07-01 18:20:46 +03:00
|
|
|
reason.to_opt_reason(),
|
2019-10-10 23:37:41 +03:00
|
|
|
issue,
|
2021-11-13 16:39:54 +09:00
|
|
|
None,
|
2019-10-10 23:37:41 +03:00
|
|
|
is_soft,
|
|
|
|
span,
|
|
|
|
soft_handler,
|
2025-01-12 20:52:30 +00:00
|
|
|
stability::UnstableKind::Regular,
|
2019-10-10 23:37:41 +03:00
|
|
|
);
|
2019-06-22 02:44:45 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if let Some(depr) = &ext.deprecation {
|
2023-11-21 20:07:32 +01:00
|
|
|
let path = pprust::path_to_string(path);
|
2024-04-21 13:24:25 +00:00
|
|
|
stability::early_report_macro_deprecation(
|
2020-07-20 10:44:43 -04:00
|
|
|
&mut self.lint_buffer,
|
2024-04-21 13:24:25 +00:00
|
|
|
depr,
|
2020-07-20 10:44:43 -04:00
|
|
|
span,
|
2020-11-12 22:29:52 +03:00
|
|
|
node_id,
|
2024-04-21 13:24:25 +00:00
|
|
|
path,
|
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,
|
2024-09-10 16:19:40 +10:00
|
|
|
binding: Option<NameBinding<'ra>>,
|
2019-04-20 19:36:05 +03:00
|
|
|
res: Option<Res>,
|
|
|
|
span: Span,
|
|
|
|
) {
|
|
|
|
if let Some(Res::NonMacroAttr(kind)) = res {
|
2025-01-19 19:15:00 +00:00
|
|
|
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,
|
|
|
|
});
|
2018-12-12 04:11:46 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-06-18 16:45:50 +03:00
|
|
|
fn report_out_of_scope_macro_calls(
|
|
|
|
&mut self,
|
|
|
|
path: &ast::Path,
|
2024-09-10 16:19:40 +10:00
|
|
|
parent_scope: &ParentScope<'ra>,
|
2024-06-18 16:45:50 +03:00
|
|
|
invoc_in_mod_inert_attr: Option<(LocalDefId, NodeId)>,
|
2024-09-10 16:19:40 +10:00
|
|
|
binding: Option<NameBinding<'ra>>,
|
2024-06-18 16:45:50 +03:00
|
|
|
) {
|
|
|
|
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,
|
2024-06-18 16:45:50 +03:00
|
|
|
);
|
|
|
|
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 {
|
2024-07-23 00:23:54 +00:00
|
|
|
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(),
|
|
|
|
};
|
2024-06-18 16:45:50 +03:00
|
|
|
self.tcx.sess.psess.buffer_lint(
|
|
|
|
OUT_OF_SCOPE_MACRO_CALLS,
|
|
|
|
path.span,
|
|
|
|
node_id,
|
2024-07-23 00:23:54 +00:00
|
|
|
BuiltinLintDiag::OutOfScopeMacroCalls {
|
|
|
|
span: path.span,
|
|
|
|
path: pprust::path_to_string(path),
|
2025-02-19 18:52:53 +00:00
|
|
|
location,
|
2024-07-23 00:23:54 +00:00
|
|
|
},
|
2024-06-18 16:45:50 +03:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-20 19:51:09 -04:00
|
|
|
pub(crate) fn check_reserved_macro_name(&mut self, ident: Ident, res: Res) {
|
2019-06-30 01:24:34 +03:00
|
|
|
// 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 {
|
2022-06-15 00:31:21 +09:00
|
|
|
let macro_kind = self.get_macro(res).map(|macro_data| macro_data.ext.macro_kind());
|
2019-06-30 01:24:34 +03:00
|
|
|
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 });
|
2019-06-30 01:24:34 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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.
|
2024-10-26 18:51:15 +03:00
|
|
|
pub(crate) fn compile_macro(
|
|
|
|
&mut self,
|
|
|
|
macro_def: &ast::MacroDef,
|
|
|
|
ident: Ident,
|
2025-02-09 22:49:33 +01:00
|
|
|
attrs: &[rustc_hir::Attribute],
|
2024-10-26 18:51:15 +03:00
|
|
|
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,
|
|
|
|
);
|
2019-06-20 11:52:31 +03:00
|
|
|
|
2023-11-24 23:39:42 +03:00
|
|
|
if let Some(builtin_name) = ext.builtin_name {
|
2019-06-20 11:52:31 +03:00
|
|
|
// The macro was marked with `#[rustc_builtin_macro]`.
|
2025-03-17 16:50:55 +01:00
|
|
|
if let Some(builtin_ext_kind) = self.builtin_macros.get(&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.
|
2025-03-17 16:50:55 +01:00
|
|
|
ext.kind = builtin_ext_kind.clone();
|
|
|
|
rule_spans = Vec::new();
|
2019-06-20 11:52:31 +03:00
|
|
|
} else {
|
2024-10-26 18:51:15 +03:00
|
|
|
self.dcx().emit_err(errors::CannotFindBuiltinMacroWithName { span, ident });
|
2019-06-20 11:52:31 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2025-02-03 06:44:41 +03:00
|
|
|
MacroData { ext: Arc::new(ext), rule_spans, macro_rules: macro_def.macro_rules }
|
2019-06-20 11:52:31 +03:00
|
|
|
}
|
2024-03-12 10:55:17 +08:00
|
|
|
|
|
|
|
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) {
|
2024-03-12 10:55:17 +08:00
|
|
|
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)
|
|
|
|
}
|
2016-09-07 23:21:59 +00:00
|
|
|
}
|