diff options
Diffstat (limited to 'crates/ra_hir/src/ty/infer.rs')
-rw-r--r-- | crates/ra_hir/src/ty/infer.rs | 54 |
1 files changed, 28 insertions, 26 deletions
diff --git a/crates/ra_hir/src/ty/infer.rs b/crates/ra_hir/src/ty/infer.rs index db9a8c9d1..7b6dfd61b 100644 --- a/crates/ra_hir/src/ty/infer.rs +++ b/crates/ra_hir/src/ty/infer.rs | |||
@@ -22,11 +22,13 @@ 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 | body::Body, | ||
25 | data::{ConstData, FunctionData}, | 26 | data::{ConstData, FunctionData}, |
26 | path::known, | 27 | expr::{BindingAnnotation, ExprId, PatId}, |
28 | path::{known, Path}, | ||
27 | resolver::{HasResolver, Resolver, TypeNs}, | 29 | resolver::{HasResolver, Resolver, TypeNs}, |
28 | type_ref::{Mutability, TypeRef}, | 30 | type_ref::{Mutability, TypeRef}, |
29 | AdtId, AssocItemId, DefWithBodyId, | 31 | AdtId, AssocItemId, DefWithBodyId, FunctionId, StructFieldId, TypeAliasId, |
30 | }; | 32 | }; |
31 | use hir_expand::{diagnostics::DiagnosticSink, name}; | 33 | use hir_expand::{diagnostics::DiagnosticSink, name}; |
32 | use ra_arena::map::ArenaMap; | 34 | use ra_arena::map::ArenaMap; |
@@ -34,16 +36,14 @@ use ra_prof::profile; | |||
34 | use test_utils::tested_by; | 36 | use test_utils::tested_by; |
35 | 37 | ||
36 | use super::{ | 38 | use super::{ |
39 | primitive::{FloatTy, IntTy}, | ||
37 | traits::{Guidance, Obligation, ProjectionPredicate, Solution}, | 40 | traits::{Guidance, Obligation, ProjectionPredicate, Solution}, |
38 | ApplicationTy, InEnvironment, ProjectionTy, Substs, TraitEnvironment, TraitRef, Ty, TypeCtor, | 41 | ApplicationTy, InEnvironment, ProjectionTy, Substs, TraitEnvironment, TraitRef, Ty, TypeCtor, |
39 | TypeWalk, Uncertain, | 42 | TypeWalk, Uncertain, |
40 | }; | 43 | }; |
41 | use crate::{ | 44 | use crate::{ |
42 | code_model::TypeAlias, | 45 | db::HirDatabase, ty::infer::diagnostics::InferenceDiagnostic, AssocItem, DefWithBody, |
43 | db::HirDatabase, | 46 | VariantDef, |
44 | expr::{BindingAnnotation, Body, ExprId, PatId}, | ||
45 | ty::infer::diagnostics::InferenceDiagnostic, | ||
46 | AssocItem, DefWithBody, FloatTy, Function, IntTy, Path, StructField, VariantDef, | ||
47 | }; | 47 | }; |
48 | 48 | ||
49 | macro_rules! ty_app { | 49 | macro_rules! ty_app { |
@@ -121,11 +121,11 @@ pub struct TypeMismatch { | |||
121 | #[derive(Clone, PartialEq, Eq, Debug, Default)] | 121 | #[derive(Clone, PartialEq, Eq, Debug, Default)] |
122 | pub struct InferenceResult { | 122 | pub struct InferenceResult { |
123 | /// For each method call expr, records the function it resolves to. | 123 | /// For each method call expr, records the function it resolves to. |
124 | method_resolutions: FxHashMap<ExprId, Function>, | 124 | method_resolutions: FxHashMap<ExprId, FunctionId>, |
125 | /// For each field access expr, records the field it resolves to. | 125 | /// For each field access expr, records the field it resolves to. |
126 | field_resolutions: FxHashMap<ExprId, StructField>, | 126 | field_resolutions: FxHashMap<ExprId, StructFieldId>, |
127 | /// For each field in record literal, records the field it resolves to. | 127 | /// For each field in record literal, records the field it resolves to. |
128 | record_field_resolutions: FxHashMap<ExprId, StructField>, | 128 | record_field_resolutions: FxHashMap<ExprId, StructFieldId>, |
129 | /// For each struct literal, records the variant it resolves to. | 129 | /// For each struct literal, records the variant it resolves to. |
130 | variant_resolutions: FxHashMap<ExprOrPatId, VariantDef>, | 130 | variant_resolutions: FxHashMap<ExprOrPatId, VariantDef>, |
131 | /// For each associated item record what it resolves to | 131 | /// For each associated item record what it resolves to |
@@ -137,13 +137,13 @@ pub struct InferenceResult { | |||
137 | } | 137 | } |
138 | 138 | ||
139 | impl InferenceResult { | 139 | impl InferenceResult { |
140 | pub fn method_resolution(&self, expr: ExprId) -> Option<Function> { | 140 | pub fn method_resolution(&self, expr: ExprId) -> Option<FunctionId> { |
141 | self.method_resolutions.get(&expr).copied() | 141 | self.method_resolutions.get(&expr).copied() |
142 | } | 142 | } |
143 | pub fn field_resolution(&self, expr: ExprId) -> Option<StructField> { | 143 | pub fn field_resolution(&self, expr: ExprId) -> Option<StructFieldId> { |
144 | self.field_resolutions.get(&expr).copied() | 144 | self.field_resolutions.get(&expr).copied() |
145 | } | 145 | } |
146 | pub fn record_field_resolution(&self, expr: ExprId) -> Option<StructField> { | 146 | pub fn record_field_resolution(&self, expr: ExprId) -> Option<StructFieldId> { |
147 | self.record_field_resolutions.get(&expr).copied() | 147 | self.record_field_resolutions.get(&expr).copied() |
148 | } | 148 | } |
149 | pub fn variant_resolution_for_expr(&self, id: ExprId) -> Option<VariantDef> { | 149 | pub fn variant_resolution_for_expr(&self, id: ExprId) -> Option<VariantDef> { |
@@ -164,7 +164,7 @@ impl InferenceResult { | |||
164 | pub(crate) fn add_diagnostics( | 164 | pub(crate) fn add_diagnostics( |
165 | &self, | 165 | &self, |
166 | db: &impl HirDatabase, | 166 | db: &impl HirDatabase, |
167 | owner: Function, | 167 | owner: FunctionId, |
168 | sink: &mut DiagnosticSink, | 168 | sink: &mut DiagnosticSink, |
169 | ) { | 169 | ) { |
170 | self.diagnostics.iter().for_each(|it| it.add_to(db, owner, sink)) | 170 | self.diagnostics.iter().for_each(|it| it.add_to(db, owner, sink)) |
@@ -243,11 +243,11 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> { | |||
243 | self.result.type_of_expr.insert(expr, ty); | 243 | self.result.type_of_expr.insert(expr, ty); |
244 | } | 244 | } |
245 | 245 | ||
246 | fn write_method_resolution(&mut self, expr: ExprId, func: Function) { | 246 | fn write_method_resolution(&mut self, expr: ExprId, func: FunctionId) { |
247 | self.result.method_resolutions.insert(expr, func); | 247 | self.result.method_resolutions.insert(expr, func); |
248 | } | 248 | } |
249 | 249 | ||
250 | fn write_field_resolution(&mut self, expr: ExprId, field: StructField) { | 250 | fn write_field_resolution(&mut self, expr: ExprId, field: StructFieldId) { |
251 | self.result.field_resolutions.insert(expr, field); | 251 | self.result.field_resolutions.insert(expr, field); |
252 | } | 252 | } |
253 | 253 | ||
@@ -557,22 +557,22 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> { | |||
557 | self.infer_expr(self.body.body_expr, &Expectation::has_type(self.return_ty.clone())); | 557 | self.infer_expr(self.body.body_expr, &Expectation::has_type(self.return_ty.clone())); |
558 | } | 558 | } |
559 | 559 | ||
560 | fn resolve_into_iter_item(&self) -> Option<TypeAlias> { | 560 | fn resolve_into_iter_item(&self) -> Option<TypeAliasId> { |
561 | let path = known::std_iter_into_iterator(); | 561 | let path = known::std_iter_into_iterator(); |
562 | let trait_ = self.resolver.resolve_known_trait(self.db, &path)?; | 562 | let trait_ = self.resolver.resolve_known_trait(self.db, &path)?; |
563 | self.db.trait_data(trait_).associated_type_by_name(&name::ITEM_TYPE).map(TypeAlias::from) | 563 | self.db.trait_data(trait_).associated_type_by_name(&name::ITEM_TYPE) |
564 | } | 564 | } |
565 | 565 | ||
566 | fn resolve_ops_try_ok(&self) -> Option<TypeAlias> { | 566 | fn resolve_ops_try_ok(&self) -> Option<TypeAliasId> { |
567 | let path = known::std_ops_try(); | 567 | let path = known::std_ops_try(); |
568 | let trait_ = self.resolver.resolve_known_trait(self.db, &path)?; | 568 | let trait_ = self.resolver.resolve_known_trait(self.db, &path)?; |
569 | self.db.trait_data(trait_).associated_type_by_name(&name::OK_TYPE).map(TypeAlias::from) | 569 | self.db.trait_data(trait_).associated_type_by_name(&name::OK_TYPE) |
570 | } | 570 | } |
571 | 571 | ||
572 | fn resolve_future_future_output(&self) -> Option<TypeAlias> { | 572 | fn resolve_future_future_output(&self) -> Option<TypeAliasId> { |
573 | let path = known::std_future_future(); | 573 | let path = known::std_future_future(); |
574 | let trait_ = self.resolver.resolve_known_trait(self.db, &path)?; | 574 | let trait_ = self.resolver.resolve_known_trait(self.db, &path)?; |
575 | self.db.trait_data(trait_).associated_type_by_name(&name::OUTPUT_TYPE).map(TypeAlias::from) | 575 | self.db.trait_data(trait_).associated_type_by_name(&name::OUTPUT_TYPE) |
576 | } | 576 | } |
577 | 577 | ||
578 | fn resolve_boxed_box(&self) -> Option<AdtId> { | 578 | fn resolve_boxed_box(&self) -> Option<AdtId> { |
@@ -696,9 +696,10 @@ impl Expectation { | |||
696 | } | 696 | } |
697 | 697 | ||
698 | mod diagnostics { | 698 | mod diagnostics { |
699 | use hir_def::{expr::ExprId, FunctionId, HasSource, Lookup}; | ||
699 | use hir_expand::diagnostics::DiagnosticSink; | 700 | use hir_expand::diagnostics::DiagnosticSink; |
700 | 701 | ||
701 | use crate::{db::HirDatabase, diagnostics::NoSuchField, expr::ExprId, Function, HasSource}; | 702 | use crate::{db::HirDatabase, diagnostics::NoSuchField}; |
702 | 703 | ||
703 | #[derive(Debug, PartialEq, Eq, Clone)] | 704 | #[derive(Debug, PartialEq, Eq, Clone)] |
704 | pub(super) enum InferenceDiagnostic { | 705 | pub(super) enum InferenceDiagnostic { |
@@ -709,13 +710,14 @@ mod diagnostics { | |||
709 | pub(super) fn add_to( | 710 | pub(super) fn add_to( |
710 | &self, | 711 | &self, |
711 | db: &impl HirDatabase, | 712 | db: &impl HirDatabase, |
712 | owner: Function, | 713 | owner: FunctionId, |
713 | sink: &mut DiagnosticSink, | 714 | sink: &mut DiagnosticSink, |
714 | ) { | 715 | ) { |
715 | match self { | 716 | match self { |
716 | InferenceDiagnostic::NoSuchField { expr, field } => { | 717 | InferenceDiagnostic::NoSuchField { expr, field } => { |
717 | let file = owner.source(db).file_id; | 718 | let file = owner.lookup(db).source(db).file_id; |
718 | let field = owner.body_source_map(db).field_syntax(*expr, *field); | 719 | let (_, source_map) = db.body_with_source_map(owner.into()); |
720 | let field = source_map.field_syntax(*expr, *field); | ||
719 | sink.push(NoSuchField { file, field }) | 721 | sink.push(NoSuchField { file, field }) |
720 | } | 722 | } |
721 | } | 723 | } |