diff options
-rw-r--r-- | crates/ra_hir/src/code_model.rs | 31 | ||||
-rw-r--r-- | crates/ra_hir_def/src/resolver.rs | 42 |
2 files changed, 47 insertions, 26 deletions
diff --git a/crates/ra_hir/src/code_model.rs b/crates/ra_hir/src/code_model.rs index 3695115e4..e854b22bf 100644 --- a/crates/ra_hir/src/code_model.rs +++ b/crates/ra_hir/src/code_model.rs | |||
@@ -265,8 +265,10 @@ impl StructField { | |||
265 | self.parent.variant_data(db).fields()[self.id].name.clone() | 265 | self.parent.variant_data(db).fields()[self.id].name.clone() |
266 | } | 266 | } |
267 | 267 | ||
268 | pub fn ty(&self, db: &impl HirDatabase) -> Ty { | 268 | pub fn ty(&self, db: &impl HirDatabase) -> Type { |
269 | db.field_types(self.parent.into())[self.id].clone() | 269 | let var_id = self.parent.into(); |
270 | let ty = db.field_types(var_id)[self.id].clone(); | ||
271 | Type::new(db, self.parent.module(db).id.krate.into(), var_id, ty) | ||
270 | } | 272 | } |
271 | 273 | ||
272 | pub fn parent_def(&self, _db: &impl HirDatabase) -> VariantDef { | 274 | pub fn parent_def(&self, _db: &impl HirDatabase) -> VariantDef { |
@@ -940,15 +942,19 @@ pub struct Type { | |||
940 | } | 942 | } |
941 | 943 | ||
942 | impl Type { | 944 | impl Type { |
945 | fn new(db: &impl HirDatabase, krate: CrateId, lexical_env: impl HasResolver, ty: Ty) -> Type { | ||
946 | let resolver = lexical_env.resolver(db); | ||
947 | let environment = TraitEnvironment::lower(db, &resolver); | ||
948 | Type { krate, ty: InEnvironment { value: ty, environment } } | ||
949 | } | ||
950 | |||
943 | fn from_def( | 951 | fn from_def( |
944 | db: &impl HirDatabase, | 952 | db: &impl HirDatabase, |
945 | krate: CrateId, | 953 | krate: CrateId, |
946 | def: impl HasResolver + Into<TyDefId>, | 954 | def: impl HasResolver + Into<TyDefId>, |
947 | ) -> Type { | 955 | ) -> Type { |
948 | let resolver = def.resolver(db); | ||
949 | let environment = TraitEnvironment::lower(db, &resolver); | ||
950 | let ty = db.ty(def.into()); | 956 | let ty = db.ty(def.into()); |
951 | Type { krate, ty: InEnvironment { value: ty, environment } } | 957 | Type::new(db, krate, def, ty) |
952 | } | 958 | } |
953 | 959 | ||
954 | pub fn is_bool(&self) -> bool { | 960 | pub fn is_bool(&self) -> bool { |
@@ -1039,11 +1045,16 @@ impl Type { | |||
1039 | ) -> Vec<(StructField, Type)> { | 1045 | ) -> Vec<(StructField, Type)> { |
1040 | // FIXME: check that ty and def match | 1046 | // FIXME: check that ty and def match |
1041 | match &self.ty.value { | 1047 | match &self.ty.value { |
1042 | Ty::Apply(a_ty) => def | 1048 | Ty::Apply(a_ty) => { |
1043 | .fields(db) | 1049 | let field_types = db.field_types(def.into()); |
1044 | .into_iter() | 1050 | def.fields(db) |
1045 | .map(|it| (it, self.derived(it.ty(db).subst(&a_ty.parameters)))) | 1051 | .into_iter() |
1046 | .collect(), | 1052 | .map(|it| { |
1053 | let ty = field_types[it.id].clone().subst(&a_ty.parameters); | ||
1054 | (it, self.derived(ty)) | ||
1055 | }) | ||
1056 | .collect() | ||
1057 | } | ||
1047 | _ => Vec::new(), | 1058 | _ => Vec::new(), |
1048 | } | 1059 | } |
1049 | } | 1060 | } |
diff --git a/crates/ra_hir_def/src/resolver.rs b/crates/ra_hir_def/src/resolver.rs index 4c859e497..9484a61d5 100644 --- a/crates/ra_hir_def/src/resolver.rs +++ b/crates/ra_hir_def/src/resolver.rs | |||
@@ -19,7 +19,7 @@ use crate::{ | |||
19 | per_ns::PerNs, | 19 | per_ns::PerNs, |
20 | AdtId, AstItemDef, ConstId, ContainerId, DefWithBodyId, EnumId, EnumVariantId, FunctionId, | 20 | AdtId, AstItemDef, ConstId, ContainerId, DefWithBodyId, EnumId, EnumVariantId, FunctionId, |
21 | GenericDefId, HasModule, ImplId, LocalModuleId, Lookup, ModuleDefId, ModuleId, StaticId, | 21 | GenericDefId, HasModule, ImplId, LocalModuleId, Lookup, ModuleDefId, ModuleId, StaticId, |
22 | StructId, TraitId, TypeAliasId, TypeParamId, | 22 | StructId, TraitId, TypeAliasId, TypeParamId, VariantId, |
23 | }; | 23 | }; |
24 | 24 | ||
25 | #[derive(Debug, Clone, Default)] | 25 | #[derive(Debug, Clone, Default)] |
@@ -544,16 +544,6 @@ impl HasResolver for FunctionId { | |||
544 | } | 544 | } |
545 | } | 545 | } |
546 | 546 | ||
547 | impl HasResolver for DefWithBodyId { | ||
548 | fn resolver(self, db: &impl DefDatabase) -> Resolver { | ||
549 | match self { | ||
550 | DefWithBodyId::ConstId(c) => c.resolver(db), | ||
551 | DefWithBodyId::FunctionId(f) => f.resolver(db), | ||
552 | DefWithBodyId::StaticId(s) => s.resolver(db), | ||
553 | } | ||
554 | } | ||
555 | } | ||
556 | |||
557 | impl HasResolver for ConstId { | 547 | impl HasResolver for ConstId { |
558 | fn resolver(self, db: &impl DefDatabase) -> Resolver { | 548 | fn resolver(self, db: &impl DefDatabase) -> Resolver { |
559 | self.lookup(db).container.resolver(db) | 549 | self.lookup(db).container.resolver(db) |
@@ -572,6 +562,25 @@ impl HasResolver for TypeAliasId { | |||
572 | } | 562 | } |
573 | } | 563 | } |
574 | 564 | ||
565 | impl HasResolver for ImplId { | ||
566 | fn resolver(self, db: &impl DefDatabase) -> Resolver { | ||
567 | self.module(db) | ||
568 | .resolver(db) | ||
569 | .push_generic_params_scope(db, self.into()) | ||
570 | .push_impl_block_scope(self) | ||
571 | } | ||
572 | } | ||
573 | |||
574 | impl HasResolver for DefWithBodyId { | ||
575 | fn resolver(self, db: &impl DefDatabase) -> Resolver { | ||
576 | match self { | ||
577 | DefWithBodyId::ConstId(c) => c.resolver(db), | ||
578 | DefWithBodyId::FunctionId(f) => f.resolver(db), | ||
579 | DefWithBodyId::StaticId(s) => s.resolver(db), | ||
580 | } | ||
581 | } | ||
582 | } | ||
583 | |||
575 | impl HasResolver for ContainerId { | 584 | impl HasResolver for ContainerId { |
576 | fn resolver(self, db: &impl DefDatabase) -> Resolver { | 585 | fn resolver(self, db: &impl DefDatabase) -> Resolver { |
577 | match self { | 586 | match self { |
@@ -596,11 +605,12 @@ impl HasResolver for GenericDefId { | |||
596 | } | 605 | } |
597 | } | 606 | } |
598 | 607 | ||
599 | impl HasResolver for ImplId { | 608 | impl HasResolver for VariantId { |
600 | fn resolver(self, db: &impl DefDatabase) -> Resolver { | 609 | fn resolver(self, db: &impl DefDatabase) -> Resolver { |
601 | self.module(db) | 610 | match self { |
602 | .resolver(db) | 611 | VariantId::EnumVariantId(it) => it.parent.resolver(db), |
603 | .push_generic_params_scope(db, self.into()) | 612 | VariantId::StructId(it) => it.resolver(db), |
604 | .push_impl_block_scope(self) | 613 | VariantId::UnionId(it) => it.resolver(db), |
614 | } | ||
605 | } | 615 | } |
606 | } | 616 | } |