-
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_hir_analysis/src/coherence/inherent_impls_overlap.rs
Outdated
Show resolved
Hide resolved
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.
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 see any justified uses of new_without_normalize
, it can be removed.
@@ -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
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 should use Macros20NormalizedIdent::new
.
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
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 should use Macros20NormalizedIdent::new
.
.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
.
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 should use Macros20NormalizedIdent::with_dummy_span
, please rebase on master.
This comment was marked as resolved.
This comment was marked as resolved.
This comment was marked as resolved.
This comment was marked as resolved.
Signed-off-by: xizheyin <[email protected]>
I rebased it @rustbot ready |
} | ||
} | ||
|
||
impl Deref for 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.
This is mostly for norm_ident.as_str()
, right?
Then it's probably better to just add that method to normalized idents.
Idents are not normally passed by reference besides that special case.
And I also don't see anything taking &mut Ident
, so better to avoid DerefMut
too.
@@ -969,7 +969,7 @@ impl<'a, 'ra, 'tcx> BuildReducedGraphVisitor<'a, 'ra, 'tcx> { | |||
self.r.potentially_unused_imports.push(import); | |||
let imported_binding = self.r.import(binding, import); | |||
if parent == self.r.graph_root { | |||
let ident = ident.normalize_to_macros_2_0(); | |||
let ident = Macros20NormalizedIdent::new(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.
let ident = Macros20NormalizedIdent::new(ident); | |
let norm_ident = Macros20NormalizedIdent::new(ident); |
Let's use a more disambiguated naming in contexts where both normalized and non-normalized idents exist.
@@ -690,7 +690,7 @@ impl<'ra> Module<'ra> { | |||
return; | |||
} | |||
if let Res::Def(DefKind::Trait | DefKind::TraitAlias, def_id) = binding.res() { | |||
collected_traits.push((name, binding, r.as_ref().get_module(def_id))) | |||
collected_traits.push((name.0, binding, r.as_ref().get_module(def_id))) |
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.
ModuleData::traits
always contain normalized idents and can use Macros20NormalizedIdent
.
This pr introduce ModernIdent type to unify macro 2.0 hygiene handling
normalize_to_macros_2_0()
r? @petrochenkov