Rollup merge of #95405 - cjgillot:probe, r=petrochenkov
Move name resolution logic to a dedicated file The code resolution logic from an Ident is scattered between several files. The first commits creates `rustc_resolve::probe` module to hold the different mutually recursive functions together. Just a move, no code change. The following commits attempt to make the logic a bit more readable. The two fields `last_import_segment` and `unusable_binding` are replaced by function parameters. In order to manage the fallout, `maybe_` variants of the function are added, dedicated to speculative resolution. r? `@petrochenkov`
This commit is contained in:
commit
2743c13de0
8 changed files with 2337 additions and 2191 deletions
|
@ -297,6 +297,7 @@ impl<'a, 'b> BuildReducedGraphVisitor<'a, 'b> {
|
|||
Some(TypeNS),
|
||||
parent_scope,
|
||||
if finalize { Finalize::SimplePath(id, path.span) } else { Finalize::No },
|
||||
None,
|
||||
) {
|
||||
PathResult::Module(ModuleOrUniformRoot::Module(module)) => {
|
||||
let res = module.res().expect("visibility resolved to unnamed block");
|
||||
|
@ -1124,12 +1125,11 @@ impl<'a, 'b> BuildReducedGraphVisitor<'a, 'b> {
|
|||
});
|
||||
} else {
|
||||
for ident in single_imports.iter().cloned() {
|
||||
let result = self.r.resolve_ident_in_module(
|
||||
let result = self.r.maybe_resolve_ident_in_module(
|
||||
ModuleOrUniformRoot::Module(module),
|
||||
ident,
|
||||
MacroNS,
|
||||
&self.parent_scope,
|
||||
None,
|
||||
);
|
||||
if let Ok(binding) = result {
|
||||
let import = macro_use_import(self, ident.span);
|
||||
|
|
|
@ -1,19 +1,24 @@
|
|||
use std::ptr;
|
||||
|
||||
use rustc_ast::{self as ast, Path};
|
||||
use rustc_ast::ptr::P;
|
||||
use rustc_ast::visit::{self, Visitor};
|
||||
use rustc_ast::{self as ast, Crate, ItemKind, ModKind, NodeId, Path, CRATE_NODE_ID};
|
||||
use rustc_ast_pretty::pprust;
|
||||
use rustc_data_structures::fx::FxHashSet;
|
||||
use rustc_errors::{
|
||||
struct_span_err, Applicability, Diagnostic, DiagnosticBuilder, ErrorGuaranteed, MultiSpan,
|
||||
};
|
||||
use rustc_errors::struct_span_err;
|
||||
use rustc_errors::{Applicability, Diagnostic, DiagnosticBuilder, ErrorGuaranteed, MultiSpan};
|
||||
use rustc_feature::BUILTIN_ATTRIBUTES;
|
||||
use rustc_hir::def::Namespace::{self, *};
|
||||
use rustc_hir::def::{self, CtorKind, CtorOf, DefKind, NonMacroAttrKind};
|
||||
use rustc_hir::def::{self, CtorKind, CtorOf, DefKind, NonMacroAttrKind, PerNS};
|
||||
use rustc_hir::def_id::{DefId, CRATE_DEF_INDEX, LOCAL_CRATE};
|
||||
use rustc_hir::PrimTy;
|
||||
use rustc_middle::bug;
|
||||
use rustc_middle::ty::DefIdTree;
|
||||
use rustc_session::lint::builtin::ABSOLUTE_PATHS_NOT_STARTING_WITH_CRATE;
|
||||
use rustc_session::lint::builtin::MACRO_EXPANDED_MACRO_EXPORTS_ACCESSED_BY_ABSOLUTE_PATHS;
|
||||
use rustc_session::lint::BuiltinLintDiagnostics;
|
||||
use rustc_session::Session;
|
||||
use rustc_span::edition::Edition;
|
||||
use rustc_span::hygiene::MacroKind;
|
||||
use rustc_span::lev_distance::find_best_match_for_name;
|
||||
use rustc_span::source_map::SourceMap;
|
||||
|
@ -22,11 +27,13 @@ use rustc_span::{BytePos, Span};
|
|||
use tracing::debug;
|
||||
|
||||
use crate::imports::{Import, ImportKind, ImportResolver};
|
||||
use crate::late::Rib;
|
||||
use crate::path_names_to_string;
|
||||
use crate::{AmbiguityError, AmbiguityErrorMisc, AmbiguityKind};
|
||||
use crate::{BindingError, HasGenericParams, MacroRulesScope, Module, ModuleOrUniformRoot};
|
||||
use crate::{Finalize, NameBinding, NameBindingKind, PrivacyError, VisResolutionError};
|
||||
use crate::{ParentScope, PathResult, ResolutionError, Resolver, Scope, ScopeSet, Segment};
|
||||
use crate::{AmbiguityError, AmbiguityErrorMisc, AmbiguityKind, BindingError, Finalize};
|
||||
use crate::{HasGenericParams, MacroRulesScope, Module, ModuleKind, ModuleOrUniformRoot};
|
||||
use crate::{LexicalScopeBinding, NameBinding, NameBindingKind, PrivacyError, VisResolutionError};
|
||||
use crate::{ParentScope, PathResult, ResolutionError, Resolver, Scope, ScopeSet};
|
||||
use crate::{Segment, UseError};
|
||||
|
||||
type Res = def::Res<ast::NodeId>;
|
||||
|
||||
|
@ -82,6 +89,390 @@ fn reduce_impl_span_to_impl_keyword(sm: &SourceMap, impl_span: Span) -> Span {
|
|||
}
|
||||
|
||||
impl<'a> Resolver<'a> {
|
||||
crate fn report_errors(&mut self, krate: &Crate) {
|
||||
self.report_with_use_injections(krate);
|
||||
|
||||
for &(span_use, span_def) in &self.macro_expanded_macro_export_errors {
|
||||
let msg = "macro-expanded `macro_export` macros from the current crate \
|
||||
cannot be referred to by absolute paths";
|
||||
self.lint_buffer.buffer_lint_with_diagnostic(
|
||||
MACRO_EXPANDED_MACRO_EXPORTS_ACCESSED_BY_ABSOLUTE_PATHS,
|
||||
CRATE_NODE_ID,
|
||||
span_use,
|
||||
msg,
|
||||
BuiltinLintDiagnostics::MacroExpandedMacroExportsAccessedByAbsolutePaths(span_def),
|
||||
);
|
||||
}
|
||||
|
||||
for ambiguity_error in &self.ambiguity_errors {
|
||||
self.report_ambiguity_error(ambiguity_error);
|
||||
}
|
||||
|
||||
let mut reported_spans = FxHashSet::default();
|
||||
for error in &self.privacy_errors {
|
||||
if reported_spans.insert(error.dedup_span) {
|
||||
self.report_privacy_error(error);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn report_with_use_injections(&mut self, krate: &Crate) {
|
||||
for UseError { mut err, candidates, def_id, instead, suggestion } in
|
||||
self.use_injections.drain(..)
|
||||
{
|
||||
let (span, found_use) = if let Some(def_id) = def_id.as_local() {
|
||||
UsePlacementFinder::check(krate, self.def_id_to_node_id[def_id])
|
||||
} else {
|
||||
(None, false)
|
||||
};
|
||||
if !candidates.is_empty() {
|
||||
show_candidates(
|
||||
&self.definitions,
|
||||
self.session,
|
||||
&mut err,
|
||||
span,
|
||||
&candidates,
|
||||
instead,
|
||||
found_use,
|
||||
);
|
||||
} else if let Some((span, msg, sugg, appl)) = suggestion {
|
||||
err.span_suggestion(span, msg, sugg, appl);
|
||||
}
|
||||
err.emit();
|
||||
}
|
||||
}
|
||||
|
||||
crate fn report_conflict<'b>(
|
||||
&mut self,
|
||||
parent: Module<'_>,
|
||||
ident: Ident,
|
||||
ns: Namespace,
|
||||
new_binding: &NameBinding<'b>,
|
||||
old_binding: &NameBinding<'b>,
|
||||
) {
|
||||
// Error on the second of two conflicting names
|
||||
if old_binding.span.lo() > new_binding.span.lo() {
|
||||
return self.report_conflict(parent, ident, ns, old_binding, new_binding);
|
||||
}
|
||||
|
||||
let container = match parent.kind {
|
||||
ModuleKind::Def(kind, _, _) => kind.descr(parent.def_id()),
|
||||
ModuleKind::Block(..) => "block",
|
||||
};
|
||||
|
||||
let old_noun = match old_binding.is_import() {
|
||||
true => "import",
|
||||
false => "definition",
|
||||
};
|
||||
|
||||
let new_participle = match new_binding.is_import() {
|
||||
true => "imported",
|
||||
false => "defined",
|
||||
};
|
||||
|
||||
let (name, span) =
|
||||
(ident.name, self.session.source_map().guess_head_span(new_binding.span));
|
||||
|
||||
if let Some(s) = self.name_already_seen.get(&name) {
|
||||
if s == &span {
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
let old_kind = match (ns, old_binding.module()) {
|
||||
(ValueNS, _) => "value",
|
||||
(MacroNS, _) => "macro",
|
||||
(TypeNS, _) if old_binding.is_extern_crate() => "extern crate",
|
||||
(TypeNS, Some(module)) if module.is_normal() => "module",
|
||||
(TypeNS, Some(module)) if module.is_trait() => "trait",
|
||||
(TypeNS, _) => "type",
|
||||
};
|
||||
|
||||
let msg = format!("the name `{}` is defined multiple times", name);
|
||||
|
||||
let mut err = match (old_binding.is_extern_crate(), new_binding.is_extern_crate()) {
|
||||
(true, true) => struct_span_err!(self.session, span, E0259, "{}", msg),
|
||||
(true, _) | (_, true) => match new_binding.is_import() && old_binding.is_import() {
|
||||
true => struct_span_err!(self.session, span, E0254, "{}", msg),
|
||||
false => struct_span_err!(self.session, span, E0260, "{}", msg),
|
||||
},
|
||||
_ => match (old_binding.is_import(), new_binding.is_import()) {
|
||||
(false, false) => struct_span_err!(self.session, span, E0428, "{}", msg),
|
||||
(true, true) => struct_span_err!(self.session, span, E0252, "{}", msg),
|
||||
_ => struct_span_err!(self.session, span, E0255, "{}", msg),
|
||||
},
|
||||
};
|
||||
|
||||
err.note(&format!(
|
||||
"`{}` must be defined only once in the {} namespace of this {}",
|
||||
name,
|
||||
ns.descr(),
|
||||
container
|
||||
));
|
||||
|
||||
err.span_label(span, format!("`{}` re{} here", name, new_participle));
|
||||
err.span_label(
|
||||
self.session.source_map().guess_head_span(old_binding.span),
|
||||
format!("previous {} of the {} `{}` here", old_noun, old_kind, name),
|
||||
);
|
||||
|
||||
// See https://github.com/rust-lang/rust/issues/32354
|
||||
use NameBindingKind::Import;
|
||||
let import = match (&new_binding.kind, &old_binding.kind) {
|
||||
// If there are two imports where one or both have attributes then prefer removing the
|
||||
// import without attributes.
|
||||
(Import { import: new, .. }, Import { import: old, .. })
|
||||
if {
|
||||
!new_binding.span.is_dummy()
|
||||
&& !old_binding.span.is_dummy()
|
||||
&& (new.has_attributes || old.has_attributes)
|
||||
} =>
|
||||
{
|
||||
if old.has_attributes {
|
||||
Some((new, new_binding.span, true))
|
||||
} else {
|
||||
Some((old, old_binding.span, true))
|
||||
}
|
||||
}
|
||||
// Otherwise prioritize the new binding.
|
||||
(Import { import, .. }, other) if !new_binding.span.is_dummy() => {
|
||||
Some((import, new_binding.span, other.is_import()))
|
||||
}
|
||||
(other, Import { import, .. }) if !old_binding.span.is_dummy() => {
|
||||
Some((import, old_binding.span, other.is_import()))
|
||||
}
|
||||
_ => None,
|
||||
};
|
||||
|
||||
// Check if the target of the use for both bindings is the same.
|
||||
let duplicate = new_binding.res().opt_def_id() == old_binding.res().opt_def_id();
|
||||
let has_dummy_span = new_binding.span.is_dummy() || old_binding.span.is_dummy();
|
||||
let from_item =
|
||||
self.extern_prelude.get(&ident).map_or(true, |entry| entry.introduced_by_item);
|
||||
// Only suggest removing an import if both bindings are to the same def, if both spans
|
||||
// aren't dummy spans. Further, if both bindings are imports, then the ident must have
|
||||
// been introduced by an item.
|
||||
let should_remove_import = duplicate
|
||||
&& !has_dummy_span
|
||||
&& ((new_binding.is_extern_crate() || old_binding.is_extern_crate()) || from_item);
|
||||
|
||||
match import {
|
||||
Some((import, span, true)) if should_remove_import && import.is_nested() => {
|
||||
self.add_suggestion_for_duplicate_nested_use(&mut err, import, span)
|
||||
}
|
||||
Some((import, _, true)) if should_remove_import && !import.is_glob() => {
|
||||
// Simple case - remove the entire import. Due to the above match arm, this can
|
||||
// only be a single use so just remove it entirely.
|
||||
err.tool_only_span_suggestion(
|
||||
import.use_span_with_attributes,
|
||||
"remove unnecessary import",
|
||||
String::new(),
|
||||
Applicability::MaybeIncorrect,
|
||||
);
|
||||
}
|
||||
Some((import, span, _)) => {
|
||||
self.add_suggestion_for_rename_of_use(&mut err, name, import, span)
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
|
||||
err.emit();
|
||||
self.name_already_seen.insert(name, span);
|
||||
}
|
||||
|
||||
/// This function adds a suggestion to change the binding name of a new import that conflicts
|
||||
/// with an existing import.
|
||||
///
|
||||
/// ```text,ignore (diagnostic)
|
||||
/// help: you can use `as` to change the binding name of the import
|
||||
/// |
|
||||
/// LL | use foo::bar as other_bar;
|
||||
/// | ^^^^^^^^^^^^^^^^^^^^^
|
||||
/// ```
|
||||
fn add_suggestion_for_rename_of_use(
|
||||
&self,
|
||||
err: &mut Diagnostic,
|
||||
name: Symbol,
|
||||
import: &Import<'_>,
|
||||
binding_span: Span,
|
||||
) {
|
||||
let suggested_name = if name.as_str().chars().next().unwrap().is_uppercase() {
|
||||
format!("Other{}", name)
|
||||
} else {
|
||||
format!("other_{}", name)
|
||||
};
|
||||
|
||||
let mut suggestion = None;
|
||||
match import.kind {
|
||||
ImportKind::Single { type_ns_only: true, .. } => {
|
||||
suggestion = Some(format!("self as {}", suggested_name))
|
||||
}
|
||||
ImportKind::Single { source, .. } => {
|
||||
if let Some(pos) =
|
||||
source.span.hi().0.checked_sub(binding_span.lo().0).map(|pos| pos as usize)
|
||||
{
|
||||
if let Ok(snippet) = self.session.source_map().span_to_snippet(binding_span) {
|
||||
if pos <= snippet.len() {
|
||||
suggestion = Some(format!(
|
||||
"{} as {}{}",
|
||||
&snippet[..pos],
|
||||
suggested_name,
|
||||
if snippet.ends_with(';') { ";" } else { "" }
|
||||
))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
ImportKind::ExternCrate { source, target } => {
|
||||
suggestion = Some(format!(
|
||||
"extern crate {} as {};",
|
||||
source.unwrap_or(target.name),
|
||||
suggested_name,
|
||||
))
|
||||
}
|
||||
_ => unreachable!(),
|
||||
}
|
||||
|
||||
let rename_msg = "you can use `as` to change the binding name of the import";
|
||||
if let Some(suggestion) = suggestion {
|
||||
err.span_suggestion(
|
||||
binding_span,
|
||||
rename_msg,
|
||||
suggestion,
|
||||
Applicability::MaybeIncorrect,
|
||||
);
|
||||
} else {
|
||||
err.span_label(binding_span, rename_msg);
|
||||
}
|
||||
}
|
||||
|
||||
/// This function adds a suggestion to remove an unnecessary binding from an import that is
|
||||
/// nested. In the following example, this function will be invoked to remove the `a` binding
|
||||
/// in the second use statement:
|
||||
///
|
||||
/// ```ignore (diagnostic)
|
||||
/// use issue_52891::a;
|
||||
/// use issue_52891::{d, a, e};
|
||||
/// ```
|
||||
///
|
||||
/// The following suggestion will be added:
|
||||
///
|
||||
/// ```ignore (diagnostic)
|
||||
/// use issue_52891::{d, a, e};
|
||||
/// ^-- help: remove unnecessary import
|
||||
/// ```
|
||||
///
|
||||
/// If the nested use contains only one import then the suggestion will remove the entire
|
||||
/// line.
|
||||
///
|
||||
/// It is expected that the provided import is nested - this isn't checked by the
|
||||
/// function. If this invariant is not upheld, this function's behaviour will be unexpected
|
||||
/// as characters expected by span manipulations won't be present.
|
||||
fn add_suggestion_for_duplicate_nested_use(
|
||||
&self,
|
||||
err: &mut Diagnostic,
|
||||
import: &Import<'_>,
|
||||
binding_span: Span,
|
||||
) {
|
||||
assert!(import.is_nested());
|
||||
let message = "remove unnecessary import";
|
||||
|
||||
// Two examples will be used to illustrate the span manipulations we're doing:
|
||||
//
|
||||
// - Given `use issue_52891::{d, a, e};` where `a` is a duplicate then `binding_span` is
|
||||
// `a` and `import.use_span` is `issue_52891::{d, a, e};`.
|
||||
// - Given `use issue_52891::{d, e, a};` where `a` is a duplicate then `binding_span` is
|
||||
// `a` and `import.use_span` is `issue_52891::{d, e, a};`.
|
||||
|
||||
let (found_closing_brace, span) =
|
||||
find_span_of_binding_until_next_binding(self.session, binding_span, import.use_span);
|
||||
|
||||
// If there was a closing brace then identify the span to remove any trailing commas from
|
||||
// previous imports.
|
||||
if found_closing_brace {
|
||||
if let Some(span) = extend_span_to_previous_binding(self.session, span) {
|
||||
err.tool_only_span_suggestion(
|
||||
span,
|
||||
message,
|
||||
String::new(),
|
||||
Applicability::MaybeIncorrect,
|
||||
);
|
||||
} else {
|
||||
// Remove the entire line if we cannot extend the span back, this indicates an
|
||||
// `issue_52891::{self}` case.
|
||||
err.span_suggestion(
|
||||
import.use_span_with_attributes,
|
||||
message,
|
||||
String::new(),
|
||||
Applicability::MaybeIncorrect,
|
||||
);
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
err.span_suggestion(span, message, String::new(), Applicability::MachineApplicable);
|
||||
}
|
||||
|
||||
crate fn lint_if_path_starts_with_module(
|
||||
&mut self,
|
||||
finalize: Finalize,
|
||||
path: &[Segment],
|
||||
second_binding: Option<&NameBinding<'_>>,
|
||||
) {
|
||||
let (diag_id, diag_span) = match finalize {
|
||||
Finalize::No => return,
|
||||
Finalize::SimplePath(id, path_span) => (id, path_span),
|
||||
Finalize::UsePath { root_id, root_span, .. } => (root_id, root_span),
|
||||
Finalize::QPathTrait { qpath_id, qpath_span, .. } => (qpath_id, qpath_span),
|
||||
};
|
||||
|
||||
let first_name = match path.get(0) {
|
||||
// In the 2018 edition this lint is a hard error, so nothing to do
|
||||
Some(seg) if seg.ident.span.rust_2015() && self.session.rust_2015() => seg.ident.name,
|
||||
_ => return,
|
||||
};
|
||||
|
||||
// We're only interested in `use` paths which should start with
|
||||
// `{{root}}` currently.
|
||||
if first_name != kw::PathRoot {
|
||||
return;
|
||||
}
|
||||
|
||||
match path.get(1) {
|
||||
// If this import looks like `crate::...` it's already good
|
||||
Some(Segment { ident, .. }) if ident.name == kw::Crate => return,
|
||||
// Otherwise go below to see if it's an extern crate
|
||||
Some(_) => {}
|
||||
// If the path has length one (and it's `PathRoot` most likely)
|
||||
// then we don't know whether we're gonna be importing a crate or an
|
||||
// item in our crate. Defer this lint to elsewhere
|
||||
None => return,
|
||||
}
|
||||
|
||||
// If the first element of our path was actually resolved to an
|
||||
// `ExternCrate` (also used for `crate::...`) then no need to issue a
|
||||
// warning, this looks all good!
|
||||
if let Some(binding) = second_binding {
|
||||
if let NameBindingKind::Import { import, .. } = binding.kind {
|
||||
// Careful: we still want to rewrite paths from renamed extern crates.
|
||||
if let ImportKind::ExternCrate { source: None, .. } = import.kind {
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
let diag = BuiltinLintDiagnostics::AbsPathWithModule(diag_span);
|
||||
self.lint_buffer.buffer_lint_with_diagnostic(
|
||||
ABSOLUTE_PATHS_NOT_STARTING_WITH_CRATE,
|
||||
diag_id,
|
||||
diag_span,
|
||||
"absolute paths must start with `self`, `super`, \
|
||||
`crate`, or an external crate name in the 2018 edition",
|
||||
diag,
|
||||
);
|
||||
}
|
||||
|
||||
crate fn add_module_candidates(
|
||||
&mut self,
|
||||
module: Module<'a>,
|
||||
|
@ -1076,6 +1467,8 @@ impl<'a> Resolver<'a> {
|
|||
&parent_scope,
|
||||
None,
|
||||
false,
|
||||
false,
|
||||
None,
|
||||
) {
|
||||
let desc = match binding.res() {
|
||||
Res::Def(DefKind::Macro(MacroKind::Bang), _) => {
|
||||
|
@ -1223,7 +1616,7 @@ impl<'a> Resolver<'a> {
|
|||
}
|
||||
}
|
||||
|
||||
crate fn report_ambiguity_error(&self, ambiguity_error: &AmbiguityError<'_>) {
|
||||
fn report_ambiguity_error(&self, ambiguity_error: &AmbiguityError<'_>) {
|
||||
let AmbiguityError { kind, ident, b1, b2, misc1, misc2 } = *ambiguity_error;
|
||||
let (b1, b2, misc1, misc2, swapped) = if b2.span.is_dummy() && !b1.span.is_dummy() {
|
||||
// We have to print the span-less alternative first, otherwise formatting looks bad.
|
||||
|
@ -1289,7 +1682,7 @@ impl<'a> Resolver<'a> {
|
|||
None
|
||||
}
|
||||
|
||||
crate fn report_privacy_error(&self, privacy_error: &PrivacyError<'_>) {
|
||||
fn report_privacy_error(&self, privacy_error: &PrivacyError<'_>) {
|
||||
let PrivacyError { ident, binding, .. } = *privacy_error;
|
||||
|
||||
let res = binding.res();
|
||||
|
@ -1375,6 +1768,188 @@ impl<'a> Resolver<'a> {
|
|||
sugg => sugg,
|
||||
}
|
||||
}
|
||||
|
||||
crate fn report_path_resolution_error(
|
||||
&mut self,
|
||||
path: &[Segment],
|
||||
opt_ns: Option<Namespace>, // `None` indicates a module path in import
|
||||
parent_scope: &ParentScope<'a>,
|
||||
ribs: Option<&PerNS<Vec<Rib<'a>>>>,
|
||||
unusable_binding: Option<&'a NameBinding<'a>>,
|
||||
module: Option<ModuleOrUniformRoot<'a>>,
|
||||
i: usize,
|
||||
ident: Ident,
|
||||
) -> (String, Option<Suggestion>) {
|
||||
let is_last = i == path.len() - 1;
|
||||
let ns = if is_last { opt_ns.unwrap_or(TypeNS) } else { TypeNS };
|
||||
let module_res = match module {
|
||||
Some(ModuleOrUniformRoot::Module(module)) => module.res(),
|
||||
_ => None,
|
||||
};
|
||||
if module_res == self.graph_root.res() {
|
||||
let is_mod = |res| matches!(res, Res::Def(DefKind::Mod, _));
|
||||
let mut candidates = self.lookup_import_candidates(ident, TypeNS, parent_scope, is_mod);
|
||||
candidates
|
||||
.sort_by_cached_key(|c| (c.path.segments.len(), pprust::path_to_string(&c.path)));
|
||||
if let Some(candidate) = candidates.get(0) {
|
||||
(
|
||||
String::from("unresolved import"),
|
||||
Some((
|
||||
vec![(ident.span, pprust::path_to_string(&candidate.path))],
|
||||
String::from("a similar path exists"),
|
||||
Applicability::MaybeIncorrect,
|
||||
)),
|
||||
)
|
||||
} else if self.session.edition() == Edition::Edition2015 {
|
||||
(format!("maybe a missing crate `{}`?", ident), None)
|
||||
} else {
|
||||
(format!("could not find `{}` in the crate root", ident), None)
|
||||
}
|
||||
} else if i > 0 {
|
||||
let parent = path[i - 1].ident.name;
|
||||
let parent = match parent {
|
||||
// ::foo is mounted at the crate root for 2015, and is the extern
|
||||
// prelude for 2018+
|
||||
kw::PathRoot if self.session.edition() > Edition::Edition2015 => {
|
||||
"the list of imported crates".to_owned()
|
||||
}
|
||||
kw::PathRoot | kw::Crate => "the crate root".to_owned(),
|
||||
_ => format!("`{}`", parent),
|
||||
};
|
||||
|
||||
let mut msg = format!("could not find `{}` in {}", ident, parent);
|
||||
if ns == TypeNS || ns == ValueNS {
|
||||
let ns_to_try = if ns == TypeNS { ValueNS } else { TypeNS };
|
||||
let binding = if let Some(module) = module {
|
||||
self.resolve_ident_in_module(
|
||||
module,
|
||||
ident,
|
||||
ns_to_try,
|
||||
parent_scope,
|
||||
None,
|
||||
false,
|
||||
unusable_binding,
|
||||
).ok()
|
||||
} else if let Some(ribs) = ribs
|
||||
&& let Some(TypeNS | ValueNS) = opt_ns
|
||||
{
|
||||
match self.resolve_ident_in_lexical_scope(
|
||||
ident,
|
||||
ns_to_try,
|
||||
parent_scope,
|
||||
Finalize::No,
|
||||
&ribs[ns_to_try],
|
||||
unusable_binding,
|
||||
) {
|
||||
// we found a locally-imported or available item/module
|
||||
Some(LexicalScopeBinding::Item(binding)) => Some(binding),
|
||||
_ => None,
|
||||
}
|
||||
} else {
|
||||
let scopes = ScopeSet::All(ns_to_try, opt_ns.is_none());
|
||||
self.early_resolve_ident_in_lexical_scope(
|
||||
ident,
|
||||
scopes,
|
||||
parent_scope,
|
||||
None,
|
||||
false,
|
||||
false,
|
||||
unusable_binding,
|
||||
).ok()
|
||||
};
|
||||
if let Some(binding) = binding {
|
||||
let mut found = |what| {
|
||||
msg = format!(
|
||||
"expected {}, found {} `{}` in {}",
|
||||
ns.descr(),
|
||||
what,
|
||||
ident,
|
||||
parent
|
||||
)
|
||||
};
|
||||
if binding.module().is_some() {
|
||||
found("module")
|
||||
} else {
|
||||
match binding.res() {
|
||||
Res::Def(kind, id) => found(kind.descr(id)),
|
||||
_ => found(ns_to_try.descr()),
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
(msg, None)
|
||||
} else if ident.name.as_str().chars().next().map_or(false, |c| c.is_ascii_uppercase()) {
|
||||
// Check whether the name refers to an item in the value namespace.
|
||||
let binding = if let Some(ribs) = ribs {
|
||||
self.resolve_ident_in_lexical_scope(
|
||||
ident,
|
||||
ValueNS,
|
||||
parent_scope,
|
||||
Finalize::No,
|
||||
&ribs[ValueNS],
|
||||
unusable_binding,
|
||||
)
|
||||
} else {
|
||||
None
|
||||
};
|
||||
let match_span = match binding {
|
||||
// Name matches a local variable. For example:
|
||||
// ```
|
||||
// fn f() {
|
||||
// let Foo: &str = "";
|
||||
// println!("{}", Foo::Bar); // Name refers to local
|
||||
// // variable `Foo`.
|
||||
// }
|
||||
// ```
|
||||
Some(LexicalScopeBinding::Res(Res::Local(id))) => {
|
||||
Some(*self.pat_span_map.get(&id).unwrap())
|
||||
}
|
||||
// Name matches item from a local name binding
|
||||
// created by `use` declaration. For example:
|
||||
// ```
|
||||
// pub Foo: &str = "";
|
||||
//
|
||||
// mod submod {
|
||||
// use super::Foo;
|
||||
// println!("{}", Foo::Bar); // Name refers to local
|
||||
// // binding `Foo`.
|
||||
// }
|
||||
// ```
|
||||
Some(LexicalScopeBinding::Item(name_binding)) => Some(name_binding.span),
|
||||
_ => None,
|
||||
};
|
||||
let suggestion = if let Some(span) = match_span {
|
||||
Some((
|
||||
vec![(span, String::from(""))],
|
||||
format!("`{}` is defined here, but is not a type", ident),
|
||||
Applicability::MaybeIncorrect,
|
||||
))
|
||||
} else {
|
||||
None
|
||||
};
|
||||
|
||||
(format!("use of undeclared type `{}`", ident), suggestion)
|
||||
} else {
|
||||
let suggestion = if ident.name == sym::alloc {
|
||||
Some((
|
||||
vec![],
|
||||
String::from("add `extern crate alloc` to use the `alloc` crate"),
|
||||
Applicability::MaybeIncorrect,
|
||||
))
|
||||
} else {
|
||||
self.find_similarly_named_module_or_crate(ident.name, &parent_scope.module).map(
|
||||
|sugg| {
|
||||
(
|
||||
vec![(ident.span, sugg.to_string())],
|
||||
String::from("there is a crate or module with a similar name"),
|
||||
Applicability::MaybeIncorrect,
|
||||
)
|
||||
},
|
||||
)
|
||||
};
|
||||
(format!("use of undeclared crate or module `{}`", ident), suggestion)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a, 'b> ImportResolver<'a, 'b> {
|
||||
|
@ -1422,7 +1997,7 @@ impl<'a, 'b> ImportResolver<'a, 'b> {
|
|||
) -> Option<(Vec<Segment>, Vec<String>)> {
|
||||
// Replace first ident with `self` and check if that is valid.
|
||||
path[0].ident.name = kw::SelfLower;
|
||||
let result = self.r.resolve_path(&path, None, parent_scope, Finalize::No);
|
||||
let result = self.r.maybe_resolve_path(&path, None, parent_scope);
|
||||
debug!("make_missing_self_suggestion: path={:?} result={:?}", path, result);
|
||||
if let PathResult::Module(..) = result { Some((path, Vec::new())) } else { None }
|
||||
}
|
||||
|
@ -1441,7 +2016,7 @@ impl<'a, 'b> ImportResolver<'a, 'b> {
|
|||
) -> Option<(Vec<Segment>, Vec<String>)> {
|
||||
// Replace first ident with `crate` and check if that is valid.
|
||||
path[0].ident.name = kw::Crate;
|
||||
let result = self.r.resolve_path(&path, None, parent_scope, Finalize::No);
|
||||
let result = self.r.maybe_resolve_path(&path, None, parent_scope);
|
||||
debug!("make_missing_crate_suggestion: path={:?} result={:?}", path, result);
|
||||
if let PathResult::Module(..) = result {
|
||||
Some((
|
||||
|
@ -1472,7 +2047,7 @@ impl<'a, 'b> ImportResolver<'a, 'b> {
|
|||
) -> Option<(Vec<Segment>, Vec<String>)> {
|
||||
// Replace first ident with `crate` and check if that is valid.
|
||||
path[0].ident.name = kw::Super;
|
||||
let result = self.r.resolve_path(&path, None, parent_scope, Finalize::No);
|
||||
let result = self.r.maybe_resolve_path(&path, None, parent_scope);
|
||||
debug!("make_missing_super_suggestion: path={:?} result={:?}", path, result);
|
||||
if let PathResult::Module(..) = result { Some((path, Vec::new())) } else { None }
|
||||
}
|
||||
|
@ -1506,7 +2081,7 @@ impl<'a, 'b> ImportResolver<'a, 'b> {
|
|||
for name in extern_crate_names.into_iter() {
|
||||
// Replace first ident with a crate name and check if that is valid.
|
||||
path[0].ident.name = name;
|
||||
let result = self.r.resolve_path(&path, None, parent_scope, Finalize::No);
|
||||
let result = self.r.maybe_resolve_path(&path, None, parent_scope);
|
||||
debug!(
|
||||
"make_external_crate_suggestion: name={:?} path={:?} result={:?}",
|
||||
name, path, result
|
||||
|
@ -1673,7 +2248,7 @@ impl<'a, 'b> ImportResolver<'a, 'b> {
|
|||
/// use foo::{a, b, c};
|
||||
/// ^^^
|
||||
/// ```
|
||||
pub(crate) fn find_span_of_binding_until_next_binding(
|
||||
fn find_span_of_binding_until_next_binding(
|
||||
sess: &Session,
|
||||
binding_span: Span,
|
||||
use_span: Span,
|
||||
|
@ -1724,7 +2299,7 @@ pub(crate) fn find_span_of_binding_until_next_binding(
|
|||
/// use foo::{a, b, c};
|
||||
/// --- binding span
|
||||
/// ```
|
||||
pub(crate) fn extend_span_to_previous_binding(sess: &Session, binding_span: Span) -> Option<Span> {
|
||||
fn extend_span_to_previous_binding(sess: &Session, binding_span: Span) -> Option<Span> {
|
||||
let source_map = sess.source_map();
|
||||
|
||||
// `prev_source` will contain all of the source that came before the span.
|
||||
|
@ -1812,7 +2387,7 @@ fn find_span_immediately_after_crate_name(
|
|||
/// When an entity with a given name is not available in scope, we search for
|
||||
/// entities with that name in all crates. This method allows outputting the
|
||||
/// results of this search in a programmer-friendly way
|
||||
crate fn show_candidates(
|
||||
fn show_candidates(
|
||||
definitions: &rustc_hir::definitions::Definitions,
|
||||
session: &Session,
|
||||
err: &mut Diagnostic,
|
||||
|
@ -1947,3 +2522,70 @@ crate fn show_candidates(
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
struct UsePlacementFinder {
|
||||
target_module: NodeId,
|
||||
first_legal_span: Option<Span>,
|
||||
first_use_span: Option<Span>,
|
||||
}
|
||||
|
||||
impl UsePlacementFinder {
|
||||
fn check(krate: &Crate, target_module: NodeId) -> (Option<Span>, bool) {
|
||||
let mut finder =
|
||||
UsePlacementFinder { target_module, first_legal_span: None, first_use_span: None };
|
||||
finder.visit_crate(krate);
|
||||
if let Some(use_span) = finder.first_use_span {
|
||||
(Some(use_span), true)
|
||||
} else {
|
||||
(finder.first_legal_span, false)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<'tcx> visit::Visitor<'tcx> for UsePlacementFinder {
|
||||
fn visit_crate(&mut self, c: &Crate) {
|
||||
if self.target_module == CRATE_NODE_ID {
|
||||
let inject = c.spans.inject_use_span;
|
||||
if is_span_suitable_for_use_injection(inject) {
|
||||
self.first_legal_span = Some(inject);
|
||||
}
|
||||
self.first_use_span = search_for_any_use_in_items(&c.items);
|
||||
return;
|
||||
} else {
|
||||
visit::walk_crate(self, c);
|
||||
}
|
||||
}
|
||||
|
||||
fn visit_item(&mut self, item: &'tcx ast::Item) {
|
||||
if self.target_module == item.id {
|
||||
if let ItemKind::Mod(_, ModKind::Loaded(items, _inline, mod_spans)) = &item.kind {
|
||||
let inject = mod_spans.inject_use_span;
|
||||
if is_span_suitable_for_use_injection(inject) {
|
||||
self.first_legal_span = Some(inject);
|
||||
}
|
||||
self.first_use_span = search_for_any_use_in_items(items);
|
||||
return;
|
||||
}
|
||||
} else {
|
||||
visit::walk_item(self, item);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn search_for_any_use_in_items(items: &[P<ast::Item>]) -> Option<Span> {
|
||||
for item in items {
|
||||
if let ItemKind::Use(..) = item.kind {
|
||||
if is_span_suitable_for_use_injection(item.span) {
|
||||
return Some(item.span.shrink_to_lo());
|
||||
}
|
||||
}
|
||||
}
|
||||
return None;
|
||||
}
|
||||
|
||||
fn is_span_suitable_for_use_injection(s: Span) -> bool {
|
||||
// don't suggest placing a use before the prelude
|
||||
// import or other generated ones
|
||||
!s.from_expansion()
|
||||
}
|
||||
|
|
1582
compiler/rustc_resolve/src/ident.rs
Normal file
1582
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(
|
||||
|
@ -772,7 +499,7 @@ impl<'a, 'b> ImportResolver<'a, 'b> {
|
|||
// not define any names while resolving its module path.
|
||||
let orig_vis = import.vis.replace(ty::Visibility::Invisible);
|
||||
let path_res =
|
||||
self.r.resolve_path(&import.module_path, None, &import.parent_scope, Finalize::No);
|
||||
self.r.maybe_resolve_path(&import.module_path, None, &import.parent_scope);
|
||||
import.vis.set(orig_vis);
|
||||
|
||||
match path_res {
|
||||
|
@ -812,6 +539,8 @@ impl<'a, 'b> ImportResolver<'a, 'b> {
|
|||
ns,
|
||||
&import.parent_scope,
|
||||
None,
|
||||
false,
|
||||
None,
|
||||
);
|
||||
import.vis.set(orig_vis);
|
||||
source_bindings[ns].set(binding);
|
||||
|
@ -857,10 +586,8 @@ impl<'a, 'b> ImportResolver<'a, 'b> {
|
|||
/// consolidate multiple unresolved import errors into a single diagnostic.
|
||||
fn finalize_import(&mut self, import: &'b Import<'b>) -> Option<UnresolvedImportError> {
|
||||
let orig_vis = import.vis.replace(ty::Visibility::Invisible);
|
||||
let orig_unusable_binding = match &import.kind {
|
||||
ImportKind::Single { target_bindings, .. } => {
|
||||
Some(mem::replace(&mut self.r.unusable_binding, target_bindings[TypeNS].get()))
|
||||
}
|
||||
let unusable_binding = match &import.kind {
|
||||
ImportKind::Single { target_bindings, .. } => target_bindings[TypeNS].get(),
|
||||
_ => None,
|
||||
};
|
||||
let prev_ambiguity_errors_len = self.r.ambiguity_errors.len();
|
||||
|
@ -869,12 +596,14 @@ impl<'a, 'b> ImportResolver<'a, 'b> {
|
|||
root_span: import.root_span,
|
||||
path_span: import.span,
|
||||
};
|
||||
let path_res =
|
||||
self.r.resolve_path(&import.module_path, None, &import.parent_scope, finalize);
|
||||
let path_res = self.r.resolve_path(
|
||||
&import.module_path,
|
||||
None,
|
||||
&import.parent_scope,
|
||||
finalize,
|
||||
unusable_binding,
|
||||
);
|
||||
let no_ambiguity = self.r.ambiguity_errors.len() == prev_ambiguity_errors_len;
|
||||
if let Some(orig_unusable_binding) = orig_unusable_binding {
|
||||
self.r.unusable_binding = orig_unusable_binding;
|
||||
}
|
||||
import.vis.set(orig_vis);
|
||||
if let PathResult::Failed { .. } | PathResult::NonModule(..) = path_res {
|
||||
// Consider erroneous imports used to avoid duplicate diagnostics.
|
||||
|
@ -987,18 +716,15 @@ impl<'a, 'b> ImportResolver<'a, 'b> {
|
|||
self.r.per_ns(|this, ns| {
|
||||
if !type_ns_only || ns == TypeNS {
|
||||
let orig_vis = import.vis.replace(ty::Visibility::Invisible);
|
||||
let orig_unusable_binding =
|
||||
mem::replace(&mut this.unusable_binding, target_bindings[ns].get());
|
||||
let orig_last_import_segment = mem::replace(&mut this.last_import_segment, true);
|
||||
let binding = this.resolve_ident_in_module(
|
||||
module,
|
||||
ident,
|
||||
ns,
|
||||
&import.parent_scope,
|
||||
Some(import.span),
|
||||
true,
|
||||
target_bindings[ns].get(),
|
||||
);
|
||||
this.last_import_segment = orig_last_import_segment;
|
||||
this.unusable_binding = orig_unusable_binding;
|
||||
import.vis.set(orig_vis);
|
||||
|
||||
match binding {
|
||||
|
@ -1057,6 +783,8 @@ impl<'a, 'b> ImportResolver<'a, 'b> {
|
|||
ns,
|
||||
&import.parent_scope,
|
||||
Some(import.span),
|
||||
false,
|
||||
None,
|
||||
);
|
||||
if binding.is_ok() {
|
||||
all_ns_failed = false;
|
||||
|
@ -1271,15 +999,14 @@ impl<'a, 'b> ImportResolver<'a, 'b> {
|
|||
return;
|
||||
}
|
||||
|
||||
let orig_unusable_binding =
|
||||
mem::replace(&mut this.unusable_binding, target_bindings[ns].get());
|
||||
|
||||
match this.early_resolve_ident_in_lexical_scope(
|
||||
target,
|
||||
ScopeSet::All(ns, false),
|
||||
&import.parent_scope,
|
||||
None,
|
||||
false,
|
||||
false,
|
||||
target_bindings[ns].get(),
|
||||
) {
|
||||
Ok(other_binding) => {
|
||||
is_redundant[ns] = Some(
|
||||
|
@ -1289,8 +1016,6 @@ impl<'a, 'b> ImportResolver<'a, 'b> {
|
|||
}
|
||||
Err(_) => is_redundant[ns] = Some(false),
|
||||
}
|
||||
|
||||
this.unusable_binding = orig_unusable_binding;
|
||||
}
|
||||
});
|
||||
|
||||
|
|
|
@ -8,7 +8,7 @@
|
|||
use RibKind::*;
|
||||
|
||||
use crate::{path_names_to_string, BindingError, Finalize, LexicalScopeBinding};
|
||||
use crate::{Module, ModuleOrUniformRoot, ParentScope, PathResult};
|
||||
use crate::{Module, ModuleOrUniformRoot, NameBinding, ParentScope, PathResult};
|
||||
use crate::{ResolutionError, Resolver, Segment, UseError};
|
||||
|
||||
use rustc_ast::ptr::P;
|
||||
|
@ -487,6 +487,7 @@ impl<'a: 'ast, 'ast> Visitor<'ast> for LateResolutionVisitor<'a, '_, 'ast> {
|
|||
self_ty,
|
||||
TypeNS,
|
||||
Finalize::SimplePath(ty.id, ty.span),
|
||||
None,
|
||||
)
|
||||
.map_or(Res::Err, |d| d.res());
|
||||
self.r.record_partial_res(ty.id, PartialRes::new(res));
|
||||
|
@ -676,12 +677,8 @@ impl<'a: 'ast, 'ast> Visitor<'ast> for LateResolutionVisitor<'a, '_, 'ast> {
|
|||
// checking.
|
||||
if path.segments.len() == 1 && path.segments[0].args.is_none() {
|
||||
let mut check_ns = |ns| {
|
||||
self.resolve_ident_in_lexical_scope(
|
||||
path.segments[0].ident,
|
||||
ns,
|
||||
Finalize::No,
|
||||
)
|
||||
.is_some()
|
||||
self.maybe_resolve_ident_in_lexical_scope(path.segments[0].ident, ns)
|
||||
.is_some()
|
||||
};
|
||||
if !check_ns(TypeNS) && check_ns(ValueNS) {
|
||||
// This must be equivalent to `visit_anon_const`, but we cannot call it
|
||||
|
@ -750,11 +747,27 @@ impl<'a: 'ast, 'b, 'ast> LateResolutionVisitor<'a, 'b, 'ast> {
|
|||
}
|
||||
}
|
||||
|
||||
fn maybe_resolve_ident_in_lexical_scope(
|
||||
&mut self,
|
||||
ident: Ident,
|
||||
ns: Namespace,
|
||||
) -> Option<LexicalScopeBinding<'a>> {
|
||||
self.r.resolve_ident_in_lexical_scope(
|
||||
ident,
|
||||
ns,
|
||||
&self.parent_scope,
|
||||
Finalize::No,
|
||||
&self.ribs[ns],
|
||||
None,
|
||||
)
|
||||
}
|
||||
|
||||
fn resolve_ident_in_lexical_scope(
|
||||
&mut self,
|
||||
ident: Ident,
|
||||
ns: Namespace,
|
||||
finalize: Finalize,
|
||||
unusable_binding: Option<&'a NameBinding<'a>>,
|
||||
) -> Option<LexicalScopeBinding<'a>> {
|
||||
self.r.resolve_ident_in_lexical_scope(
|
||||
ident,
|
||||
|
@ -762,6 +775,7 @@ impl<'a: 'ast, 'b, 'ast> LateResolutionVisitor<'a, 'b, 'ast> {
|
|||
&self.parent_scope,
|
||||
finalize,
|
||||
&self.ribs[ns],
|
||||
unusable_binding,
|
||||
)
|
||||
}
|
||||
|
||||
|
@ -771,7 +785,14 @@ impl<'a: 'ast, 'b, 'ast> LateResolutionVisitor<'a, 'b, 'ast> {
|
|||
opt_ns: Option<Namespace>, // `None` indicates a module path in import
|
||||
finalize: Finalize,
|
||||
) -> PathResult<'a> {
|
||||
self.r.resolve_path_with_ribs(path, opt_ns, &self.parent_scope, finalize, Some(&self.ribs))
|
||||
self.r.resolve_path_with_ribs(
|
||||
path,
|
||||
opt_ns,
|
||||
&self.parent_scope,
|
||||
finalize,
|
||||
Some(&self.ribs),
|
||||
None,
|
||||
)
|
||||
}
|
||||
|
||||
// AST resolution
|
||||
|
@ -934,19 +955,16 @@ impl<'a: 'ast, 'b, 'ast> LateResolutionVisitor<'a, 'b, 'ast> {
|
|||
};
|
||||
|
||||
for &ns in nss {
|
||||
match self.resolve_ident_in_lexical_scope(ident, ns, Finalize::No) {
|
||||
match self.maybe_resolve_ident_in_lexical_scope(ident, ns) {
|
||||
Some(LexicalScopeBinding::Res(..)) => {
|
||||
report_error(self, ns);
|
||||
}
|
||||
Some(LexicalScopeBinding::Item(binding)) => {
|
||||
let orig_unusable_binding =
|
||||
replace(&mut self.r.unusable_binding, Some(binding));
|
||||
if let Some(LexicalScopeBinding::Res(..)) =
|
||||
self.resolve_ident_in_lexical_scope(ident, ns, Finalize::No)
|
||||
if let Some(LexicalScopeBinding::Res(..)) = self
|
||||
.resolve_ident_in_lexical_scope(ident, ns, Finalize::No, Some(binding))
|
||||
{
|
||||
report_error(self, ns);
|
||||
}
|
||||
self.r.unusable_binding = orig_unusable_binding;
|
||||
}
|
||||
None => {}
|
||||
}
|
||||
|
@ -1802,7 +1820,7 @@ impl<'a: 'ast, 'b, 'ast> LateResolutionVisitor<'a, 'b, 'ast> {
|
|||
// also be interpreted as a path to e.g. a constant, variant, etc.
|
||||
let is_syntactic_ambiguity = !has_sub && bm == BindingMode::ByValue(Mutability::Not);
|
||||
|
||||
let ls_binding = self.resolve_ident_in_lexical_scope(ident, ValueNS, Finalize::No)?;
|
||||
let ls_binding = self.maybe_resolve_ident_in_lexical_scope(ident, ValueNS)?;
|
||||
let (res, binding) = match ls_binding {
|
||||
LexicalScopeBinding::Item(binding)
|
||||
if is_syntactic_ambiguity && binding.is_ambiguity() =>
|
||||
|
@ -2071,17 +2089,14 @@ impl<'a: 'ast, 'b, 'ast> LateResolutionVisitor<'a, 'b, 'ast> {
|
|||
}
|
||||
|
||||
fn self_type_is_available(&mut self) -> bool {
|
||||
let binding = self.resolve_ident_in_lexical_scope(
|
||||
Ident::with_dummy_span(kw::SelfUpper),
|
||||
TypeNS,
|
||||
Finalize::No,
|
||||
);
|
||||
let binding = self
|
||||
.maybe_resolve_ident_in_lexical_scope(Ident::with_dummy_span(kw::SelfUpper), TypeNS);
|
||||
if let Some(LexicalScopeBinding::Res(res)) = binding { res != Res::Err } else { false }
|
||||
}
|
||||
|
||||
fn self_value_is_available(&mut self, self_span: Span) -> bool {
|
||||
let ident = Ident::new(kw::SelfLower, self_span);
|
||||
let binding = self.resolve_ident_in_lexical_scope(ident, ValueNS, Finalize::No);
|
||||
let binding = self.maybe_resolve_ident_in_lexical_scope(ident, ValueNS);
|
||||
if let Some(LexicalScopeBinding::Res(res)) = binding { res != Res::Err } else { false }
|
||||
}
|
||||
|
||||
|
|
|
@ -1271,12 +1271,11 @@ impl<'a: 'ast, 'ast> LateResolutionVisitor<'a, '_, 'ast> {
|
|||
|
||||
// Look for associated items in the current trait.
|
||||
if let Some((module, _)) = self.current_trait_ref {
|
||||
if let Ok(binding) = self.r.resolve_ident_in_module(
|
||||
if let Ok(binding) = self.r.maybe_resolve_ident_in_module(
|
||||
ModuleOrUniformRoot::Module(module),
|
||||
ident,
|
||||
ns,
|
||||
&self.parent_scope,
|
||||
None,
|
||||
) {
|
||||
let res = binding.res();
|
||||
if filter_fn(res) {
|
||||
|
|
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,
|
||||
|
@ -415,7 +412,7 @@ impl<'a> ResolverExpand for Resolver<'a> {
|
|||
|
||||
let mut indeterminate = false;
|
||||
for ns in [TypeNS, ValueNS, MacroNS].iter().copied() {
|
||||
match self.resolve_path(path, Some(ns), &parent_scope, Finalize::No) {
|
||||
match self.maybe_resolve_path(path, Some(ns), &parent_scope) {
|
||||
PathResult::Module(ModuleOrUniformRoot::Module(_)) => return Ok(true),
|
||||
PathResult::NonModule(partial_res) if partial_res.unresolved_segments() == 0 => {
|
||||
return Ok(true);
|
||||
|
@ -575,7 +572,7 @@ impl<'a> Resolver<'a> {
|
|||
}
|
||||
|
||||
let res = if path.len() > 1 {
|
||||
let res = match self.resolve_path(&path, Some(MacroNS), parent_scope, Finalize::No) {
|
||||
let res = match self.maybe_resolve_path(&path, Some(MacroNS), parent_scope) {
|
||||
PathResult::NonModule(path_res) if path_res.unresolved_segments() == 0 => {
|
||||
Ok(path_res.base_res())
|
||||
}
|
||||
|
@ -607,6 +604,8 @@ impl<'a> Resolver<'a> {
|
|||
parent_scope,
|
||||
None,
|
||||
force,
|
||||
false,
|
||||
None,
|
||||
);
|
||||
if let Err(Determinacy::Undetermined) = binding {
|
||||
return Err(Determinacy::Undetermined);
|
||||
|
@ -630,355 +629,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],
|
||||
|
@ -1024,6 +674,7 @@ impl<'a> Resolver<'a> {
|
|||
Some(MacroNS),
|
||||
&parent_scope,
|
||||
Finalize::SimplePath(ast::CRATE_NODE_ID, path_span),
|
||||
None,
|
||||
) {
|
||||
PathResult::NonModule(path_res) if path_res.unresolved_segments() == 0 => {
|
||||
let res = path_res.base_res();
|
||||
|
@ -1059,6 +710,8 @@ impl<'a> Resolver<'a> {
|
|||
&parent_scope,
|
||||
Some(ident.span),
|
||||
true,
|
||||
false,
|
||||
None,
|
||||
) {
|
||||
Ok(binding) => {
|
||||
let initial_res = initial_binding.map(|initial_binding| {
|
||||
|
@ -1100,6 +753,8 @@ impl<'a> Resolver<'a> {
|
|||
&parent_scope,
|
||||
Some(ident.span),
|
||||
true,
|
||||
false,
|
||||
None,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue