diff options
author | Ekaterina Babshukova <[email protected]> | 2019-07-21 12:11:45 +0100 |
---|---|---|
committer | Ekaterina Babshukova <[email protected]> | 2019-07-21 12:41:33 +0100 |
commit | 5fe19d2fbd2daa05b2cd3b1ebb6fa926e9d86c36 (patch) | |
tree | 8d0e74c6c5a734fc64edf2504aabfd11c09354c4 /crates/ra_hir | |
parent | 7bde8012cb28c44de7ffc779003781d385323808 (diff) |
provide completion in struct patterns
Diffstat (limited to 'crates/ra_hir')
-rw-r--r-- | crates/ra_hir/src/source_binder.rs | 9 | ||||
-rw-r--r-- | crates/ra_hir/src/ty.rs | 2 | ||||
-rw-r--r-- | crates/ra_hir/src/ty/infer.rs | 22 |
3 files changed, 23 insertions, 10 deletions
diff --git a/crates/ra_hir/src/source_binder.rs b/crates/ra_hir/src/source_binder.rs index 4c173a4f7..fc9bc33d2 100644 --- a/crates/ra_hir/src/source_binder.rs +++ b/crates/ra_hir/src/source_binder.rs | |||
@@ -266,9 +266,14 @@ impl SourceAnalyzer { | |||
266 | self.infer.as_ref()?.field_resolution(expr_id) | 266 | self.infer.as_ref()?.field_resolution(expr_id) |
267 | } | 267 | } |
268 | 268 | ||
269 | pub fn resolve_variant(&self, struct_lit: &ast::StructLit) -> Option<crate::VariantDef> { | 269 | pub fn resolve_struct_literal(&self, struct_lit: &ast::StructLit) -> Option<crate::VariantDef> { |
270 | let expr_id = self.body_source_map.as_ref()?.node_expr(&struct_lit.clone().into())?; | 270 | let expr_id = self.body_source_map.as_ref()?.node_expr(&struct_lit.clone().into())?; |
271 | self.infer.as_ref()?.variant_resolution(expr_id) | 271 | self.infer.as_ref()?.variant_resolution_for_expr(expr_id) |
272 | } | ||
273 | |||
274 | pub fn resolve_struct_pattern(&self, struct_pat: &ast::StructPat) -> Option<crate::VariantDef> { | ||
275 | let pat_id = self.body_source_map.as_ref()?.node_pat(&struct_pat.clone().into())?; | ||
276 | self.infer.as_ref()?.variant_resolution_for_pat(pat_id) | ||
272 | } | 277 | } |
273 | 278 | ||
274 | pub fn resolve_macro_call( | 279 | pub fn resolve_macro_call( |
diff --git a/crates/ra_hir/src/ty.rs b/crates/ra_hir/src/ty.rs index 4cf714f5d..82589e504 100644 --- a/crates/ra_hir/src/ty.rs +++ b/crates/ra_hir/src/ty.rs | |||
@@ -472,7 +472,7 @@ impl Ty { | |||
472 | 472 | ||
473 | /// Returns the type parameters of this type if it has some (i.e. is an ADT | 473 | /// Returns the type parameters of this type if it has some (i.e. is an ADT |
474 | /// or function); so if `self` is `Option<u32>`, this returns the `u32`. | 474 | /// or function); so if `self` is `Option<u32>`, this returns the `u32`. |
475 | fn substs(&self) -> Option<Substs> { | 475 | pub fn substs(&self) -> Option<Substs> { |
476 | match self { | 476 | match self { |
477 | Ty::Apply(ApplicationTy { parameters, .. }) => Some(parameters.clone()), | 477 | Ty::Apply(ApplicationTy { parameters, .. }) => Some(parameters.clone()), |
478 | _ => None, | 478 | _ => None, |
diff --git a/crates/ra_hir/src/ty/infer.rs b/crates/ra_hir/src/ty/infer.rs index a82dff711..594c5bc79 100644 --- a/crates/ra_hir/src/ty/infer.rs +++ b/crates/ra_hir/src/ty/infer.rs | |||
@@ -113,7 +113,8 @@ pub struct InferenceResult { | |||
113 | method_resolutions: FxHashMap<ExprId, Function>, | 113 | method_resolutions: FxHashMap<ExprId, Function>, |
114 | /// For each field access expr, records the field it resolves to. | 114 | /// For each field access expr, records the field it resolves to. |
115 | field_resolutions: FxHashMap<ExprId, StructField>, | 115 | field_resolutions: FxHashMap<ExprId, StructField>, |
116 | variant_resolutions: FxHashMap<ExprId, VariantDef>, | 116 | /// For each struct literal, records the variant it resolves to. |
117 | variant_resolutions: FxHashMap<ExprOrPatId, VariantDef>, | ||
117 | /// For each associated item record what it resolves to | 118 | /// For each associated item record what it resolves to |
118 | assoc_resolutions: FxHashMap<ExprOrPatId, ImplItem>, | 119 | assoc_resolutions: FxHashMap<ExprOrPatId, ImplItem>, |
119 | diagnostics: Vec<InferenceDiagnostic>, | 120 | diagnostics: Vec<InferenceDiagnostic>, |
@@ -128,8 +129,11 @@ impl InferenceResult { | |||
128 | pub fn field_resolution(&self, expr: ExprId) -> Option<StructField> { | 129 | pub fn field_resolution(&self, expr: ExprId) -> Option<StructField> { |
129 | self.field_resolutions.get(&expr).copied() | 130 | self.field_resolutions.get(&expr).copied() |
130 | } | 131 | } |
131 | pub fn variant_resolution(&self, expr: ExprId) -> Option<VariantDef> { | 132 | pub fn variant_resolution_for_expr(&self, id: ExprId) -> Option<VariantDef> { |
132 | self.variant_resolutions.get(&expr).copied() | 133 | self.variant_resolutions.get(&id.into()).copied() |
134 | } | ||
135 | pub fn variant_resolution_for_pat(&self, id: PatId) -> Option<VariantDef> { | ||
136 | self.variant_resolutions.get(&id.into()).copied() | ||
133 | } | 137 | } |
134 | pub fn assoc_resolutions_for_expr(&self, id: ExprId) -> Option<ImplItem> { | 138 | pub fn assoc_resolutions_for_expr(&self, id: ExprId) -> Option<ImplItem> { |
135 | self.assoc_resolutions.get(&id.into()).copied() | 139 | self.assoc_resolutions.get(&id.into()).copied() |
@@ -218,8 +222,8 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> { | |||
218 | self.result.field_resolutions.insert(expr, field); | 222 | self.result.field_resolutions.insert(expr, field); |
219 | } | 223 | } |
220 | 224 | ||
221 | fn write_variant_resolution(&mut self, expr: ExprId, variant: VariantDef) { | 225 | fn write_variant_resolution(&mut self, id: ExprOrPatId, variant: VariantDef) { |
222 | self.result.variant_resolutions.insert(expr, variant); | 226 | self.result.variant_resolutions.insert(id, variant); |
223 | } | 227 | } |
224 | 228 | ||
225 | fn write_assoc_resolution(&mut self, id: ExprOrPatId, item: ImplItem) { | 229 | fn write_assoc_resolution(&mut self, id: ExprOrPatId, item: ImplItem) { |
@@ -678,8 +682,12 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> { | |||
678 | subpats: &[FieldPat], | 682 | subpats: &[FieldPat], |
679 | expected: &Ty, | 683 | expected: &Ty, |
680 | default_bm: BindingMode, | 684 | default_bm: BindingMode, |
685 | id: PatId, | ||
681 | ) -> Ty { | 686 | ) -> Ty { |
682 | let (ty, def) = self.resolve_variant(path); | 687 | let (ty, def) = self.resolve_variant(path); |
688 | if let Some(variant) = def { | ||
689 | self.write_variant_resolution(id.into(), variant); | ||
690 | } | ||
683 | 691 | ||
684 | self.unify(&ty, expected); | 692 | self.unify(&ty, expected); |
685 | 693 | ||
@@ -762,7 +770,7 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> { | |||
762 | self.infer_tuple_struct_pat(p.as_ref(), subpats, expected, default_bm) | 770 | self.infer_tuple_struct_pat(p.as_ref(), subpats, expected, default_bm) |
763 | } | 771 | } |
764 | Pat::Struct { path: ref p, args: ref fields } => { | 772 | Pat::Struct { path: ref p, args: ref fields } => { |
765 | self.infer_struct_pat(p.as_ref(), fields, expected, default_bm) | 773 | self.infer_struct_pat(p.as_ref(), fields, expected, default_bm, pat) |
766 | } | 774 | } |
767 | Pat::Path(path) => { | 775 | Pat::Path(path) => { |
768 | // FIXME use correct resolver for the surrounding expression | 776 | // FIXME use correct resolver for the surrounding expression |
@@ -1064,7 +1072,7 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> { | |||
1064 | Expr::StructLit { path, fields, spread } => { | 1072 | Expr::StructLit { path, fields, spread } => { |
1065 | let (ty, def_id) = self.resolve_variant(path.as_ref()); | 1073 | let (ty, def_id) = self.resolve_variant(path.as_ref()); |
1066 | if let Some(variant) = def_id { | 1074 | if let Some(variant) = def_id { |
1067 | self.write_variant_resolution(tgt_expr, variant); | 1075 | self.write_variant_resolution(tgt_expr.into(), variant); |
1068 | } | 1076 | } |
1069 | 1077 | ||
1070 | let substs = ty.substs().unwrap_or_else(Substs::empty); | 1078 | let substs = ty.substs().unwrap_or_else(Substs::empty); |