Skip to content

Commit d6b68ca

Browse files
Auto merge of #150741 - petrochenkov:modsplitfast, r=<try>
resolve: Use `Macros20NormalizedIdent` in more interfaces
2 parents d9617c8 + 99a7d28 commit d6b68ca

8 files changed

Lines changed: 82 additions & 88 deletions

File tree

compiler/rustc_resolve/src/build_reduced_graph.rs

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -49,13 +49,13 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
4949
pub(crate) fn plant_decl_into_local_module(
5050
&mut self,
5151
parent: Module<'ra>,
52-
ident: Ident,
52+
ident: Macros20NormalizedIdent,
5353
ns: Namespace,
5454
decl: Decl<'ra>,
5555
) {
5656
if let Err(old_decl) = self.try_plant_decl_into_local_module(parent, ident, ns, decl, false)
5757
{
58-
self.report_conflict(parent, ident, ns, old_decl, decl);
58+
self.report_conflict(parent, ident.0, ns, old_decl, decl);
5959
}
6060
}
6161

@@ -71,14 +71,15 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
7171
expn_id: LocalExpnId,
7272
) {
7373
let decl = self.arenas.new_def_decl(res, vis.to_def_id(), span, expn_id);
74+
let ident = Macros20NormalizedIdent::new(ident);
7475
self.plant_decl_into_local_module(parent, ident, ns, decl);
7576
}
7677

7778
/// Create a name definitinon from the given components, and put it into the extern module.
7879
fn define_extern(
7980
&self,
8081
parent: Module<'ra>,
81-
ident: Ident,
82+
ident: Macros20NormalizedIdent,
8283
ns: Namespace,
8384
child_index: usize,
8485
res: Res,
@@ -280,6 +281,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
280281
)
281282
};
282283
let ModChild { ident, res, vis, ref reexport_chain } = *child;
284+
let ident = Macros20NormalizedIdent::new(ident);
283285
let span = child_span(self, reexport_chain, res);
284286
let res = res.expect_non_local();
285287
let expansion = parent_scope.expansion;
@@ -532,7 +534,7 @@ impl<'a, 'ra, 'tcx> BuildReducedGraphVisitor<'a, 'ra, 'tcx> {
532534
if target.name != kw::Underscore {
533535
self.r.per_ns(|this, ns| {
534536
if !type_ns_only || ns == TypeNS {
535-
let key = BindingKey::new(target, ns);
537+
let key = BindingKey::new(Macros20NormalizedIdent::new(target), ns);
536538
this.resolution_or_default(current_module, key)
537539
.borrow_mut(this)
538540
.single_imports
@@ -1023,11 +1025,11 @@ impl<'a, 'ra, 'tcx> BuildReducedGraphVisitor<'a, 'ra, 'tcx> {
10231025
}
10241026
self.r.potentially_unused_imports.push(import);
10251027
let import_decl = self.r.new_import_decl(decl, import);
1028+
let ident = Macros20NormalizedIdent::new(ident);
10261029
if ident.name != kw::Underscore && parent == self.r.graph_root {
1027-
let norm_ident = Macros20NormalizedIdent::new(ident);
10281030
// FIXME: this error is technically unnecessary now when extern prelude is split into
10291031
// two scopes, remove it with lang team approval.
1030-
if let Some(entry) = self.r.extern_prelude.get(&norm_ident)
1032+
if let Some(entry) = self.r.extern_prelude.get(&ident)
10311033
&& expansion != LocalExpnId::ROOT
10321034
&& orig_name.is_some()
10331035
&& entry.item_decl.is_none()
@@ -1038,7 +1040,7 @@ impl<'a, 'ra, 'tcx> BuildReducedGraphVisitor<'a, 'ra, 'tcx> {
10381040
}
10391041

10401042
use indexmap::map::Entry;
1041-
match self.r.extern_prelude.entry(norm_ident) {
1043+
match self.r.extern_prelude.entry(ident) {
10421044
Entry::Occupied(mut occupied) => {
10431045
let entry = occupied.get_mut();
10441046
if entry.item_decl.is_some() {
@@ -1290,8 +1292,8 @@ impl<'a, 'ra, 'tcx> BuildReducedGraphVisitor<'a, 'ra, 'tcx> {
12901292
self.r.local_macro_def_scopes.insert(def_id, parent_scope.module);
12911293

12921294
if macro_rules {
1293-
let ident = ident.normalize_to_macros_2_0();
1294-
self.r.macro_names.insert(ident);
1295+
let ident = Macros20NormalizedIdent::new(ident);
1296+
self.r.macro_names.insert(ident.0);
12951297
let is_macro_export = ast::attr::contains_name(&item.attrs, sym::macro_export);
12961298
let vis = if is_macro_export {
12971299
Visibility::Public
@@ -1320,8 +1322,8 @@ impl<'a, 'ra, 'tcx> BuildReducedGraphVisitor<'a, 'ra, 'tcx> {
13201322
let import_decl = self.r.new_import_decl(decl, import);
13211323
self.r.plant_decl_into_local_module(self.r.graph_root, ident, MacroNS, import_decl);
13221324
} else {
1323-
self.r.check_reserved_macro_name(ident, res);
1324-
self.insert_unused_macro(ident, def_id, item.id);
1325+
self.r.check_reserved_macro_name(ident.0, res);
1326+
self.insert_unused_macro(ident.0, def_id, item.id);
13251327
}
13261328
self.r.feed_visibility(feed, vis);
13271329
let scope = self.r.arenas.alloc_macro_rules_scope(MacroRulesScope::Def(
@@ -1488,7 +1490,7 @@ impl<'a, 'ra, 'tcx> Visitor<'a> for BuildReducedGraphVisitor<'a, 'ra, 'tcx> {
14881490
{
14891491
// Don't add underscore names, they cannot be looked up anyway.
14901492
let impl_def_id = self.r.tcx.local_parent(local_def_id);
1491-
let key = BindingKey::new(ident, ns);
1493+
let key = BindingKey::new(Macros20NormalizedIdent::new(ident), ns);
14921494
self.r.impl_binding_keys.entry(impl_def_id).or_default().insert(key);
14931495
}
14941496

compiler/rustc_resolve/src/diagnostics.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1187,7 +1187,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
11871187
.get(&expn_id)
11881188
.into_iter()
11891189
.flatten()
1190-
.map(|(ident, _)| TypoSuggestion::typo_from_ident(*ident, res)),
1190+
.map(|(ident, _)| TypoSuggestion::typo_from_ident(ident.0, res)),
11911191
);
11921192
}
11931193
}
@@ -1199,7 +1199,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
11991199
let res = macro_rules_def.decl.res();
12001200
if filter_fn(res) {
12011201
suggestions
1202-
.push(TypoSuggestion::typo_from_ident(macro_rules_def.ident, res))
1202+
.push(TypoSuggestion::typo_from_ident(macro_rules_def.ident.0, res))
12031203
}
12041204
}
12051205
}
@@ -2892,7 +2892,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
28922892
return None;
28932893
}
28942894

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

compiler/rustc_resolve/src/ident.rs

Lines changed: 26 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@ 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};
13+
use smallvec::SmallVec;
1314
use tracing::{debug, instrument};
1415

1516
use crate::errors::{ParamKindInEnumDiscriminant, ParamKindInNonTrivialAnonConst};
@@ -50,7 +51,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
5051
mut self: CmResolver<'r, 'ra, 'tcx>,
5152
scope_set: ScopeSet<'ra>,
5253
parent_scope: &ParentScope<'ra>,
53-
ctxt: SyntaxContext,
54+
orig_ctxt: SyntaxContext,
5455
derive_fallback_lint_id: Option<NodeId>,
5556
mut visitor: impl FnMut(
5657
&mut CmResolver<'r, 'ra, 'tcx>,
@@ -100,7 +101,6 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
100101
// 4c. Standard library prelude (de-facto closed, controlled).
101102
// 6. Language prelude: builtin attributes (closed, controlled).
102103

103-
let rust_2015 = ctxt.edition().is_rust_2015();
104104
let (ns, macro_kind) = match scope_set {
105105
ScopeSet::All(ns)
106106
| ScopeSet::Module(ns, _)
@@ -123,7 +123,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
123123
TypeNS | ValueNS => Scope::ModuleNonGlobs(module, None),
124124
MacroNS => Scope::DeriveHelpers(parent_scope.expansion),
125125
};
126-
let mut ctxt = ctxt.normalize_to_macros_2_0();
126+
let mut ctxt = orig_ctxt.normalize_to_macros_2_0();
127127
let mut use_prelude = !module.no_implicit_prelude;
128128

129129
loop {
@@ -148,7 +148,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
148148
true
149149
}
150150
Scope::ModuleNonGlobs(..) | Scope::ModuleGlobs(..) => true,
151-
Scope::MacroUsePrelude => use_prelude || rust_2015,
151+
Scope::MacroUsePrelude => use_prelude || orig_ctxt.edition().is_rust_2015(),
152152
Scope::BuiltinAttrs => true,
153153
Scope::ExternPreludeItems | Scope::ExternPreludeFlags => {
154154
use_prelude || module_and_extern_prelude || extern_prelude
@@ -397,7 +397,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
397397
assert!(force || finalize.is_none()); // `finalize` implies `force`
398398

399399
// Make sure `self`, `super` etc produce an error when passed to here.
400-
if orig_ident.is_path_segment_keyword() && !matches!(scope_set, ScopeSet::Module(..)) {
400+
if !matches!(scope_set, ScopeSet::Module(..)) && orig_ident.is_path_segment_keyword() {
401401
return Err(Determinacy::Determined);
402402
}
403403

@@ -424,7 +424,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
424424
// }
425425
// So we have to save the innermost solution and continue searching in outer scopes
426426
// to detect potential ambiguities.
427-
let mut innermost_results: Vec<(Decl<'_>, Scope<'_>)> = Vec::new();
427+
let mut innermost_results: SmallVec<[(Decl<'_>, Scope<'_>); 2]> = SmallVec::new();
428428
let mut determinacy = Determinacy::Determined;
429429

430430
// Go through all the scopes and try to resolve the name.
@@ -434,12 +434,14 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
434434
orig_ident.span.ctxt(),
435435
derive_fallback_lint_id,
436436
|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);
437440
let res = match this.reborrow().resolve_ident_in_scope(
438-
orig_ident,
441+
ident,
439442
ns,
440443
scope,
441444
use_prelude,
442-
ctxt,
443445
scope_set,
444446
parent_scope,
445447
// Shadowed decls don't need to be marked as used or non-speculatively loaded.
@@ -508,19 +510,17 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
508510

509511
fn resolve_ident_in_scope<'r>(
510512
mut self: CmResolver<'r, 'ra, 'tcx>,
511-
orig_ident: Ident,
513+
ident: Macros20NormalizedIdent,
512514
ns: Namespace,
513515
scope: Scope<'ra>,
514516
use_prelude: UsePrelude,
515-
ctxt: SyntaxContext,
516517
scope_set: ScopeSet<'ra>,
517518
parent_scope: &ParentScope<'ra>,
518519
finalize: Option<Finalize>,
519520
force: bool,
520521
ignore_decl: Option<Decl<'ra>>,
521522
ignore_import: Option<Import<'ra>>,
522523
) -> Result<Decl<'ra>, ControlFlow<Determinacy, Determinacy>> {
523-
let ident = Ident::new(orig_ident.name, orig_ident.span.with_ctxt(ctxt));
524524
let ret = match scope {
525525
Scope::DeriveHelpers(expn_id) => {
526526
if let Some(decl) = self
@@ -599,11 +599,11 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
599599
self.get_mut().lint_buffer.buffer_lint(
600600
PROC_MACRO_DERIVE_RESOLUTION_FALLBACK,
601601
lint_id,
602-
orig_ident.span,
602+
ident.span,
603603
errors::ProcMacroDeriveResolutionFallback {
604-
span: orig_ident.span,
604+
span: ident.span,
605605
ns_descr: ns.descr(),
606-
ident,
606+
ident: ident.0,
607607
},
608608
);
609609
}
@@ -649,11 +649,11 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
649649
self.get_mut().lint_buffer.buffer_lint(
650650
PROC_MACRO_DERIVE_RESOLUTION_FALLBACK,
651651
lint_id,
652-
orig_ident.span,
652+
ident.span,
653653
errors::ProcMacroDeriveResolutionFallback {
654-
span: orig_ident.span,
654+
span: ident.span,
655655
ns_descr: ns.descr(),
656-
ident,
656+
ident: ident.0,
657657
},
658658
);
659659
}
@@ -699,7 +699,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
699699
let mut result = Err(Determinacy::Determined);
700700
if let Some(prelude) = self.prelude
701701
&& let Ok(decl) = self.reborrow().resolve_ident_in_scope_set(
702-
ident,
702+
ident.0,
703703
ScopeSet::Module(ns, prelude),
704704
parent_scope,
705705
None,
@@ -983,7 +983,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
983983
fn resolve_ident_in_module_non_globs_unadjusted<'r>(
984984
mut self: CmResolver<'r, 'ra, 'tcx>,
985985
module: Module<'ra>,
986-
ident: Ident,
986+
ident: Macros20NormalizedIdent,
987987
ns: Namespace,
988988
parent_scope: &ParentScope<'ra>,
989989
shadowing: Shadowing,
@@ -1006,7 +1006,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
10061006

10071007
if let Some(finalize) = finalize {
10081008
return self.get_mut().finalize_module_binding(
1009-
ident,
1009+
ident.0,
10101010
binding,
10111011
parent_scope,
10121012
module,
@@ -1046,7 +1046,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
10461046
fn resolve_ident_in_module_globs_unadjusted<'r>(
10471047
mut self: CmResolver<'r, 'ra, 'tcx>,
10481048
module: Module<'ra>,
1049-
ident: Ident,
1049+
ident: Macros20NormalizedIdent,
10501050
ns: Namespace,
10511051
parent_scope: &ParentScope<'ra>,
10521052
shadowing: Shadowing,
@@ -1067,7 +1067,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
10671067

10681068
if let Some(finalize) = finalize {
10691069
return self.get_mut().finalize_module_binding(
1070-
ident,
1070+
ident.0,
10711071
binding,
10721072
parent_scope,
10731073
module,
@@ -1136,9 +1136,8 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
11361136
None => return Err(ControlFlow::Continue(Undetermined)),
11371137
};
11381138
let tmp_parent_scope;
1139-
let (mut adjusted_parent_scope, mut ident) =
1140-
(parent_scope, ident.normalize_to_macros_2_0());
1141-
match ident.span.glob_adjust(module.expansion, glob_import.span) {
1139+
let (mut adjusted_parent_scope, mut ident) = (parent_scope, ident);
1140+
match ident.0.span.glob_adjust(module.expansion, glob_import.span) {
11421141
Some(Some(def)) => {
11431142
tmp_parent_scope =
11441143
ParentScope { module: self.expn_def_scope(def), ..*parent_scope };
@@ -1148,7 +1147,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
11481147
None => continue,
11491148
};
11501149
let result = self.reborrow().resolve_ident_in_scope_set(
1151-
ident,
1150+
ident.0,
11521151
ScopeSet::Module(ns, module),
11531152
adjusted_parent_scope,
11541153
None,

0 commit comments

Comments
 (0)