aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_hir/src/code_model.rs
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ra_hir/src/code_model.rs')
-rw-r--r--crates/ra_hir/src/code_model.rs61
1 files changed, 40 insertions, 21 deletions
diff --git a/crates/ra_hir/src/code_model.rs b/crates/ra_hir/src/code_model.rs
index 3f645a1dd..5f480c304 100644
--- a/crates/ra_hir/src/code_model.rs
+++ b/crates/ra_hir/src/code_model.rs
@@ -19,7 +19,7 @@ use hir_def::{
19use hir_expand::{ 19use hir_expand::{
20 diagnostics::DiagnosticSink, 20 diagnostics::DiagnosticSink,
21 name::{name, AsName}, 21 name::{name, AsName},
22 MacroDefId, 22 MacroDefId, MacroDefKind,
23}; 23};
24use hir_ty::{ 24use hir_ty::{
25 autoderef, display::HirFormatter, expr::ExprValidator, method_resolution, ApplicationTy, 25 autoderef, display::HirFormatter, expr::ExprValidator, method_resolution, ApplicationTy,
@@ -762,13 +762,12 @@ impl MacroDef {
762 762
763 /// Indicate it is a proc-macro 763 /// Indicate it is a proc-macro
764 pub fn is_proc_macro(&self) -> bool { 764 pub fn is_proc_macro(&self) -> bool {
765 match self.id.kind { 765 matches!(self.id.kind, MacroDefKind::CustomDerive(_))
766 hir_expand::MacroDefKind::Declarative => false, 766 }
767 hir_expand::MacroDefKind::BuiltIn(_) => false, 767
768 hir_expand::MacroDefKind::BuiltInDerive(_) => false, 768 /// Indicate it is a derive macro
769 hir_expand::MacroDefKind::BuiltInEager(_) => false, 769 pub fn is_derive_macro(&self) -> bool {
770 hir_expand::MacroDefKind::CustomDerive(_) => true, 770 matches!(self.id.kind, MacroDefKind::CustomDerive(_) | MacroDefKind::BuiltInDerive(_))
771 }
772 } 771 }
773} 772}
774 773
@@ -953,6 +952,16 @@ impl TypeParam {
953 pub fn module(self, db: &dyn HirDatabase) -> Module { 952 pub fn module(self, db: &dyn HirDatabase) -> Module {
954 self.id.parent.module(db.upcast()).into() 953 self.id.parent.module(db.upcast()).into()
955 } 954 }
955
956 pub fn ty(self, db: &dyn HirDatabase) -> Type {
957 let resolver = self.id.parent.resolver(db.upcast());
958 let environment = TraitEnvironment::lower(db, &resolver);
959 let ty = Ty::Placeholder(self.id);
960 Type {
961 krate: self.id.parent.module(db.upcast()).krate,
962 ty: InEnvironment { value: ty, environment },
963 }
964 }
956} 965}
957 966
958// FIXME: rename from `ImplDef` to `Impl` 967// FIXME: rename from `ImplDef` to `Impl`
@@ -1136,6 +1145,13 @@ impl Type {
1136 matches!(&self.ty.value, Ty::Apply(ApplicationTy { ctor: TypeCtor::Closure { .. }, .. })) 1145 matches!(&self.ty.value, Ty::Apply(ApplicationTy { ctor: TypeCtor::Closure { .. }, .. }))
1137 } 1146 }
1138 1147
1148 pub fn is_fn(&self) -> bool {
1149 matches!(&self.ty.value,
1150 Ty::Apply(ApplicationTy { ctor: TypeCtor::FnDef(..), .. }) |
1151 Ty::Apply(ApplicationTy { ctor: TypeCtor::FnPtr { .. }, .. })
1152 )
1153 }
1154
1139 pub fn contains_unknown(&self) -> bool { 1155 pub fn contains_unknown(&self) -> bool {
1140 return go(&self.ty.value); 1156 return go(&self.ty.value);
1141 1157
@@ -1150,18 +1166,21 @@ impl Type {
1150 1166
1151 pub fn fields(&self, db: &dyn HirDatabase) -> Vec<(Field, Type)> { 1167 pub fn fields(&self, db: &dyn HirDatabase) -> Vec<(Field, Type)> {
1152 if let Ty::Apply(a_ty) = &self.ty.value { 1168 if let Ty::Apply(a_ty) = &self.ty.value {
1153 if let TypeCtor::Adt(AdtId::StructId(s)) = a_ty.ctor { 1169 let variant_id = match a_ty.ctor {
1154 let var_def = s.into(); 1170 TypeCtor::Adt(AdtId::StructId(s)) => s.into(),
1155 return db 1171 TypeCtor::Adt(AdtId::UnionId(u)) => u.into(),
1156 .field_types(var_def) 1172 _ => return Vec::new(),
1157 .iter() 1173 };
1158 .map(|(local_id, ty)| { 1174
1159 let def = Field { parent: var_def.into(), id: local_id }; 1175 return db
1160 let ty = ty.clone().subst(&a_ty.parameters); 1176 .field_types(variant_id)
1161 (def, self.derived(ty)) 1177 .iter()
1162 }) 1178 .map(|(local_id, ty)| {
1163 .collect(); 1179 let def = Field { parent: variant_id.into(), id: local_id };
1164 } 1180 let ty = ty.clone().subst(&a_ty.parameters);
1181 (def, self.derived(ty))
1182 })
1183 .collect();
1165 }; 1184 };
1166 Vec::new() 1185 Vec::new()
1167 } 1186 }
@@ -1192,7 +1211,7 @@ impl Type {
1192 1211
1193 // This would be nicer if it just returned an iterator, but that runs into 1212 // This would be nicer if it just returned an iterator, but that runs into
1194 // lifetime problems, because we need to borrow temp `CrateImplDefs`. 1213 // lifetime problems, because we need to borrow temp `CrateImplDefs`.
1195 pub fn iterate_impl_items<T>( 1214 pub fn iterate_assoc_items<T>(
1196 self, 1215 self,
1197 db: &dyn HirDatabase, 1216 db: &dyn HirDatabase,
1198 krate: Crate, 1217 krate: Crate,