1
Fork 0

resolve: Partially unify early and late scope-relative ident resolution

This commit is contained in:
Vadim Petrochenkov 2021-03-13 22:23:18 +03:00
parent afaf33dcaf
commit ee0357af3b
5 changed files with 97 additions and 144 deletions

View file

@ -24,7 +24,8 @@ use rustc_hir::def_id;
use rustc_hir::PrimTy;
use rustc_middle::middle::stability;
use rustc_middle::ty;
use rustc_session::lint::builtin::{LEGACY_DERIVE_HELPERS, SOFT_UNSTABLE, UNUSED_MACROS};
use rustc_session::lint::builtin::{LEGACY_DERIVE_HELPERS, PROC_MACRO_DERIVE_RESOLUTION_FALLBACK};
use rustc_session::lint::builtin::{SOFT_UNSTABLE, UNUSED_MACROS};
use rustc_session::lint::BuiltinLintDiagnostics;
use rustc_session::parse::feature_err;
use rustc_session::Session;
@ -642,7 +643,7 @@ impl<'a> Resolver<'a> {
crate fn early_resolve_ident_in_lexical_scope(
&mut self,
orig_ident: Ident,
scope_set: ScopeSet,
scope_set: ScopeSet<'a>,
parent_scope: &ParentScope<'a>,
record_used: bool,
force: bool,
@ -669,6 +670,7 @@ impl<'a> Resolver<'a> {
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.
@ -777,19 +779,34 @@ impl<'a> Resolver<'a> {
Err((Determinacy::Determined, _)) => Err(Determinacy::Determined),
}
}
Scope::Module(module) => {
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,
true,
!matches!(scope_set, ScopeSet::Late(..)),
record_used,
path_span,
);
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() {
@ -873,7 +890,7 @@ impl<'a> Resolver<'a> {
Ok((binding, flags))
if sub_namespace_match(binding.macro_kind(), macro_kind) =>
{
if !record_used {
if !record_used || matches!(scope_set, ScopeSet::Late(..)) {
return Some(Ok(binding));
}