aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMarcus Klaas de Vries <[email protected]>2019-01-18 14:38:11 +0000
committerAleksey Kladov <[email protected]>2019-01-19 12:37:26 +0000
commitbcbfa2cc1146dfa23acb3e61f7ec053733a8fac1 (patch)
tree7586d897724f0bf75b466945c4f9f34cd59197d7
parent4277f420aa947f0c0739506ec229da94e76ee67b (diff)
Finish move of StructField for pattern type inference
-rw-r--r--crates/ra_hir/src/code_model_api.rs18
-rw-r--r--crates/ra_hir/src/ty.rs13
-rw-r--r--crates/ra_syntax/tests/data/parser/inline/ok/0102_field_pat_list.txt46
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 {
158 158
159#[derive(Debug, Clone, PartialEq, Eq, Hash)] 159#[derive(Debug, Clone, PartialEq, Eq, Hash)]
160pub struct StructField { 160pub struct StructField {
161 struct_: Struct, 161 parent: DefId,
162 name: Name, 162 name: Name,
163} 163}
164 164
@@ -166,8 +166,9 @@ impl StructField {
166 pub fn name(&self) -> &Name { 166 pub fn name(&self) -> &Name {
167 &self.name 167 &self.name
168 } 168 }
169
169 pub fn ty(&self, db: &impl HirDatabase) -> Option<Ty> { 170 pub fn ty(&self, db: &impl HirDatabase) -> Option<Ty> {
170 db.type_for_field(self.struct_.def_id, self.name.clone()) 171 db.type_for_field(self.parent, self.name.clone())
171 } 172 }
172} 173}
173 174
@@ -191,7 +192,7 @@ impl Struct {
191 .fields() 192 .fields()
192 .iter() 193 .iter()
193 .map(|it| StructField { 194 .map(|it| StructField {
194 struct_: self.clone(), 195 parent: self.def_id,
195 name: it.name.clone(), 196 name: it.name.clone(),
196 }) 197 })
197 .collect() 198 .collect()
@@ -255,6 +256,17 @@ impl EnumVariant {
255 db.enum_variant_data(self.def_id).variant_data.clone() 256 db.enum_variant_data(self.def_id).variant_data.clone()
256 } 257 }
257 258
259 pub fn fields(&self, db: &impl HirDatabase) -> Vec<StructField> {
260 self.variant_data(db)
261 .fields()
262 .iter()
263 .map(|it| StructField {
264 parent: self.def_id,
265 name: it.name.clone(),
266 })
267 .collect()
268 }
269
258 pub fn source(&self, db: &impl HirDatabase) -> (HirFileId, TreeArc<ast::EnumVariant>) { 270 pub fn source(&self, db: &impl HirDatabase) -> (HirFileId, TreeArc<ast::EnumVariant>) {
259 def_id_to_ast(db, self.def_id) 271 def_id_to_ast(db, self.def_id)
260 } 272 }
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> {
872 } 872 }
873 } 873 }
874 874
875 // TODO: add fields method for tuple like structs and variants
876 // TODO: and add tests!
877
878 fn resolve_fields(&self, path: Option<&Path>) -> Option<(Ty, Vec<StructField>)> { 875 fn resolve_fields(&self, path: Option<&Path>) -> Option<(Ty, Vec<StructField>)> {
879 let def_id = self.module.resolve_path(self.db, path?).take_types()?; 876 let def_id = self.module.resolve_path(self.db, path?).take_types()?;
880 let def = def_id.resolve(self.db); 877 let def = def_id.resolve(self.db);
881 878
882 match def { 879 match def {
883 Def::Struct(s) => { 880 Def::Struct(s) => {
884 let fields: Vec<_> = s.fields(self.db); 881 let fields = s.fields(self.db);
885 Some((type_for_struct(self.db, s), fields)) 882 Some((type_for_struct(self.db, s), fields))
886 } 883 }
887 // Def::EnumVariant(ev) => { 884 Def::EnumVariant(ev) => {
888 // let fields: Vec<_> = ev.variant_data(self.db).fields().to_owned(); 885 let fields = ev.fields(self.db);
889 // Some((type_for_enum_variant(self.db, ev), fields)) 886 Some((type_for_enum_variant(self.db, ev), fields))
890 // } 887 }
891 _ => None, 888 _ => None,
892 } 889 }
893 } 890 }
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)
43 FIELD_PAT_LIST@[40; 56) 43 FIELD_PAT_LIST@[40; 56)
44 L_CURLY@[40; 41) 44 L_CURLY@[40; 41)
45 WHITESPACE@[41; 42) 45 WHITESPACE@[41; 42)
46 BIND_PAT@[42; 43) 46 FIELD_PAT@[42; 43)
47 NAME@[42; 43) 47 BIND_PAT@[42; 43)
48 IDENT@[42; 43) "f" 48 NAME@[42; 43)
49 IDENT@[42; 43) "f"
49 COMMA@[43; 44) 50 COMMA@[43; 44)
50 WHITESPACE@[44; 45) 51 WHITESPACE@[44; 45)
51 BIND_PAT@[45; 54) 52 FIELD_PAT@[45; 54)
52 REF_KW@[45; 48) 53 BIND_PAT@[45; 54)
53 WHITESPACE@[48; 49) 54 REF_KW@[45; 48)
54 MUT_KW@[49; 52) 55 WHITESPACE@[48; 49)
55 WHITESPACE@[52; 53) 56 MUT_KW@[49; 52)
56 NAME@[53; 54) 57 WHITESPACE@[52; 53)
57 IDENT@[53; 54) "g" 58 NAME@[53; 54)
59 IDENT@[53; 54) "g"
58 WHITESPACE@[54; 55) 60 WHITESPACE@[54; 55)
59 R_CURLY@[55; 56) 61 R_CURLY@[55; 56)
60 WHITESPACE@[56; 57) 62 WHITESPACE@[56; 57)
@@ -77,11 +79,13 @@ SOURCE_FILE@[0; 119)
77 FIELD_PAT_LIST@[73; 84) 79 FIELD_PAT_LIST@[73; 84)
78 L_CURLY@[73; 74) 80 L_CURLY@[73; 74)
79 WHITESPACE@[74; 75) 81 WHITESPACE@[74; 75)
80 IDENT@[75; 76) "h" 82 FIELD_PAT@[75; 79)
81 COLON@[76; 77) 83 NAME@[75; 76)
82 WHITESPACE@[77; 78) 84 IDENT@[75; 76) "h"
83 PLACEHOLDER_PAT@[78; 79) 85 COLON@[76; 77)
84 UNDERSCORE@[78; 79) 86 WHITESPACE@[77; 78)
87 PLACEHOLDER_PAT@[78; 79)
88 UNDERSCORE@[78; 79)
85 COMMA@[79; 80) 89 COMMA@[79; 80)
86 WHITESPACE@[80; 81) 90 WHITESPACE@[80; 81)
87 DOTDOT@[81; 83) 91 DOTDOT@[81; 83)
@@ -106,11 +110,13 @@ SOURCE_FILE@[0; 119)
106 FIELD_PAT_LIST@[101; 110) 110 FIELD_PAT_LIST@[101; 110)
107 L_CURLY@[101; 102) 111 L_CURLY@[101; 102)
108 WHITESPACE@[102; 103) 112 WHITESPACE@[102; 103)
109 IDENT@[103; 104) "h" 113 FIELD_PAT@[103; 107)
110 COLON@[104; 105) 114 NAME@[103; 104)
111 WHITESPACE@[105; 106) 115 IDENT@[103; 104) "h"
112 PLACEHOLDER_PAT@[106; 107) 116 COLON@[104; 105)
113 UNDERSCORE@[106; 107) 117 WHITESPACE@[105; 106)
118 PLACEHOLDER_PAT@[106; 107)
119 UNDERSCORE@[106; 107)
114 COMMA@[107; 108) 120 COMMA@[107; 108)
115 WHITESPACE@[108; 109) 121 WHITESPACE@[108; 109)
116 R_CURLY@[109; 110) 122 R_CURLY@[109; 110)