From ba12e83c26b24358e1bfbae0f913f8dfa13fc68f Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Thu, 19 Dec 2019 18:12:46 +0100 Subject: Add body as a possible container for items --- crates/ra_hir_def/src/lib.rs | 24 ++++++++++++------------ crates/ra_hir_def/src/resolver.rs | 1 + crates/ra_hir_ty/src/infer/path.rs | 2 +- crates/ra_hir_ty/src/method_resolution.rs | 10 +++++----- crates/ra_hir_ty/src/utils.rs | 2 +- 5 files changed, 20 insertions(+), 19 deletions(-) diff --git a/crates/ra_hir_def/src/lib.rs b/crates/ra_hir_def/src/lib.rs index f085bbe87..4fc3127c4 100644 --- a/crates/ra_hir_def/src/lib.rs +++ b/crates/ra_hir_def/src/lib.rs @@ -335,6 +335,7 @@ pub enum ContainerId { ModuleId(ModuleId), ImplId(ImplId), TraitId(TraitId), + DefWithBodyId(DefWithBodyId), } /// A Data Type @@ -478,33 +479,32 @@ pub trait HasModule { fn module(&self, db: &impl db::DefDatabase) -> ModuleId; } -impl HasModule for FunctionLoc { +impl HasModule for ContainerId { fn module(&self, db: &impl db::DefDatabase) -> ModuleId { - match self.container { + match *self { ContainerId::ModuleId(it) => it, ContainerId::ImplId(it) => it.lookup(db).container, ContainerId::TraitId(it) => it.lookup(db).container, + ContainerId::DefWithBodyId(it) => it.module(db), } } } +impl HasModule for FunctionLoc { + fn module(&self, db: &impl db::DefDatabase) -> ModuleId { + self.container.module(db) + } +} + impl HasModule for TypeAliasLoc { fn module(&self, db: &impl db::DefDatabase) -> ModuleId { - match self.container { - ContainerId::ModuleId(it) => it, - ContainerId::ImplId(it) => it.lookup(db).container, - ContainerId::TraitId(it) => it.lookup(db).container, - } + self.container.module(db) } } impl HasModule for ConstLoc { fn module(&self, db: &impl db::DefDatabase) -> ModuleId { - match self.container { - ContainerId::ModuleId(it) => it, - ContainerId::ImplId(it) => it.lookup(db).container, - ContainerId::TraitId(it) => it.lookup(db).container, - } + self.container.module(db) } } diff --git a/crates/ra_hir_def/src/resolver.rs b/crates/ra_hir_def/src/resolver.rs index 2694c0438..250329271 100644 --- a/crates/ra_hir_def/src/resolver.rs +++ b/crates/ra_hir_def/src/resolver.rs @@ -586,6 +586,7 @@ impl HasResolver for ContainerId { ContainerId::TraitId(it) => it.resolver(db), ContainerId::ImplId(it) => it.resolver(db), ContainerId::ModuleId(it) => it.resolver(db), + ContainerId::DefWithBodyId(it) => it.resolver(db), } } } diff --git a/crates/ra_hir_ty/src/infer/path.rs b/crates/ra_hir_ty/src/infer/path.rs index 402a89386..31c90ea1e 100644 --- a/crates/ra_hir_ty/src/infer/path.rs +++ b/crates/ra_hir_ty/src/infer/path.rs @@ -237,7 +237,7 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> { })); Some(substs) } - ContainerId::ModuleId(_) => None, + ContainerId::ModuleId(_) | ContainerId::DefWithBodyId(_) => None, }; self.write_assoc_resolution(id, item.into()); diff --git a/crates/ra_hir_ty/src/method_resolution.rs b/crates/ra_hir_ty/src/method_resolution.rs index 848e306e9..d0b2b016d 100644 --- a/crates/ra_hir_ty/src/method_resolution.rs +++ b/crates/ra_hir_ty/src/method_resolution.rs @@ -6,8 +6,8 @@ use std::sync::Arc; use arrayvec::ArrayVec; use hir_def::{ - lang_item::LangItemTarget, resolver::Resolver, type_ref::Mutability, AssocItemId, FunctionId, - HasModule, ImplId, Lookup, TraitId, + lang_item::LangItemTarget, resolver::Resolver, type_ref::Mutability, AssocItemId, ContainerId, + FunctionId, HasModule, ImplId, Lookup, TraitId, }; use hir_expand::name::Name; use ra_db::CrateId; @@ -451,12 +451,12 @@ fn transform_receiver_ty( self_ty: &Canonical, ) -> Option { let substs = match function_id.lookup(db).container { - hir_def::ContainerId::TraitId(_) => Substs::build_for_def(db, function_id) + ContainerId::TraitId(_) => Substs::build_for_def(db, function_id) .push(self_ty.value.clone()) .fill_with_unknown() .build(), - hir_def::ContainerId::ImplId(impl_id) => inherent_impl_substs(db, impl_id, &self_ty)?, - hir_def::ContainerId::ModuleId(_) => unreachable!(), + ContainerId::ImplId(impl_id) => inherent_impl_substs(db, impl_id, &self_ty)?, + ContainerId::ModuleId(_) | ContainerId::DefWithBodyId(_) => unreachable!(), }; let sig = db.callable_item_signature(function_id.into()); Some(sig.params()[0].clone().subst(&substs)) diff --git a/crates/ra_hir_ty/src/utils.rs b/crates/ra_hir_ty/src/utils.rs index 29799a8cb..34defc1a2 100644 --- a/crates/ra_hir_ty/src/utils.rs +++ b/crates/ra_hir_ty/src/utils.rs @@ -157,6 +157,6 @@ fn parent_generic_def(db: &impl DefDatabase, def: GenericDefId) -> Option Some(it.into()), ContainerId::TraitId(it) => Some(it.into()), - ContainerId::ModuleId(_) => None, + ContainerId::ModuleId(_) | ContainerId::DefWithBodyId(_) => None, } } -- cgit v1.2.3