Cleanup Field ty

This commit is contained in:
Aleksey Kladov 2019-12-08 12:16:57 +01:00
parent 08d3166c8b
commit 200bda3daf
2 changed files with 47 additions and 26 deletions

View file

@ -265,8 +265,10 @@ impl StructField {
self.parent.variant_data(db).fields()[self.id].name.clone() self.parent.variant_data(db).fields()[self.id].name.clone()
} }
pub fn ty(&self, db: &impl HirDatabase) -> Ty { pub fn ty(&self, db: &impl HirDatabase) -> Type {
db.field_types(self.parent.into())[self.id].clone() 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 { pub fn parent_def(&self, _db: &impl HirDatabase) -> VariantDef {
@ -940,15 +942,19 @@ pub struct Type {
} }
impl 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( fn from_def(
db: &impl HirDatabase, db: &impl HirDatabase,
krate: CrateId, krate: CrateId,
def: impl HasResolver + Into<TyDefId>, def: impl HasResolver + Into<TyDefId>,
) -> Type { ) -> Type {
let resolver = def.resolver(db);
let environment = TraitEnvironment::lower(db, &resolver);
let ty = db.ty(def.into()); 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 { pub fn is_bool(&self) -> bool {
@ -1039,11 +1045,16 @@ impl Type {
) -> Vec<(StructField, Type)> { ) -> Vec<(StructField, Type)> {
// FIXME: check that ty and def match // FIXME: check that ty and def match
match &self.ty.value { match &self.ty.value {
Ty::Apply(a_ty) => def Ty::Apply(a_ty) => {
.fields(db) let field_types = db.field_types(def.into());
.into_iter() def.fields(db)
.map(|it| (it, self.derived(it.ty(db).subst(&a_ty.parameters)))) .into_iter()
.collect(), .map(|it| {
let ty = field_types[it.id].clone().subst(&a_ty.parameters);
(it, self.derived(ty))
})
.collect()
}
_ => Vec::new(), _ => Vec::new(),
} }
} }

View file

@ -19,7 +19,7 @@ use crate::{
per_ns::PerNs, per_ns::PerNs,
AdtId, AstItemDef, ConstId, ContainerId, DefWithBodyId, EnumId, EnumVariantId, FunctionId, AdtId, AstItemDef, ConstId, ContainerId, DefWithBodyId, EnumId, EnumVariantId, FunctionId,
GenericDefId, HasModule, ImplId, LocalModuleId, Lookup, ModuleDefId, ModuleId, StaticId, GenericDefId, HasModule, ImplId, LocalModuleId, Lookup, ModuleDefId, ModuleId, StaticId,
StructId, TraitId, TypeAliasId, TypeParamId, StructId, TraitId, TypeAliasId, TypeParamId, VariantId,
}; };
#[derive(Debug, Clone, Default)] #[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 { impl HasResolver for ConstId {
fn resolver(self, db: &impl DefDatabase) -> Resolver { fn resolver(self, db: &impl DefDatabase) -> Resolver {
self.lookup(db).container.resolver(db) 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 { impl HasResolver for ContainerId {
fn resolver(self, db: &impl DefDatabase) -> Resolver { fn resolver(self, db: &impl DefDatabase) -> Resolver {
match self { 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 { fn resolver(self, db: &impl DefDatabase) -> Resolver {
self.module(db) match self {
.resolver(db) VariantId::EnumVariantId(it) => it.parent.resolver(db),
.push_generic_params_scope(db, self.into()) VariantId::StructId(it) => it.resolver(db),
.push_impl_block_scope(self) VariantId::UnionId(it) => it.resolver(db),
}
} }
} }