From 200bda3daf66f338e9bb1d833146f06fb81f829e Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Sun, 8 Dec 2019 12:16:57 +0100 Subject: Cleanup Field ty --- crates/ra_hir/src/code_model.rs | 31 +++++++++++++++++++---------- crates/ra_hir_def/src/resolver.rs | 42 ++++++++++++++++++++++++--------------- 2 files changed, 47 insertions(+), 26 deletions(-) (limited to 'crates') 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 { self.parent.variant_data(db).fields()[self.id].name.clone() } - pub fn ty(&self, db: &impl HirDatabase) -> Ty { - db.field_types(self.parent.into())[self.id].clone() + pub fn ty(&self, db: &impl HirDatabase) -> Type { + let var_id = self.parent.into(); + let ty = db.field_types(var_id)[self.id].clone(); + Type::new(db, self.parent.module(db).id.krate.into(), var_id, ty) } pub fn parent_def(&self, _db: &impl HirDatabase) -> VariantDef { @@ -940,15 +942,19 @@ pub struct Type { } impl Type { + fn new(db: &impl HirDatabase, krate: CrateId, lexical_env: impl HasResolver, ty: Ty) -> Type { + let resolver = lexical_env.resolver(db); + let environment = TraitEnvironment::lower(db, &resolver); + Type { krate, ty: InEnvironment { value: ty, environment } } + } + fn from_def( db: &impl HirDatabase, krate: CrateId, def: impl HasResolver + Into, ) -> Type { - let resolver = def.resolver(db); - let environment = TraitEnvironment::lower(db, &resolver); let ty = db.ty(def.into()); - Type { krate, ty: InEnvironment { value: ty, environment } } + Type::new(db, krate, def, ty) } pub fn is_bool(&self) -> bool { @@ -1039,11 +1045,16 @@ impl Type { ) -> Vec<(StructField, Type)> { // FIXME: check that ty and def match match &self.ty.value { - Ty::Apply(a_ty) => def - .fields(db) - .into_iter() - .map(|it| (it, self.derived(it.ty(db).subst(&a_ty.parameters)))) - .collect(), + Ty::Apply(a_ty) => { + let field_types = db.field_types(def.into()); + def.fields(db) + .into_iter() + .map(|it| { + let ty = field_types[it.id].clone().subst(&a_ty.parameters); + (it, self.derived(ty)) + }) + .collect() + } _ => Vec::new(), } } 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::{ per_ns::PerNs, AdtId, AstItemDef, ConstId, ContainerId, DefWithBodyId, EnumId, EnumVariantId, FunctionId, GenericDefId, HasModule, ImplId, LocalModuleId, Lookup, ModuleDefId, ModuleId, StaticId, - StructId, TraitId, TypeAliasId, TypeParamId, + StructId, TraitId, TypeAliasId, TypeParamId, VariantId, }; #[derive(Debug, Clone, Default)] @@ -544,16 +544,6 @@ impl HasResolver for FunctionId { } } -impl HasResolver for DefWithBodyId { - fn resolver(self, db: &impl DefDatabase) -> Resolver { - match self { - DefWithBodyId::ConstId(c) => c.resolver(db), - DefWithBodyId::FunctionId(f) => f.resolver(db), - DefWithBodyId::StaticId(s) => s.resolver(db), - } - } -} - impl HasResolver for ConstId { fn resolver(self, db: &impl DefDatabase) -> Resolver { self.lookup(db).container.resolver(db) @@ -572,6 +562,25 @@ impl HasResolver for TypeAliasId { } } +impl HasResolver for ImplId { + fn resolver(self, db: &impl DefDatabase) -> Resolver { + self.module(db) + .resolver(db) + .push_generic_params_scope(db, self.into()) + .push_impl_block_scope(self) + } +} + +impl HasResolver for DefWithBodyId { + fn resolver(self, db: &impl DefDatabase) -> Resolver { + match self { + DefWithBodyId::ConstId(c) => c.resolver(db), + DefWithBodyId::FunctionId(f) => f.resolver(db), + DefWithBodyId::StaticId(s) => s.resolver(db), + } + } +} + impl HasResolver for ContainerId { fn resolver(self, db: &impl DefDatabase) -> Resolver { match self { @@ -596,11 +605,12 @@ impl HasResolver for GenericDefId { } } -impl HasResolver for ImplId { +impl HasResolver for VariantId { fn resolver(self, db: &impl DefDatabase) -> Resolver { - self.module(db) - .resolver(db) - .push_generic_params_scope(db, self.into()) - .push_impl_block_scope(self) + match self { + VariantId::EnumVariantId(it) => it.parent.resolver(db), + VariantId::StructId(it) => it.resolver(db), + VariantId::UnionId(it) => it.resolver(db), + } } } -- cgit v1.2.3