From bcbfa2cc1146dfa23acb3e61f7ec053733a8fac1 Mon Sep 17 00:00:00 2001 From: Marcus Klaas de Vries Date: Fri, 18 Jan 2019 15:38:11 +0100 Subject: Finish move of StructField for pattern type inference --- crates/ra_hir/src/code_model_api.rs | 18 +++++++++++++++--- crates/ra_hir/src/ty.rs | 13 +++++-------- 2 files changed, 20 insertions(+), 11 deletions(-) (limited to 'crates/ra_hir/src') diff --git a/crates/ra_hir/src/code_model_api.rs b/crates/ra_hir/src/code_model_api.rs index 860fd7539..11e919c1a 100644 --- a/crates/ra_hir/src/code_model_api.rs +++ b/crates/ra_hir/src/code_model_api.rs @@ -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 { - 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 { + 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) { def_id_to_ast(db, self.def_id) } diff --git a/crates/ra_hir/src/ty.rs b/crates/ra_hir/src/ty.rs index b880fb3d8..8d6493887 100644 --- a/crates/ra_hir/src/ty.rs +++ b/crates/ra_hir/src/ty.rs @@ -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)> { 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, } } -- cgit v1.2.3