Finish move of StructField for pattern type inference

This commit is contained in:
Marcus Klaas de Vries 2019-01-18 15:38:11 +01:00 committed by Aleksey Kladov
parent 4277f420aa
commit bcbfa2cc11
3 changed files with 46 additions and 31 deletions

View file

@ -158,7 +158,7 @@ impl Module {
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct StructField {
struct_: Struct,
parent: DefId,
name: Name,
}
@ -166,8 +166,9 @@ impl StructField {
pub fn name(&self) -> &Name {
&self.name
}
pub fn ty(&self, db: &impl HirDatabase) -> Option<Ty> {
db.type_for_field(self.struct_.def_id, self.name.clone())
db.type_for_field(self.parent, self.name.clone())
}
}
@ -191,7 +192,7 @@ impl Struct {
.fields()
.iter()
.map(|it| StructField {
struct_: self.clone(),
parent: self.def_id,
name: it.name.clone(),
})
.collect()
@ -255,6 +256,17 @@ impl EnumVariant {
db.enum_variant_data(self.def_id).variant_data.clone()
}
pub fn fields(&self, db: &impl HirDatabase) -> Vec<StructField> {
self.variant_data(db)
.fields()
.iter()
.map(|it| StructField {
parent: self.def_id,
name: it.name.clone(),
})
.collect()
}
pub fn source(&self, db: &impl HirDatabase) -> (HirFileId, TreeArc<ast::EnumVariant>) {
def_id_to_ast(db, self.def_id)
}

View file

@ -872,22 +872,19 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> {
}
}
// TODO: add fields method for tuple like structs and variants
// TODO: and add tests!
fn resolve_fields(&self, path: Option<&Path>) -> Option<(Ty, Vec<StructField>)> {
let def_id = self.module.resolve_path(self.db, path?).take_types()?;
let def = def_id.resolve(self.db);
match def {
Def::Struct(s) => {
let fields: Vec<_> = s.fields(self.db);
let fields = s.fields(self.db);
Some((type_for_struct(self.db, s), fields))
}
// Def::EnumVariant(ev) => {
// let fields: Vec<_> = ev.variant_data(self.db).fields().to_owned();
// Some((type_for_enum_variant(self.db, ev), fields))
// }
Def::EnumVariant(ev) => {
let fields = ev.fields(self.db);
Some((type_for_enum_variant(self.db, ev), fields))
}
_ => None,
}
}