-
Notifications
You must be signed in to change notification settings - Fork 13.6k
Introduce ModernIdent type to unify macro 2.0 hygiene handling #144439
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Conversation
HIR ty lowering was modified cc @fmease |
What problem is this intended to solve? |
compiler/rustc_span/src/symbol.rs
Outdated
/// different macro 2.0 macros. | ||
// FIXME: Use more often | ||
#[derive(Copy, Clone, Eq, PartialEq, Hash)] | ||
pub struct ModernIdent(pub Ident); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
pub struct ModernIdent(pub Ident); | |
pub struct Macros20NormalizedIdent(pub Ident); |
The modern / legacy terminology for macros 2.0 / macro_rules is not used anymore.
compiler/rustc_span/src/symbol.rs
Outdated
} | ||
} | ||
|
||
impl std::borrow::Borrow<Ident> for ModernIdent { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is there any specific reason for this impl?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We can convert it into Ident by passing a reference, and some parts of the code don't have to be changed to ident.0
Do I need to delete this?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The by-ref coercions are enabled by Deref
, if I understand the case correctly.
Also, Ident
s should usually be passed by value, unless some generic trait requires a reference.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
agree with petrochenkov here. also I believe borrow generally should not be implemented for two types where one has more invariants than the other.
@@ -65,8 +65,7 @@ impl<'tcx> InherentOverlapChecker<'tcx> { | |||
fn compare_hygienically(&self, item1: ty::AssocItem, item2: ty::AssocItem) -> bool { | |||
// Symbols and namespace match, compare hygienically. | |||
item1.namespace() == item2.namespace() | |||
&& item1.ident(self.tcx).normalize_to_macros_2_0() | |||
== item2.ident(self.tcx).normalize_to_macros_2_0() | |||
&& ModernIdent::new(item1.ident(self.tcx)) == ModernIdent::new(item2.ident(self.tcx)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think replacing the normalize_to_macros_2_0
calls in general is an improvement.
The new ident type makes sense only if it's stored in something or passed to something having an invariant.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That makes sense, so I need to delete some unnecessary substitutions.
compiler/rustc_span/src/symbol.rs
Outdated
impl ModernIdent { | ||
#[inline] | ||
pub fn new(ident: Ident) -> Self { | ||
Self(ident.normalize_to_macros_2_0()) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Self(ident.normalize_to_macros_2_0()) | |
ModernIdent(ident.normalize_to_macros_2_0()) |
Could you avoid using Self
outside of generic code?
compiler/rustc_span/src/symbol.rs
Outdated
} | ||
|
||
#[inline] | ||
pub fn with_dummy_span(name: Symbol) -> Self { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This method isn't used anywhere.
compiler/rustc_resolve/src/macros.rs
Outdated
@@ -1139,14 +1139,16 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> { | |||
} | |||
} | |||
|
|||
pub(crate) fn check_reserved_macro_name(&self, ident: Ident, res: Res) { | |||
pub(crate) fn check_reserved_macro_name(&self, ident: ModernIdent, res: Res) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
pub(crate) fn check_reserved_macro_name(&self, ident: ModernIdent, res: Res) { | |
pub(crate) fn check_reserved_macro_name(&self, ident: Ident, res: Res) { |
This function would take a Symbol
if it not needed the span for the error.
In general, for displaying spans non-normalized idents are better.
In general, I have mixed feelings about both this and |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In this version, I removed some unnecessary uses.
The modifications are mainly centered around two fields.
- BindingKey::ident
- Resolver::extern_prelude
// For search in index map for `Ident` as a index key | ||
pub unsafe fn new_without_normalize(ident: Ident) -> Self { | ||
Macros20NormalizedIdent(ident) | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In the last commit, I implemented Borrow
to look up in IndexMap<Macro20NormalizedIdent>
with Ident
as key. But in the new version, I implemented this function to temporarily convert the Ident
to Macro20NormalizedIdent
without normalizing it to match the type. And I also apply this function to some Ident
s that were not normalized using normalize_to_macros_2_0()
in the original code.
I'll mark three places where I use this function and I'm not sure if I should use new
or it.
@@ -203,7 +203,7 @@ impl<'a, 'ra, 'tcx> UnusedImportCheckVisitor<'a, 'ra, 'tcx> { | |||
if self | |||
.r | |||
.extern_prelude | |||
.get(&extern_crate.ident) | |||
.get(&unsafe { Macros20NormalizedIdent::new_without_normalize(extern_crate.ident) }) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Here use new_without_normalize
for indexing
let from_item = self | ||
.extern_prelude | ||
.get(&unsafe { Macros20NormalizedIdent::new_without_normalize(ident) }) | ||
.is_none_or(|entry| entry.introduced_by_item); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Here also for Indexing
.map(|(name, _)| (Ident::from_str(name), Default::default())) | ||
.map(|(name, _)| { | ||
( | ||
unsafe { | ||
Macros20NormalizedIdent::new_without_normalize(Ident::from_str(name)) | ||
}, | ||
Default::default(), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Here will return extern_prelude
which petrochenkov suggest to convert to Macro20NormalizedIdent
.
struct BindingKey { | ||
/// The identifier for the binding, always the `normalize_to_macros_2_0` version of the | ||
/// identifier. | ||
ident: Ident, | ||
ident: Macros20NormalizedIdent, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Main Change(1/2)
extern_prelude: FxIndexMap<Ident, ExternPreludeEntry<'ra>>, | ||
extern_prelude: FxIndexMap<Macros20NormalizedIdent, ExternPreludeEntry<'ra>>, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Main change (2/2)
☔ The latest upstream changes (presumably #143884) made this pull request unmergeable. Please resolve the merge conflicts. |
Signed-off-by: xizheyin <[email protected]>
☔ The latest upstream changes (presumably #144658) made this pull request unmergeable. Please resolve the merge conflicts. |
This pr introduce ModernIdent type to unify macro 2.0 hygiene handling
normalize_to_macros_2_0()
r? @petrochenkov