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()
}
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<TyDefId>,
) -> 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)
Ty::Apply(a_ty) => {
let field_types = db.field_types(def.into());
def.fields(db)
.into_iter()
.map(|it| (it, self.derived(it.ty(db).subst(&a_ty.parameters))))
.collect(),
.map(|it| {
let ty = field_types[it.id].clone().subst(&a_ty.parameters);
(it, self.derived(ty))
})
.collect()
}
_ => Vec::new(),
}
}

View file

@ -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),
}
}
}