diff options
Diffstat (limited to 'crates/ra_hir/src/code_model.rs')
-rw-r--r-- | crates/ra_hir/src/code_model.rs | 49 |
1 files changed, 40 insertions, 9 deletions
diff --git a/crates/ra_hir/src/code_model.rs b/crates/ra_hir/src/code_model.rs index b4b47057d..54da937ea 100644 --- a/crates/ra_hir/src/code_model.rs +++ b/crates/ra_hir/src/code_model.rs | |||
@@ -28,7 +28,8 @@ use crate::{ | |||
28 | expr::{BindingAnnotation, Body, BodySourceMap, ExprValidator, Pat, PatId}, | 28 | expr::{BindingAnnotation, Body, BodySourceMap, ExprValidator, Pat, PatId}, |
29 | ty::display::HirFormatter, | 29 | ty::display::HirFormatter, |
30 | ty::{ | 30 | ty::{ |
31 | self, InEnvironment, InferenceResult, TraitEnvironment, TraitRef, Ty, TypeCtor, TypeWalk, | 31 | self, InEnvironment, InferenceResult, TraitEnvironment, TraitRef, Ty, TyDefId, TypeCtor, |
32 | TypeWalk, | ||
32 | }, | 33 | }, |
33 | CallableDef, Either, HirDisplay, Name, Source, | 34 | CallableDef, Either, HirDisplay, Name, Source, |
34 | }; | 35 | }; |
@@ -498,12 +499,9 @@ impl Adt { | |||
498 | let subst = db.generic_defaults(self.into()); | 499 | let subst = db.generic_defaults(self.into()); |
499 | subst.iter().any(|ty| ty == &Ty::Unknown) | 500 | subst.iter().any(|ty| ty == &Ty::Unknown) |
500 | } | 501 | } |
501 | pub fn ty(self, db: &impl HirDatabase) -> Ty { | 502 | pub fn ty(self, db: &impl HirDatabase) -> Type { |
502 | match self { | 503 | let id = AdtId::from(self); |
503 | Adt::Struct(it) => it.ty(db), | 504 | Type::from_def(db, id.module(db).krate, id) |
504 | Adt::Union(it) => it.ty(db), | ||
505 | Adt::Enum(it) => it.ty(db), | ||
506 | } | ||
507 | } | 505 | } |
508 | 506 | ||
509 | pub fn module(self, db: &impl DefDatabase) -> Module { | 507 | pub fn module(self, db: &impl DefDatabase) -> Module { |
@@ -795,8 +793,8 @@ impl TypeAlias { | |||
795 | db.type_alias_data(self.id).type_ref.clone() | 793 | db.type_alias_data(self.id).type_ref.clone() |
796 | } | 794 | } |
797 | 795 | ||
798 | pub fn ty(self, db: &impl HirDatabase) -> Ty { | 796 | pub fn ty(self, db: &impl HirDatabase) -> Type { |
799 | db.ty(self.id.into()) | 797 | Type::from_def(db, self.id.lookup(db).module(db).krate, self.id) |
800 | } | 798 | } |
801 | 799 | ||
802 | pub fn name(self, db: &impl DefDatabase) -> Name { | 800 | pub fn name(self, db: &impl DefDatabase) -> Name { |
@@ -989,6 +987,17 @@ pub struct Type { | |||
989 | } | 987 | } |
990 | 988 | ||
991 | impl Type { | 989 | impl Type { |
990 | fn from_def( | ||
991 | db: &impl HirDatabase, | ||
992 | krate: CrateId, | ||
993 | def: impl HasResolver + Into<TyDefId>, | ||
994 | ) -> Type { | ||
995 | let resolver = def.resolver(db); | ||
996 | let environment = TraitEnvironment::lower(db, &resolver); | ||
997 | let ty = db.ty(def.into()); | ||
998 | Type { krate, ty: InEnvironment { value: ty, environment } } | ||
999 | } | ||
1000 | |||
992 | pub fn is_bool(&self) -> bool { | 1001 | pub fn is_bool(&self) -> bool { |
993 | match &self.ty.value { | 1002 | match &self.ty.value { |
994 | Ty::Apply(a_ty) => match a_ty.ctor { | 1003 | Ty::Apply(a_ty) => match a_ty.ctor { |
@@ -1097,6 +1106,28 @@ impl Type { | |||
1097 | .map(move |ty| self.derived(ty)) | 1106 | .map(move |ty| self.derived(ty)) |
1098 | } | 1107 | } |
1099 | 1108 | ||
1109 | // This would be nicer if it just returned an iterator, but that runs into | ||
1110 | // lifetime problems, because we need to borrow temp `CrateImplBlocks`. | ||
1111 | pub fn iterate_impl_items<T>( | ||
1112 | self, | ||
1113 | db: &impl HirDatabase, | ||
1114 | krate: Crate, | ||
1115 | mut callback: impl FnMut(AssocItem) -> Option<T>, | ||
1116 | ) -> Option<T> { | ||
1117 | for krate in self.ty.value.def_crates(db, krate.crate_id)? { | ||
1118 | let impls = db.impls_in_crate(krate); | ||
1119 | |||
1120 | for impl_block in impls.lookup_impl_blocks(&self.ty.value) { | ||
1121 | for &item in db.impl_data(impl_block).items.iter() { | ||
1122 | if let Some(result) = callback(item.into()) { | ||
1123 | return Some(result); | ||
1124 | } | ||
1125 | } | ||
1126 | } | ||
1127 | } | ||
1128 | None | ||
1129 | } | ||
1130 | |||
1100 | // FIXME: remove | 1131 | // FIXME: remove |
1101 | pub fn into_ty(self) -> Ty { | 1132 | pub fn into_ty(self) -> Ty { |
1102 | self.ty.value | 1133 | self.ty.value |