Skip to content

Commit de6d33c

Browse files
committed
Auto merge of #151550 - petrochenkov:packhyg2, r=nnethercote
resolve: Replace `Macros20NormalizedIdent` with `IdentKey` This is a continuation of #150741 and #150982 based on the ideas from #151491 (comment). Before this PR `Macros20NormalizedIdent` was used as a key in various "identifier -> its resolution" maps in `rustc_resolve`. `Macros20NormalizedIdent` is a newtype around `Ident` in which `SyntaxContext` (packed inside `Span`) is guaranteed to be normalized using `normalize_to_macros_2_0`. This type is also used in a number of functions looking up identifiers in those maps. `Macros20NormalizedIdent` still contains span locations, which are useless and ignored during hash map lookups and comparisons due to `Ident`'s special `PartialEq` and `Hash` impls. This PR replaces `Macros20NormalizedIdent` with a new type called `IdentKey`, which contains only a symbol and a normalized unpacked syntax context. (E.g. `IdentKey` == `Macros20NormalizedIdent` minus span locations.) So we avoid keeping additional data and doing some syntax context packing/unpacking. Along with `IdentKey` you can often see `orig_ident_span: Span` being passed around. This is an unnormalized span of the original `Ident` from which `IdentKey` was obtained. It is not used in map keys, but it is used in a number of other scenarios: - diagnostics - edition checks - `allow_unstable` checks This is because `normalize_to_macros_2_0` normalization is lossy and the normalized spans / syntax contexts no longer contain parts of macro backtraces, while the original span contains everything.
2 parents 1e5065a + 5726e37 commit de6d33c

File tree

19 files changed

+495
-411
lines changed

19 files changed

+495
-411
lines changed

compiler/rustc_resolve/src/build_reduced_graph.rs

Lines changed: 51 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ use rustc_middle::metadata::{ModChild, Reexport};
2626
use rustc_middle::ty::{Feed, Visibility};
2727
use rustc_middle::{bug, span_bug};
2828
use rustc_span::hygiene::{ExpnId, LocalExpnId, MacroKind};
29-
use rustc_span::{Ident, Macros20NormalizedIdent, Span, Symbol, kw, sym};
29+
use rustc_span::{Ident, Span, Symbol, kw, sym};
3030
use thin_vec::ThinVec;
3131
use tracing::debug;
3232

@@ -36,9 +36,9 @@ use crate::imports::{ImportData, ImportKind};
3636
use crate::macros::{MacroRulesDecl, MacroRulesScope, MacroRulesScopeRef};
3737
use crate::ref_mut::CmCell;
3838
use crate::{
39-
BindingKey, Decl, DeclData, DeclKind, ExternPreludeEntry, Finalize, MacroData, Module,
40-
ModuleKind, ModuleOrUniformRoot, ParentScope, PathResult, ResolutionError, Resolver, Segment,
41-
Used, VisResolutionError, errors,
39+
BindingKey, Decl, DeclData, DeclKind, ExternPreludeEntry, Finalize, IdentKey, MacroData,
40+
Module, ModuleKind, ModuleOrUniformRoot, ParentScope, PathResult, ResolutionError, Resolver,
41+
Segment, Used, VisResolutionError, errors,
4242
};
4343

4444
type Res = def::Res<NodeId>;
@@ -48,36 +48,40 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
4848
/// and report an error in case of a collision.
4949
pub(crate) fn plant_decl_into_local_module(
5050
&mut self,
51-
ident: Macros20NormalizedIdent,
51+
ident: IdentKey,
52+
orig_ident_span: Span,
5253
ns: Namespace,
5354
decl: Decl<'ra>,
5455
) {
55-
if let Err(old_decl) = self.try_plant_decl_into_local_module(ident, ns, decl, false) {
56-
self.report_conflict(ident.0, ns, old_decl, decl);
56+
if let Err(old_decl) =
57+
self.try_plant_decl_into_local_module(ident, orig_ident_span, ns, decl, false)
58+
{
59+
self.report_conflict(ident, ns, old_decl, decl);
5760
}
5861
}
5962

6063
/// Create a name definitinon from the given components, and put it into the local module.
6164
fn define_local(
6265
&mut self,
6366
parent: Module<'ra>,
64-
ident: Ident,
67+
orig_ident: Ident,
6568
ns: Namespace,
6669
res: Res,
6770
vis: Visibility,
6871
span: Span,
6972
expn_id: LocalExpnId,
7073
) {
7174
let decl = self.arenas.new_def_decl(res, vis.to_def_id(), span, expn_id, Some(parent));
72-
let ident = Macros20NormalizedIdent::new(ident);
73-
self.plant_decl_into_local_module(ident, ns, decl);
75+
let ident = IdentKey::new(orig_ident);
76+
self.plant_decl_into_local_module(ident, orig_ident.span, ns, decl);
7477
}
7578

7679
/// Create a name definitinon from the given components, and put it into the extern module.
7780
fn define_extern(
7881
&self,
7982
parent: Module<'ra>,
80-
ident: Macros20NormalizedIdent,
83+
ident: IdentKey,
84+
orig_ident_span: Span,
8185
ns: Namespace,
8286
child_index: usize,
8387
res: Res,
@@ -102,7 +106,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
102106
let key =
103107
BindingKey::new_disambiguated(ident, ns, || (child_index + 1).try_into().unwrap()); // 0 indicates no underscore
104108
if self
105-
.resolution_or_default(parent, key)
109+
.resolution_or_default(parent, key, orig_ident_span)
106110
.borrow_mut_unchecked()
107111
.non_glob_decl
108112
.replace(decl)
@@ -279,8 +283,8 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
279283
.unwrap_or_else(|| res.def_id()),
280284
)
281285
};
282-
let ModChild { ident, res, vis, ref reexport_chain } = *child;
283-
let ident = Macros20NormalizedIdent::new(ident);
286+
let ModChild { ident: orig_ident, res, vis, ref reexport_chain } = *child;
287+
let ident = IdentKey::new(orig_ident);
284288
let span = child_span(self, reexport_chain, res);
285289
let res = res.expect_non_local();
286290
let expansion = parent_scope.expansion;
@@ -293,7 +297,18 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
293297

294298
// Record primary definitions.
295299
let define_extern = |ns| {
296-
self.define_extern(parent, ident, ns, child_index, res, vis, span, expansion, ambig)
300+
self.define_extern(
301+
parent,
302+
ident,
303+
orig_ident.span,
304+
ns,
305+
child_index,
306+
res,
307+
vis,
308+
span,
309+
expansion,
310+
ambig,
311+
)
297312
};
298313
match res {
299314
Res::Def(
@@ -533,8 +548,8 @@ impl<'a, 'ra, 'tcx> BuildReducedGraphVisitor<'a, 'ra, 'tcx> {
533548
if target.name != kw::Underscore {
534549
self.r.per_ns(|this, ns| {
535550
if !type_ns_only || ns == TypeNS {
536-
let key = BindingKey::new(Macros20NormalizedIdent::new(target), ns);
537-
this.resolution_or_default(current_module, key)
551+
let key = BindingKey::new(IdentKey::new(target), ns);
552+
this.resolution_or_default(current_module, key, target.span)
538553
.borrow_mut(this)
539554
.single_imports
540555
.insert(import);
@@ -974,7 +989,7 @@ impl<'a, 'ra, 'tcx> BuildReducedGraphVisitor<'a, 'ra, 'tcx> {
974989
&mut self,
975990
orig_name: Option<Symbol>,
976991
item: &Item,
977-
ident: Ident,
992+
orig_ident: Ident,
978993
local_def_id: LocalDefId,
979994
vis: Visibility,
980995
) {
@@ -983,7 +998,7 @@ impl<'a, 'ra, 'tcx> BuildReducedGraphVisitor<'a, 'ra, 'tcx> {
983998
let parent = parent_scope.module;
984999
let expansion = parent_scope.expansion;
9851000

986-
let (used, module, decl) = if orig_name.is_none() && ident.name == kw::SelfLower {
1001+
let (used, module, decl) = if orig_name.is_none() && orig_ident.name == kw::SelfLower {
9871002
self.r.dcx().emit_err(errors::ExternCrateSelfRequiresRenaming { span: sp });
9881003
return;
9891004
} else if orig_name == Some(kw::SelfLower) {
@@ -1008,7 +1023,7 @@ impl<'a, 'ra, 'tcx> BuildReducedGraphVisitor<'a, 'ra, 'tcx> {
10081023
})
10091024
.unwrap_or((true, None, self.r.dummy_decl));
10101025
let import = self.r.arenas.alloc_import(ImportData {
1011-
kind: ImportKind::ExternCrate { source: orig_name, target: ident, id: item.id },
1026+
kind: ImportKind::ExternCrate { source: orig_name, target: orig_ident, id: item.id },
10121027
root_id: item.id,
10131028
parent_scope,
10141029
imported_module: CmCell::new(module),
@@ -1026,7 +1041,7 @@ impl<'a, 'ra, 'tcx> BuildReducedGraphVisitor<'a, 'ra, 'tcx> {
10261041
}
10271042
self.r.potentially_unused_imports.push(import);
10281043
let import_decl = self.r.new_import_decl(decl, import);
1029-
let ident = Macros20NormalizedIdent::new(ident);
1044+
let ident = IdentKey::new(orig_ident);
10301045
if ident.name != kw::Underscore && parent == self.r.graph_root {
10311046
// FIXME: this error is technically unnecessary now when extern prelude is split into
10321047
// two scopes, remove it with lang team approval.
@@ -1045,20 +1060,20 @@ impl<'a, 'ra, 'tcx> BuildReducedGraphVisitor<'a, 'ra, 'tcx> {
10451060
Entry::Occupied(mut occupied) => {
10461061
let entry = occupied.get_mut();
10471062
if entry.item_decl.is_some() {
1048-
let msg = format!("extern crate `{ident}` already in extern prelude");
1063+
let msg = format!("extern crate `{orig_ident}` already in extern prelude");
10491064
self.r.tcx.dcx().span_delayed_bug(item.span, msg);
10501065
} else {
1051-
entry.item_decl = Some((import_decl, orig_name.is_some()));
1066+
entry.item_decl = Some((import_decl, orig_ident.span, orig_name.is_some()));
10521067
}
10531068
entry
10541069
}
10551070
Entry::Vacant(vacant) => vacant.insert(ExternPreludeEntry {
1056-
item_decl: Some((import_decl, true)),
1071+
item_decl: Some((import_decl, orig_ident.span, true)),
10571072
flag_decl: None,
10581073
}),
10591074
};
10601075
}
1061-
self.r.plant_decl_into_local_module(ident, TypeNS, import_decl);
1076+
self.r.plant_decl_into_local_module(ident, orig_ident.span, TypeNS, import_decl);
10621077
}
10631078

10641079
/// Constructs the reduced graph for one foreign item.
@@ -1159,7 +1174,7 @@ impl<'a, 'ra, 'tcx> BuildReducedGraphVisitor<'a, 'ra, 'tcx> {
11591174
if let Some(span) = import_all {
11601175
let import = macro_use_import(self, span, false);
11611176
self.r.potentially_unused_imports.push(import);
1162-
module.for_each_child_mut(self, |this, ident, ns, binding| {
1177+
module.for_each_child_mut(self, |this, ident, _, ns, binding| {
11631178
if ns == MacroNS {
11641179
let import =
11651180
if this.r.is_accessible_from(binding.vis(), this.parent_scope.module) {
@@ -1270,7 +1285,7 @@ impl<'a, 'ra, 'tcx> BuildReducedGraphVisitor<'a, 'ra, 'tcx> {
12701285
let expansion = parent_scope.expansion;
12711286
let feed = self.r.feed(item.id);
12721287
let def_id = feed.key();
1273-
let (res, ident, span, macro_rules) = match &item.kind {
1288+
let (res, orig_ident, span, macro_rules) = match &item.kind {
12741289
ItemKind::MacroDef(ident, def) => {
12751290
(self.res(def_id), *ident, item.span, def.macro_rules)
12761291
}
@@ -1293,8 +1308,8 @@ impl<'a, 'ra, 'tcx> BuildReducedGraphVisitor<'a, 'ra, 'tcx> {
12931308
self.r.local_macro_def_scopes.insert(def_id, parent_scope.module);
12941309

12951310
if macro_rules {
1296-
let ident = Macros20NormalizedIdent::new(ident);
1297-
self.r.macro_names.insert(ident.0);
1311+
let ident = IdentKey::new(orig_ident);
1312+
self.r.macro_names.insert(ident);
12981313
let is_macro_export = ast::attr::contains_name(&item.attrs, sym::macro_export);
12991314
let vis = if is_macro_export {
13001315
Visibility::Public
@@ -1326,17 +1341,18 @@ impl<'a, 'ra, 'tcx> BuildReducedGraphVisitor<'a, 'ra, 'tcx> {
13261341
});
13271342
self.r.import_use_map.insert(import, Used::Other);
13281343
let import_decl = self.r.new_import_decl(decl, import);
1329-
self.r.plant_decl_into_local_module(ident, MacroNS, import_decl);
1344+
self.r.plant_decl_into_local_module(ident, orig_ident.span, MacroNS, import_decl);
13301345
} else {
1331-
self.r.check_reserved_macro_name(ident.0, res);
1332-
self.insert_unused_macro(ident.0, def_id, item.id);
1346+
self.r.check_reserved_macro_name(ident.name, orig_ident.span, res);
1347+
self.insert_unused_macro(orig_ident, def_id, item.id);
13331348
}
13341349
self.r.feed_visibility(feed, vis);
13351350
let scope = self.r.arenas.alloc_macro_rules_scope(MacroRulesScope::Def(
13361351
self.r.arenas.alloc_macro_rules_decl(MacroRulesDecl {
13371352
parent_macro_rules_scope: parent_scope.macro_rules,
13381353
decl,
13391354
ident,
1355+
orig_ident_span: orig_ident.span,
13401356
}),
13411357
));
13421358
self.r.macro_rules_scopes.insert(def_id, scope);
@@ -1352,9 +1368,9 @@ impl<'a, 'ra, 'tcx> BuildReducedGraphVisitor<'a, 'ra, 'tcx> {
13521368
_ => self.resolve_visibility(&item.vis),
13531369
};
13541370
if !vis.is_public() {
1355-
self.insert_unused_macro(ident, def_id, item.id);
1371+
self.insert_unused_macro(orig_ident, def_id, item.id);
13561372
}
1357-
self.r.define_local(module, ident, MacroNS, res, vis, span, expansion);
1373+
self.r.define_local(module, orig_ident, MacroNS, res, vis, span, expansion);
13581374
self.r.feed_visibility(feed, vis);
13591375
self.parent_scope.macro_rules
13601376
}
@@ -1496,7 +1512,7 @@ impl<'a, 'ra, 'tcx> Visitor<'a> for BuildReducedGraphVisitor<'a, 'ra, 'tcx> {
14961512
{
14971513
// Don't add underscore names, they cannot be looked up anyway.
14981514
let impl_def_id = self.r.tcx.local_parent(local_def_id);
1499-
let key = BindingKey::new(Macros20NormalizedIdent::new(ident), ns);
1515+
let key = BindingKey::new(IdentKey::new(ident), ns);
15001516
self.r.impl_binding_keys.entry(impl_def_id).or_default().insert(key);
15011517
}
15021518

compiler/rustc_resolve/src/check_unused.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,10 @@ use rustc_session::lint::BuiltinLintDiag;
3333
use rustc_session::lint::builtin::{
3434
MACRO_USE_EXTERN_CRATE, UNUSED_EXTERN_CRATES, UNUSED_IMPORTS, UNUSED_QUALIFICATIONS,
3535
};
36-
use rustc_span::{DUMMY_SP, Ident, Macros20NormalizedIdent, Span, kw};
36+
use rustc_span::{DUMMY_SP, Ident, Span, kw};
3737

3838
use crate::imports::{Import, ImportKind};
39-
use crate::{DeclKind, LateDecl, Resolver, module_to_string};
39+
use crate::{DeclKind, IdentKey, LateDecl, Resolver, module_to_string};
4040

4141
struct UnusedImport {
4242
use_tree: ast::UseTree,
@@ -203,7 +203,7 @@ impl<'a, 'ra, 'tcx> UnusedImportCheckVisitor<'a, 'ra, 'tcx> {
203203
if self
204204
.r
205205
.extern_prelude
206-
.get(&Macros20NormalizedIdent::new(extern_crate.ident))
206+
.get(&IdentKey::new(extern_crate.ident))
207207
.is_none_or(|entry| entry.introduced_by_item())
208208
{
209209
continue;

0 commit comments

Comments
 (0)