diff options
Diffstat (limited to 'crates/ra_hir/src/ty/infer.rs')
-rw-r--r-- | crates/ra_hir/src/ty/infer.rs | 32 |
1 files changed, 18 insertions, 14 deletions
diff --git a/crates/ra_hir/src/ty/infer.rs b/crates/ra_hir/src/ty/infer.rs index 69b13baef..6fd00d457 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}, |
@@ -33,7 +34,6 @@ use ra_prof::profile; | |||
33 | use test_utils::tested_by; | 34 | use test_utils::tested_by; |
34 | 35 | ||
35 | use super::{ | 36 | use super::{ |
36 | lower, | ||
37 | traits::{Guidance, Obligation, ProjectionPredicate, Solution}, | 37 | traits::{Guidance, Obligation, ProjectionPredicate, Solution}, |
38 | ApplicationTy, InEnvironment, ProjectionTy, Substs, TraitEnvironment, TraitRef, Ty, TypableDef, | 38 | ApplicationTy, InEnvironment, ProjectionTy, Substs, TraitEnvironment, TraitRef, Ty, TypableDef, |
39 | TypeCtor, TypeWalk, Uncertain, | 39 | TypeCtor, TypeWalk, Uncertain, |
@@ -43,8 +43,7 @@ use crate::{ | |||
43 | db::HirDatabase, | 43 | db::HirDatabase, |
44 | expr::{BindingAnnotation, Body, ExprId, PatId}, | 44 | expr::{BindingAnnotation, Body, ExprId, PatId}, |
45 | ty::infer::diagnostics::InferenceDiagnostic, | 45 | ty::infer::diagnostics::InferenceDiagnostic, |
46 | Adt, AssocItem, ConstData, DefWithBody, FloatTy, FnData, Function, HasBody, IntTy, Path, | 46 | Adt, AssocItem, DefWithBody, FloatTy, Function, IntTy, Path, StructField, Trait, VariantDef, |
47 | StructField, Trait, VariantDef, | ||
48 | }; | 47 | }; |
49 | 48 | ||
50 | macro_rules! ty_app { | 49 | macro_rules! ty_app { |
@@ -68,10 +67,10 @@ pub fn infer_query(db: &impl HirDatabase, def: DefWithBody) -> Arc<InferenceResu | |||
68 | let resolver = DefWithBodyId::from(def).resolver(db); | 67 | let resolver = DefWithBodyId::from(def).resolver(db); |
69 | let mut ctx = InferenceContext::new(db, def, resolver); | 68 | let mut ctx = InferenceContext::new(db, def, resolver); |
70 | 69 | ||
71 | match def { | 70 | match &def { |
72 | DefWithBody::Const(ref c) => ctx.collect_const(&c.data(db)), | 71 | DefWithBody::Const(c) => ctx.collect_const(&db.const_data(c.id)), |
73 | DefWithBody::Function(ref f) => ctx.collect_fn(&f.data(db)), | 72 | DefWithBody::Function(f) => ctx.collect_fn(&db.function_data(f.id)), |
74 | DefWithBody::Static(ref s) => ctx.collect_const(&s.data(db)), | 73 | DefWithBody::Static(s) => ctx.collect_const(&db.static_data(s.id)), |
75 | } | 74 | } |
76 | 75 | ||
77 | ctx.infer_body(); | 76 | ctx.infer_body(); |
@@ -125,6 +124,8 @@ pub struct InferenceResult { | |||
125 | method_resolutions: FxHashMap<ExprId, Function>, | 124 | method_resolutions: FxHashMap<ExprId, Function>, |
126 | /// For each field access expr, records the field it resolves to. | 125 | /// For each field access expr, records the field it resolves to. |
127 | field_resolutions: FxHashMap<ExprId, StructField>, | 126 | field_resolutions: FxHashMap<ExprId, StructField>, |
127 | /// For each field in record literal, records the field it resolves to. | ||
128 | record_field_resolutions: FxHashMap<ExprId, StructField>, | ||
128 | /// For each struct literal, records the variant it resolves to. | 129 | /// For each struct literal, records the variant it resolves to. |
129 | variant_resolutions: FxHashMap<ExprOrPatId, VariantDef>, | 130 | variant_resolutions: FxHashMap<ExprOrPatId, VariantDef>, |
130 | /// For each associated item record what it resolves to | 131 | /// For each associated item record what it resolves to |
@@ -142,6 +143,9 @@ impl InferenceResult { | |||
142 | pub fn field_resolution(&self, expr: ExprId) -> Option<StructField> { | 143 | pub fn field_resolution(&self, expr: ExprId) -> Option<StructField> { |
143 | self.field_resolutions.get(&expr).copied() | 144 | self.field_resolutions.get(&expr).copied() |
144 | } | 145 | } |
146 | pub fn record_field_resolution(&self, expr: ExprId) -> Option<StructField> { | ||
147 | self.record_field_resolutions.get(&expr).copied() | ||
148 | } | ||
145 | pub fn variant_resolution_for_expr(&self, id: ExprId) -> Option<VariantDef> { | 149 | pub fn variant_resolution_for_expr(&self, id: ExprId) -> Option<VariantDef> { |
146 | self.variant_resolutions.get(&id.into()).copied() | 150 | self.variant_resolutions.get(&id.into()).copied() |
147 | } | 151 | } |
@@ -211,11 +215,11 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> { | |||
211 | var_unification_table: InPlaceUnificationTable::new(), | 215 | var_unification_table: InPlaceUnificationTable::new(), |
212 | obligations: Vec::default(), | 216 | obligations: Vec::default(), |
213 | return_ty: Ty::Unknown, // set in collect_fn_signature | 217 | return_ty: Ty::Unknown, // set in collect_fn_signature |
214 | trait_env: lower::trait_env(db, &resolver), | 218 | trait_env: TraitEnvironment::lower(db, &resolver), |
215 | coerce_unsized_map: Self::init_coerce_unsized_map(db, &resolver), | 219 | coerce_unsized_map: Self::init_coerce_unsized_map(db, &resolver), |
216 | db, | 220 | db, |
217 | owner, | 221 | owner, |
218 | body: owner.body(db), | 222 | body: db.body(owner.into()), |
219 | resolver, | 223 | resolver, |
220 | } | 224 | } |
221 | } | 225 | } |
@@ -559,21 +563,21 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> { | |||
559 | } | 563 | } |
560 | 564 | ||
561 | fn collect_const(&mut self, data: &ConstData) { | 565 | fn collect_const(&mut self, data: &ConstData) { |
562 | self.return_ty = self.make_ty(data.type_ref()); | 566 | self.return_ty = self.make_ty(&data.type_ref); |
563 | } | 567 | } |
564 | 568 | ||
565 | fn collect_fn(&mut self, data: &FnData) { | 569 | fn collect_fn(&mut self, data: &FunctionData) { |
566 | let body = Arc::clone(&self.body); // avoid borrow checker problem | 570 | let body = Arc::clone(&self.body); // avoid borrow checker problem |
567 | for (type_ref, pat) in data.params().iter().zip(body.params()) { | 571 | for (type_ref, pat) in data.params.iter().zip(body.params.iter()) { |
568 | let ty = self.make_ty(type_ref); | 572 | let ty = self.make_ty(type_ref); |
569 | 573 | ||
570 | self.infer_pat(*pat, &ty, BindingMode::default()); | 574 | self.infer_pat(*pat, &ty, BindingMode::default()); |
571 | } | 575 | } |
572 | self.return_ty = self.make_ty(data.ret_type()); | 576 | self.return_ty = self.make_ty(&data.ret_type); |
573 | } | 577 | } |
574 | 578 | ||
575 | fn infer_body(&mut self) { | 579 | fn infer_body(&mut self) { |
576 | self.infer_expr(self.body.body_expr(), &Expectation::has_type(self.return_ty.clone())); | 580 | self.infer_expr(self.body.body_expr, &Expectation::has_type(self.return_ty.clone())); |
577 | } | 581 | } |
578 | 582 | ||
579 | fn resolve_into_iter_item(&self) -> Option<TypeAlias> { | 583 | fn resolve_into_iter_item(&self) -> Option<TypeAlias> { |