aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_hir/src/ty.rs
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ra_hir/src/ty.rs')
-rw-r--r--crates/ra_hir/src/ty.rs31
1 files changed, 15 insertions, 16 deletions
diff --git a/crates/ra_hir/src/ty.rs b/crates/ra_hir/src/ty.rs
index 09b816cc7..ac0568a32 100644
--- a/crates/ra_hir/src/ty.rs
+++ b/crates/ra_hir/src/ty.rs
@@ -31,9 +31,8 @@ use join_to_string::join;
31use rustc_hash::FxHashMap; 31use rustc_hash::FxHashMap;
32 32
33use crate::{ 33use crate::{
34 Def, DefId, Module, Function, Struct, Enum, EnumVariant, Path, Name, ImplBlock, 34 Def, DefId, Module, Function, Struct, StructField, Enum, EnumVariant, Path, Name, ImplBlock,
35 FnSignature, FnScopes, 35 FnSignature, FnScopes,
36 adt::StructField,
37 db::HirDatabase, 36 db::HirDatabase,
38 type_ref::{TypeRef, Mutability}, 37 type_ref::{TypeRef, Mutability},
39 name::KnownName, 38 name::KnownName,
@@ -873,24 +872,22 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> {
873 } 872 }
874 } 873 }
875 874
875 // TODO: add fields method for tuple like structs and variants
876 // TODO: and add tests!
877
876 fn resolve_fields(&self, path: Option<&Path>) -> Option<(Ty, Vec<StructField>)> { 878 fn resolve_fields(&self, path: Option<&Path>) -> Option<(Ty, Vec<StructField>)> {
877 let def_id = self.module.resolve_path(self.db, path?).take_types()?; 879 let def_id = self.module.resolve_path(self.db, path?).take_types()?;
878 let def = def_id.resolve(self.db); 880 let def = def_id.resolve(self.db);
879 881
880 match def { 882 match def {
881 Def::Struct(s) => { 883 Def::Struct(s) => {
882 let fields: Vec<_> = self 884 let fields: Vec<_> = s.fields(self.db);
883 .db
884 .struct_data(s.def_id())
885 .variant_data
886 .fields()
887 .to_owned();
888 Some((type_for_struct(self.db, s), fields)) 885 Some((type_for_struct(self.db, s), fields))
889 } 886 }
890 Def::EnumVariant(ev) => { 887 // Def::EnumVariant(ev) => {
891 let fields: Vec<_> = ev.variant_data(self.db).fields().to_owned(); 888 // let fields: Vec<_> = ev.variant_data(self.db).fields().to_owned();
892 Some((type_for_enum_variant(self.db, ev), fields)) 889 // Some((type_for_enum_variant(self.db, ev), fields))
893 } 890 // }
894 _ => None, 891 _ => None,
895 } 892 }
896 } 893 }
@@ -903,7 +900,8 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> {
903 for (i, &subpat) in subpats.iter().enumerate() { 900 for (i, &subpat) in subpats.iter().enumerate() {
904 let expected_ty = fields 901 let expected_ty = fields
905 .get(i) 902 .get(i)
906 .map_or(Ty::Unknown, |field| self.make_ty(&field.type_ref)); 903 .and_then(|field| field.ty(self.db))
904 .unwrap_or(Ty::Unknown);
907 self.infer_pat(subpat, &Expectation::has_type(expected_ty)); 905 self.infer_pat(subpat, &Expectation::has_type(expected_ty));
908 } 906 }
909 907
@@ -916,9 +914,10 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> {
916 .unwrap_or((Ty::Unknown, Vec::new())); 914 .unwrap_or((Ty::Unknown, Vec::new()));
917 915
918 for subpat in subpats { 916 for subpat in subpats {
919 let matching_field = fields.iter().find(|field| field.name == subpat.name); 917 let matching_field = fields.iter().find(|field| field.name() == &subpat.name);
920 let expected_ty = 918 let expected_ty = matching_field
921 matching_field.map_or(Ty::Unknown, |field| self.make_ty(&field.type_ref)); 919 .and_then(|field| field.ty(self.db))
920 .unwrap_or(Ty::Unknown);
922 self.infer_pat(subpat.pat, &Expectation::has_type(expected_ty)); 921 self.infer_pat(subpat.pat, &Expectation::has_type(expected_ty));
923 } 922 }
924 923