Skip to content

Commit d2ad9c8

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

File tree

9 files changed

+108
-40
lines changed

9 files changed

+108
-40
lines changed

compiler/rustc_resolve/src/build_reduced_graph.rs

Lines changed: 2 additions & 2 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,7 +969,7 @@ 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 parent == self.r.graph_root {
972-
let ident = ident.normalize_to_macros_2_0();
972+
let ident = Macros20NormalizedIdent::new(ident);
973973
if let Some(entry) = self.r.extern_prelude.get(&ident)
974974
&& expansion != LocalExpnId::ROOT
975975
&& orig_name.is_some()

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(&unsafe { Macros20NormalizedIdent::new_without_normalize(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
@@ -32,7 +32,7 @@ use rustc_span::edit_distance::find_best_match_for_name;
3232
use rustc_span::edition::Edition;
3333
use rustc_span::hygiene::MacroKind;
3434
use rustc_span::source_map::SourceMap;
35-
use rustc_span::{BytePos, Ident, Span, Symbol, SyntaxContext, kw, sym};
35+
use rustc_span::{BytePos, Ident, Macros20NormalizedIdent, Span, Symbol, SyntaxContext, kw, sym};
3636
use thin_vec::{ThinVec, thin_vec};
3737
use tracing::{debug, instrument};
3838

@@ -322,8 +322,10 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
322322
// Check if the target of the use for both bindings is the same.
323323
let duplicate = new_binding.res().opt_def_id() == old_binding.res().opt_def_id();
324324
let has_dummy_span = new_binding.span.is_dummy() || old_binding.span.is_dummy();
325-
let from_item =
326-
self.extern_prelude.get(&ident).is_none_or(|entry| entry.introduced_by_item);
325+
let from_item = self
326+
.extern_prelude
327+
.get(&unsafe { Macros20NormalizedIdent::new_without_normalize(ident) })
328+
.is_none_or(|entry| entry.introduced_by_item);
327329
// Only suggest removing an import if both bindings are to the same def, if both spans
328330
// aren't dummy spans. Further, if both bindings are imports, then the ident must have
329331
// been introduced by an item.
@@ -532,7 +534,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
532534
module.for_each_child(self, |_this, ident, _ns, binding| {
533535
let res = binding.res();
534536
if filter_fn(res) && ctxt.is_none_or(|ctxt| ctxt == ident.span.ctxt()) {
535-
names.push(TypoSuggestion::typo_from_ident(ident, res));
537+
names.push(TypoSuggestion::typo_from_ident(ident.0, res));
536538
}
537539
});
538540
}
@@ -1100,7 +1102,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
11001102
Scope::ExternPrelude => {
11011103
suggestions.extend(this.extern_prelude.iter().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
@@ -1455,7 +1457,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
14551457
if needs_disambiguation {
14561458
crate_path.push(ast::PathSegment::path_root(rustc_span::DUMMY_SP));
14571459
}
1458-
crate_path.push(ast::PathSegment::from_ident(ident));
1460+
crate_path.push(ast::PathSegment::from_ident(ident.0));
14591461

14601462
suggestions.extend(self.lookup_import_candidates_from_module(
14611463
lookup_ident,

compiler/rustc_resolve/src/imports.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -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,
@@ -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
@@ -1475,7 +1475,10 @@ impl<'ast, 'ra, 'tcx> LateResolutionVisitor<'_, 'ast, 'ra, 'tcx> {
14751475
})
14761476
.collect();
14771477
if let [target] = targets.as_slice() {
1478-
return Some(TypoSuggestion::single_item_from_ident(target.0.ident, target.1));
1478+
return Some(TypoSuggestion::single_item_from_ident(
1479+
target.0.ident.0,
1480+
target.1,
1481+
));
14791482
}
14801483
}
14811484
}
@@ -2495,7 +2498,7 @@ impl<'ast, 'ra, 'tcx> LateResolutionVisitor<'_, 'ast, 'ra, 'tcx> {
24952498
Res::Def(DefKind::Mod, crate_id.as_def_id());
24962499

24972500
filter_fn(crate_mod).then(|| {
2498-
TypoSuggestion::typo_from_ident(*ident, crate_mod)
2501+
TypoSuggestion::typo_from_ident(ident.0, crate_mod)
24992502
})
25002503
})
25012504
}));
@@ -2657,7 +2660,7 @@ impl<'ast, 'ra, 'tcx> LateResolutionVisitor<'_, 'ast, 'ra, 'tcx> {
26572660
if let Some(module_def_id) = name_binding.res().module_like_def_id() {
26582661
// form the path
26592662
let mut path_segments = path_segments.clone();
2660-
path_segments.push(ast::PathSegment::from_ident(ident));
2663+
path_segments.push(ast::PathSegment::from_ident(ident.0));
26612664
let doc_visible = doc_visible
26622665
&& (module_def_id.is_local() || !r.tcx.is_doc_hidden(module_def_id));
26632666
if module_def_id == def_id {
@@ -2696,7 +2699,7 @@ impl<'ast, 'ra, 'tcx> LateResolutionVisitor<'_, 'ast, 'ra, 'tcx> {
26962699
enum_module.for_each_child(self.r, |_, ident, _, name_binding| {
26972700
if let Res::Def(DefKind::Ctor(CtorOf::Variant, kind), def_id) = name_binding.res() {
26982701
let mut segms = enum_import_suggestion.path.segments.clone();
2699-
segms.push(ast::PathSegment::from_ident(ident));
2702+
segms.push(ast::PathSegment::from_ident(ident.0));
27002703
let path = Path { span: name_binding.span, segments: segms, tokens: None };
27012704
variants.push((path, def_id, kind));
27022705
}

compiler/rustc_resolve/src/lib.rs

Lines changed: 23 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

@@ -659,7 +659,7 @@ impl<'ra> Module<'ra> {
659659
fn for_each_child<'tcx, R: AsRef<Resolver<'ra, 'tcx>>>(
660660
self,
661661
resolver: &R,
662-
mut f: impl FnMut(&R, Ident, Namespace, NameBinding<'ra>),
662+
mut f: impl FnMut(&R, Macros20NormalizedIdent, Namespace, NameBinding<'ra>),
663663
) {
664664
for (key, name_resolution) in resolver.as_ref().resolutions(self).borrow().iter() {
665665
if let Some(binding) = name_resolution.borrow().best_binding() {
@@ -671,7 +671,7 @@ impl<'ra> Module<'ra> {
671671
fn for_each_child_mut<'tcx, R: AsMut<Resolver<'ra, 'tcx>>>(
672672
self,
673673
resolver: &mut R,
674-
mut f: impl FnMut(&mut R, Ident, Namespace, NameBinding<'ra>),
674+
mut f: impl FnMut(&mut R, Macros20NormalizedIdent, Namespace, NameBinding<'ra>),
675675
) {
676676
for (key, name_resolution) in resolver.as_mut().resolutions(self).borrow().iter() {
677677
if let Some(binding) = name_resolution.borrow().best_binding() {
@@ -690,7 +690,7 @@ impl<'ra> Module<'ra> {
690690
return;
691691
}
692692
if let Res::Def(DefKind::Trait | DefKind::TraitAlias, def_id) = binding.res() {
693-
collected_traits.push((name, binding, r.as_ref().get_module(def_id)))
693+
collected_traits.push((name.0, binding, r.as_ref().get_module(def_id)))
694694
}
695695
});
696696
*traits = Some(collected_traits.into_boxed_slice());
@@ -1049,7 +1049,7 @@ pub struct Resolver<'ra, 'tcx> {
10491049
graph_root: Module<'ra>,
10501050

10511051
prelude: Option<Module<'ra>>,
1052-
extern_prelude: FxIndexMap<Ident, ExternPreludeEntry<'ra>>,
1052+
extern_prelude: FxIndexMap<Macros20NormalizedIdent, ExternPreludeEntry<'ra>>,
10531053

10541054
/// N.B., this is used only for better diagnostics, not name resolution itself.
10551055
field_names: LocalDefIdMap<Vec<Ident>>,
@@ -1482,19 +1482,28 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
14821482
let mut invocation_parents = FxHashMap::default();
14831483
invocation_parents.insert(LocalExpnId::ROOT, InvocationParent::ROOT);
14841484

1485-
let mut extern_prelude: FxIndexMap<Ident, ExternPreludeEntry<'_>> = tcx
1485+
let mut extern_prelude: FxIndexMap<Macros20NormalizedIdent, ExternPreludeEntry<'_>> = tcx
14861486
.sess
14871487
.opts
14881488
.externs
14891489
.iter()
14901490
.filter(|(_, entry)| entry.add_prelude)
1491-
.map(|(name, _)| (Ident::from_str(name), Default::default()))
1491+
.map(|(name, _)| {
1492+
(
1493+
unsafe {
1494+
Macros20NormalizedIdent::new_without_normalize(Ident::from_str(name))
1495+
},
1496+
Default::default(),
1497+
)
1498+
})
14921499
.collect();
14931500

14941501
if !attr::contains_name(attrs, sym::no_core) {
1495-
extern_prelude.insert(Ident::with_dummy_span(sym::core), Default::default());
1502+
extern_prelude
1503+
.insert(Macros20NormalizedIdent::with_dummy_span(sym::core), Default::default());
14961504
if !attr::contains_name(attrs, sym::no_std) {
1497-
extern_prelude.insert(Ident::with_dummy_span(sym::std), Default::default());
1505+
extern_prelude
1506+
.insert(Macros20NormalizedIdent::with_dummy_span(sym::std), Default::default());
14981507
}
14991508
}
15001509

@@ -2005,7 +2014,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
20052014
// Avoid marking `extern crate` items that refer to a name from extern prelude,
20062015
// but not introduce it, as used if they are accessed from lexical scope.
20072016
if used == Used::Scope {
2008-
if let Some(entry) = self.extern_prelude.get(&ident.normalize_to_macros_2_0()) {
2017+
if let Some(entry) = self.extern_prelude.get(&Macros20NormalizedIdent::new(ident)) {
20092018
if !entry.introduced_by_item && entry.binding == Some(used_binding) {
20102019
return;
20112020
}
@@ -2168,7 +2177,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
21682177
return None;
21692178
}
21702179

2171-
let norm_ident = ident.normalize_to_macros_2_0();
2180+
let norm_ident = Macros20NormalizedIdent::new(ident);
21722181
let binding = self.extern_prelude.get(&norm_ident).cloned().and_then(|entry| {
21732182
Some(if let Some(binding) = entry.binding {
21742183
if finalize {

compiler/rustc_resolve/src/macros.rs

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