aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_hir
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 /crates/ra_hir
parent4277f420aa947f0c0739506ec229da94e76ee67b (diff)
Finish move of StructField for pattern type inference
Diffstat (limited to 'crates/ra_hir')
-rw-r--r--crates/ra_hir/src/code_model_api.rs18
-rw-r--r--crates/ra_hir/src/ty.rs13
2 files changed, 20 insertions, 11 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 }