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 +++--- .../data/parser/inline/ok/0102_field_pat_list.txt | 46 ++++++++++++---------- 3 files changed, 46 insertions(+), 31 deletions(-) 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, } } diff --git a/crates/ra_syntax/tests/data/parser/inline/ok/0102_field_pat_list.txt b/crates/ra_syntax/tests/data/parser/inline/ok/0102_field_pat_list.txt index 13a51a9d9..ec48eada1 100644 --- a/crates/ra_syntax/tests/data/parser/inline/ok/0102_field_pat_list.txt +++ b/crates/ra_syntax/tests/data/parser/inline/ok/0102_field_pat_list.txt @@ -43,18 +43,20 @@ SOURCE_FILE@[0; 119) FIELD_PAT_LIST@[40; 56) L_CURLY@[40; 41) WHITESPACE@[41; 42) - BIND_PAT@[42; 43) - NAME@[42; 43) - IDENT@[42; 43) "f" + FIELD_PAT@[42; 43) + BIND_PAT@[42; 43) + NAME@[42; 43) + IDENT@[42; 43) "f" COMMA@[43; 44) WHITESPACE@[44; 45) - BIND_PAT@[45; 54) - REF_KW@[45; 48) - WHITESPACE@[48; 49) - MUT_KW@[49; 52) - WHITESPACE@[52; 53) - NAME@[53; 54) - IDENT@[53; 54) "g" + FIELD_PAT@[45; 54) + BIND_PAT@[45; 54) + REF_KW@[45; 48) + WHITESPACE@[48; 49) + MUT_KW@[49; 52) + WHITESPACE@[52; 53) + NAME@[53; 54) + IDENT@[53; 54) "g" WHITESPACE@[54; 55) R_CURLY@[55; 56) WHITESPACE@[56; 57) @@ -77,11 +79,13 @@ SOURCE_FILE@[0; 119) FIELD_PAT_LIST@[73; 84) L_CURLY@[73; 74) WHITESPACE@[74; 75) - IDENT@[75; 76) "h" - COLON@[76; 77) - WHITESPACE@[77; 78) - PLACEHOLDER_PAT@[78; 79) - UNDERSCORE@[78; 79) + FIELD_PAT@[75; 79) + NAME@[75; 76) + IDENT@[75; 76) "h" + COLON@[76; 77) + WHITESPACE@[77; 78) + PLACEHOLDER_PAT@[78; 79) + UNDERSCORE@[78; 79) COMMA@[79; 80) WHITESPACE@[80; 81) DOTDOT@[81; 83) @@ -106,11 +110,13 @@ SOURCE_FILE@[0; 119) FIELD_PAT_LIST@[101; 110) L_CURLY@[101; 102) WHITESPACE@[102; 103) - IDENT@[103; 104) "h" - COLON@[104; 105) - WHITESPACE@[105; 106) - PLACEHOLDER_PAT@[106; 107) - UNDERSCORE@[106; 107) + FIELD_PAT@[103; 107) + NAME@[103; 104) + IDENT@[103; 104) "h" + COLON@[104; 105) + WHITESPACE@[105; 106) + PLACEHOLDER_PAT@[106; 107) + UNDERSCORE@[106; 107) COMMA@[107; 108) WHITESPACE@[108; 109) R_CURLY@[109; 110) -- cgit v1.2.3