Skip to content

Commit 6e7b9d5

Browse files
committed
Introduce ModernIdent type to unify macro 2.0 hygiene handling
Signed-off-by: xizheyin <[email protected]>
1 parent dc0bae1 commit 6e7b9d5

File tree

9 files changed

+97
-44
lines changed

9 files changed

+97
-44
lines changed

compiler/rustc_resolve/src/build_reduced_graph.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ use rustc_middle::metadata::ModChild;
2727
use rustc_middle::ty::{Feed, Visibility};
2828
use rustc_middle::{bug, span_bug};
2929
use rustc_span::hygiene::{ExpnId, LocalExpnId, MacroKind};
30-
use rustc_span::{Ident, Span, Symbol, kw, sym};
30+
use rustc_span::{Ident, Macros20NormalizedIdent, Span, Symbol, kw, sym};
3131
use thin_vec::ThinVec;
3232
use tracing::debug;
3333

@@ -969,8 +969,8 @@ impl<'a, 'ra, 'tcx> BuildReducedGraphVisitor<'a, 'ra, 'tcx> {
969969
self.r.potentially_unused_imports.push(import);
970970
let imported_binding = self.r.import(binding, import);
971971
if ident.name != kw::Underscore && parent == self.r.graph_root {
972-
let ident = ident.normalize_to_macros_2_0();
973-
if let Some(entry) = self.r.extern_prelude.get(&ident)
972+
let norm_ident = Macros20NormalizedIdent::new(ident);
973+
if let Some(entry) = self.r.extern_prelude.get(&norm_ident)
974974
&& expansion != LocalExpnId::ROOT
975975
&& orig_name.is_some()
976976
&& !entry.is_import()
@@ -986,7 +986,7 @@ impl<'a, 'ra, 'tcx> BuildReducedGraphVisitor<'a, 'ra, 'tcx> {
986986
}
987987

988988
use indexmap::map::Entry;
989-
match self.r.extern_prelude.entry(ident) {
989+
match self.r.extern_prelude.entry(norm_ident) {
990990
Entry::Occupied(mut occupied) => {
991991
let entry = occupied.get_mut();
992992
if let Some(old_binding) = entry.binding.get()

compiler/rustc_resolve/src/check_unused.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ 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, Span, kw};
36+
use rustc_span::{DUMMY_SP, Ident, Macros20NormalizedIdent, Span, kw};
3737

3838
use crate::imports::{Import, ImportKind};
3939
use crate::{LexicalScopeBinding, NameBindingKind, Resolver, module_to_string};
@@ -203,7 +203,7 @@ impl<'a, 'ra, 'tcx> UnusedImportCheckVisitor<'a, 'ra, 'tcx> {
203203
if self
204204
.r
205205
.extern_prelude
206-
.get(&extern_crate.ident)
206+
.get(&Macros20NormalizedIdent::new(extern_crate.ident))
207207
.is_none_or(|entry| entry.introduced_by_item)
208208
{
209209
continue;

compiler/rustc_resolve/src/diagnostics.rs

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ use rustc_span::edit_distance::find_best_match_for_name;
3030
use rustc_span::edition::Edition;
3131
use rustc_span::hygiene::MacroKind;
3232
use rustc_span::source_map::SourceMap;
33-
use rustc_span::{BytePos, Ident, Span, Symbol, SyntaxContext, kw, sym};
33+
use rustc_span::{BytePos, Ident, Macros20NormalizedIdent, Span, Symbol, SyntaxContext, kw, sym};
3434
use thin_vec::{ThinVec, thin_vec};
3535
use tracing::{debug, instrument};
3636

@@ -320,8 +320,10 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
320320
// Check if the target of the use for both bindings is the same.
321321
let duplicate = new_binding.res().opt_def_id() == old_binding.res().opt_def_id();
322322
let has_dummy_span = new_binding.span.is_dummy() || old_binding.span.is_dummy();
323-
let from_item =
324-
self.extern_prelude.get(&ident).is_none_or(|entry| entry.introduced_by_item);
323+
let from_item = self
324+
.extern_prelude
325+
.get(&Macros20NormalizedIdent::new(ident))
326+
.is_none_or(|entry| entry.introduced_by_item);
325327
// Only suggest removing an import if both bindings are to the same def, if both spans
326328
// aren't dummy spans. Further, if both bindings are imports, then the ident must have
327329
// been introduced by an item.
@@ -530,7 +532,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
530532
module.for_each_child(self, |_this, ident, _ns, binding| {
531533
let res = binding.res();
532534
if filter_fn(res) && ctxt.is_none_or(|ctxt| ctxt == ident.span.ctxt()) {
533-
names.push(TypoSuggestion::typo_from_ident(ident, res));
535+
names.push(TypoSuggestion::typo_from_ident(ident.0, res));
534536
}
535537
});
536538
}
@@ -1100,7 +1102,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
11001102
Scope::ExternPrelude => {
11011103
suggestions.extend(this.extern_prelude.keys().filter_map(|ident| {
11021104
let res = Res::Def(DefKind::Mod, CRATE_DEF_ID.to_def_id());
1103-
filter_fn(res).then_some(TypoSuggestion::typo_from_ident(*ident, res))
1105+
filter_fn(res).then_some(TypoSuggestion::typo_from_ident(ident.0, res))
11041106
}));
11051107
}
11061108
Scope::ToolPrelude => {
@@ -1246,7 +1248,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
12461248
};
12471249
segms.append(&mut path_segments.clone());
12481250

1249-
segms.push(ast::PathSegment::from_ident(ident));
1251+
segms.push(ast::PathSegment::from_ident(ident.0));
12501252
let path = Path { span: name_binding.span, segments: segms, tokens: None };
12511253

12521254
if child_accessible
@@ -1319,7 +1321,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
13191321
if let Some(def_id) = name_binding.res().module_like_def_id() {
13201322
// form the path
13211323
let mut path_segments = path_segments.clone();
1322-
path_segments.push(ast::PathSegment::from_ident(ident));
1324+
path_segments.push(ast::PathSegment::from_ident(ident.0));
13231325

13241326
let alias_import = if let NameBindingKind::Import { import, .. } =
13251327
name_binding.kind
@@ -1453,7 +1455,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
14531455
if needs_disambiguation {
14541456
crate_path.push(ast::PathSegment::path_root(rustc_span::DUMMY_SP));
14551457
}
1456-
crate_path.push(ast::PathSegment::from_ident(ident));
1458+
crate_path.push(ast::PathSegment::from_ident(ident.0));
14571459

14581460
suggestions.extend(self.lookup_import_candidates_from_module(
14591461
lookup_ident,

compiler/rustc_resolve/src/imports.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -489,7 +489,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
489489
// Define or update `binding` in `module`s glob importers.
490490
for import in glob_importers.iter() {
491491
let mut ident = key.ident;
492-
let scope = match ident.span.reverse_glob_adjust(module.expansion, import.span) {
492+
let scope = match ident.0.span.reverse_glob_adjust(module.expansion, import.span) {
493493
Some(Some(def)) => self.expn_def_scope(def),
494494
Some(None) => import.parent_scope.module,
495495
None => continue,
@@ -498,7 +498,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
498498
let imported_binding = self.import(binding, *import);
499499
let _ = self.try_define_local(
500500
import.parent_scope.module,
501-
ident,
501+
ident.0,
502502
key.ns,
503503
imported_binding,
504504
warn_ambiguity,
@@ -1504,7 +1504,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
15041504
})
15051505
.collect::<Vec<_>>();
15061506
for (mut key, binding) in bindings {
1507-
let scope = match key.ident.span.reverse_glob_adjust(module.expansion, import.span) {
1507+
let scope = match key.ident.0.span.reverse_glob_adjust(module.expansion, import.span) {
15081508
Some(Some(def)) => self.expn_def_scope(def),
15091509
Some(None) => import.parent_scope.module,
15101510
None => continue,
@@ -1517,7 +1517,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
15171517
.is_some_and(|binding| binding.warn_ambiguity_recursive());
15181518
let _ = self.try_define_local(
15191519
import.parent_scope.module,
1520-
key.ident,
1520+
key.ident.0,
15211521
key.ns,
15221522
imported_binding,
15231523
warn_ambiguity,
@@ -1550,7 +1550,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
15501550
next_binding = binding;
15511551
}
15521552

1553-
children.push(ModChild { ident, res, vis: binding.vis, reexport_chain });
1553+
children.push(ModChild { ident: ident.0, res, vis: binding.vis, reexport_chain });
15541554
}
15551555
});
15561556

compiler/rustc_resolve/src/late/diagnostics.rs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1472,7 +1472,10 @@ impl<'ast, 'ra, 'tcx> LateResolutionVisitor<'_, 'ast, 'ra, 'tcx> {
14721472
})
14731473
.collect();
14741474
if let [target] = targets.as_slice() {
1475-
return Some(TypoSuggestion::single_item_from_ident(target.0.ident, target.1));
1475+
return Some(TypoSuggestion::single_item_from_ident(
1476+
target.0.ident.0,
1477+
target.1,
1478+
));
14761479
}
14771480
}
14781481
}
@@ -2479,7 +2482,7 @@ impl<'ast, 'ra, 'tcx> LateResolutionVisitor<'_, 'ast, 'ra, 'tcx> {
24792482
names.extend(self.r.extern_prelude.keys().flat_map(|ident| {
24802483
let res = Res::Def(DefKind::Mod, CRATE_DEF_ID.to_def_id());
24812484
filter_fn(res)
2482-
.then_some(TypoSuggestion::typo_from_ident(*ident, res))
2485+
.then_some(TypoSuggestion::typo_from_ident(ident.0, res))
24832486
}));
24842487

24852488
if let Some(prelude) = self.r.prelude {
@@ -2639,7 +2642,7 @@ impl<'ast, 'ra, 'tcx> LateResolutionVisitor<'_, 'ast, 'ra, 'tcx> {
26392642
if let Some(module_def_id) = name_binding.res().module_like_def_id() {
26402643
// form the path
26412644
let mut path_segments = path_segments.clone();
2642-
path_segments.push(ast::PathSegment::from_ident(ident));
2645+
path_segments.push(ast::PathSegment::from_ident(ident.0));
26432646
let doc_visible = doc_visible
26442647
&& (module_def_id.is_local() || !r.tcx.is_doc_hidden(module_def_id));
26452648
if module_def_id == def_id {
@@ -2678,7 +2681,7 @@ impl<'ast, 'ra, 'tcx> LateResolutionVisitor<'_, 'ast, 'ra, 'tcx> {
26782681
enum_module.for_each_child(self.r, |_, ident, _, name_binding| {
26792682
if let Res::Def(DefKind::Ctor(CtorOf::Variant, kind), def_id) = name_binding.res() {
26802683
let mut segms = enum_import_suggestion.path.segments.clone();
2681-
segms.push(ast::PathSegment::from_ident(ident));
2684+
segms.push(ast::PathSegment::from_ident(ident.0));
26822685
let path = Path { span: name_binding.span, segments: segms, tokens: None };
26832686
variants.push((path, def_id, kind));
26842687
}

compiler/rustc_resolve/src/lib.rs

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ use rustc_query_system::ich::StableHashingContext;
7171
use rustc_session::lint::builtin::PRIVATE_MACRO_USE;
7272
use rustc_session::lint::{BuiltinLintDiag, LintBuffer};
7373
use rustc_span::hygiene::{ExpnId, LocalExpnId, MacroKind, SyntaxContext, Transparency};
74-
use rustc_span::{DUMMY_SP, Ident, Span, Symbol, kw, sym};
74+
use rustc_span::{DUMMY_SP, Ident, Macros20NormalizedIdent, Span, Symbol, kw, sym};
7575
use smallvec::{SmallVec, smallvec};
7676
use tracing::debug;
7777

@@ -531,7 +531,7 @@ impl ModuleKind {
531531
struct BindingKey {
532532
/// The identifier for the binding, always the `normalize_to_macros_2_0` version of the
533533
/// identifier.
534-
ident: Ident,
534+
ident: Macros20NormalizedIdent,
535535
ns: Namespace,
536536
/// When we add an underscore binding (with ident `_`) to some module, this field has
537537
/// a non-zero value that uniquely identifies this binding in that module.
@@ -543,7 +543,7 @@ struct BindingKey {
543543

544544
impl BindingKey {
545545
fn new(ident: Ident, ns: Namespace) -> Self {
546-
BindingKey { ident: ident.normalize_to_macros_2_0(), ns, disambiguator: 0 }
546+
BindingKey { ident: Macros20NormalizedIdent::new(ident), ns, disambiguator: 0 }
547547
}
548548

549549
fn new_disambiguated(
@@ -552,7 +552,7 @@ impl BindingKey {
552552
disambiguator: impl FnOnce() -> u32,
553553
) -> BindingKey {
554554
let disambiguator = if ident.name == kw::Underscore { disambiguator() } else { 0 };
555-
BindingKey { ident: ident.normalize_to_macros_2_0(), ns, disambiguator }
555+
BindingKey { ident: Macros20NormalizedIdent::new(ident), ns, disambiguator }
556556
}
557557
}
558558

@@ -593,7 +593,8 @@ struct ModuleData<'ra> {
593593
globs: RefCell<Vec<Import<'ra>>>,
594594

595595
/// Used to memoize the traits in this module for faster searches through all traits in scope.
596-
traits: RefCell<Option<Box<[(Ident, NameBinding<'ra>, Option<Module<'ra>>)]>>>,
596+
traits:
597+
RefCell<Option<Box<[(Macros20NormalizedIdent, NameBinding<'ra>, Option<Module<'ra>>)]>>>,
597598

598599
/// Span of the module itself. Used for error reporting.
599600
span: Span,
@@ -659,7 +660,7 @@ impl<'ra> Module<'ra> {
659660
fn for_each_child<'tcx, R: AsRef<Resolver<'ra, 'tcx>>>(
660661
self,
661662
resolver: &R,
662-
mut f: impl FnMut(&R, Ident, Namespace, NameBinding<'ra>),
663+
mut f: impl FnMut(&R, Macros20NormalizedIdent, Namespace, NameBinding<'ra>),
663664
) {
664665
for (key, name_resolution) in resolver.as_ref().resolutions(self).borrow().iter() {
665666
if let Some(binding) = name_resolution.borrow().best_binding() {
@@ -671,7 +672,7 @@ impl<'ra> Module<'ra> {
671672
fn for_each_child_mut<'tcx, R: AsMut<Resolver<'ra, 'tcx>>>(
672673
self,
673674
resolver: &mut R,
674-
mut f: impl FnMut(&mut R, Ident, Namespace, NameBinding<'ra>),
675+
mut f: impl FnMut(&mut R, Macros20NormalizedIdent, Namespace, NameBinding<'ra>),
675676
) {
676677
for (key, name_resolution) in resolver.as_mut().resolutions(self).borrow().iter() {
677678
if let Some(binding) = name_resolution.borrow().best_binding() {
@@ -1054,7 +1055,7 @@ pub struct Resolver<'ra, 'tcx> {
10541055
graph_root: Module<'ra>,
10551056

10561057
prelude: Option<Module<'ra>>,
1057-
extern_prelude: FxIndexMap<Ident, ExternPreludeEntry<'ra>>,
1058+
extern_prelude: FxIndexMap<Macros20NormalizedIdent, ExternPreludeEntry<'ra>>,
10581059

10591060
/// N.B., this is used only for better diagnostics, not name resolution itself.
10601061
field_names: LocalDefIdMap<Vec<Ident>>,
@@ -1499,17 +1500,19 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
14991500
&& let name = Symbol::intern(name)
15001501
&& name.can_be_raw()
15011502
{
1502-
Some((Ident::with_dummy_span(name), Default::default()))
1503+
Some((Macros20NormalizedIdent::with_dummy_span(name), Default::default()))
15031504
} else {
15041505
None
15051506
}
15061507
})
15071508
.collect();
15081509

15091510
if !attr::contains_name(attrs, sym::no_core) {
1510-
extern_prelude.insert(Ident::with_dummy_span(sym::core), Default::default());
1511+
extern_prelude
1512+
.insert(Macros20NormalizedIdent::with_dummy_span(sym::core), Default::default());
15111513
if !attr::contains_name(attrs, sym::no_std) {
1512-
extern_prelude.insert(Ident::with_dummy_span(sym::std), Default::default());
1514+
extern_prelude
1515+
.insert(Macros20NormalizedIdent::with_dummy_span(sym::std), Default::default());
15131516
}
15141517
}
15151518

@@ -1879,7 +1882,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
18791882
for &(trait_name, trait_binding, trait_module) in traits.as_ref().unwrap().iter() {
18801883
if self.trait_may_have_item(trait_module, assoc_item) {
18811884
let def_id = trait_binding.res().def_id();
1882-
let import_ids = self.find_transitive_imports(&trait_binding.kind, trait_name);
1885+
let import_ids = self.find_transitive_imports(&trait_binding.kind, trait_name.0);
18831886
found_traits.push(TraitCandidate { def_id, import_ids });
18841887
}
18851888
}
@@ -2020,7 +2023,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
20202023
// Avoid marking `extern crate` items that refer to a name from extern prelude,
20212024
// but not introduce it, as used if they are accessed from lexical scope.
20222025
if used == Used::Scope {
2023-
if let Some(entry) = self.extern_prelude.get(&ident.normalize_to_macros_2_0()) {
2026+
if let Some(entry) = self.extern_prelude.get(&Macros20NormalizedIdent::new(ident)) {
20242027
if !entry.introduced_by_item && entry.binding.get() == Some(used_binding) {
20252028
return;
20262029
}
@@ -2179,7 +2182,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
21792182

21802183
fn extern_prelude_get(&mut self, ident: Ident, finalize: bool) -> Option<NameBinding<'ra>> {
21812184
let mut record_use = None;
2182-
let entry = self.extern_prelude.get(&ident.normalize_to_macros_2_0());
2185+
let entry = self.extern_prelude.get(&Macros20NormalizedIdent::new(ident));
21832186
let binding = entry.and_then(|entry| match entry.binding.get() {
21842187
Some(binding) if binding.is_import() => {
21852188
if finalize {

compiler/rustc_resolve/src/macros.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -536,11 +536,11 @@ impl<'ra, 'tcx> ResolverExpand for Resolver<'ra, 'tcx> {
536536
target_trait.for_each_child(self, |this, ident, ns, _binding| {
537537
// FIXME: Adjust hygiene for idents from globs, like for glob imports.
538538
if let Some(overriding_keys) = this.impl_binding_keys.get(&impl_def_id)
539-
&& overriding_keys.contains(&BindingKey::new(ident, ns))
539+
&& overriding_keys.contains(&BindingKey::new(ident.0, ns))
540540
{
541541
// The name is overridden, do not produce it from the glob delegation.
542542
} else {
543-
idents.push((ident, None));
543+
idents.push((ident.0, None));
544544
}
545545
});
546546
Ok(idents)

compiler/rustc_span/src/lib.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,8 @@ pub use span_encoding::{DUMMY_SP, Span};
6666

6767
pub mod symbol;
6868
pub use symbol::{
69-
ByteSymbol, Ident, MacroRulesNormalizedIdent, STDLIB_STABLE_CRATES, Symbol, kw, sym,
69+
ByteSymbol, Ident, MacroRulesNormalizedIdent, Macros20NormalizedIdent, STDLIB_STABLE_CRATES,
70+
Symbol, kw, sym,
7071
};
7172

7273
mod analyze_source_file;

0 commit comments

Comments
 (0)