diff options
author | Marcus Klaas de Vries <[email protected]> | 2019-01-18 13:12:42 +0000 |
---|---|---|
committer | Aleksey Kladov <[email protected]> | 2019-01-19 12:37:26 +0000 |
commit | d2769837f1f9c94b977bd6022090f8e5cd02410c (patch) | |
tree | b495669e57321654481883964dd0186b65ca540e /crates | |
parent | b5466f3fb36005c313205d944430498f17aea6fa (diff) |
Move pattern type inference from adt::StructField to core_model_impl (WIP)
Diffstat (limited to 'crates')
-rw-r--r-- | crates/ra_hir/src/lib.rs | 1 | ||||
-rw-r--r-- | crates/ra_hir/src/ty.rs | 31 |
2 files changed, 16 insertions, 16 deletions
diff --git a/crates/ra_hir/src/lib.rs b/crates/ra_hir/src/lib.rs index 75b9c6b43..f1402a286 100644 --- a/crates/ra_hir/src/lib.rs +++ b/crates/ra_hir/src/lib.rs | |||
@@ -51,6 +51,7 @@ pub use self::code_model_api::{ | |||
51 | Module, ModuleSource, Problem, | 51 | Module, ModuleSource, Problem, |
52 | Struct, Enum, EnumVariant, | 52 | Struct, Enum, EnumVariant, |
53 | Function, FnSignature, ScopeEntryWithSyntax, | 53 | Function, FnSignature, ScopeEntryWithSyntax, |
54 | StructField, | ||
54 | Static, Const, | 55 | Static, Const, |
55 | Trait, Type, | 56 | Trait, Type, |
56 | }; | 57 | }; |
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; | |||
31 | use rustc_hash::FxHashMap; | 31 | use rustc_hash::FxHashMap; |
32 | 32 | ||
33 | use crate::{ | 33 | use 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 | ||