diff options
Diffstat (limited to 'crates')
-rw-r--r-- | crates/ra_hir/src/code_model.rs | 2 | ||||
-rw-r--r-- | crates/ra_hir/src/source_binder.rs | 6 | ||||
-rw-r--r-- | crates/ra_hir/src/ty/infer.rs | 54 | ||||
-rw-r--r-- | crates/ra_hir/src/ty/infer/expr.rs | 12 | ||||
-rw-r--r-- | crates/ra_hir/src/ty/method_resolution.rs | 7 |
5 files changed, 41 insertions, 40 deletions
diff --git a/crates/ra_hir/src/code_model.rs b/crates/ra_hir/src/code_model.rs index 54da937ea..6d71bde92 100644 --- a/crates/ra_hir/src/code_model.rs +++ b/crates/ra_hir/src/code_model.rs | |||
@@ -647,7 +647,7 @@ impl Function { | |||
647 | 647 | ||
648 | pub fn diagnostics(self, db: &impl HirDatabase, sink: &mut DiagnosticSink) { | 648 | pub fn diagnostics(self, db: &impl HirDatabase, sink: &mut DiagnosticSink) { |
649 | let infer = self.infer(db); | 649 | let infer = self.infer(db); |
650 | infer.add_diagnostics(db, self, sink); | 650 | infer.add_diagnostics(db, self.id, sink); |
651 | let mut validator = ExprValidator::new(self, infer, sink); | 651 | let mut validator = ExprValidator::new(self, infer, sink); |
652 | validator.validate_body(db); | 652 | validator.validate_body(db); |
653 | } | 653 | } |
diff --git a/crates/ra_hir/src/source_binder.rs b/crates/ra_hir/src/source_binder.rs index b9d3a1713..0de36abd1 100644 --- a/crates/ra_hir/src/source_binder.rs +++ b/crates/ra_hir/src/source_binder.rs | |||
@@ -214,17 +214,17 @@ impl SourceAnalyzer { | |||
214 | 214 | ||
215 | pub fn resolve_method_call(&self, call: &ast::MethodCallExpr) -> Option<Function> { | 215 | pub fn resolve_method_call(&self, call: &ast::MethodCallExpr) -> Option<Function> { |
216 | let expr_id = self.expr_id(&call.clone().into())?; | 216 | let expr_id = self.expr_id(&call.clone().into())?; |
217 | self.infer.as_ref()?.method_resolution(expr_id) | 217 | self.infer.as_ref()?.method_resolution(expr_id).map(Function::from) |
218 | } | 218 | } |
219 | 219 | ||
220 | pub fn resolve_field(&self, field: &ast::FieldExpr) -> Option<crate::StructField> { | 220 | pub fn resolve_field(&self, field: &ast::FieldExpr) -> Option<crate::StructField> { |
221 | let expr_id = self.expr_id(&field.clone().into())?; | 221 | let expr_id = self.expr_id(&field.clone().into())?; |
222 | self.infer.as_ref()?.field_resolution(expr_id) | 222 | self.infer.as_ref()?.field_resolution(expr_id).map(|it| it.into()) |
223 | } | 223 | } |
224 | 224 | ||
225 | pub fn resolve_record_field(&self, field: &ast::RecordField) -> Option<crate::StructField> { | 225 | pub fn resolve_record_field(&self, field: &ast::RecordField) -> Option<crate::StructField> { |
226 | let expr_id = self.expr_id(&field.expr()?)?; | 226 | let expr_id = self.expr_id(&field.expr()?)?; |
227 | self.infer.as_ref()?.record_field_resolution(expr_id) | 227 | self.infer.as_ref()?.record_field_resolution(expr_id).map(|it| it.into()) |
228 | } | 228 | } |
229 | 229 | ||
230 | pub fn resolve_record_literal(&self, record_lit: &ast::RecordLit) -> Option<crate::VariantDef> { | 230 | pub fn resolve_record_literal(&self, record_lit: &ast::RecordLit) -> Option<crate::VariantDef> { |
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 | } |
diff --git a/crates/ra_hir/src/ty/infer/expr.rs b/crates/ra_hir/src/ty/infer/expr.rs index 57f845dfa..d9ea6da42 100644 --- a/crates/ra_hir/src/ty/infer/expr.rs +++ b/crates/ra_hir/src/ty/infer/expr.rs | |||
@@ -100,7 +100,7 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> { | |||
100 | let projection = ProjectionPredicate { | 100 | let projection = ProjectionPredicate { |
101 | ty: pat_ty.clone(), | 101 | ty: pat_ty.clone(), |
102 | projection_ty: ProjectionTy { | 102 | projection_ty: ProjectionTy { |
103 | associated_ty: into_iter_item_alias.id, | 103 | associated_ty: into_iter_item_alias, |
104 | parameters: Substs::single(iterable_ty), | 104 | parameters: Substs::single(iterable_ty), |
105 | }, | 105 | }, |
106 | }; | 106 | }; |
@@ -230,7 +230,7 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> { | |||
230 | } | 230 | } |
231 | }); | 231 | }); |
232 | if let Some(field_def) = field_def { | 232 | if let Some(field_def) = field_def { |
233 | self.result.record_field_resolutions.insert(field.expr, field_def); | 233 | self.result.record_field_resolutions.insert(field.expr, field_def.into()); |
234 | } | 234 | } |
235 | let field_ty = field_def | 235 | let field_ty = field_def |
236 | .map_or(Ty::Unknown, |it| field_types[it.id].clone()) | 236 | .map_or(Ty::Unknown, |it| field_types[it.id].clone()) |
@@ -262,7 +262,7 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> { | |||
262 | self.db.struct_data(s).variant_data.field(name).map(|local_id| { | 262 | self.db.struct_data(s).variant_data.field(name).map(|local_id| { |
263 | let field = StructFieldId { parent: s.into(), local_id }.into(); | 263 | let field = StructFieldId { parent: s.into(), local_id }.into(); |
264 | self.write_field_resolution(tgt_expr, field); | 264 | self.write_field_resolution(tgt_expr, field); |
265 | self.db.field_types(s.into())[field.id] | 265 | self.db.field_types(s.into())[field.local_id] |
266 | .clone() | 266 | .clone() |
267 | .subst(&a_ty.parameters) | 267 | .subst(&a_ty.parameters) |
268 | }) | 268 | }) |
@@ -285,7 +285,7 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> { | |||
285 | let projection = ProjectionPredicate { | 285 | let projection = ProjectionPredicate { |
286 | ty: ty.clone(), | 286 | ty: ty.clone(), |
287 | projection_ty: ProjectionTy { | 287 | projection_ty: ProjectionTy { |
288 | associated_ty: future_future_output_alias.id, | 288 | associated_ty: future_future_output_alias, |
289 | parameters: Substs::single(inner_ty), | 289 | parameters: Substs::single(inner_ty), |
290 | }, | 290 | }, |
291 | }; | 291 | }; |
@@ -304,7 +304,7 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> { | |||
304 | let projection = ProjectionPredicate { | 304 | let projection = ProjectionPredicate { |
305 | ty: ty.clone(), | 305 | ty: ty.clone(), |
306 | projection_ty: ProjectionTy { | 306 | projection_ty: ProjectionTy { |
307 | associated_ty: ops_try_ok_alias.id, | 307 | associated_ty: ops_try_ok_alias, |
308 | parameters: Substs::single(inner_ty), | 308 | parameters: Substs::single(inner_ty), |
309 | }, | 309 | }, |
310 | }; | 310 | }; |
@@ -557,7 +557,7 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> { | |||
557 | Some((ty, func)) => { | 557 | Some((ty, func)) => { |
558 | let ty = canonicalized_receiver.decanonicalize_ty(ty); | 558 | let ty = canonicalized_receiver.decanonicalize_ty(ty); |
559 | self.write_method_resolution(tgt_expr, func); | 559 | self.write_method_resolution(tgt_expr, func); |
560 | (ty, self.db.value_ty(func.id.into()), Some(self.db.generic_params(func.id.into()))) | 560 | (ty, self.db.value_ty(func.into()), Some(self.db.generic_params(func.into()))) |
561 | } | 561 | } |
562 | None => (receiver_ty, Ty::Unknown, None), | 562 | None => (receiver_ty, Ty::Unknown, None), |
563 | }; | 563 | }; |
diff --git a/crates/ra_hir/src/ty/method_resolution.rs b/crates/ra_hir/src/ty/method_resolution.rs index 02e81fb34..5cc249855 100644 --- a/crates/ra_hir/src/ty/method_resolution.rs +++ b/crates/ra_hir/src/ty/method_resolution.rs | |||
@@ -7,7 +7,7 @@ use std::sync::Arc; | |||
7 | use arrayvec::ArrayVec; | 7 | use arrayvec::ArrayVec; |
8 | use hir_def::{ | 8 | use hir_def::{ |
9 | lang_item::LangItemTarget, resolver::HasResolver, resolver::Resolver, type_ref::Mutability, | 9 | lang_item::LangItemTarget, resolver::HasResolver, resolver::Resolver, type_ref::Mutability, |
10 | AssocItemId, AstItemDef, HasModule, ImplId, TraitId, | 10 | AssocItemId, AstItemDef, FunctionId, HasModule, ImplId, TraitId, |
11 | }; | 11 | }; |
12 | use hir_expand::name::Name; | 12 | use hir_expand::name::Name; |
13 | use ra_db::CrateId; | 13 | use ra_db::CrateId; |
@@ -18,7 +18,6 @@ use crate::{ | |||
18 | db::HirDatabase, | 18 | db::HirDatabase, |
19 | ty::primitive::{FloatBitness, Uncertain}, | 19 | ty::primitive::{FloatBitness, Uncertain}, |
20 | ty::{utils::all_super_traits, Ty, TypeCtor}, | 20 | ty::{utils::all_super_traits, Ty, TypeCtor}, |
21 | Function, | ||
22 | }; | 21 | }; |
23 | 22 | ||
24 | use super::{autoderef, Canonical, InEnvironment, TraitEnvironment, TraitRef}; | 23 | use super::{autoderef, Canonical, InEnvironment, TraitEnvironment, TraitRef}; |
@@ -154,10 +153,10 @@ pub(crate) fn lookup_method( | |||
154 | db: &impl HirDatabase, | 153 | db: &impl HirDatabase, |
155 | name: &Name, | 154 | name: &Name, |
156 | resolver: &Resolver, | 155 | resolver: &Resolver, |
157 | ) -> Option<(Ty, Function)> { | 156 | ) -> Option<(Ty, FunctionId)> { |
158 | iterate_method_candidates(ty, db, resolver, Some(name), LookupMode::MethodCall, |ty, f| match f | 157 | iterate_method_candidates(ty, db, resolver, Some(name), LookupMode::MethodCall, |ty, f| match f |
159 | { | 158 | { |
160 | AssocItemId::FunctionId(f) => Some((ty.clone(), f.into())), | 159 | AssocItemId::FunctionId(f) => Some((ty.clone(), f)), |
161 | _ => None, | 160 | _ => None, |
162 | }) | 161 | }) |
163 | } | 162 | } |