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.rs124
1 files changed, 68 insertions, 56 deletions
diff --git a/crates/hir-def/src/item_scope.rs b/crates/hir-def/src/item_scope.rs
index e96e38ecee..2c3eb5c8e5 100644
--- a/crates/hir-def/src/item_scope.rs
+++ b/crates/hir-def/src/item_scope.rs
@@ -16,7 +16,7 @@ use syntax::ast;
use crate::{
db::DefDatabase,
- per_ns::PerNs,
+ per_ns::{Item, MacrosItem, PerNs, TypesItem, ValuesItem},
visibility::{Visibility, VisibilityExplicitness},
AdtId, BuiltinType, ConstId, ExternCrateId, FxIndexMap, HasModule, ImplId, LocalModuleId,
Lookup, MacroId, ModuleDefId, ModuleId, TraitId, UseId,
@@ -80,9 +80,9 @@ pub struct ItemScope {
/// Defs visible in this scope. This includes `declarations`, but also
/// imports. The imports belong to this module and can be resolved by using them on
/// the `use_imports_*` fields.
- types: FxIndexMap<Name, (ModuleDefId, Visibility, Option<ImportOrExternCrate>)>,
- values: FxIndexMap<Name, (ModuleDefId, Visibility, Option<ImportId>)>,
- macros: FxIndexMap<Name, (MacroId, Visibility, Option<ImportId>)>,
+ types: FxIndexMap<Name, TypesItem>,
+ values: FxIndexMap<Name, ValuesItem>,
+ macros: FxIndexMap<Name, MacrosItem>,
unresolved: FxHashSet<Name>,
/// The defs declared in this scope. Each def has a single scope where it is
@@ -92,7 +92,7 @@ pub struct ItemScope {
impls: Vec<ImplId>,
unnamed_consts: Vec<ConstId>,
/// Traits imported via `use Trait as _;`.
- unnamed_trait_imports: FxHashMap<TraitId, (Visibility, Option<ImportId>)>,
+ unnamed_trait_imports: FxHashMap<TraitId, Item<()>>,
// the resolutions of the imports of this scope
use_imports_types: FxHashMap<ImportOrExternCrate, ImportOrDef>,
@@ -187,7 +187,7 @@ impl ItemScope {
import = i;
}
ImportOrDef::Def(ModuleDefId::MacroId(def)) => {
- res.macros = Some((def, Visibility::Public, None));
+ res.macros = Some(Item { def, vis: Visibility::Public, import: None });
break;
}
_ => break,
@@ -203,7 +203,7 @@ impl ItemScope {
import = i;
}
ImportOrDef::Def(def) => {
- res.types = Some((def, Visibility::Public, None));
+ res.types = Some(Item { def, vis: Visibility::Public, import: None });
break;
}
_ => break,
@@ -219,7 +219,7 @@ impl ItemScope {
import = i;
}
ImportOrDef::Def(def) => {
- res.values = Some((def, Visibility::Public, None));
+ res.values = Some(Item { def, vis: Visibility::Public, import: None });
break;
}
_ => break,
@@ -253,8 +253,8 @@ impl ItemScope {
}
pub(crate) fn modules_in_scope(&self) -> impl Iterator<Item = (ModuleId, Visibility)> + '_ {
- self.types.values().copied().filter_map(|(def, vis, _)| match def {
- ModuleDefId::ModuleId(module) => Some((module, vis)),
+ self.types.values().filter_map(|ns| match ns.def {
+ ModuleDefId::ModuleId(module) => Some((module, ns.vis)),
_ => None,
})
}
@@ -283,20 +283,20 @@ impl ItemScope {
}
pub(crate) fn type_(&self, name: &Name) -> Option<(ModuleDefId, Visibility)> {
- self.types.get(name).copied().map(|(a, b, _)| (a, b))
+ self.types.get(name).map(|item| (item.def, item.vis))
}
/// XXX: this is O(N) rather than O(1), try to not introduce new usages.
pub(crate) fn name_of(&self, item: ItemInNs) -> Option<(&Name, Visibility, /*declared*/ bool)> {
match item {
- ItemInNs::Macros(def) => self.macros.iter().find_map(|(name, &(other_def, vis, i))| {
- (other_def == def).then_some((name, vis, i.is_none()))
+ ItemInNs::Macros(def) => self.macros.iter().find_map(|(name, other_def)| {
+ (other_def.def == def).then_some((name, other_def.vis, other_def.import.is_none()))
}),
- ItemInNs::Types(def) => self.types.iter().find_map(|(name, &(other_def, vis, i))| {
- (other_def == def).then_some((name, vis, i.is_none()))
+ ItemInNs::Types(def) => self.types.iter().find_map(|(name, other_def)| {
+ (other_def.def == def).then_some((name, other_def.vis, other_def.import.is_none()))
}),
- ItemInNs::Values(def) => self.values.iter().find_map(|(name, &(other_def, vis, i))| {
- (other_def == def).then_some((name, vis, i.is_none()))
+ ItemInNs::Values(def) => self.values.iter().find_map(|(name, other_def)| {
+ (other_def.def == def).then_some((name, other_def.vis, other_def.import.is_none()))
}),
}
}
@@ -311,22 +311,34 @@ impl ItemScope {
ItemInNs::Macros(def) => self
.macros
.iter()
- .filter_map(|(name, &(other_def, vis, i))| {
- (other_def == def).then_some((name, vis, i.is_none()))
+ .filter_map(|(name, other_def)| {
+ (other_def.def == def).then_some((
+ name,
+ other_def.vis,
+ other_def.import.is_none(),
+ ))
})
.find_map(|(a, b, c)| cb(a, b, c)),
ItemInNs::Types(def) => self
.types
.iter()
- .filter_map(|(name, &(other_def, vis, i))| {
- (other_def == def).then_some((name, vis, i.is_none()))
+ .filter_map(|(name, other_def)| {
+ (other_def.def == def).then_some((
+ name,
+ other_def.vis,
+ other_def.import.is_none(),
+ ))
})
.find_map(|(a, b, c)| cb(a, b, c)),
ItemInNs::Values(def) => self
.values
.iter()
- .filter_map(|(name, &(other_def, vis, i))| {
- (other_def == def).then_some((name, vis, i.is_none()))
+ .filter_map(|(name, other_def)| {
+ (other_def.def == def).then_some((
+ name,
+ other_def.vis,
+ other_def.import.is_none(),
+ ))
})
.find_map(|(a, b, c)| cb(a, b, c)),
}
@@ -335,7 +347,7 @@ impl ItemScope {
pub(crate) fn traits(&self) -> impl Iterator<Item = TraitId> + '_ {
self.types
.values()
- .filter_map(|&(def, _, _)| match def {
+ .filter_map(|def| match def.def {
ModuleDefId::TraitId(t) => Some(t),
_ => None,
})
@@ -344,13 +356,13 @@ impl ItemScope {
pub(crate) fn resolutions(&self) -> impl Iterator<Item = (Option<Name>, PerNs)> + '_ {
self.entries().map(|(name, res)| (Some(name.clone()), res)).chain(
- self.unnamed_trait_imports.iter().map(|(tr, (vis, i))| {
+ self.unnamed_trait_imports.iter().map(|(tr, trait_)| {
(
None,
PerNs::types(
ModuleDefId::TraitId(*tr),
- *vis,
- i.map(ImportOrExternCrate::Import),
+ trait_.vis,
+ trait_.import.map(ImportOrExternCrate::Import),
),
)
}),
@@ -464,12 +476,12 @@ impl ItemScope {
// FIXME: This is only used in collection, we should move the relevant parts of it out of ItemScope
pub(crate) fn unnamed_trait_vis(&self, tr: TraitId) -> Option<Visibility> {
- self.unnamed_trait_imports.get(&tr).copied().map(|(a, _)| a)
+ self.unnamed_trait_imports.get(&tr).map(|trait_| trait_.vis)
}
pub(crate) fn push_unnamed_trait(&mut self, tr: TraitId, vis: Visibility) {
// FIXME: import
- self.unnamed_trait_imports.insert(tr, (vis, None));
+ self.unnamed_trait_imports.insert(tr, Item { def: (), vis, import: None });
}
pub(crate) fn push_res_with_import(
@@ -502,7 +514,7 @@ impl ItemScope {
}
None | Some(ImportType::Glob(_)) => None,
};
- let prev = std::mem::replace(&mut fld.2, import);
+ let prev = std::mem::replace(&mut fld.import, import);
if let Some(import) = import {
self.use_imports_types.insert(
import,
@@ -513,7 +525,7 @@ impl ItemScope {
Some(ImportOrExternCrate::ExternCrate(import)) => {
ImportOrDef::ExternCrate(import)
}
- None => ImportOrDef::Def(fld.0),
+ None => ImportOrDef::Def(fld.def),
},
);
}
@@ -540,7 +552,7 @@ impl ItemScope {
}
None | Some(ImportType::Glob(_)) => None,
};
- let prev = std::mem::replace(&mut fld.2, import);
+ let prev = std::mem::replace(&mut fld.import, import);
if let Some(import) = import {
self.use_imports_types.insert(
import,
@@ -551,7 +563,7 @@ impl ItemScope {
Some(ImportOrExternCrate::ExternCrate(import)) => {
ImportOrDef::ExternCrate(import)
}
- None => ImportOrDef::Def(fld.0),
+ None => ImportOrDef::Def(fld.def),
},
);
}
@@ -579,13 +591,13 @@ impl ItemScope {
Some(ImportType::Import(import)) => Some(import),
_ => None,
};
- let prev = std::mem::replace(&mut fld.2, import);
+ let prev = std::mem::replace(&mut fld.import, import);
if let Some(import) = import {
self.use_imports_values.insert(
import,
match prev {
Some(import) => ImportOrDef::Import(import),
- None => ImportOrDef::Def(fld.0),
+ None => ImportOrDef::Def(fld.def),
},
);
}
@@ -599,13 +611,13 @@ impl ItemScope {
Some(ImportType::Import(import)) => Some(import),
_ => None,
};
- let prev = std::mem::replace(&mut fld.2, import);
+ let prev = std::mem::replace(&mut fld.import, import);
if let Some(import) = import {
self.use_imports_values.insert(
import,
match prev {
Some(import) => ImportOrDef::Import(import),
- None => ImportOrDef::Def(fld.0),
+ None => ImportOrDef::Def(fld.def),
},
);
}
@@ -631,13 +643,13 @@ impl ItemScope {
Some(ImportType::Import(import)) => Some(import),
_ => None,
};
- let prev = std::mem::replace(&mut fld.2, import);
+ let prev = std::mem::replace(&mut fld.import, import);
if let Some(import) = import {
self.use_imports_macros.insert(
import,
match prev {
Some(import) => ImportOrDef::Import(import),
- None => ImportOrDef::Def(fld.0.into()),
+ None => ImportOrDef::Def(fld.def.into()),
},
);
}
@@ -651,13 +663,13 @@ impl ItemScope {
Some(ImportType::Import(import)) => Some(import),
_ => None,
};
- let prev = std::mem::replace(&mut fld.2, import);
+ let prev = std::mem::replace(&mut fld.import, import);
if let Some(import) = import {
self.use_imports_macros.insert(
import,
match prev {
Some(import) => ImportOrDef::Import(import),
- None => ImportOrDef::Def(fld.0.into()),
+ None => ImportOrDef::Def(fld.def.into()),
},
);
}
@@ -680,19 +692,19 @@ impl ItemScope {
pub(crate) fn censor_non_proc_macros(&mut self, this_module: ModuleId) {
self.types
.values_mut()
- .map(|(_, vis, _)| vis)
- .chain(self.values.values_mut().map(|(_, vis, _)| vis))
- .chain(self.unnamed_trait_imports.values_mut().map(|(vis, _)| vis))
+ .map(|def| &mut def.vis)
+ .chain(self.values.values_mut().map(|def| &mut def.vis))
+ .chain(self.unnamed_trait_imports.values_mut().map(|def| &mut def.vis))
.for_each(|vis| {
*vis = Visibility::Module(this_module, VisibilityExplicitness::Implicit)
});
- for (mac, vis, import) in self.macros.values_mut() {
- if matches!(mac, MacroId::ProcMacroId(_) if import.is_none()) {
+ for mac in self.macros.values_mut() {
+ if matches!(mac.def, MacroId::ProcMacroId(_) if mac.import.is_none()) {
continue;
}
- *vis = Visibility::Module(this_module, VisibilityExplicitness::Implicit);
+ mac.vis = Visibility::Module(this_module, VisibilityExplicitness::Implicit);
}
}
@@ -707,23 +719,23 @@ impl ItemScope {
name.map_or("_".to_owned(), |name| name.display(db, Edition::LATEST).to_string())
);
- if let Some((.., i)) = def.types {
+ if let Some(Item { import, .. }) = def.types {
buf.push_str(" t");
- match i {
+ match import {
Some(ImportOrExternCrate::Import(_)) => buf.push('i'),
Some(ImportOrExternCrate::ExternCrate(_)) => buf.push('e'),
None => (),
}
}
- if let Some((.., i)) = def.values {
+ if let Some(Item { import, .. }) = def.values {
buf.push_str(" v");
- if i.is_some() {
+ if import.is_some() {
buf.push('i');
}
}
- if let Some((.., i)) = def.macros {
+ if let Some(Item { import, .. }) = def.macros {
buf.push_str(" m");
- if i.is_some() {
+ if import.is_some() {
buf.push('i');
}
}
@@ -781,19 +793,19 @@ impl ItemScope {
pub(crate) fn update_visibility_types(&mut self, name: &Name, vis: Visibility) {
let res =
self.types.get_mut(name).expect("tried to update visibility of non-existent type");
- res.1 = vis;
+ res.vis = vis;
}
pub(crate) fn update_visibility_values(&mut self, name: &Name, vis: Visibility) {
let res =
self.values.get_mut(name).expect("tried to update visibility of non-existent value");
- res.1 = vis;
+ res.vis = vis;
}
pub(crate) fn update_visibility_macros(&mut self, name: &Name, vis: Visibility) {
let res =
self.macros.get_mut(name).expect("tried to update visibility of non-existent macro");
- res.1 = vis;
+ res.vis = vis;
}
}