Skip to content

Commit 16f3e2b

Browse files
Merge #9716
9716: internal: Stop reexporting `hir_def`'s `ItemInNs` from HIR r=jonas-schievink a=jonas-schievink Resolves a FIXME and simplifies downstream code bors r+ Co-authored-by: Jonas Schievink <[email protected]>
2 parents 1662c34 + 18f86ba commit 16f3e2b

File tree

6 files changed

+60
-27
lines changed

6 files changed

+60
-27
lines changed

crates/hir/src/from_id.rs

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,13 @@
55
66
use hir_def::{
77
expr::{LabelId, PatId},
8-
item_scope::ItemInNs,
98
AdtId, AssocItemId, DefWithBodyId, EnumVariantId, FieldId, GenericDefId, GenericParamId,
109
ModuleDefId, VariantId,
1110
};
1211

1312
use crate::{
14-
Adt, AssocItem, BuiltinType, DefWithBody, Field, GenericDef, GenericParam, Label, Local,
15-
MacroDef, ModuleDef, Variant, VariantDef,
13+
Adt, AssocItem, BuiltinType, DefWithBody, Field, GenericDef, GenericParam, ItemInNs, Label,
14+
Local, ModuleDef, Variant, VariantDef,
1615
};
1716

1817
macro_rules! from_id {
@@ -258,19 +257,22 @@ impl From<(DefWithBodyId, LabelId)> for Label {
258257
}
259258
}
260259

261-
impl From<MacroDef> for ItemInNs {
262-
fn from(macro_def: MacroDef) -> Self {
263-
ItemInNs::Macros(macro_def.into())
260+
impl From<hir_def::item_scope::ItemInNs> for ItemInNs {
261+
fn from(it: hir_def::item_scope::ItemInNs) -> Self {
262+
match it {
263+
hir_def::item_scope::ItemInNs::Types(it) => ItemInNs::Types(it.into()),
264+
hir_def::item_scope::ItemInNs::Values(it) => ItemInNs::Values(it.into()),
265+
hir_def::item_scope::ItemInNs::Macros(it) => ItemInNs::Macros(it.into()),
266+
}
264267
}
265268
}
266269

267-
impl From<ModuleDef> for ItemInNs {
268-
fn from(module_def: ModuleDef) -> Self {
269-
match module_def {
270-
ModuleDef::Static(_) | ModuleDef::Const(_) | ModuleDef::Function(_) => {
271-
ItemInNs::Values(module_def.into())
272-
}
273-
_ => ItemInNs::Types(module_def.into()),
270+
impl From<ItemInNs> for hir_def::item_scope::ItemInNs {
271+
fn from(it: ItemInNs) -> Self {
272+
match it {
273+
ItemInNs::Types(it) => Self::Types(it.into()),
274+
ItemInNs::Values(it) => Self::Values(it.into()),
275+
ItemInNs::Macros(it) => Self::Macros(it.into()),
274276
}
275277
}
276278
}

crates/hir/src/lib.rs

Lines changed: 40 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,6 @@ pub use {
108108
attr::{Attr, Attrs, AttrsWithOwner, Documentation},
109109
find_path::PrefixKind,
110110
import_map,
111-
item_scope::ItemInNs, // FIXME: don't re-export ItemInNs, as it uses raw ids.
112111
nameres::ModuleSource,
113112
path::{ModPath, PathKind},
114113
type_ref::{Mutability, TypeRef},
@@ -194,9 +193,11 @@ impl Crate {
194193
query: import_map::Query,
195194
) -> impl Iterator<Item = Either<ModuleDef, MacroDef>> {
196195
let _p = profile::span("query_external_importables");
197-
import_map::search_dependencies(db, self.into(), query).into_iter().map(|item| match item {
198-
ItemInNs::Types(mod_id) | ItemInNs::Values(mod_id) => Either::Left(mod_id.into()),
199-
ItemInNs::Macros(mac_id) => Either::Right(mac_id.into()),
196+
import_map::search_dependencies(db, self.into(), query).into_iter().map(|item| {
197+
match ItemInNs::from(item) {
198+
ItemInNs::Types(mod_id) | ItemInNs::Values(mod_id) => Either::Left(mod_id),
199+
ItemInNs::Macros(mac_id) => Either::Right(mac_id),
200+
}
200201
})
201202
}
202203

@@ -656,7 +657,7 @@ impl Module {
656657
/// Finds a path that can be used to refer to the given item from within
657658
/// this module, if possible.
658659
pub fn find_use_path(self, db: &dyn DefDatabase, item: impl Into<ItemInNs>) -> Option<ModPath> {
659-
hir_def::find_path::find_path(db, item.into(), self.into())
660+
hir_def::find_path::find_path(db, item.into().into(), self.into())
660661
}
661662

662663
/// Finds a path that can be used to refer to the given item from within
@@ -667,7 +668,7 @@ impl Module {
667668
item: impl Into<ItemInNs>,
668669
prefix_kind: PrefixKind,
669670
) -> Option<ModPath> {
670-
hir_def::find_path::find_path_prefixed(db, item.into(), self.into(), prefix_kind)
671+
hir_def::find_path::find_path_prefixed(db, item.into().into(), self.into(), prefix_kind)
671672
}
672673
}
673674

@@ -1567,6 +1568,39 @@ impl MacroDef {
15671568
}
15681569
}
15691570

1571+
#[derive(Clone, Copy, PartialEq, Eq, Debug, Hash)]
1572+
pub enum ItemInNs {
1573+
Types(ModuleDef),
1574+
Values(ModuleDef),
1575+
Macros(MacroDef),
1576+
}
1577+
1578+
impl From<MacroDef> for ItemInNs {
1579+
fn from(it: MacroDef) -> Self {
1580+
Self::Macros(it)
1581+
}
1582+
}
1583+
1584+
impl From<ModuleDef> for ItemInNs {
1585+
fn from(module_def: ModuleDef) -> Self {
1586+
match module_def {
1587+
ModuleDef::Static(_) | ModuleDef::Const(_) | ModuleDef::Function(_) => {
1588+
ItemInNs::Values(module_def)
1589+
}
1590+
_ => ItemInNs::Types(module_def),
1591+
}
1592+
}
1593+
}
1594+
1595+
impl ItemInNs {
1596+
pub fn as_module_def(self) -> Option<ModuleDef> {
1597+
match self {
1598+
ItemInNs::Types(id) | ItemInNs::Values(id) => Some(id),
1599+
ItemInNs::Macros(_) => None,
1600+
}
1601+
}
1602+
}
1603+
15701604
/// Invariant: `inner.as_assoc_item(db).is_some()`
15711605
/// We do not actively enforce this invariant.
15721606
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]

crates/ide_assists/src/handlers/qualify_path.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ fn find_trait_method(
176176
}
177177

178178
fn item_as_trait(db: &RootDatabase, item: hir::ItemInNs) -> Option<hir::Trait> {
179-
let item_module_def = hir::ModuleDef::from(item.as_module_def_id()?);
179+
let item_module_def = item.as_module_def()?;
180180

181181
if let hir::ModuleDef::Trait(trait_) = item_module_def {
182182
Some(trait_)

crates/ide_assists/src/handlers/replace_derive_with_manual_impl.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ pub(crate) fn replace_derive_with_manual_impl(
6767
items_locator::AssocItemSearch::Exclude,
6868
Some(items_locator::DEFAULT_QUERY_SEARCH_LIMIT.inner()),
6969
)
70-
.filter_map(|item| match ModuleDef::from(item.as_module_def_id()?) {
70+
.filter_map(|item| match item.as_module_def()? {
7171
ModuleDef::Trait(trait_) => Some(trait_),
7272
_ => None,
7373
})

crates/ide_db/src/helpers/import_assets.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -620,6 +620,5 @@ fn path_import_candidate(
620620
}
621621

622622
fn item_as_assoc(db: &RootDatabase, item: ItemInNs) -> Option<AssocItem> {
623-
item.as_module_def_id()
624-
.and_then(|module_def_id| ModuleDef::from(module_def_id).as_assoc_item(db))
623+
item.as_module_def().and_then(|module_def| module_def.as_assoc_item(db))
625624
}

crates/ide_db/src/items_locator.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
use either::Either;
66
use hir::{
77
import_map::{self, ImportKind},
8-
AsAssocItem, Crate, ItemInNs, ModuleDef, Semantics,
8+
AsAssocItem, Crate, ItemInNs, Semantics,
99
};
1010
use limit::Limit;
1111
use syntax::{ast, AstNode, SyntaxKind::NAME};
@@ -147,7 +147,5 @@ fn get_name_definition(
147147
}
148148

149149
fn is_assoc_item(item: ItemInNs, db: &RootDatabase) -> bool {
150-
item.as_module_def_id()
151-
.and_then(|module_def_id| ModuleDef::from(module_def_id).as_assoc_item(db))
152-
.is_some()
150+
item.as_module_def().and_then(|module_def| module_def.as_assoc_item(db)).is_some()
153151
}

0 commit comments

Comments
 (0)