diff --git a/crates/hir/src/from_id.rs b/crates/hir/src/from_id.rs index 179b9d51ef..98c51652d9 100644 --- a/crates/hir/src/from_id.rs +++ b/crates/hir/src/from_id.rs @@ -5,14 +5,13 @@ use hir_def::{ expr::{LabelId, PatId}, - item_scope::ItemInNs, AdtId, AssocItemId, DefWithBodyId, EnumVariantId, FieldId, GenericDefId, GenericParamId, ModuleDefId, VariantId, }; use crate::{ - Adt, AssocItem, BuiltinType, DefWithBody, Field, GenericDef, GenericParam, Label, Local, - MacroDef, ModuleDef, Variant, VariantDef, + Adt, AssocItem, BuiltinType, DefWithBody, Field, GenericDef, GenericParam, ItemInNs, Label, + Local, ModuleDef, Variant, VariantDef, }; macro_rules! from_id { @@ -258,19 +257,22 @@ impl From<(DefWithBodyId, LabelId)> for Label { } } -impl From for ItemInNs { - fn from(macro_def: MacroDef) -> Self { - ItemInNs::Macros(macro_def.into()) +impl From for ItemInNs { + fn from(it: hir_def::item_scope::ItemInNs) -> Self { + match it { + hir_def::item_scope::ItemInNs::Types(it) => ItemInNs::Types(it.into()), + hir_def::item_scope::ItemInNs::Values(it) => ItemInNs::Values(it.into()), + hir_def::item_scope::ItemInNs::Macros(it) => ItemInNs::Macros(it.into()), + } } } -impl From for ItemInNs { - fn from(module_def: ModuleDef) -> Self { - match module_def { - ModuleDef::Static(_) | ModuleDef::Const(_) | ModuleDef::Function(_) => { - ItemInNs::Values(module_def.into()) - } - _ => ItemInNs::Types(module_def.into()), +impl From for hir_def::item_scope::ItemInNs { + fn from(it: ItemInNs) -> Self { + match it { + ItemInNs::Types(it) => Self::Types(it.into()), + ItemInNs::Values(it) => Self::Values(it.into()), + ItemInNs::Macros(it) => Self::Macros(it.into()), } } } diff --git a/crates/hir/src/lib.rs b/crates/hir/src/lib.rs index 162ff55471..7ee302306f 100644 --- a/crates/hir/src/lib.rs +++ b/crates/hir/src/lib.rs @@ -108,7 +108,6 @@ pub use { attr::{Attr, Attrs, AttrsWithOwner, Documentation}, find_path::PrefixKind, import_map, - item_scope::ItemInNs, // FIXME: don't re-export ItemInNs, as it uses raw ids. nameres::ModuleSource, path::{ModPath, PathKind}, type_ref::{Mutability, TypeRef}, @@ -194,9 +193,11 @@ impl Crate { query: import_map::Query, ) -> impl Iterator> { let _p = profile::span("query_external_importables"); - import_map::search_dependencies(db, self.into(), query).into_iter().map(|item| match item { - ItemInNs::Types(mod_id) | ItemInNs::Values(mod_id) => Either::Left(mod_id.into()), - ItemInNs::Macros(mac_id) => Either::Right(mac_id.into()), + import_map::search_dependencies(db, self.into(), query).into_iter().map(|item| { + match ItemInNs::from(item) { + ItemInNs::Types(mod_id) | ItemInNs::Values(mod_id) => Either::Left(mod_id), + ItemInNs::Macros(mac_id) => Either::Right(mac_id), + } }) } @@ -656,7 +657,7 @@ impl Module { /// Finds a path that can be used to refer to the given item from within /// this module, if possible. pub fn find_use_path(self, db: &dyn DefDatabase, item: impl Into) -> Option { - hir_def::find_path::find_path(db, item.into(), self.into()) + hir_def::find_path::find_path(db, item.into().into(), self.into()) } /// Finds a path that can be used to refer to the given item from within @@ -667,7 +668,7 @@ impl Module { item: impl Into, prefix_kind: PrefixKind, ) -> Option { - hir_def::find_path::find_path_prefixed(db, item.into(), self.into(), prefix_kind) + hir_def::find_path::find_path_prefixed(db, item.into().into(), self.into(), prefix_kind) } } @@ -1567,6 +1568,39 @@ impl MacroDef { } } +#[derive(Clone, Copy, PartialEq, Eq, Debug, Hash)] +pub enum ItemInNs { + Types(ModuleDef), + Values(ModuleDef), + Macros(MacroDef), +} + +impl From for ItemInNs { + fn from(it: MacroDef) -> Self { + Self::Macros(it) + } +} + +impl From for ItemInNs { + fn from(module_def: ModuleDef) -> Self { + match module_def { + ModuleDef::Static(_) | ModuleDef::Const(_) | ModuleDef::Function(_) => { + ItemInNs::Values(module_def) + } + _ => ItemInNs::Types(module_def), + } + } +} + +impl ItemInNs { + pub fn as_module_def(self) -> Option { + match self { + ItemInNs::Types(id) | ItemInNs::Values(id) => Some(id), + ItemInNs::Macros(_) => None, + } + } +} + /// Invariant: `inner.as_assoc_item(db).is_some()` /// We do not actively enforce this invariant. #[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)] diff --git a/crates/ide_assists/src/handlers/qualify_path.rs b/crates/ide_assists/src/handlers/qualify_path.rs index 1d7be183a7..28d7f3a346 100644 --- a/crates/ide_assists/src/handlers/qualify_path.rs +++ b/crates/ide_assists/src/handlers/qualify_path.rs @@ -176,7 +176,7 @@ fn find_trait_method( } fn item_as_trait(db: &RootDatabase, item: hir::ItemInNs) -> Option { - let item_module_def = hir::ModuleDef::from(item.as_module_def_id()?); + let item_module_def = item.as_module_def()?; if let hir::ModuleDef::Trait(trait_) = item_module_def { Some(trait_) diff --git a/crates/ide_assists/src/handlers/replace_derive_with_manual_impl.rs b/crates/ide_assists/src/handlers/replace_derive_with_manual_impl.rs index 73b623d122..a69c1c0080 100644 --- a/crates/ide_assists/src/handlers/replace_derive_with_manual_impl.rs +++ b/crates/ide_assists/src/handlers/replace_derive_with_manual_impl.rs @@ -67,7 +67,7 @@ pub(crate) fn replace_derive_with_manual_impl( items_locator::AssocItemSearch::Exclude, Some(items_locator::DEFAULT_QUERY_SEARCH_LIMIT.inner()), ) - .filter_map(|item| match ModuleDef::from(item.as_module_def_id()?) { + .filter_map(|item| match item.as_module_def()? { ModuleDef::Trait(trait_) => Some(trait_), _ => None, }) diff --git a/crates/ide_db/src/helpers/import_assets.rs b/crates/ide_db/src/helpers/import_assets.rs index 5017c7f522..23489aca99 100644 --- a/crates/ide_db/src/helpers/import_assets.rs +++ b/crates/ide_db/src/helpers/import_assets.rs @@ -620,6 +620,5 @@ fn path_import_candidate( } fn item_as_assoc(db: &RootDatabase, item: ItemInNs) -> Option { - item.as_module_def_id() - .and_then(|module_def_id| ModuleDef::from(module_def_id).as_assoc_item(db)) + item.as_module_def().and_then(|module_def| module_def.as_assoc_item(db)) } diff --git a/crates/ide_db/src/items_locator.rs b/crates/ide_db/src/items_locator.rs index 1ececb6c85..bf170ffb83 100644 --- a/crates/ide_db/src/items_locator.rs +++ b/crates/ide_db/src/items_locator.rs @@ -5,7 +5,7 @@ use either::Either; use hir::{ import_map::{self, ImportKind}, - AsAssocItem, Crate, ItemInNs, ModuleDef, Semantics, + AsAssocItem, Crate, ItemInNs, Semantics, }; use limit::Limit; use syntax::{ast, AstNode, SyntaxKind::NAME}; @@ -147,7 +147,5 @@ fn get_name_definition( } fn is_assoc_item(item: ItemInNs, db: &RootDatabase) -> bool { - item.as_module_def_id() - .and_then(|module_def_id| ModuleDef::from(module_def_id).as_assoc_item(db)) - .is_some() + item.as_module_def().and_then(|module_def| module_def.as_assoc_item(db)).is_some() }