aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_hir_ty/src/infer.rs
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ra_hir_ty/src/infer.rs')
-rw-r--r--crates/ra_hir_ty/src/infer.rs37
1 files changed, 20 insertions, 17 deletions
diff --git a/crates/ra_hir_ty/src/infer.rs b/crates/ra_hir_ty/src/infer.rs
index 3719f76a6..28f32a0a4 100644
--- a/crates/ra_hir_ty/src/infer.rs
+++ b/crates/ra_hir_ty/src/infer.rs
@@ -18,8 +18,6 @@ use std::mem;
18use std::ops::Index; 18use std::ops::Index;
19use std::sync::Arc; 19use std::sync::Arc;
20 20
21use rustc_hash::FxHashMap;
22
23use hir_def::{ 21use hir_def::{
24 body::Body, 22 body::Body,
25 data::{ConstData, FunctionData, StaticData}, 23 data::{ConstData, FunctionData, StaticData},
@@ -28,13 +26,15 @@ use hir_def::{
28 path::{path, Path}, 26 path::{path, Path},
29 resolver::{HasResolver, Resolver, TypeNs}, 27 resolver::{HasResolver, Resolver, TypeNs},
30 type_ref::{Mutability, TypeRef}, 28 type_ref::{Mutability, TypeRef},
31 AdtId, AssocItemId, DefWithBodyId, EnumVariantId, FieldId, FunctionId, TraitId, TypeAliasId, 29 AdtId, AssocItemId, DefWithBodyId, EnumVariantId, FieldId, FunctionId, Lookup, TraitId,
32 VariantId, 30 TypeAliasId, VariantId,
33}; 31};
34use hir_expand::{diagnostics::DiagnosticSink, name::name}; 32use hir_expand::{diagnostics::DiagnosticSink, name::name};
35use ra_arena::map::ArenaMap; 33use ra_arena::map::ArenaMap;
36use ra_prof::profile; 34use ra_prof::profile;
37use ra_syntax::SmolStr; 35use ra_syntax::SmolStr;
36use rustc_hash::FxHashMap;
37use stdx::impl_from;
38 38
39use super::{ 39use super::{
40 primitive::{FloatTy, IntTy}, 40 primitive::{FloatTy, IntTy},
@@ -84,8 +84,7 @@ enum ExprOrPatId {
84 ExprId(ExprId), 84 ExprId(ExprId),
85 PatId(PatId), 85 PatId(PatId),
86} 86}
87 87impl_from!(ExprId, PatId for ExprOrPatId);
88impl_froms!(ExprOrPatId: ExprId, PatId);
89 88
90/// Binding modes inferred for patterns. 89/// Binding modes inferred for patterns.
91/// https://doc.rust-lang.org/reference/patterns.html#binding-modes 90/// https://doc.rust-lang.org/reference/patterns.html#binding-modes
@@ -169,7 +168,7 @@ impl InferenceResult {
169 pub fn add_diagnostics( 168 pub fn add_diagnostics(
170 &self, 169 &self,
171 db: &dyn HirDatabase, 170 db: &dyn HirDatabase,
172 owner: FunctionId, 171 owner: DefWithBodyId,
173 sink: &mut DiagnosticSink, 172 sink: &mut DiagnosticSink,
174 ) { 173 ) {
175 self.diagnostics.iter().for_each(|it| it.add_to(db, owner, sink)) 174 self.diagnostics.iter().for_each(|it| it.add_to(db, owner, sink))
@@ -376,17 +375,21 @@ impl<'a> InferenceContext<'a> {
376 ) -> Ty { 375 ) -> Ty {
377 match assoc_ty { 376 match assoc_ty {
378 Some(res_assoc_ty) => { 377 Some(res_assoc_ty) => {
378 let trait_ = match res_assoc_ty.lookup(self.db.upcast()).container {
379 hir_def::AssocContainerId::TraitId(trait_) => trait_,
380 _ => panic!("resolve_associated_type called with non-associated type"),
381 };
379 let ty = self.table.new_type_var(); 382 let ty = self.table.new_type_var();
380 let builder = Substs::build_for_def(self.db, res_assoc_ty) 383 let substs = Substs::build_for_def(self.db, res_assoc_ty)
381 .push(inner_ty) 384 .push(inner_ty)
382 .fill(params.iter().cloned()); 385 .fill(params.iter().cloned())
386 .build();
387 let trait_ref = TraitRef { trait_, substs: substs.clone() };
383 let projection = ProjectionPredicate { 388 let projection = ProjectionPredicate {
384 ty: ty.clone(), 389 ty: ty.clone(),
385 projection_ty: ProjectionTy { 390 projection_ty: ProjectionTy { associated_ty: res_assoc_ty, parameters: substs },
386 associated_ty: res_assoc_ty,
387 parameters: builder.build(),
388 },
389 }; 391 };
392 self.obligations.push(Obligation::Trait(trait_ref));
390 self.obligations.push(Obligation::Projection(projection)); 393 self.obligations.push(Obligation::Projection(projection));
391 self.resolve_ty_as_possible(ty) 394 self.resolve_ty_as_possible(ty)
392 } 395 }
@@ -757,7 +760,7 @@ impl std::ops::BitOrAssign for Diverges {
757} 760}
758 761
759mod diagnostics { 762mod diagnostics {
760 use hir_def::{expr::ExprId, FunctionId}; 763 use hir_def::{expr::ExprId, DefWithBodyId};
761 use hir_expand::diagnostics::DiagnosticSink; 764 use hir_expand::diagnostics::DiagnosticSink;
762 765
763 use crate::{ 766 use crate::{
@@ -775,17 +778,17 @@ mod diagnostics {
775 pub(super) fn add_to( 778 pub(super) fn add_to(
776 &self, 779 &self,
777 db: &dyn HirDatabase, 780 db: &dyn HirDatabase,
778 owner: FunctionId, 781 owner: DefWithBodyId,
779 sink: &mut DiagnosticSink, 782 sink: &mut DiagnosticSink,
780 ) { 783 ) {
781 match self { 784 match self {
782 InferenceDiagnostic::NoSuchField { expr, field } => { 785 InferenceDiagnostic::NoSuchField { expr, field } => {
783 let (_, source_map) = db.body_with_source_map(owner.into()); 786 let (_, source_map) = db.body_with_source_map(owner);
784 let field = source_map.field_syntax(*expr, *field); 787 let field = source_map.field_syntax(*expr, *field);
785 sink.push(NoSuchField { file: field.file_id, field: field.value }) 788 sink.push(NoSuchField { file: field.file_id, field: field.value })
786 } 789 }
787 InferenceDiagnostic::BreakOutsideOfLoop { expr } => { 790 InferenceDiagnostic::BreakOutsideOfLoop { expr } => {
788 let (_, source_map) = db.body_with_source_map(owner.into()); 791 let (_, source_map) = db.body_with_source_map(owner);
789 let ptr = source_map 792 let ptr = source_map
790 .expr_syntax(*expr) 793 .expr_syntax(*expr)
791 .expect("break outside of loop in synthetic syntax"); 794 .expect("break outside of loop in synthetic syntax");