Skip to content

Commit 1c6af14

Browse files
committed
resolve: Avoid double normalization in resolve_ident_in_module
1 parent 3987fc1 commit 1c6af14

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
@@ -923,7 +923,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
923923
pub(crate) fn resolve_ident_in_module<'r>(
924924
self: CmResolver<'r, 'ra, 'tcx>,
925925
module: ModuleOrUniformRoot<'ra>,
926-
mut ident: Ident,
926+
ident: Ident,
927927
ns: Namespace,
928928
parent_scope: &ParentScope<'ra>,
929929
finalize: Option<Finalize>,
@@ -932,15 +932,14 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
932932
) -> Result<Decl<'ra>, Determinacy> {
933933
match module {
934934
ModuleOrUniformRoot::Module(module) => {
935-
let tmp_parent_scope;
936-
let mut adjusted_parent_scope = parent_scope;
937-
if let Some(def) = ident.span.normalize_to_macros_2_0_and_adjust(module.expansion) {
938-
tmp_parent_scope =
939-
ParentScope { module: self.expn_def_scope(def), ..*parent_scope };
940-
adjusted_parent_scope = &tmp_parent_scope;
941-
}
942-
self.resolve_ident_in_scope_set(
943-
ident,
935+
let (ident_key, def) = IdentKey::new_adjusted(ident, module.expansion);
936+
let adjusted_parent_scope = match def {
937+
Some(def) => ParentScope { module: self.expn_def_scope(def), ..*parent_scope },
938+
None => *parent_scope,
939+
};
940+
self.resolve_ident_in_scope_set_inner(
941+
ident_key,
942+
ident.span,
944943
ScopeSet::Module(ns, module),
945944
&adjusted_parent_scope,
946945
finalize,
@@ -962,9 +961,9 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
962961
if ns != TypeNS {
963962
Err(Determined)
964963
} else {
965-
ident.span.normalize_to_macros_2_0_and_adjust(ExpnId::root());
966-
self.resolve_ident_in_scope_set(
967-
ident,
964+
self.resolve_ident_in_scope_set_inner(
965+
IdentKey::new_adjusted(ident, ExpnId::root()).0,
966+
ident.span,
968967
ScopeSet::ExternPrelude,
969968
parent_scope,
970969
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)