aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_hir/src/ty/infer
diff options
context:
space:
mode:
authorSeivan Heidari <[email protected]>2019-11-22 00:54:34 +0000
committerSeivan Heidari <[email protected]>2019-11-22 00:54:34 +0000
commita63a269ec84192114cc1ac7f079e96144ae877a1 (patch)
treebfa5b8496a958e825423d09f6abbdf5db5c54efa /crates/ra_hir/src/ty/infer
parent358a1bcd708c622836723e5201b6de77cc9ff327 (diff)
parentc9273828b3c44fba62d1b989480c287d923839d2 (diff)
Merge branch 'master' of https://github.com/rust-analyzer/rust-analyzer into feature/themes
Diffstat (limited to 'crates/ra_hir/src/ty/infer')
-rw-r--r--crates/ra_hir/src/ty/infer/coerce.rs10
-rw-r--r--crates/ra_hir/src/ty/infer/expr.rs16
-rw-r--r--crates/ra_hir/src/ty/infer/path.rs39
3 files changed, 36 insertions, 29 deletions
diff --git a/crates/ra_hir/src/ty/infer/coerce.rs b/crates/ra_hir/src/ty/infer/coerce.rs
index 6ea135126..0772b9df5 100644
--- a/crates/ra_hir/src/ty/infer/coerce.rs
+++ b/crates/ra_hir/src/ty/infer/coerce.rs
@@ -4,19 +4,19 @@
4//! 4//!
5//! See: https://doc.rust-lang.org/nomicon/coercions.html 5//! See: https://doc.rust-lang.org/nomicon/coercions.html
6 6
7use hir_def::resolver::Resolver;
7use rustc_hash::FxHashMap; 8use rustc_hash::FxHashMap;
8
9use test_utils::tested_by; 9use test_utils::tested_by;
10 10
11use super::{InferTy, InferenceContext, TypeVarValue};
12use crate::{ 11use crate::{
13 db::HirDatabase, 12 db::HirDatabase,
14 lang_item::LangItemTarget, 13 lang_item::LangItemTarget,
15 resolve::Resolver,
16 ty::{autoderef, Substs, Ty, TypeCtor, TypeWalk}, 14 ty::{autoderef, Substs, Ty, TypeCtor, TypeWalk},
17 Adt, Mutability, 15 Adt, Mutability,
18}; 16};
19 17
18use super::{InferTy, InferenceContext, TypeVarValue};
19
20impl<'a, D: HirDatabase> InferenceContext<'a, D> { 20impl<'a, D: HirDatabase> InferenceContext<'a, D> {
21 /// Unify two types, but may coerce the first one to the second one 21 /// Unify two types, but may coerce the first one to the second one
22 /// using "implicit coercion rules" if needed. 22 /// using "implicit coercion rules" if needed.
@@ -49,8 +49,8 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> {
49 resolver: &Resolver, 49 resolver: &Resolver,
50 ) -> FxHashMap<(TypeCtor, TypeCtor), usize> { 50 ) -> FxHashMap<(TypeCtor, TypeCtor), usize> {
51 let krate = resolver.krate().unwrap(); 51 let krate = resolver.krate().unwrap();
52 let impls = match db.lang_item(krate, "coerce_unsized".into()) { 52 let impls = match db.lang_item(krate.into(), "coerce_unsized".into()) {
53 Some(LangItemTarget::Trait(trait_)) => db.impls_for_trait(krate, trait_), 53 Some(LangItemTarget::Trait(trait_)) => db.impls_for_trait(krate.into(), trait_),
54 _ => return FxHashMap::default(), 54 _ => return FxHashMap::default(),
55 }; 55 };
56 56
diff --git a/crates/ra_hir/src/ty/infer/expr.rs b/crates/ra_hir/src/ty/infer/expr.rs
index 5e68a1678..20a7e9352 100644
--- a/crates/ra_hir/src/ty/infer/expr.rs
+++ b/crates/ra_hir/src/ty/infer/expr.rs
@@ -5,15 +5,15 @@ use std::sync::Arc;
5 5
6use hir_def::{ 6use hir_def::{
7 builtin_type::Signedness, 7 builtin_type::Signedness,
8 generics::GenericParams,
8 path::{GenericArg, GenericArgs}, 9 path::{GenericArg, GenericArgs},
10 resolver::resolver_for_expr,
9}; 11};
10use hir_expand::name; 12use hir_expand::name;
11 13
12use super::{BindingMode, Expectation, InferenceContext, InferenceDiagnostic, TypeMismatch};
13use crate::{ 14use crate::{
14 db::HirDatabase, 15 db::HirDatabase,
15 expr::{self, Array, BinaryOp, Expr, ExprId, Literal, Statement, UnaryOp}, 16 expr::{Array, BinaryOp, Expr, ExprId, Literal, Statement, UnaryOp},
16 generics::{GenericParams, HasGenericParams},
17 ty::{ 17 ty::{
18 autoderef, method_resolution, op, CallableDef, InferTy, IntTy, Mutability, Namespace, 18 autoderef, method_resolution, op, CallableDef, InferTy, IntTy, Mutability, Namespace,
19 Obligation, ProjectionPredicate, ProjectionTy, Substs, TraitRef, Ty, TypeCtor, TypeWalk, 19 Obligation, ProjectionPredicate, ProjectionTy, Substs, TraitRef, Ty, TypeCtor, TypeWalk,
@@ -22,6 +22,8 @@ use crate::{
22 Adt, Name, 22 Adt, Name,
23}; 23};
24 24
25use super::{BindingMode, Expectation, InferenceContext, InferenceDiagnostic, TypeMismatch};
26
25impl<'a, D: HirDatabase> InferenceContext<'a, D> { 27impl<'a, D: HirDatabase> InferenceContext<'a, D> {
26 pub(super) fn infer_expr(&mut self, tgt_expr: ExprId, expected: &Expectation) -> Ty { 28 pub(super) fn infer_expr(&mut self, tgt_expr: ExprId, expected: &Expectation) -> Ty {
27 let ty = self.infer_expr_inner(tgt_expr, expected); 29 let ty = self.infer_expr_inner(tgt_expr, expected);
@@ -186,7 +188,7 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> {
186 } 188 }
187 Expr::Path(p) => { 189 Expr::Path(p) => {
188 // FIXME this could be more efficient... 190 // FIXME this could be more efficient...
189 let resolver = expr::resolver_for_expr(self.db, self.owner, tgt_expr); 191 let resolver = resolver_for_expr(self.db, self.owner.into(), tgt_expr);
190 self.infer_path(&resolver, p, tgt_expr.into()).unwrap_or(Ty::Unknown) 192 self.infer_path(&resolver, p, tgt_expr.into()).unwrap_or(Ty::Unknown)
191 } 193 }
192 Expr::Continue => Ty::simple(TypeCtor::Never), 194 Expr::Continue => Ty::simple(TypeCtor::Never),
@@ -532,7 +534,7 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> {
532 ( 534 (
533 ty, 535 ty,
534 self.db.type_for_def(func.into(), Namespace::Values), 536 self.db.type_for_def(func.into(), Namespace::Values),
535 Some(func.generic_params(self.db)), 537 Some(self.db.generic_params(func.id.into())),
536 ) 538 )
537 } 539 }
538 None => (receiver_ty, Ty::Unknown, None), 540 None => (receiver_ty, Ty::Unknown, None),
@@ -643,7 +645,9 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> {
643 if let Some(trait_) = f.parent_trait(self.db) { 645 if let Some(trait_) = f.parent_trait(self.db) {
644 // construct a TraitDef 646 // construct a TraitDef
645 let substs = a_ty.parameters.prefix( 647 let substs = a_ty.parameters.prefix(
646 trait_.generic_params(self.db).count_params_including_parent(), 648 self.db
649 .generic_params(trait_.id.into())
650 .count_params_including_parent(),
647 ); 651 );
648 self.obligations.push(Obligation::Trait(TraitRef { trait_, substs })); 652 self.obligations.push(Obligation::Trait(TraitRef { trait_, substs }));
649 } 653 }
diff --git a/crates/ra_hir/src/ty/infer/path.rs b/crates/ra_hir/src/ty/infer/path.rs
index 31ca675aa..ee54d8217 100644
--- a/crates/ra_hir/src/ty/infer/path.rs
+++ b/crates/ra_hir/src/ty/infer/path.rs
@@ -1,16 +1,18 @@
1//! Path expression resolution. 1//! Path expression resolution.
2 2
3use hir_def::path::PathSegment; 3use hir_def::{
4 path::PathSegment,
5 resolver::{ResolveValueResult, Resolver, TypeNs, ValueNs},
6};
4 7
5use super::{ExprOrPatId, InferenceContext, TraitRef};
6use crate::{ 8use crate::{
7 db::HirDatabase, 9 db::HirDatabase,
8 generics::HasGenericParams,
9 resolve::{ResolveValueResult, Resolver, TypeNs, ValueNs},
10 ty::{method_resolution, Namespace, Substs, Ty, TypableDef, TypeWalk}, 10 ty::{method_resolution, Namespace, Substs, Ty, TypableDef, TypeWalk},
11 AssocItem, Container, Name, Path, 11 AssocItem, Container, Function, Name, Path,
12}; 12};
13 13
14use super::{ExprOrPatId, InferenceContext, TraitRef};
15
14impl<'a, D: HirDatabase> InferenceContext<'a, D> { 16impl<'a, D: HirDatabase> InferenceContext<'a, D> {
15 pub(super) fn infer_path( 17 pub(super) fn infer_path(
16 &mut self, 18 &mut self,
@@ -60,11 +62,11 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> {
60 let ty = self.resolve_ty_as_possible(&mut vec![], ty); 62 let ty = self.resolve_ty_as_possible(&mut vec![], ty);
61 return Some(ty); 63 return Some(ty);
62 } 64 }
63 ValueNs::Function(it) => it.into(), 65 ValueNs::FunctionId(it) => it.into(),
64 ValueNs::Const(it) => it.into(), 66 ValueNs::ConstId(it) => it.into(),
65 ValueNs::Static(it) => it.into(), 67 ValueNs::StaticId(it) => it.into(),
66 ValueNs::Struct(it) => it.into(), 68 ValueNs::StructId(it) => it.into(),
67 ValueNs::EnumVariant(it) => it.into(), 69 ValueNs::EnumVariantId(it) => it.into(),
68 }; 70 };
69 71
70 let mut ty = self.db.type_for_def(typable, Namespace::Values); 72 let mut ty = self.db.type_for_def(typable, Namespace::Values);
@@ -94,13 +96,13 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> {
94 let is_before_last = remaining_segments.len() == 1; 96 let is_before_last = remaining_segments.len() == 1;
95 97
96 match (def, is_before_last) { 98 match (def, is_before_last) {
97 (TypeNs::Trait(trait_), true) => { 99 (TypeNs::TraitId(trait_), true) => {
98 let segment = 100 let segment =
99 remaining_segments.last().expect("there should be at least one segment here"); 101 remaining_segments.last().expect("there should be at least one segment here");
100 let trait_ref = TraitRef::from_resolved_path( 102 let trait_ref = TraitRef::from_resolved_path(
101 self.db, 103 self.db,
102 &self.resolver, 104 &self.resolver,
103 trait_, 105 trait_.into(),
104 resolved_segment, 106 resolved_segment,
105 None, 107 None,
106 ); 108 );
@@ -160,8 +162,8 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> {
160 AssocItem::TypeAlias(_) => None, 162 AssocItem::TypeAlias(_) => None,
161 })?; 163 })?;
162 let def = match item { 164 let def = match item {
163 AssocItem::Function(f) => ValueNs::Function(f), 165 AssocItem::Function(f) => ValueNs::FunctionId(f.id),
164 AssocItem::Const(c) => ValueNs::Const(c), 166 AssocItem::Const(c) => ValueNs::ConstId(c.id),
165 AssocItem::TypeAlias(_) => unreachable!(), 167 AssocItem::TypeAlias(_) => unreachable!(),
166 }; 168 };
167 let substs = Substs::build_for_def(self.db, item) 169 let substs = Substs::build_for_def(self.db, item)
@@ -193,8 +195,8 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> {
193 method_resolution::LookupMode::Path, 195 method_resolution::LookupMode::Path,
194 move |_ty, item| { 196 move |_ty, item| {
195 let def = match item { 197 let def = match item {
196 AssocItem::Function(f) => ValueNs::Function(f), 198 AssocItem::Function(f) => ValueNs::FunctionId(f.id),
197 AssocItem::Const(c) => ValueNs::Const(c), 199 AssocItem::Const(c) => ValueNs::ConstId(c.id),
198 AssocItem::TypeAlias(_) => unreachable!(), 200 AssocItem::TypeAlias(_) => unreachable!(),
199 }; 201 };
200 let substs = match item.container(self.db) { 202 let substs = match item.container(self.db) {
@@ -224,9 +226,10 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> {
224 } 226 }
225 227
226 fn find_self_types(&self, def: &ValueNs, actual_def_ty: Ty) -> Option<Substs> { 228 fn find_self_types(&self, def: &ValueNs, actual_def_ty: Ty) -> Option<Substs> {
227 if let ValueNs::Function(func) = def { 229 if let ValueNs::FunctionId(func) = def {
230 let func = Function::from(*func);
228 // We only do the infer if parent has generic params 231 // We only do the infer if parent has generic params
229 let gen = func.generic_params(self.db); 232 let gen = self.db.generic_params(func.id.into());
230 if gen.count_parent_params() == 0 { 233 if gen.count_parent_params() == 0 {
231 return None; 234 return None;
232 } 235 }