mirror of
https://github.com/rust-lang/rust.git
synced 2025-01-31 09:04:18 +00:00
Move ident resolution to a dedicated module.
This commit is contained in:
parent
341883d051
commit
e9a52c27d2
1645
compiler/rustc_resolve/src/ident.rs
Normal file
1645
compiler/rustc_resolve/src/ident.rs
Normal file
File diff suppressed because it is too large
Load Diff
@ -2,12 +2,11 @@
|
||||
|
||||
use crate::diagnostics::Suggestion;
|
||||
use crate::Determinacy::{self, *};
|
||||
use crate::Namespace::{self, MacroNS, TypeNS};
|
||||
use crate::Namespace::{MacroNS, TypeNS};
|
||||
use crate::{module_to_string, names_to_string};
|
||||
use crate::{AmbiguityError, AmbiguityErrorMisc, AmbiguityKind};
|
||||
use crate::{BindingKey, ModuleKind, ResolutionError, Resolver, Segment};
|
||||
use crate::{Finalize, Module, ModuleOrUniformRoot, ParentScope, PerNS, ScopeSet, Weak};
|
||||
use crate::{NameBinding, NameBindingKind, PathResult, PrivacyError, ToNameBinding};
|
||||
use crate::{AmbiguityKind, BindingKey, ModuleKind, ResolutionError, Resolver, Segment};
|
||||
use crate::{Finalize, Module, ModuleOrUniformRoot, ParentScope, PerNS, ScopeSet};
|
||||
use crate::{NameBinding, NameBindingKind, PathResult};
|
||||
|
||||
use rustc_ast::NodeId;
|
||||
use rustc_data_structures::fx::FxHashSet;
|
||||
@ -125,15 +124,15 @@ impl<'a> Import<'a> {
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Default, Debug)]
|
||||
/// Records information about the resolution of a name in a namespace of a module.
|
||||
pub struct NameResolution<'a> {
|
||||
#[derive(Clone, Default, Debug)]
|
||||
crate struct NameResolution<'a> {
|
||||
/// Single imports that may define the name in the namespace.
|
||||
/// Imports are arena-allocated, so it's ok to use pointers as keys.
|
||||
single_imports: FxHashSet<Interned<'a, Import<'a>>>,
|
||||
pub single_imports: FxHashSet<Interned<'a, Import<'a>>>,
|
||||
/// The least shadowable known binding for this name, or None if there are no known bindings.
|
||||
pub binding: Option<&'a NameBinding<'a>>,
|
||||
shadowed_glob: Option<&'a NameBinding<'a>>,
|
||||
pub shadowed_glob: Option<&'a NameBinding<'a>>,
|
||||
}
|
||||
|
||||
impl<'a> NameResolution<'a> {
|
||||
@ -169,278 +168,6 @@ fn pub_use_of_private_extern_crate_hack(import: &Import<'_>, binding: &NameBindi
|
||||
}
|
||||
|
||||
impl<'a> Resolver<'a> {
|
||||
crate fn resolve_ident_in_module_unadjusted(
|
||||
&mut self,
|
||||
module: ModuleOrUniformRoot<'a>,
|
||||
ident: Ident,
|
||||
ns: Namespace,
|
||||
parent_scope: &ParentScope<'a>,
|
||||
finalize: Option<Span>,
|
||||
) -> Result<&'a NameBinding<'a>, Determinacy> {
|
||||
self.resolve_ident_in_module_unadjusted_ext(
|
||||
module,
|
||||
ident,
|
||||
ns,
|
||||
parent_scope,
|
||||
false,
|
||||
finalize,
|
||||
)
|
||||
.map_err(|(determinacy, _)| determinacy)
|
||||
}
|
||||
|
||||
/// Attempts to resolve `ident` in namespaces `ns` of `module`.
|
||||
/// Invariant: if `finalize` is `Some`, expansion and import resolution must be complete.
|
||||
crate fn resolve_ident_in_module_unadjusted_ext(
|
||||
&mut self,
|
||||
module: ModuleOrUniformRoot<'a>,
|
||||
ident: Ident,
|
||||
ns: Namespace,
|
||||
parent_scope: &ParentScope<'a>,
|
||||
restricted_shadowing: bool,
|
||||
finalize: Option<Span>,
|
||||
) -> Result<&'a NameBinding<'a>, (Determinacy, Weak)> {
|
||||
let module = match module {
|
||||
ModuleOrUniformRoot::Module(module) => module,
|
||||
ModuleOrUniformRoot::CrateRootAndExternPrelude => {
|
||||
assert!(!restricted_shadowing);
|
||||
let binding = self.early_resolve_ident_in_lexical_scope(
|
||||
ident,
|
||||
ScopeSet::AbsolutePath(ns),
|
||||
parent_scope,
|
||||
finalize,
|
||||
finalize.is_some(),
|
||||
);
|
||||
return binding.map_err(|determinacy| (determinacy, Weak::No));
|
||||
}
|
||||
ModuleOrUniformRoot::ExternPrelude => {
|
||||
assert!(!restricted_shadowing);
|
||||
return if ns != TypeNS {
|
||||
Err((Determined, Weak::No))
|
||||
} else if let Some(binding) = self.extern_prelude_get(ident, finalize.is_some()) {
|
||||
Ok(binding)
|
||||
} else if !self.graph_root.unexpanded_invocations.borrow().is_empty() {
|
||||
// Macro-expanded `extern crate` items can add names to extern prelude.
|
||||
Err((Undetermined, Weak::No))
|
||||
} else {
|
||||
Err((Determined, Weak::No))
|
||||
};
|
||||
}
|
||||
ModuleOrUniformRoot::CurrentScope => {
|
||||
assert!(!restricted_shadowing);
|
||||
if ns == TypeNS {
|
||||
if ident.name == kw::Crate || ident.name == kw::DollarCrate {
|
||||
let module = self.resolve_crate_root(ident);
|
||||
let binding =
|
||||
(module, ty::Visibility::Public, module.span, LocalExpnId::ROOT)
|
||||
.to_name_binding(self.arenas);
|
||||
return Ok(binding);
|
||||
} else if ident.name == kw::Super || ident.name == kw::SelfLower {
|
||||
// FIXME: Implement these with renaming requirements so that e.g.
|
||||
// `use super;` doesn't work, but `use super as name;` does.
|
||||
// Fall through here to get an error from `early_resolve_...`.
|
||||
}
|
||||
}
|
||||
|
||||
let scopes = ScopeSet::All(ns, true);
|
||||
let binding = self.early_resolve_ident_in_lexical_scope(
|
||||
ident,
|
||||
scopes,
|
||||
parent_scope,
|
||||
finalize,
|
||||
finalize.is_some(),
|
||||
);
|
||||
return binding.map_err(|determinacy| (determinacy, Weak::No));
|
||||
}
|
||||
};
|
||||
|
||||
let key = self.new_key(ident, ns);
|
||||
let resolution =
|
||||
self.resolution(module, key).try_borrow_mut().map_err(|_| (Determined, Weak::No))?; // This happens when there is a cycle of imports.
|
||||
|
||||
if let Some(binding) = resolution.binding && let Some(path_span) = finalize {
|
||||
if !restricted_shadowing && binding.expansion != LocalExpnId::ROOT {
|
||||
if let NameBindingKind::Res(_, true) = binding.kind {
|
||||
self.macro_expanded_macro_export_errors.insert((path_span, binding.span));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
let check_usable = |this: &mut Self, binding: &'a NameBinding<'a>| {
|
||||
if let Some(unusable_binding) = this.unusable_binding {
|
||||
if ptr::eq(binding, unusable_binding) {
|
||||
return Err((Determined, Weak::No));
|
||||
}
|
||||
}
|
||||
let usable = this.is_accessible_from(binding.vis, parent_scope.module);
|
||||
if usable { Ok(binding) } else { Err((Determined, Weak::No)) }
|
||||
};
|
||||
|
||||
if let Some(path_span) = finalize {
|
||||
return resolution
|
||||
.binding
|
||||
.and_then(|binding| {
|
||||
// If the primary binding is unusable, search further and return the shadowed glob
|
||||
// binding if it exists. What we really want here is having two separate scopes in
|
||||
// a module - one for non-globs and one for globs, but until that's done use this
|
||||
// hack to avoid inconsistent resolution ICEs during import validation.
|
||||
if let Some(unusable_binding) = self.unusable_binding {
|
||||
if ptr::eq(binding, unusable_binding) {
|
||||
return resolution.shadowed_glob;
|
||||
}
|
||||
}
|
||||
Some(binding)
|
||||
})
|
||||
.ok_or((Determined, Weak::No))
|
||||
.and_then(|binding| {
|
||||
if self.last_import_segment && check_usable(self, binding).is_err() {
|
||||
Err((Determined, Weak::No))
|
||||
} else {
|
||||
self.record_use(ident, binding, restricted_shadowing);
|
||||
|
||||
if let Some(shadowed_glob) = resolution.shadowed_glob {
|
||||
// Forbid expanded shadowing to avoid time travel.
|
||||
if restricted_shadowing
|
||||
&& binding.expansion != LocalExpnId::ROOT
|
||||
&& binding.res() != shadowed_glob.res()
|
||||
{
|
||||
self.ambiguity_errors.push(AmbiguityError {
|
||||
kind: AmbiguityKind::GlobVsExpanded,
|
||||
ident,
|
||||
b1: binding,
|
||||
b2: shadowed_glob,
|
||||
misc1: AmbiguityErrorMisc::None,
|
||||
misc2: AmbiguityErrorMisc::None,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
if !self.is_accessible_from(binding.vis, parent_scope.module) {
|
||||
self.privacy_errors.push(PrivacyError {
|
||||
ident,
|
||||
binding,
|
||||
dedup_span: path_span,
|
||||
});
|
||||
}
|
||||
|
||||
Ok(binding)
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
// Items and single imports are not shadowable, if we have one, then it's determined.
|
||||
if let Some(binding) = resolution.binding {
|
||||
if !binding.is_glob_import() {
|
||||
return check_usable(self, binding);
|
||||
}
|
||||
}
|
||||
|
||||
// --- From now on we either have a glob resolution or no resolution. ---
|
||||
|
||||
// Check if one of single imports can still define the name,
|
||||
// if it can then our result is not determined and can be invalidated.
|
||||
for single_import in &resolution.single_imports {
|
||||
if !self.is_accessible_from(single_import.vis.get(), parent_scope.module) {
|
||||
continue;
|
||||
}
|
||||
let Some(module) = single_import.imported_module.get() else {
|
||||
return Err((Undetermined, Weak::No));
|
||||
};
|
||||
let ImportKind::Single { source: ident, .. } = single_import.kind else {
|
||||
unreachable!();
|
||||
};
|
||||
match self.resolve_ident_in_module(module, ident, ns, &single_import.parent_scope, None)
|
||||
{
|
||||
Err(Determined) => continue,
|
||||
Ok(binding)
|
||||
if !self.is_accessible_from(binding.vis, single_import.parent_scope.module) =>
|
||||
{
|
||||
continue;
|
||||
}
|
||||
Ok(_) | Err(Undetermined) => return Err((Undetermined, Weak::No)),
|
||||
}
|
||||
}
|
||||
|
||||
// So we have a resolution that's from a glob import. This resolution is determined
|
||||
// if it cannot be shadowed by some new item/import expanded from a macro.
|
||||
// This happens either if there are no unexpanded macros, or expanded names cannot
|
||||
// shadow globs (that happens in macro namespace or with restricted shadowing).
|
||||
//
|
||||
// Additionally, any macro in any module can plant names in the root module if it creates
|
||||
// `macro_export` macros, so the root module effectively has unresolved invocations if any
|
||||
// module has unresolved invocations.
|
||||
// However, it causes resolution/expansion to stuck too often (#53144), so, to make
|
||||
// progress, we have to ignore those potential unresolved invocations from other modules
|
||||
// and prohibit access to macro-expanded `macro_export` macros instead (unless restricted
|
||||
// shadowing is enabled, see `macro_expanded_macro_export_errors`).
|
||||
let unexpanded_macros = !module.unexpanded_invocations.borrow().is_empty();
|
||||
if let Some(binding) = resolution.binding {
|
||||
if !unexpanded_macros || ns == MacroNS || restricted_shadowing {
|
||||
return check_usable(self, binding);
|
||||
} else {
|
||||
return Err((Undetermined, Weak::No));
|
||||
}
|
||||
}
|
||||
|
||||
// --- From now on we have no resolution. ---
|
||||
|
||||
// Now we are in situation when new item/import can appear only from a glob or a macro
|
||||
// expansion. With restricted shadowing names from globs and macro expansions cannot
|
||||
// shadow names from outer scopes, so we can freely fallback from module search to search
|
||||
// in outer scopes. For `early_resolve_ident_in_lexical_scope` to continue search in outer
|
||||
// scopes we return `Undetermined` with `Weak::Yes`.
|
||||
|
||||
// Check if one of unexpanded macros can still define the name,
|
||||
// if it can then our "no resolution" result is not determined and can be invalidated.
|
||||
if unexpanded_macros {
|
||||
return Err((Undetermined, Weak::Yes));
|
||||
}
|
||||
|
||||
// Check if one of glob imports can still define the name,
|
||||
// if it can then our "no resolution" result is not determined and can be invalidated.
|
||||
for glob_import in module.globs.borrow().iter() {
|
||||
if !self.is_accessible_from(glob_import.vis.get(), parent_scope.module) {
|
||||
continue;
|
||||
}
|
||||
let module = match glob_import.imported_module.get() {
|
||||
Some(ModuleOrUniformRoot::Module(module)) => module,
|
||||
Some(_) => continue,
|
||||
None => return Err((Undetermined, Weak::Yes)),
|
||||
};
|
||||
let tmp_parent_scope;
|
||||
let (mut adjusted_parent_scope, mut ident) =
|
||||
(parent_scope, ident.normalize_to_macros_2_0());
|
||||
match ident.span.glob_adjust(module.expansion, glob_import.span) {
|
||||
Some(Some(def)) => {
|
||||
tmp_parent_scope =
|
||||
ParentScope { module: self.expn_def_scope(def), ..*parent_scope };
|
||||
adjusted_parent_scope = &tmp_parent_scope;
|
||||
}
|
||||
Some(None) => {}
|
||||
None => continue,
|
||||
};
|
||||
let result = self.resolve_ident_in_module_unadjusted(
|
||||
ModuleOrUniformRoot::Module(module),
|
||||
ident,
|
||||
ns,
|
||||
adjusted_parent_scope,
|
||||
None,
|
||||
);
|
||||
|
||||
match result {
|
||||
Err(Determined) => continue,
|
||||
Ok(binding)
|
||||
if !self.is_accessible_from(binding.vis, glob_import.parent_scope.module) =>
|
||||
{
|
||||
continue;
|
||||
}
|
||||
Ok(_) | Err(Undetermined) => return Err((Undetermined, Weak::Yes)),
|
||||
}
|
||||
}
|
||||
|
||||
// No resolution and no one else can define the name - determinate error.
|
||||
Err((Determined, Weak::No))
|
||||
}
|
||||
|
||||
// Given a binding and an import that resolves to it,
|
||||
// return the corresponding binding defined by the import.
|
||||
crate fn import(
|
||||
|
File diff suppressed because it is too large
Load Diff
@ -3,9 +3,9 @@
|
||||
|
||||
use crate::imports::ImportResolver;
|
||||
use crate::Namespace::*;
|
||||
use crate::{AmbiguityError, AmbiguityErrorMisc, AmbiguityKind, BuiltinMacroState, Determinacy};
|
||||
use crate::{DeriveData, Finalize, ParentScope, ResolutionError, Resolver, Scope, ScopeSet, Weak};
|
||||
use crate::{ModuleKind, ModuleOrUniformRoot, NameBinding, PathResult, Segment, ToNameBinding};
|
||||
use crate::{BuiltinMacroState, Determinacy};
|
||||
use crate::{DeriveData, Finalize, ParentScope, ResolutionError, Resolver, ScopeSet};
|
||||
use crate::{ModuleKind, ModuleOrUniformRoot, NameBinding, PathResult, Segment};
|
||||
use rustc_ast::{self as ast, Inline, ItemKind, ModKind, NodeId};
|
||||
use rustc_ast_lowering::ResolverAstLowering;
|
||||
use rustc_ast_pretty::pprust;
|
||||
@ -18,14 +18,11 @@ use rustc_expand::base::{Annotatable, DeriveResolutions, Indeterminate, Resolver
|
||||
use rustc_expand::base::{SyntaxExtension, SyntaxExtensionKind};
|
||||
use rustc_expand::compile_declarative_macro;
|
||||
use rustc_expand::expand::{AstFragment, Invocation, InvocationKind, SupportsMacroExpansion};
|
||||
use rustc_feature::is_builtin_attr_name;
|
||||
use rustc_hir::def::{self, DefKind, NonMacroAttrKind};
|
||||
use rustc_hir::def_id::{CrateNum, LocalDefId};
|
||||
use rustc_hir::PrimTy;
|
||||
use rustc_middle::middle::stability;
|
||||
use rustc_middle::ty::{self, RegisteredTools};
|
||||
use rustc_session::lint::builtin::{LEGACY_DERIVE_HELPERS, PROC_MACRO_DERIVE_RESOLUTION_FALLBACK};
|
||||
use rustc_session::lint::builtin::{SOFT_UNSTABLE, UNUSED_MACROS};
|
||||
use rustc_middle::ty::RegisteredTools;
|
||||
use rustc_session::lint::builtin::{LEGACY_DERIVE_HELPERS, SOFT_UNSTABLE, UNUSED_MACROS};
|
||||
use rustc_session::lint::BuiltinLintDiagnostics;
|
||||
use rustc_session::parse::feature_err;
|
||||
use rustc_session::Session;
|
||||
@ -35,7 +32,7 @@ use rustc_span::hygiene::{AstPass, MacroKind};
|
||||
use rustc_span::symbol::{kw, sym, Ident, Symbol};
|
||||
use rustc_span::{Span, DUMMY_SP};
|
||||
use std::cell::Cell;
|
||||
use std::{mem, ptr};
|
||||
use std::mem;
|
||||
|
||||
type Res = def::Res<NodeId>;
|
||||
|
||||
@ -73,10 +70,10 @@ pub enum MacroRulesScope<'a> {
|
||||
/// in a module (including derives) and hurt performance.
|
||||
pub(crate) type MacroRulesScopeRef<'a> = Interned<'a, Cell<MacroRulesScope<'a>>>;
|
||||
|
||||
// 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.
|
||||
fn sub_namespace_match(candidate: Option<MacroKind>, requirement: Option<MacroKind>) -> bool {
|
||||
/// Macro namespace is separated into two sub-namespaces, one for bang macros and
|
||||
/// one for attribute-like macros (attributes, derives).
|
||||
/// We ignore resolutions from one sub-namespace when searching names in scope for another.
|
||||
crate fn sub_namespace_match(candidate: Option<MacroKind>, requirement: Option<MacroKind>) -> bool {
|
||||
#[derive(PartialEq)]
|
||||
enum SubNS {
|
||||
Bang,
|
||||
@ -630,355 +627,6 @@ impl<'a> Resolver<'a> {
|
||||
res.map(|res| (self.get_macro(res), res))
|
||||
}
|
||||
|
||||
// Resolve an identifier in lexical scope.
|
||||
// This is a variation of `fn resolve_ident_in_lexical_scope` that can be run during
|
||||
// expansion and import resolution (perhaps they can be merged in the future).
|
||||
// The function is used for resolving initial segments of macro paths (e.g., `foo` in
|
||||
// `foo::bar!(); or `foo!();`) and also for import paths on 2018 edition.
|
||||
crate fn early_resolve_ident_in_lexical_scope(
|
||||
&mut self,
|
||||
orig_ident: Ident,
|
||||
scope_set: ScopeSet<'a>,
|
||||
parent_scope: &ParentScope<'a>,
|
||||
finalize: Option<Span>,
|
||||
force: bool,
|
||||
) -> Result<&'a NameBinding<'a>, Determinacy> {
|
||||
bitflags::bitflags! {
|
||||
struct Flags: u8 {
|
||||
const MACRO_RULES = 1 << 0;
|
||||
const MODULE = 1 << 1;
|
||||
const MISC_SUGGEST_CRATE = 1 << 2;
|
||||
const MISC_SUGGEST_SELF = 1 << 3;
|
||||
const MISC_FROM_PRELUDE = 1 << 4;
|
||||
}
|
||||
}
|
||||
|
||||
assert!(force || !finalize.is_some()); // `finalize` implies `force`
|
||||
|
||||
// Make sure `self`, `super` etc produce an error when passed to here.
|
||||
if orig_ident.is_path_segment_keyword() {
|
||||
return Err(Determinacy::Determined);
|
||||
}
|
||||
|
||||
let (ns, macro_kind, is_import) = match scope_set {
|
||||
ScopeSet::All(ns, is_import) => (ns, None, is_import),
|
||||
ScopeSet::AbsolutePath(ns) => (ns, None, false),
|
||||
ScopeSet::Macro(macro_kind) => (MacroNS, Some(macro_kind), false),
|
||||
ScopeSet::Late(ns, ..) => (ns, None, false),
|
||||
};
|
||||
|
||||
// This is *the* result, resolution from the scope closest to the resolved identifier.
|
||||
// However, sometimes this result is "weak" because it comes from a glob import or
|
||||
// a macro expansion, and in this case it cannot shadow names from outer scopes, e.g.
|
||||
// mod m { ... } // solution in outer scope
|
||||
// {
|
||||
// use prefix::*; // imports another `m` - innermost solution
|
||||
// // weak, cannot shadow the outer `m`, need to report ambiguity error
|
||||
// m::mac!();
|
||||
// }
|
||||
// So we have to save the innermost solution and continue searching in outer scopes
|
||||
// to detect potential ambiguities.
|
||||
let mut innermost_result: Option<(&NameBinding<'_>, Flags)> = None;
|
||||
let mut determinacy = Determinacy::Determined;
|
||||
|
||||
// Go through all the scopes and try to resolve the name.
|
||||
let break_result = self.visit_scopes(
|
||||
scope_set,
|
||||
parent_scope,
|
||||
orig_ident.span.ctxt(),
|
||||
|this, scope, use_prelude, ctxt| {
|
||||
let ident = Ident::new(orig_ident.name, orig_ident.span.with_ctxt(ctxt));
|
||||
let ok = |res, span, arenas| {
|
||||
Ok((
|
||||
(res, ty::Visibility::Public, span, LocalExpnId::ROOT)
|
||||
.to_name_binding(arenas),
|
||||
Flags::empty(),
|
||||
))
|
||||
};
|
||||
let result = match scope {
|
||||
Scope::DeriveHelpers(expn_id) => {
|
||||
if let Some(attr) = this
|
||||
.helper_attrs
|
||||
.get(&expn_id)
|
||||
.and_then(|attrs| attrs.iter().rfind(|i| ident == **i))
|
||||
{
|
||||
let binding = (
|
||||
Res::NonMacroAttr(NonMacroAttrKind::DeriveHelper),
|
||||
ty::Visibility::Public,
|
||||
attr.span,
|
||||
expn_id,
|
||||
)
|
||||
.to_name_binding(this.arenas);
|
||||
Ok((binding, Flags::empty()))
|
||||
} else {
|
||||
Err(Determinacy::Determined)
|
||||
}
|
||||
}
|
||||
Scope::DeriveHelpersCompat => {
|
||||
let mut result = Err(Determinacy::Determined);
|
||||
for derive in parent_scope.derives {
|
||||
let parent_scope = &ParentScope { derives: &[], ..*parent_scope };
|
||||
match this.resolve_macro_path(
|
||||
derive,
|
||||
Some(MacroKind::Derive),
|
||||
parent_scope,
|
||||
true,
|
||||
force,
|
||||
) {
|
||||
Ok((Some(ext), _)) => {
|
||||
if ext.helper_attrs.contains(&ident.name) {
|
||||
result = ok(
|
||||
Res::NonMacroAttr(NonMacroAttrKind::DeriveHelperCompat),
|
||||
derive.span,
|
||||
this.arenas,
|
||||
);
|
||||
break;
|
||||
}
|
||||
}
|
||||
Ok(_) | Err(Determinacy::Determined) => {}
|
||||
Err(Determinacy::Undetermined) => {
|
||||
result = Err(Determinacy::Undetermined)
|
||||
}
|
||||
}
|
||||
}
|
||||
result
|
||||
}
|
||||
Scope::MacroRules(macro_rules_scope) => match macro_rules_scope.get() {
|
||||
MacroRulesScope::Binding(macro_rules_binding)
|
||||
if ident == macro_rules_binding.ident =>
|
||||
{
|
||||
Ok((macro_rules_binding.binding, Flags::MACRO_RULES))
|
||||
}
|
||||
MacroRulesScope::Invocation(_) => Err(Determinacy::Undetermined),
|
||||
_ => Err(Determinacy::Determined),
|
||||
},
|
||||
Scope::CrateRoot => {
|
||||
let root_ident = Ident::new(kw::PathRoot, ident.span);
|
||||
let root_module = this.resolve_crate_root(root_ident);
|
||||
let binding = this.resolve_ident_in_module_ext(
|
||||
ModuleOrUniformRoot::Module(root_module),
|
||||
ident,
|
||||
ns,
|
||||
parent_scope,
|
||||
finalize,
|
||||
);
|
||||
match binding {
|
||||
Ok(binding) => Ok((binding, Flags::MODULE | Flags::MISC_SUGGEST_CRATE)),
|
||||
Err((Determinacy::Undetermined, Weak::No)) => {
|
||||
return Some(Err(Determinacy::determined(force)));
|
||||
}
|
||||
Err((Determinacy::Undetermined, Weak::Yes)) => {
|
||||
Err(Determinacy::Undetermined)
|
||||
}
|
||||
Err((Determinacy::Determined, _)) => Err(Determinacy::Determined),
|
||||
}
|
||||
}
|
||||
Scope::Module(module, derive_fallback_lint_id) => {
|
||||
let adjusted_parent_scope = &ParentScope { module, ..*parent_scope };
|
||||
let binding = this.resolve_ident_in_module_unadjusted_ext(
|
||||
ModuleOrUniformRoot::Module(module),
|
||||
ident,
|
||||
ns,
|
||||
adjusted_parent_scope,
|
||||
!matches!(scope_set, ScopeSet::Late(..)),
|
||||
finalize,
|
||||
);
|
||||
match binding {
|
||||
Ok(binding) => {
|
||||
if let Some(lint_id) = derive_fallback_lint_id {
|
||||
this.lint_buffer.buffer_lint_with_diagnostic(
|
||||
PROC_MACRO_DERIVE_RESOLUTION_FALLBACK,
|
||||
lint_id,
|
||||
orig_ident.span,
|
||||
&format!(
|
||||
"cannot find {} `{}` in this scope",
|
||||
ns.descr(),
|
||||
ident
|
||||
),
|
||||
BuiltinLintDiagnostics::ProcMacroDeriveResolutionFallback(
|
||||
orig_ident.span,
|
||||
),
|
||||
);
|
||||
}
|
||||
let misc_flags = if ptr::eq(module, this.graph_root) {
|
||||
Flags::MISC_SUGGEST_CRATE
|
||||
} else if module.is_normal() {
|
||||
Flags::MISC_SUGGEST_SELF
|
||||
} else {
|
||||
Flags::empty()
|
||||
};
|
||||
Ok((binding, Flags::MODULE | misc_flags))
|
||||
}
|
||||
Err((Determinacy::Undetermined, Weak::No)) => {
|
||||
return Some(Err(Determinacy::determined(force)));
|
||||
}
|
||||
Err((Determinacy::Undetermined, Weak::Yes)) => {
|
||||
Err(Determinacy::Undetermined)
|
||||
}
|
||||
Err((Determinacy::Determined, _)) => Err(Determinacy::Determined),
|
||||
}
|
||||
}
|
||||
Scope::RegisteredAttrs => match this.registered_attrs.get(&ident).cloned() {
|
||||
Some(ident) => ok(
|
||||
Res::NonMacroAttr(NonMacroAttrKind::Registered),
|
||||
ident.span,
|
||||
this.arenas,
|
||||
),
|
||||
None => Err(Determinacy::Determined),
|
||||
},
|
||||
Scope::MacroUsePrelude => {
|
||||
match this.macro_use_prelude.get(&ident.name).cloned() {
|
||||
Some(binding) => Ok((binding, Flags::MISC_FROM_PRELUDE)),
|
||||
None => Err(Determinacy::determined(
|
||||
this.graph_root.unexpanded_invocations.borrow().is_empty(),
|
||||
)),
|
||||
}
|
||||
}
|
||||
Scope::BuiltinAttrs => {
|
||||
if is_builtin_attr_name(ident.name) {
|
||||
ok(
|
||||
Res::NonMacroAttr(NonMacroAttrKind::Builtin(ident.name)),
|
||||
DUMMY_SP,
|
||||
this.arenas,
|
||||
)
|
||||
} else {
|
||||
Err(Determinacy::Determined)
|
||||
}
|
||||
}
|
||||
Scope::ExternPrelude => {
|
||||
match this.extern_prelude_get(ident, finalize.is_some()) {
|
||||
Some(binding) => Ok((binding, Flags::empty())),
|
||||
None => Err(Determinacy::determined(
|
||||
this.graph_root.unexpanded_invocations.borrow().is_empty(),
|
||||
)),
|
||||
}
|
||||
}
|
||||
Scope::ToolPrelude => match this.registered_tools.get(&ident).cloned() {
|
||||
Some(ident) => ok(Res::ToolMod, ident.span, this.arenas),
|
||||
None => Err(Determinacy::Determined),
|
||||
},
|
||||
Scope::StdLibPrelude => {
|
||||
let mut result = Err(Determinacy::Determined);
|
||||
if let Some(prelude) = this.prelude {
|
||||
if let Ok(binding) = this.resolve_ident_in_module_unadjusted(
|
||||
ModuleOrUniformRoot::Module(prelude),
|
||||
ident,
|
||||
ns,
|
||||
parent_scope,
|
||||
None,
|
||||
) {
|
||||
if use_prelude || this.is_builtin_macro(binding.res()) {
|
||||
result = Ok((binding, Flags::MISC_FROM_PRELUDE));
|
||||
}
|
||||
}
|
||||
}
|
||||
result
|
||||
}
|
||||
Scope::BuiltinTypes => match PrimTy::from_name(ident.name) {
|
||||
Some(prim_ty) => ok(Res::PrimTy(prim_ty), DUMMY_SP, this.arenas),
|
||||
None => Err(Determinacy::Determined),
|
||||
},
|
||||
};
|
||||
|
||||
match result {
|
||||
Ok((binding, flags))
|
||||
if sub_namespace_match(binding.macro_kind(), macro_kind) =>
|
||||
{
|
||||
if finalize.is_none() || matches!(scope_set, ScopeSet::Late(..)) {
|
||||
return Some(Ok(binding));
|
||||
}
|
||||
|
||||
if let Some((innermost_binding, innermost_flags)) = innermost_result {
|
||||
// Found another solution, if the first one was "weak", report an error.
|
||||
let (res, innermost_res) = (binding.res(), innermost_binding.res());
|
||||
if res != innermost_res {
|
||||
let is_builtin = |res| {
|
||||
matches!(res, Res::NonMacroAttr(NonMacroAttrKind::Builtin(..)))
|
||||
};
|
||||
let derive_helper =
|
||||
Res::NonMacroAttr(NonMacroAttrKind::DeriveHelper);
|
||||
let derive_helper_compat =
|
||||
Res::NonMacroAttr(NonMacroAttrKind::DeriveHelperCompat);
|
||||
|
||||
let ambiguity_error_kind = if is_import {
|
||||
Some(AmbiguityKind::Import)
|
||||
} else if is_builtin(innermost_res) || is_builtin(res) {
|
||||
Some(AmbiguityKind::BuiltinAttr)
|
||||
} else if innermost_res == derive_helper_compat
|
||||
|| res == derive_helper_compat && innermost_res != derive_helper
|
||||
{
|
||||
Some(AmbiguityKind::DeriveHelper)
|
||||
} else if innermost_flags.contains(Flags::MACRO_RULES)
|
||||
&& flags.contains(Flags::MODULE)
|
||||
&& !this.disambiguate_macro_rules_vs_modularized(
|
||||
innermost_binding,
|
||||
binding,
|
||||
)
|
||||
|| flags.contains(Flags::MACRO_RULES)
|
||||
&& innermost_flags.contains(Flags::MODULE)
|
||||
&& !this.disambiguate_macro_rules_vs_modularized(
|
||||
binding,
|
||||
innermost_binding,
|
||||
)
|
||||
{
|
||||
Some(AmbiguityKind::MacroRulesVsModularized)
|
||||
} else if innermost_binding.is_glob_import() {
|
||||
Some(AmbiguityKind::GlobVsOuter)
|
||||
} else if innermost_binding
|
||||
.may_appear_after(parent_scope.expansion, binding)
|
||||
{
|
||||
Some(AmbiguityKind::MoreExpandedVsOuter)
|
||||
} else {
|
||||
None
|
||||
};
|
||||
if let Some(kind) = ambiguity_error_kind {
|
||||
let misc = |f: Flags| {
|
||||
if f.contains(Flags::MISC_SUGGEST_CRATE) {
|
||||
AmbiguityErrorMisc::SuggestCrate
|
||||
} else if f.contains(Flags::MISC_SUGGEST_SELF) {
|
||||
AmbiguityErrorMisc::SuggestSelf
|
||||
} else if f.contains(Flags::MISC_FROM_PRELUDE) {
|
||||
AmbiguityErrorMisc::FromPrelude
|
||||
} else {
|
||||
AmbiguityErrorMisc::None
|
||||
}
|
||||
};
|
||||
this.ambiguity_errors.push(AmbiguityError {
|
||||
kind,
|
||||
ident: orig_ident,
|
||||
b1: innermost_binding,
|
||||
b2: binding,
|
||||
misc1: misc(innermost_flags),
|
||||
misc2: misc(flags),
|
||||
});
|
||||
return Some(Ok(innermost_binding));
|
||||
}
|
||||
}
|
||||
} else {
|
||||
// Found the first solution.
|
||||
innermost_result = Some((binding, flags));
|
||||
}
|
||||
}
|
||||
Ok(..) | Err(Determinacy::Determined) => {}
|
||||
Err(Determinacy::Undetermined) => determinacy = Determinacy::Undetermined,
|
||||
}
|
||||
|
||||
None
|
||||
},
|
||||
);
|
||||
|
||||
if let Some(break_result) = break_result {
|
||||
return break_result;
|
||||
}
|
||||
|
||||
// The first found solution was the only one, return it.
|
||||
if let Some((binding, _)) = innermost_result {
|
||||
return Ok(binding);
|
||||
}
|
||||
|
||||
Err(Determinacy::determined(determinacy == Determinacy::Determined || force))
|
||||
}
|
||||
|
||||
crate fn finalize_macro_resolutions(&mut self) {
|
||||
let check_consistency = |this: &mut Self,
|
||||
path: &[Segment],
|
||||
|
Loading…
Reference in New Issue
Block a user