From 5fe19d2fbd2daa05b2cd3b1ebb6fa926e9d86c36 Mon Sep 17 00:00:00 2001 From: Ekaterina Babshukova Date: Sun, 21 Jul 2019 14:11:45 +0300 Subject: provide completion in struct patterns --- crates/ra_hir/src/source_binder.rs | 9 +++++++-- crates/ra_hir/src/ty.rs | 2 +- crates/ra_hir/src/ty/infer.rs | 22 +++++++++++++++------- 3 files changed, 23 insertions(+), 10 deletions(-) (limited to 'crates/ra_hir/src') 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 { self.infer.as_ref()?.field_resolution(expr_id) } - pub fn resolve_variant(&self, struct_lit: &ast::StructLit) -> Option { + pub fn resolve_struct_literal(&self, struct_lit: &ast::StructLit) -> Option { let expr_id = self.body_source_map.as_ref()?.node_expr(&struct_lit.clone().into())?; - self.infer.as_ref()?.variant_resolution(expr_id) + self.infer.as_ref()?.variant_resolution_for_expr(expr_id) + } + + pub fn resolve_struct_pattern(&self, struct_pat: &ast::StructPat) -> Option { + let pat_id = self.body_source_map.as_ref()?.node_pat(&struct_pat.clone().into())?; + self.infer.as_ref()?.variant_resolution_for_pat(pat_id) } 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 { /// Returns the type parameters of this type if it has some (i.e. is an ADT /// or function); so if `self` is `Option`, this returns the `u32`. - fn substs(&self) -> Option { + pub fn substs(&self) -> Option { match self { Ty::Apply(ApplicationTy { parameters, .. }) => Some(parameters.clone()), _ => 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 { method_resolutions: FxHashMap, /// For each field access expr, records the field it resolves to. field_resolutions: FxHashMap, - variant_resolutions: FxHashMap, + /// For each struct literal, records the variant it resolves to. + variant_resolutions: FxHashMap, /// For each associated item record what it resolves to assoc_resolutions: FxHashMap, diagnostics: Vec, @@ -128,8 +129,11 @@ impl InferenceResult { pub fn field_resolution(&self, expr: ExprId) -> Option { self.field_resolutions.get(&expr).copied() } - pub fn variant_resolution(&self, expr: ExprId) -> Option { - self.variant_resolutions.get(&expr).copied() + pub fn variant_resolution_for_expr(&self, id: ExprId) -> Option { + self.variant_resolutions.get(&id.into()).copied() + } + pub fn variant_resolution_for_pat(&self, id: PatId) -> Option { + self.variant_resolutions.get(&id.into()).copied() } pub fn assoc_resolutions_for_expr(&self, id: ExprId) -> Option { self.assoc_resolutions.get(&id.into()).copied() @@ -218,8 +222,8 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> { self.result.field_resolutions.insert(expr, field); } - fn write_variant_resolution(&mut self, expr: ExprId, variant: VariantDef) { - self.result.variant_resolutions.insert(expr, variant); + fn write_variant_resolution(&mut self, id: ExprOrPatId, variant: VariantDef) { + self.result.variant_resolutions.insert(id, variant); } fn write_assoc_resolution(&mut self, id: ExprOrPatId, item: ImplItem) { @@ -678,8 +682,12 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> { subpats: &[FieldPat], expected: &Ty, default_bm: BindingMode, + id: PatId, ) -> Ty { let (ty, def) = self.resolve_variant(path); + if let Some(variant) = def { + self.write_variant_resolution(id.into(), variant); + } self.unify(&ty, expected); @@ -762,7 +770,7 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> { self.infer_tuple_struct_pat(p.as_ref(), subpats, expected, default_bm) } Pat::Struct { path: ref p, args: ref fields } => { - self.infer_struct_pat(p.as_ref(), fields, expected, default_bm) + self.infer_struct_pat(p.as_ref(), fields, expected, default_bm, pat) } Pat::Path(path) => { // FIXME use correct resolver for the surrounding expression @@ -1064,7 +1072,7 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> { Expr::StructLit { path, fields, spread } => { let (ty, def_id) = self.resolve_variant(path.as_ref()); if let Some(variant) = def_id { - self.write_variant_resolution(tgt_expr, variant); + self.write_variant_resolution(tgt_expr.into(), variant); } let substs = ty.substs().unwrap_or_else(Substs::empty); -- cgit v1.2.3