Skip to content

Commit e520b01

Browse files
Auto merge of #150741 - petrochenkov:modsplitfast, r=<try>
resolve: Use `Macros20NormalizedIdent` in more interfaces
2 parents 4d73a00 + 32c0a85 commit e520b01

File tree

8 files changed

+77
-83
lines changed

8 files changed

+77
-83
lines changed

compiler/rustc_resolve/src/build_reduced_graph.rs

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -49,12 +49,12 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
4949
pub(crate) fn define_binding_local(
5050
&mut self,
5151
parent: Module<'ra>,
52-
ident: Ident,
52+
ident: Macros20NormalizedIdent,
5353
ns: Namespace,
5454
binding: NameBinding<'ra>,
5555
) {
5656
if let Err(old_binding) = self.try_define_local(parent, ident, ns, binding, false) {
57-
self.report_conflict(parent, ident, ns, old_binding, binding);
57+
self.report_conflict(parent, ident.0, ns, old_binding, binding);
5858
}
5959
}
6060

@@ -69,13 +69,14 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
6969
expn_id: LocalExpnId,
7070
) {
7171
let binding = self.arenas.new_res_binding(res, vis.to_def_id(), span, expn_id);
72+
let ident = Macros20NormalizedIdent::new(ident);
7273
self.define_binding_local(parent, ident, ns, binding);
7374
}
7475

7576
fn define_extern(
7677
&self,
7778
parent: Module<'ra>,
78-
ident: Ident,
79+
ident: Macros20NormalizedIdent,
7980
ns: Namespace,
8081
child_index: usize,
8182
res: Res,
@@ -277,6 +278,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
277278
)
278279
};
279280
let ModChild { ident, res, vis, ref reexport_chain } = *child;
281+
let ident = Macros20NormalizedIdent::new(ident);
280282
let span = child_span(self, reexport_chain, res);
281283
let res = res.expect_non_local();
282284
let expansion = parent_scope.expansion;
@@ -529,7 +531,7 @@ impl<'a, 'ra, 'tcx> BuildReducedGraphVisitor<'a, 'ra, 'tcx> {
529531
if target.name != kw::Underscore {
530532
self.r.per_ns(|this, ns| {
531533
if !type_ns_only || ns == TypeNS {
532-
let key = BindingKey::new(target, ns);
534+
let key = BindingKey::new(Macros20NormalizedIdent::new(target), ns);
533535
this.resolution_or_default(current_module, key)
534536
.borrow_mut(this)
535537
.single_imports
@@ -1020,11 +1022,11 @@ impl<'a, 'ra, 'tcx> BuildReducedGraphVisitor<'a, 'ra, 'tcx> {
10201022
}
10211023
self.r.potentially_unused_imports.push(import);
10221024
let imported_binding = self.r.import(binding, import);
1025+
let ident = Macros20NormalizedIdent::new(ident);
10231026
if ident.name != kw::Underscore && parent == self.r.graph_root {
1024-
let norm_ident = Macros20NormalizedIdent::new(ident);
10251027
// FIXME: this error is technically unnecessary now when extern prelude is split into
10261028
// two scopes, remove it with lang team approval.
1027-
if let Some(entry) = self.r.extern_prelude.get(&norm_ident)
1029+
if let Some(entry) = self.r.extern_prelude.get(&ident)
10281030
&& expansion != LocalExpnId::ROOT
10291031
&& orig_name.is_some()
10301032
&& entry.item_binding.is_none()
@@ -1035,7 +1037,7 @@ impl<'a, 'ra, 'tcx> BuildReducedGraphVisitor<'a, 'ra, 'tcx> {
10351037
}
10361038

10371039
use indexmap::map::Entry;
1038-
match self.r.extern_prelude.entry(norm_ident) {
1040+
match self.r.extern_prelude.entry(ident) {
10391041
Entry::Occupied(mut occupied) => {
10401042
let entry = occupied.get_mut();
10411043
if entry.item_binding.is_some() {
@@ -1292,8 +1294,8 @@ impl<'a, 'ra, 'tcx> BuildReducedGraphVisitor<'a, 'ra, 'tcx> {
12921294
self.r.local_macro_def_scopes.insert(def_id, parent_scope.module);
12931295

12941296
if macro_rules {
1295-
let ident = ident.normalize_to_macros_2_0();
1296-
self.r.macro_names.insert(ident);
1297+
let ident = Macros20NormalizedIdent::new(ident);
1298+
self.r.macro_names.insert(ident.0);
12971299
let is_macro_export = ast::attr::contains_name(&item.attrs, sym::macro_export);
12981300
let vis = if is_macro_export {
12991301
Visibility::Public
@@ -1322,8 +1324,8 @@ impl<'a, 'ra, 'tcx> BuildReducedGraphVisitor<'a, 'ra, 'tcx> {
13221324
let import_binding = self.r.import(binding, import);
13231325
self.r.define_binding_local(self.r.graph_root, ident, MacroNS, import_binding);
13241326
} else {
1325-
self.r.check_reserved_macro_name(ident, res);
1326-
self.insert_unused_macro(ident, def_id, item.id);
1327+
self.r.check_reserved_macro_name(ident.0, res);
1328+
self.insert_unused_macro(ident.0, def_id, item.id);
13271329
}
13281330
self.r.feed_visibility(feed, vis);
13291331
let scope = self.r.arenas.alloc_macro_rules_scope(MacroRulesScope::Binding(
@@ -1490,7 +1492,7 @@ impl<'a, 'ra, 'tcx> Visitor<'a> for BuildReducedGraphVisitor<'a, 'ra, 'tcx> {
14901492
{
14911493
// Don't add underscore names, they cannot be looked up anyway.
14921494
let impl_def_id = self.r.tcx.local_parent(local_def_id);
1493-
let key = BindingKey::new(ident, ns);
1495+
let key = BindingKey::new(Macros20NormalizedIdent::new(ident), ns);
14941496
self.r.impl_binding_keys.entry(impl_def_id).or_default().insert(key);
14951497
}
14961498

compiler/rustc_resolve/src/diagnostics.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1188,7 +1188,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
11881188
.get(&expn_id)
11891189
.into_iter()
11901190
.flatten()
1191-
.map(|(ident, _)| TypoSuggestion::typo_from_ident(*ident, res)),
1191+
.map(|(ident, _)| TypoSuggestion::typo_from_ident(ident.0, res)),
11921192
);
11931193
}
11941194
}
@@ -1200,7 +1200,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
12001200
let res = macro_rules_binding.binding.res();
12011201
if filter_fn(res) {
12021202
suggestions.push(TypoSuggestion::typo_from_ident(
1203-
macro_rules_binding.ident,
1203+
macro_rules_binding.ident.0,
12041204
res,
12051205
))
12061206
}
@@ -2899,7 +2899,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
28992899
return None;
29002900
}
29012901

2902-
let binding_key = BindingKey::new(ident, MacroNS);
2902+
let binding_key = BindingKey::new(Macros20NormalizedIdent::new(ident), MacroNS);
29032903
let binding = self.resolution(crate_module, binding_key)?.binding()?;
29042904
let Res::Def(DefKind::Macro(kinds), _) = binding.res() else {
29052905
return None;

compiler/rustc_resolve/src/ident.rs

Lines changed: 23 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use rustc_middle::{bug, span_bug};
99
use rustc_session::lint::builtin::PROC_MACRO_DERIVE_RESOLUTION_FALLBACK;
1010
use rustc_session::parse::feature_err;
1111
use rustc_span::hygiene::{ExpnId, ExpnKind, LocalExpnId, MacroKind, SyntaxContext};
12-
use rustc_span::{Ident, Span, kw, sym};
12+
use rustc_span::{Ident, Macros20NormalizedIdent, Span, kw, sym};
1313
use tracing::{debug, instrument};
1414

1515
use crate::errors::{ParamKindInEnumDiscriminant, ParamKindInNonTrivialAnonConst};
@@ -51,7 +51,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
5151
mut self: CmResolver<'r, 'ra, 'tcx>,
5252
scope_set: ScopeSet<'ra>,
5353
parent_scope: &ParentScope<'ra>,
54-
ctxt: SyntaxContext,
54+
orig_ctxt: SyntaxContext,
5555
derive_fallback_lint_id: Option<NodeId>,
5656
mut visitor: impl FnMut(
5757
&mut CmResolver<'r, 'ra, 'tcx>,
@@ -101,7 +101,6 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
101101
// 4c. Standard library prelude (de-facto closed, controlled).
102102
// 6. Language prelude: builtin attributes (closed, controlled).
103103

104-
let rust_2015 = ctxt.edition().is_rust_2015();
105104
let (ns, macro_kind) = match scope_set {
106105
ScopeSet::All(ns)
107106
| ScopeSet::Module(ns, _)
@@ -124,7 +123,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
124123
TypeNS | ValueNS => Scope::ModuleNonGlobs(module, None),
125124
MacroNS => Scope::DeriveHelpers(parent_scope.expansion),
126125
};
127-
let mut ctxt = ctxt.normalize_to_macros_2_0();
126+
let mut ctxt = orig_ctxt.normalize_to_macros_2_0();
128127
let mut use_prelude = !module.no_implicit_prelude;
129128

130129
loop {
@@ -149,7 +148,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
149148
true
150149
}
151150
Scope::ModuleNonGlobs(..) | Scope::ModuleGlobs(..) => true,
152-
Scope::MacroUsePrelude => use_prelude || rust_2015,
151+
Scope::MacroUsePrelude => use_prelude || orig_ctxt.edition().is_rust_2015(),
153152
Scope::BuiltinAttrs => true,
154153
Scope::ExternPreludeItems | Scope::ExternPreludeFlags => {
155154
use_prelude || module_and_extern_prelude || extern_prelude
@@ -435,12 +434,14 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
435434
orig_ident.span.ctxt(),
436435
derive_fallback_lint_id,
437436
|this, scope, use_prelude, ctxt| {
437+
let ident = Ident::new(orig_ident.name, orig_ident.span.with_ctxt(ctxt));
438+
// The passed `ctxt` is already normalized, so avoid expensive double normalization.
439+
let ident = Macros20NormalizedIdent(ident);
438440
let res = match this.reborrow().resolve_ident_in_scope(
439-
orig_ident,
441+
ident,
440442
ns,
441443
scope,
442444
use_prelude,
443-
ctxt,
444445
scope_set,
445446
parent_scope,
446447
// Shadowed bindings don't need to be marked as used or non-speculatively loaded.
@@ -509,19 +510,17 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
509510

510511
fn resolve_ident_in_scope<'r>(
511512
mut self: CmResolver<'r, 'ra, 'tcx>,
512-
orig_ident: Ident,
513+
ident: Macros20NormalizedIdent,
513514
ns: Namespace,
514515
scope: Scope<'ra>,
515516
use_prelude: UsePrelude,
516-
ctxt: SyntaxContext,
517517
scope_set: ScopeSet<'ra>,
518518
parent_scope: &ParentScope<'ra>,
519519
finalize: Option<Finalize>,
520520
force: bool,
521521
ignore_binding: Option<NameBinding<'ra>>,
522522
ignore_import: Option<Import<'ra>>,
523523
) -> Result<NameBinding<'ra>, ControlFlow<Determinacy, Determinacy>> {
524-
let ident = Ident::new(orig_ident.name, orig_ident.span.with_ctxt(ctxt));
525524
let ret = match scope {
526525
Scope::DeriveHelpers(expn_id) => {
527526
if let Some(binding) = self.helper_attrs.get(&expn_id).and_then(|attrs| {
@@ -600,11 +599,11 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
600599
self.get_mut().lint_buffer.buffer_lint(
601600
PROC_MACRO_DERIVE_RESOLUTION_FALLBACK,
602601
lint_id,
603-
orig_ident.span,
602+
ident.span,
604603
errors::ProcMacroDeriveResolutionFallback {
605-
span: orig_ident.span,
604+
span: ident.span,
606605
ns_descr: ns.descr(),
607-
ident,
606+
ident: ident.0,
608607
},
609608
);
610609
}
@@ -650,11 +649,11 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
650649
self.get_mut().lint_buffer.buffer_lint(
651650
PROC_MACRO_DERIVE_RESOLUTION_FALLBACK,
652651
lint_id,
653-
orig_ident.span,
652+
ident.span,
654653
errors::ProcMacroDeriveResolutionFallback {
655-
span: orig_ident.span,
654+
span: ident.span,
656655
ns_descr: ns.descr(),
657-
ident,
656+
ident: ident.0,
658657
},
659658
);
660659
}
@@ -700,7 +699,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
700699
let mut result = Err(Determinacy::Determined);
701700
if let Some(prelude) = self.prelude
702701
&& let Ok(binding) = self.reborrow().resolve_ident_in_scope_set(
703-
ident,
702+
ident.0,
704703
ScopeSet::Module(ns, prelude),
705704
parent_scope,
706705
None,
@@ -987,7 +986,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
987986
fn resolve_ident_in_module_non_globs_unadjusted<'r>(
988987
mut self: CmResolver<'r, 'ra, 'tcx>,
989988
module: Module<'ra>,
990-
ident: Ident,
989+
ident: Macros20NormalizedIdent,
991990
ns: Namespace,
992991
parent_scope: &ParentScope<'ra>,
993992
shadowing: Shadowing,
@@ -1010,7 +1009,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
10101009

10111010
if let Some(finalize) = finalize {
10121011
return self.get_mut().finalize_module_binding(
1013-
ident,
1012+
ident.0,
10141013
binding,
10151014
parent_scope,
10161015
module,
@@ -1050,7 +1049,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
10501049
fn resolve_ident_in_module_globs_unadjusted<'r>(
10511050
mut self: CmResolver<'r, 'ra, 'tcx>,
10521051
module: Module<'ra>,
1053-
ident: Ident,
1052+
ident: Macros20NormalizedIdent,
10541053
ns: Namespace,
10551054
parent_scope: &ParentScope<'ra>,
10561055
shadowing: Shadowing,
@@ -1071,7 +1070,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
10711070

10721071
if let Some(finalize) = finalize {
10731072
return self.get_mut().finalize_module_binding(
1074-
ident,
1073+
ident.0,
10751074
binding,
10761075
parent_scope,
10771076
module,
@@ -1140,9 +1139,8 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
11401139
None => return Err(ControlFlow::Continue(Undetermined)),
11411140
};
11421141
let tmp_parent_scope;
1143-
let (mut adjusted_parent_scope, mut ident) =
1144-
(parent_scope, ident.normalize_to_macros_2_0());
1145-
match ident.span.glob_adjust(module.expansion, glob_import.span) {
1142+
let (mut adjusted_parent_scope, mut ident) = (parent_scope, ident);
1143+
match ident.0.span.glob_adjust(module.expansion, glob_import.span) {
11461144
Some(Some(def)) => {
11471145
tmp_parent_scope =
11481146
ParentScope { module: self.expn_def_scope(def), ..*parent_scope };
@@ -1152,7 +1150,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
11521150
None => continue,
11531151
};
11541152
let result = self.reborrow().resolve_ident_in_scope_set(
1155-
ident,
1153+
ident.0,
11561154
ScopeSet::Module(ns, module),
11571155
adjusted_parent_scope,
11581156
None,

compiler/rustc_resolve/src/imports.rs

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ use rustc_session::lint::builtin::{
2020
use rustc_session::parse::feature_err;
2121
use rustc_span::edit_distance::find_best_match_for_name;
2222
use rustc_span::hygiene::LocalExpnId;
23-
use rustc_span::{Ident, Span, Symbol, kw, sym};
23+
use rustc_span::{Ident, Macros20NormalizedIdent, Span, Symbol, kw, sym};
2424
use tracing::debug;
2525

2626
use crate::Namespace::{self, *};
@@ -335,13 +335,13 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
335335
pub(crate) fn try_define_local(
336336
&mut self,
337337
module: Module<'ra>,
338-
ident: Ident,
338+
ident: Macros20NormalizedIdent,
339339
ns: Namespace,
340340
binding: NameBinding<'ra>,
341341
warn_ambiguity: bool,
342342
) -> Result<(), NameBinding<'ra>> {
343343
let res = binding.res();
344-
self.check_reserved_macro_name(ident, res);
344+
self.check_reserved_macro_name(ident.0, res);
345345
self.set_binding_parent_module(binding, module);
346346
// Even if underscore names cannot be looked up, we still need to add them to modules,
347347
// because they can be fetched by glob imports from those modules, and bring traits
@@ -480,7 +480,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
480480
let imported_binding = self.import(binding, *import);
481481
let _ = self.try_define_local(
482482
import.parent_scope.module,
483-
ident.0,
483+
ident,
484484
key.ns,
485485
imported_binding,
486486
warn_ambiguity,
@@ -504,10 +504,11 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
504504
let dummy_binding = self.import(dummy_binding, import);
505505
self.per_ns(|this, ns| {
506506
let module = import.parent_scope.module;
507-
let _ = this.try_define_local(module, target, ns, dummy_binding, false);
507+
let ident = Macros20NormalizedIdent::new(target);
508+
let _ = this.try_define_local(module, ident, ns, dummy_binding, false);
508509
// Don't remove underscores from `single_imports`, they were never added.
509510
if target.name != kw::Underscore {
510-
let key = BindingKey::new(target, ns);
511+
let key = BindingKey::new(ident, ns);
511512
this.update_local_resolution(module, key, false, |_, resolution| {
512513
resolution.single_imports.swap_remove(&import);
513514
})
@@ -887,7 +888,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
887888
let imported_binding = this.import(binding, import);
888889
this.get_mut_unchecked().define_binding_local(
889890
parent,
890-
target,
891+
Macros20NormalizedIdent::new(target),
891892
ns,
892893
imported_binding,
893894
);
@@ -896,7 +897,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
896897
Err(Determinacy::Determined) => {
897898
// Don't remove underscores from `single_imports`, they were never added.
898899
if target.name != kw::Underscore {
899-
let key = BindingKey::new(target, ns);
900+
let key = BindingKey::new(Macros20NormalizedIdent::new(target), ns);
900901
this.get_mut_unchecked().update_local_resolution(
901902
parent,
902903
key,
@@ -1512,7 +1513,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
15121513
.is_some_and(|binding| binding.warn_ambiguity_recursive());
15131514
let _ = self.try_define_local(
15141515
import.parent_scope.module,
1515-
key.ident.0,
1516+
key.ident,
15161517
key.ns,
15171518
imported_binding,
15181519
warn_ambiguity,

0 commit comments

Comments
 (0)