aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_hir
diff options
context:
space:
mode:
authorKirill Bulatov <[email protected]>2019-07-21 22:10:29 +0100
committerKirill Bulatov <[email protected]>2019-07-21 22:10:29 +0100
commit31aef808d96b779dbc8ce41e27857965e79bd96f (patch)
tree254d69a1ec3abe6d70b2dd9737ef699f33f88f62 /crates/ra_hir
parentba76017d2eb1b7606106c15478ac658dc32b6dbd (diff)
parentd690249bc81bc265cb3d1836c2922325f4fdb8af (diff)
Merge branch 'master' into add-type-lenses
Diffstat (limited to 'crates/ra_hir')
-rw-r--r--crates/ra_hir/src/source_binder.rs9
-rw-r--r--crates/ra_hir/src/ty.rs2
-rw-r--r--crates/ra_hir/src/ty/infer.rs22
-rw-r--r--crates/ra_hir/src/ty/tests.rs6
4 files changed, 25 insertions, 14 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);
diff --git a/crates/ra_hir/src/ty/tests.rs b/crates/ra_hir/src/ty/tests.rs
index 706500484..676711d0a 100644
--- a/crates/ra_hir/src/ty/tests.rs
+++ b/crates/ra_hir/src/ty/tests.rs
@@ -3211,8 +3211,7 @@ fn typing_whitespace_inside_a_function_should_not_invalidate_types() {
3211 ); 3211 );
3212 { 3212 {
3213 let file = db.parse(pos.file_id).ok().unwrap(); 3213 let file = db.parse(pos.file_id).ok().unwrap();
3214 let node = 3214 let node = file.syntax().token_at_offset(pos.offset).right_biased().unwrap().parent();
3215 algo::find_token_at_offset(file.syntax(), pos.offset).right_biased().unwrap().parent();
3216 let events = db.log_executed(|| { 3215 let events = db.log_executed(|| {
3217 SourceAnalyzer::new(&db, pos.file_id, &node, None); 3216 SourceAnalyzer::new(&db, pos.file_id, &node, None);
3218 }); 3217 });
@@ -3232,8 +3231,7 @@ fn typing_whitespace_inside_a_function_should_not_invalidate_types() {
3232 3231
3233 { 3232 {
3234 let file = db.parse(pos.file_id).ok().unwrap(); 3233 let file = db.parse(pos.file_id).ok().unwrap();
3235 let node = 3234 let node = file.syntax().token_at_offset(pos.offset).right_biased().unwrap().parent();
3236 algo::find_token_at_offset(file.syntax(), pos.offset).right_biased().unwrap().parent();
3237 let events = db.log_executed(|| { 3235 let events = db.log_executed(|| {
3238 SourceAnalyzer::new(&db, pos.file_id, &node, None); 3236 SourceAnalyzer::new(&db, pos.file_id, &node, None);
3239 }); 3237 });