Unnamed repository; edit this file 'description' to name the repository.
Diffstat (limited to 'crates/hir_def/src/item_scope.rs')
-rw-r--r--crates/hir_def/src/item_scope.rs41
1 files changed, 16 insertions, 25 deletions
diff --git a/crates/hir_def/src/item_scope.rs b/crates/hir_def/src/item_scope.rs
index fffec96bab..e6fdd41bd8 100644
--- a/crates/hir_def/src/item_scope.rs
+++ b/crates/hir_def/src/item_scope.rs
@@ -4,7 +4,7 @@
use std::collections::hash_map::Entry;
use base_db::CrateId;
-use hir_expand::{name::Name, AstId, MacroCallId, MacroDefKind};
+use hir_expand::{name::Name, AstId, MacroCallId};
use once_cell::sync::Lazy;
use profile::Count;
use rustc_hash::{FxHashMap, FxHashSet};
@@ -14,7 +14,8 @@ use syntax::ast;
use crate::{
attr::AttrId, db::DefDatabase, per_ns::PerNs, visibility::Visibility, AdtId, BuiltinType,
- ConstId, ImplId, LocalModuleId, MacroDefId, ModuleDefId, ModuleId, TraitId,
+ ConstId, HasModule, ImplId, LocalModuleId, MacroId, MacroRulesId, ModuleDefId, ModuleId,
+ TraitId,
};
#[derive(Copy, Clone)]
@@ -38,13 +39,12 @@ pub struct ItemScope {
/// imports.
types: FxHashMap<Name, (ModuleDefId, Visibility)>,
values: FxHashMap<Name, (ModuleDefId, Visibility)>,
- macros: FxHashMap<Name, (MacroDefId, Visibility)>,
+ macros: FxHashMap<Name, (MacroId, Visibility)>,
unresolved: FxHashSet<Name>,
/// The defs declared in this scope. Each def has a single scope where it is
/// declared.
declarations: Vec<ModuleDefId>,
- macro_declarations: Vec<MacroDefId>,
impls: Vec<ImplId>,
unnamed_consts: Vec<ConstId>,
@@ -62,7 +62,7 @@ pub struct ItemScope {
/// Module scoped macros will be inserted into `items` instead of here.
// FIXME: Macro shadowing in one module is not properly handled. Non-item place macros will
// be all resolved to the last one defined if shadowing happens.
- legacy_macros: FxHashMap<Name, MacroDefId>,
+ legacy_macros: FxHashMap<Name, MacroRulesId>,
attr_macros: FxHashMap<AstId<ast::Item>, MacroCallId>,
/// The derive macro invocations in this scope, keyed by the owner item over the actual derive attributes
/// paired with the derive macro invocations for the specific attribute.
@@ -108,10 +108,6 @@ impl ItemScope {
self.declarations.iter().copied()
}
- pub fn macro_declarations(&self) -> impl Iterator<Item = MacroDefId> + '_ {
- self.macro_declarations.iter().copied()
- }
-
pub fn impls(&self) -> impl Iterator<Item = ImplId> + ExactSizeIterator + '_ {
self.impls.iter().copied()
}
@@ -127,12 +123,12 @@ impl ItemScope {
}
/// Iterate over all module scoped macros
- pub(crate) fn macros<'a>(&'a self) -> impl Iterator<Item = (&'a Name, MacroDefId)> + 'a {
+ pub(crate) fn macros<'a>(&'a self) -> impl Iterator<Item = (&'a Name, MacroId)> + 'a {
self.entries().filter_map(|(name, def)| def.take_macros().map(|macro_| (name, macro_)))
}
/// Iterate over all legacy textual scoped macros visible at the end of the module
- pub(crate) fn legacy_macros<'a>(&'a self) -> impl Iterator<Item = (&'a Name, MacroDefId)> + 'a {
+ pub fn legacy_macros<'a>(&'a self) -> impl Iterator<Item = (&'a Name, MacroRulesId)> + 'a {
self.legacy_macros.iter().map(|(name, def)| (name, *def))
}
@@ -163,8 +159,8 @@ impl ItemScope {
pub(crate) fn traits<'a>(&'a self) -> impl Iterator<Item = TraitId> + 'a {
self.types
.values()
- .filter_map(|(def, _)| match def {
- ModuleDefId::TraitId(t) => Some(*t),
+ .filter_map(|&(def, _)| match def {
+ ModuleDefId::TraitId(t) => Some(t),
_ => None,
})
.chain(self.unnamed_trait_imports.keys().copied())
@@ -174,11 +170,7 @@ impl ItemScope {
self.declarations.push(def)
}
- pub(crate) fn declare_macro(&mut self, def: MacroDefId) {
- self.macro_declarations.push(def);
- }
-
- pub(crate) fn get_legacy_macro(&self, name: &Name) -> Option<MacroDefId> {
+ pub(crate) fn get_legacy_macro(&self, name: &Name) -> Option<MacroRulesId> {
self.legacy_macros.get(name).copied()
}
@@ -190,7 +182,7 @@ impl ItemScope {
self.unnamed_consts.push(konst);
}
- pub(crate) fn define_legacy_macro(&mut self, name: Name, mac: MacroDefId) {
+ pub(crate) fn define_legacy_macro(&mut self, name: Name, mac: MacroRulesId) {
self.legacy_macros.insert(name, mac);
}
@@ -320,7 +312,7 @@ impl ItemScope {
)
}
- pub(crate) fn collect_legacy_macros(&self) -> FxHashMap<Name, MacroDefId> {
+ pub(crate) fn collect_legacy_macros(&self) -> FxHashMap<Name, MacroRulesId> {
self.legacy_macros.clone()
}
@@ -334,7 +326,7 @@ impl ItemScope {
.for_each(|vis| *vis = Visibility::Module(this_module));
for (mac, vis) in self.macros.values_mut() {
- if let MacroDefKind::ProcMacro(..) = mac.kind {
+ if let MacroId::ProcMacroId(_) = mac {
// FIXME: Technically this is insufficient since reexports of proc macros are also
// forbidden. Practically nobody does that.
continue;
@@ -377,7 +369,6 @@ impl ItemScope {
macros,
unresolved,
declarations,
- macro_declarations,
impls,
unnamed_consts,
unnamed_trait_imports,
@@ -390,7 +381,6 @@ impl ItemScope {
macros.shrink_to_fit();
unresolved.shrink_to_fit();
declarations.shrink_to_fit();
- macro_declarations.shrink_to_fit();
impls.shrink_to_fit();
unnamed_consts.shrink_to_fit();
unnamed_trait_imports.shrink_to_fit();
@@ -421,6 +411,7 @@ impl PerNs {
ModuleDefId::TraitId(_) => PerNs::types(def, v),
ModuleDefId::TypeAliasId(_) => PerNs::types(def, v),
ModuleDefId::BuiltinType(_) => PerNs::types(def, v),
+ ModuleDefId::MacroId(mac) => PerNs::macros(mac, v),
}
}
}
@@ -429,7 +420,7 @@ impl PerNs {
pub enum ItemInNs {
Types(ModuleDefId),
Values(ModuleDefId),
- Macros(MacroDefId),
+ Macros(MacroId),
}
impl ItemInNs {
@@ -444,7 +435,7 @@ impl ItemInNs {
pub fn krate(&self, db: &dyn DefDatabase) -> Option<CrateId> {
match self {
ItemInNs::Types(did) | ItemInNs::Values(did) => did.module(db).map(|m| m.krate),
- ItemInNs::Macros(id) => Some(id.krate),
+ ItemInNs::Macros(id) => Some(id.module(db).krate),
}
}
}