@@ -51,7 +51,7 @@ use hir_def::{
5151 per_ns:: PerNs ,
5252 resolver:: { HasResolver , Resolver } ,
5353 src:: HasSource as _,
54- AdtId , AssocItemId , AssocItemLoc , AttrDefId , ConstId , ConstParamId , DefWithBodyId , EnumId ,
54+ AssocItemId , AssocItemLoc , AttrDefId , ConstId , ConstParamId , DefWithBodyId , EnumId ,
5555 EnumVariantId , FunctionId , GenericDefId , HasModule , ImplId , ItemContainerId , LifetimeParamId ,
5656 LocalEnumVariantId , LocalFieldId , Lookup , MacroExpander , MacroId , ModuleId , StaticId , StructId ,
5757 TraitAliasId , TraitId , TypeAliasId , TypeOrConstParamId , TypeParamId , UnionId ,
@@ -119,11 +119,9 @@ pub use {
119119 path:: { ModPath , PathKind } ,
120120 type_ref:: { Mutability , TypeRef } ,
121121 visibility:: Visibility ,
122- // FIXME: This is here since it is input of a method in `HirWrite`
123- // and things outside of hir need to implement that trait. We probably
124- // should move whole `hir_ty::display` to this crate so we will become
125- // able to use `ModuleDef` or `Definition` instead of `ModuleDefId`.
126- ModuleDefId ,
122+ // FIXME: This is here since some queries take it as input that are used
123+ // outside of hir.
124+ { AdtId , ModuleDefId } ,
127125 } ,
128126 hir_expand:: {
129127 attrs:: Attr ,
@@ -4429,3 +4427,90 @@ impl HasCrate for Module {
44294427 Module :: krate ( * self )
44304428 }
44314429}
4430+
4431+ pub trait HasContainer {
4432+ fn container ( & self , db : & dyn HirDatabase ) -> ItemContainer ;
4433+ }
4434+
4435+ impl HasContainer for Module {
4436+ fn container ( & self , db : & dyn HirDatabase ) -> ItemContainer {
4437+ // FIXME: handle block expressions as modules (their parent is in a different DefMap)
4438+ let def_map = self . id . def_map ( db. upcast ( ) ) ;
4439+ match def_map[ self . id . local_id ] . parent {
4440+ Some ( parent_id) => ItemContainer :: Module ( Module { id : def_map. module_id ( parent_id) } ) ,
4441+ None => ItemContainer :: Crate ( def_map. krate ( ) ) ,
4442+ }
4443+ }
4444+ }
4445+
4446+ impl HasContainer for Function {
4447+ fn container ( & self , db : & dyn HirDatabase ) -> ItemContainer {
4448+ container_id_to_hir ( self . id . lookup ( db. upcast ( ) ) . container )
4449+ }
4450+ }
4451+
4452+ impl HasContainer for Struct {
4453+ fn container ( & self , db : & dyn HirDatabase ) -> ItemContainer {
4454+ ItemContainer :: Module ( Module { id : self . id . lookup ( db. upcast ( ) ) . container } )
4455+ }
4456+ }
4457+
4458+ impl HasContainer for Union {
4459+ fn container ( & self , db : & dyn HirDatabase ) -> ItemContainer {
4460+ ItemContainer :: Module ( Module { id : self . id . lookup ( db. upcast ( ) ) . container } )
4461+ }
4462+ }
4463+
4464+ impl HasContainer for Enum {
4465+ fn container ( & self , db : & dyn HirDatabase ) -> ItemContainer {
4466+ ItemContainer :: Module ( Module { id : self . id . lookup ( db. upcast ( ) ) . container } )
4467+ }
4468+ }
4469+
4470+ impl HasContainer for TypeAlias {
4471+ fn container ( & self , db : & dyn HirDatabase ) -> ItemContainer {
4472+ container_id_to_hir ( self . id . lookup ( db. upcast ( ) ) . container )
4473+ }
4474+ }
4475+
4476+ impl HasContainer for Const {
4477+ fn container ( & self , db : & dyn HirDatabase ) -> ItemContainer {
4478+ container_id_to_hir ( self . id . lookup ( db. upcast ( ) ) . container )
4479+ }
4480+ }
4481+
4482+ impl HasContainer for Static {
4483+ fn container ( & self , db : & dyn HirDatabase ) -> ItemContainer {
4484+ container_id_to_hir ( self . id . lookup ( db. upcast ( ) ) . container )
4485+ }
4486+ }
4487+
4488+ impl HasContainer for Trait {
4489+ fn container ( & self , db : & dyn HirDatabase ) -> ItemContainer {
4490+ ItemContainer :: Module ( Module { id : self . id . lookup ( db. upcast ( ) ) . container } )
4491+ }
4492+ }
4493+
4494+ impl HasContainer for TraitAlias {
4495+ fn container ( & self , db : & dyn HirDatabase ) -> ItemContainer {
4496+ ItemContainer :: Module ( Module { id : self . id . lookup ( db. upcast ( ) ) . container } )
4497+ }
4498+ }
4499+
4500+ fn container_id_to_hir ( c : ItemContainerId ) -> ItemContainer {
4501+ match c {
4502+ ItemContainerId :: ExternBlockId ( _id) => ItemContainer :: ExternBlock ( ) ,
4503+ ItemContainerId :: ModuleId ( id) => ItemContainer :: Module ( Module { id } ) ,
4504+ ItemContainerId :: ImplId ( id) => ItemContainer :: Impl ( Impl { id } ) ,
4505+ ItemContainerId :: TraitId ( id) => ItemContainer :: Trait ( Trait { id } ) ,
4506+ }
4507+ }
4508+
4509+ #[ derive( Debug , Clone , Copy , PartialEq , Eq , Hash ) ]
4510+ pub enum ItemContainer {
4511+ Trait ( Trait ) ,
4512+ Impl ( Impl ) ,
4513+ Module ( Module ) ,
4514+ ExternBlock ( ) ,
4515+ Crate ( CrateId ) ,
4516+ }
0 commit comments