Skip to content

Commit 161fcde

Browse files
committed
resolve: Avoid double normalization in resolve_ident_in_module
1 parent 726a0a6 commit 161fcde

3 files changed

Lines changed: 29 additions & 15 deletions

File tree

compiler/rustc_resolve/src/ident.rs

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -905,7 +905,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
905905
pub(crate) fn resolve_ident_in_module<'r>(
906906
self: CmResolver<'r, 'ra, 'tcx>,
907907
module: ModuleOrUniformRoot<'ra>,
908-
mut ident: Ident,
908+
ident: Ident,
909909
ns: Namespace,
910910
parent_scope: &ParentScope<'ra>,
911911
finalize: Option<Finalize>,
@@ -914,15 +914,14 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
914914
) -> Result<Decl<'ra>, Determinacy> {
915915
match module {
916916
ModuleOrUniformRoot::Module(module) => {
917-
let tmp_parent_scope;
918-
let mut adjusted_parent_scope = parent_scope;
919-
if let Some(def) = ident.span.normalize_to_macros_2_0_and_adjust(module.expansion) {
920-
tmp_parent_scope =
921-
ParentScope { module: self.expn_def_scope(def), ..*parent_scope };
922-
adjusted_parent_scope = &tmp_parent_scope;
923-
}
924-
self.resolve_ident_in_scope_set(
925-
ident,
917+
let (ident_key, def) = IdentKey::new_adjusted(ident, module.expansion);
918+
let adjusted_parent_scope = match def {
919+
Some(def) => ParentScope { module: self.expn_def_scope(def), ..*parent_scope },
920+
None => *parent_scope,
921+
};
922+
self.resolve_ident_in_scope_set_inner(
923+
ident_key,
924+
ident.span,
926925
ScopeSet::Module(ns, module),
927926
&adjusted_parent_scope,
928927
finalize,
@@ -942,9 +941,9 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
942941
if ns != TypeNS {
943942
Err(Determined)
944943
} else {
945-
ident.span.normalize_to_macros_2_0_and_adjust(ExpnId::root());
946-
self.resolve_ident_in_scope_set(
947-
ident,
944+
self.resolve_ident_in_scope_set_inner(
945+
IdentKey::new_adjusted(ident, ExpnId::root()).0,
946+
ident.span,
948947
ScopeSet::ExternPrelude,
949948
parent_scope,
950949
finalize,

compiler/rustc_resolve/src/lib.rs

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -575,6 +575,12 @@ impl IdentKey {
575575
IdentKey { name: ident.name, ctxt: Macros20NormalizedSyntaxContext::new(ident.span.ctxt()) }
576576
}
577577

578+
#[inline]
579+
fn new_adjusted(ident: Ident, expn_id: ExpnId) -> (IdentKey, Option<ExpnId>) {
580+
let (ctxt, def) = Macros20NormalizedSyntaxContext::new_adjusted(ident.span.ctxt(), expn_id);
581+
(IdentKey { name: ident.name, ctxt }, def)
582+
}
583+
578584
#[inline]
579585
fn with_root_ctxt(name: Symbol) -> Self {
580586
let ctxt = Macros20NormalizedSyntaxContext::new_unchecked(SyntaxContext::root());
@@ -2724,7 +2730,7 @@ mod ref_mut {
27242730
}
27252731

27262732
mod hygiene {
2727-
use rustc_span::SyntaxContext;
2733+
use rustc_span::{ExpnId, SyntaxContext};
27282734

27292735
/// A newtype around `SyntaxContext` that can only keep contexts produced by
27302736
/// [SyntaxContext::normalize_to_macros_2_0].
@@ -2737,6 +2743,15 @@ mod hygiene {
27372743
Macros20NormalizedSyntaxContext(ctxt.normalize_to_macros_2_0())
27382744
}
27392745

2746+
#[inline]
2747+
pub(crate) fn new_adjusted(
2748+
mut ctxt: SyntaxContext,
2749+
expn_id: ExpnId,
2750+
) -> (Macros20NormalizedSyntaxContext, Option<ExpnId>) {
2751+
let def = ctxt.normalize_to_macros_2_0_and_adjust(expn_id);
2752+
(Macros20NormalizedSyntaxContext(ctxt), def)
2753+
}
2754+
27402755
#[inline]
27412756
pub(crate) fn new_unchecked(ctxt: SyntaxContext) -> Macros20NormalizedSyntaxContext {
27422757
debug_assert_eq!(ctxt, ctxt.normalize_to_macros_2_0());

compiler/rustc_span/src/hygiene.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -804,7 +804,7 @@ impl SyntaxContext {
804804

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

0 commit comments

Comments
 (0)