resolve: Avoid double normalization in resolve_ident_in_module

This commit is contained in:
Vadim Petrochenkov 2026-01-30 17:05:57 +03:00
parent 726a0a68b1
commit 161fcdeac0
3 changed files with 29 additions and 15 deletions

View file

@ -905,7 +905,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
pub(crate) fn resolve_ident_in_module<'r>(
self: CmResolver<'r, 'ra, 'tcx>,
module: ModuleOrUniformRoot<'ra>,
mut ident: Ident,
ident: Ident,
ns: Namespace,
parent_scope: &ParentScope<'ra>,
finalize: Option<Finalize>,
@ -914,15 +914,14 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
) -> Result<Decl<'ra>, Determinacy> {
match module {
ModuleOrUniformRoot::Module(module) => {
let tmp_parent_scope;
let mut adjusted_parent_scope = parent_scope;
if let Some(def) = ident.span.normalize_to_macros_2_0_and_adjust(module.expansion) {
tmp_parent_scope =
ParentScope { module: self.expn_def_scope(def), ..*parent_scope };
adjusted_parent_scope = &tmp_parent_scope;
}
self.resolve_ident_in_scope_set(
ident,
let (ident_key, def) = IdentKey::new_adjusted(ident, module.expansion);
let adjusted_parent_scope = match def {
Some(def) => ParentScope { module: self.expn_def_scope(def), ..*parent_scope },
None => *parent_scope,
};
self.resolve_ident_in_scope_set_inner(
ident_key,
ident.span,
ScopeSet::Module(ns, module),
&adjusted_parent_scope,
finalize,
@ -942,9 +941,9 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
if ns != TypeNS {
Err(Determined)
} else {
ident.span.normalize_to_macros_2_0_and_adjust(ExpnId::root());
self.resolve_ident_in_scope_set(
ident,
self.resolve_ident_in_scope_set_inner(
IdentKey::new_adjusted(ident, ExpnId::root()).0,
ident.span,
ScopeSet::ExternPrelude,
parent_scope,
finalize,

View file

@ -575,6 +575,12 @@ impl IdentKey {
IdentKey { name: ident.name, ctxt: Macros20NormalizedSyntaxContext::new(ident.span.ctxt()) }
}
#[inline]
fn new_adjusted(ident: Ident, expn_id: ExpnId) -> (IdentKey, Option<ExpnId>) {
let (ctxt, def) = Macros20NormalizedSyntaxContext::new_adjusted(ident.span.ctxt(), expn_id);
(IdentKey { name: ident.name, ctxt }, def)
}
#[inline]
fn with_root_ctxt(name: Symbol) -> Self {
let ctxt = Macros20NormalizedSyntaxContext::new_unchecked(SyntaxContext::root());
@ -2724,7 +2730,7 @@ mod ref_mut {
}
mod hygiene {
use rustc_span::SyntaxContext;
use rustc_span::{ExpnId, SyntaxContext};
/// A newtype around `SyntaxContext` that can only keep contexts produced by
/// [SyntaxContext::normalize_to_macros_2_0].
@ -2737,6 +2743,15 @@ mod hygiene {
Macros20NormalizedSyntaxContext(ctxt.normalize_to_macros_2_0())
}
#[inline]
pub(crate) fn new_adjusted(
mut ctxt: SyntaxContext,
expn_id: ExpnId,
) -> (Macros20NormalizedSyntaxContext, Option<ExpnId>) {
let def = ctxt.normalize_to_macros_2_0_and_adjust(expn_id);
(Macros20NormalizedSyntaxContext(ctxt), def)
}
#[inline]
pub(crate) fn new_unchecked(ctxt: SyntaxContext) -> Macros20NormalizedSyntaxContext {
debug_assert_eq!(ctxt, ctxt.normalize_to_macros_2_0());

View file

@ -804,7 +804,7 @@ impl SyntaxContext {
/// Like `SyntaxContext::adjust`, but also normalizes `self` to macros 2.0.
#[inline]
pub(crate) fn normalize_to_macros_2_0_and_adjust(&mut self, expn_id: ExpnId) -> Option<ExpnId> {
pub fn normalize_to_macros_2_0_and_adjust(&mut self, expn_id: ExpnId) -> Option<ExpnId> {
HygieneData::with(|data| {
*self = data.normalize_to_macros_2_0(*self);
data.adjust(self, expn_id)