diff options
Diffstat (limited to 'crates/ra_hir/src/ty/infer.rs')
-rw-r--r-- | crates/ra_hir/src/ty/infer.rs | 29 |
1 files changed, 17 insertions, 12 deletions
diff --git a/crates/ra_hir/src/ty/infer.rs b/crates/ra_hir/src/ty/infer.rs index 69b13baef..ddc7d262a 100644 --- a/crates/ra_hir/src/ty/infer.rs +++ b/crates/ra_hir/src/ty/infer.rs | |||
@@ -22,6 +22,7 @@ use ena::unify::{InPlaceUnificationTable, NoError, UnifyKey, UnifyValue}; | |||
22 | use rustc_hash::FxHashMap; | 22 | use rustc_hash::FxHashMap; |
23 | 23 | ||
24 | use hir_def::{ | 24 | use hir_def::{ |
25 | data::{ConstData, FunctionData}, | ||
25 | path::known, | 26 | path::known, |
26 | resolver::{HasResolver, Resolver, TypeNs}, | 27 | resolver::{HasResolver, Resolver, TypeNs}, |
27 | type_ref::{Mutability, TypeRef}, | 28 | type_ref::{Mutability, TypeRef}, |
@@ -43,8 +44,7 @@ use crate::{ | |||
43 | db::HirDatabase, | 44 | db::HirDatabase, |
44 | expr::{BindingAnnotation, Body, ExprId, PatId}, | 45 | expr::{BindingAnnotation, Body, ExprId, PatId}, |
45 | ty::infer::diagnostics::InferenceDiagnostic, | 46 | ty::infer::diagnostics::InferenceDiagnostic, |
46 | Adt, AssocItem, ConstData, DefWithBody, FloatTy, FnData, Function, HasBody, IntTy, Path, | 47 | Adt, AssocItem, DefWithBody, FloatTy, Function, IntTy, Path, StructField, Trait, VariantDef, |
47 | StructField, Trait, VariantDef, | ||
48 | }; | 48 | }; |
49 | 49 | ||
50 | macro_rules! ty_app { | 50 | macro_rules! ty_app { |
@@ -68,10 +68,10 @@ pub fn infer_query(db: &impl HirDatabase, def: DefWithBody) -> Arc<InferenceResu | |||
68 | let resolver = DefWithBodyId::from(def).resolver(db); | 68 | let resolver = DefWithBodyId::from(def).resolver(db); |
69 | let mut ctx = InferenceContext::new(db, def, resolver); | 69 | let mut ctx = InferenceContext::new(db, def, resolver); |
70 | 70 | ||
71 | match def { | 71 | match &def { |
72 | DefWithBody::Const(ref c) => ctx.collect_const(&c.data(db)), | 72 | DefWithBody::Const(c) => ctx.collect_const(&db.const_data(c.id)), |
73 | DefWithBody::Function(ref f) => ctx.collect_fn(&f.data(db)), | 73 | DefWithBody::Function(f) => ctx.collect_fn(&db.function_data(f.id)), |
74 | DefWithBody::Static(ref s) => ctx.collect_const(&s.data(db)), | 74 | DefWithBody::Static(s) => ctx.collect_const(&db.static_data(s.id)), |
75 | } | 75 | } |
76 | 76 | ||
77 | ctx.infer_body(); | 77 | ctx.infer_body(); |
@@ -125,6 +125,8 @@ pub struct InferenceResult { | |||
125 | method_resolutions: FxHashMap<ExprId, Function>, | 125 | method_resolutions: FxHashMap<ExprId, Function>, |
126 | /// For each field access expr, records the field it resolves to. | 126 | /// For each field access expr, records the field it resolves to. |
127 | field_resolutions: FxHashMap<ExprId, StructField>, | 127 | field_resolutions: FxHashMap<ExprId, StructField>, |
128 | /// For each field in record literal, records the field it resolves to. | ||
129 | record_field_resolutions: FxHashMap<ExprId, StructField>, | ||
128 | /// For each struct literal, records the variant it resolves to. | 130 | /// For each struct literal, records the variant it resolves to. |
129 | variant_resolutions: FxHashMap<ExprOrPatId, VariantDef>, | 131 | variant_resolutions: FxHashMap<ExprOrPatId, VariantDef>, |
130 | /// For each associated item record what it resolves to | 132 | /// For each associated item record what it resolves to |
@@ -142,6 +144,9 @@ impl InferenceResult { | |||
142 | pub fn field_resolution(&self, expr: ExprId) -> Option<StructField> { | 144 | pub fn field_resolution(&self, expr: ExprId) -> Option<StructField> { |
143 | self.field_resolutions.get(&expr).copied() | 145 | self.field_resolutions.get(&expr).copied() |
144 | } | 146 | } |
147 | pub fn record_field_resolution(&self, expr: ExprId) -> Option<StructField> { | ||
148 | self.record_field_resolutions.get(&expr).copied() | ||
149 | } | ||
145 | pub fn variant_resolution_for_expr(&self, id: ExprId) -> Option<VariantDef> { | 150 | pub fn variant_resolution_for_expr(&self, id: ExprId) -> Option<VariantDef> { |
146 | self.variant_resolutions.get(&id.into()).copied() | 151 | self.variant_resolutions.get(&id.into()).copied() |
147 | } | 152 | } |
@@ -215,7 +220,7 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> { | |||
215 | coerce_unsized_map: Self::init_coerce_unsized_map(db, &resolver), | 220 | coerce_unsized_map: Self::init_coerce_unsized_map(db, &resolver), |
216 | db, | 221 | db, |
217 | owner, | 222 | owner, |
218 | body: owner.body(db), | 223 | body: db.body(owner.into()), |
219 | resolver, | 224 | resolver, |
220 | } | 225 | } |
221 | } | 226 | } |
@@ -559,21 +564,21 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> { | |||
559 | } | 564 | } |
560 | 565 | ||
561 | fn collect_const(&mut self, data: &ConstData) { | 566 | fn collect_const(&mut self, data: &ConstData) { |
562 | self.return_ty = self.make_ty(data.type_ref()); | 567 | self.return_ty = self.make_ty(&data.type_ref); |
563 | } | 568 | } |
564 | 569 | ||
565 | fn collect_fn(&mut self, data: &FnData) { | 570 | fn collect_fn(&mut self, data: &FunctionData) { |
566 | let body = Arc::clone(&self.body); // avoid borrow checker problem | 571 | let body = Arc::clone(&self.body); // avoid borrow checker problem |
567 | for (type_ref, pat) in data.params().iter().zip(body.params()) { | 572 | for (type_ref, pat) in data.params.iter().zip(body.params.iter()) { |
568 | let ty = self.make_ty(type_ref); | 573 | let ty = self.make_ty(type_ref); |
569 | 574 | ||
570 | self.infer_pat(*pat, &ty, BindingMode::default()); | 575 | self.infer_pat(*pat, &ty, BindingMode::default()); |
571 | } | 576 | } |
572 | self.return_ty = self.make_ty(data.ret_type()); | 577 | self.return_ty = self.make_ty(&data.ret_type); |
573 | } | 578 | } |
574 | 579 | ||
575 | fn infer_body(&mut self) { | 580 | fn infer_body(&mut self) { |
576 | self.infer_expr(self.body.body_expr(), &Expectation::has_type(self.return_ty.clone())); | 581 | self.infer_expr(self.body.body_expr, &Expectation::has_type(self.return_ty.clone())); |
577 | } | 582 | } |
578 | 583 | ||
579 | fn resolve_into_iter_item(&self) -> Option<TypeAlias> { | 584 | fn resolve_into_iter_item(&self) -> Option<TypeAlias> { |