diff options
Diffstat (limited to 'crates/ra_hir/src/ty')
-rw-r--r-- | crates/ra_hir/src/ty/autoderef.rs | 7 | ||||
-rw-r--r-- | crates/ra_hir/src/ty/infer.rs | 32 | ||||
-rw-r--r-- | crates/ra_hir/src/ty/infer/coerce.rs | 10 | ||||
-rw-r--r-- | crates/ra_hir/src/ty/infer/expr.rs | 8 | ||||
-rw-r--r-- | crates/ra_hir/src/ty/infer/path.rs | 36 | ||||
-rw-r--r-- | crates/ra_hir/src/ty/lower.rs | 56 | ||||
-rw-r--r-- | crates/ra_hir/src/ty/method_resolution.rs | 21 |
7 files changed, 96 insertions, 74 deletions
diff --git a/crates/ra_hir/src/ty/autoderef.rs b/crates/ra_hir/src/ty/autoderef.rs index 872a4517d..5d8518041 100644 --- a/crates/ra_hir/src/ty/autoderef.rs +++ b/crates/ra_hir/src/ty/autoderef.rs | |||
@@ -5,11 +5,12 @@ | |||
5 | 5 | ||
6 | use std::iter::successors; | 6 | use std::iter::successors; |
7 | 7 | ||
8 | use hir_def::resolver::Resolver; | ||
8 | use hir_expand::name; | 9 | use hir_expand::name; |
9 | use log::{info, warn}; | 10 | use log::{info, warn}; |
10 | 11 | ||
11 | use super::{traits::Solution, Canonical, Substs, Ty, TypeWalk}; | 12 | use super::{traits::Solution, Canonical, Substs, Ty, TypeWalk}; |
12 | use crate::{db::HirDatabase, generics::HasGenericParams, Resolver}; | 13 | use crate::{db::HirDatabase, generics::HasGenericParams}; |
13 | 14 | ||
14 | const AUTODEREF_RECURSION_LIMIT: usize = 10; | 15 | const AUTODEREF_RECURSION_LIMIT: usize = 10; |
15 | 16 | ||
@@ -39,7 +40,7 @@ fn deref_by_trait( | |||
39 | ty: &Canonical<Ty>, | 40 | ty: &Canonical<Ty>, |
40 | ) -> Option<Canonical<Ty>> { | 41 | ) -> Option<Canonical<Ty>> { |
41 | let krate = resolver.krate()?; | 42 | let krate = resolver.krate()?; |
42 | let deref_trait = match db.lang_item(krate, "deref".into())? { | 43 | let deref_trait = match db.lang_item(krate.into(), "deref".into())? { |
43 | crate::lang_item::LangItemTarget::Trait(t) => t, | 44 | crate::lang_item::LangItemTarget::Trait(t) => t, |
44 | _ => return None, | 45 | _ => return None, |
45 | }; | 46 | }; |
@@ -71,7 +72,7 @@ fn deref_by_trait( | |||
71 | 72 | ||
72 | let canonical = super::Canonical { num_vars: 1 + ty.num_vars, value: in_env }; | 73 | let canonical = super::Canonical { num_vars: 1 + ty.num_vars, value: in_env }; |
73 | 74 | ||
74 | let solution = db.trait_solve(krate, canonical)?; | 75 | let solution = db.trait_solve(krate.into(), canonical)?; |
75 | 76 | ||
76 | match &solution { | 77 | match &solution { |
77 | Solution::Unique(vars) => { | 78 | Solution::Unique(vars) => { |
diff --git a/crates/ra_hir/src/ty/infer.rs b/crates/ra_hir/src/ty/infer.rs index 7f9e81d64..69b13baef 100644 --- a/crates/ra_hir/src/ty/infer.rs +++ b/crates/ra_hir/src/ty/infer.rs | |||
@@ -23,7 +23,9 @@ use rustc_hash::FxHashMap; | |||
23 | 23 | ||
24 | use hir_def::{ | 24 | use hir_def::{ |
25 | path::known, | 25 | path::known, |
26 | resolver::{HasResolver, Resolver, TypeNs}, | ||
26 | type_ref::{Mutability, TypeRef}, | 27 | type_ref::{Mutability, TypeRef}, |
28 | AdtId, DefWithBodyId, | ||
27 | }; | 29 | }; |
28 | use hir_expand::{diagnostics::DiagnosticSink, name}; | 30 | use hir_expand::{diagnostics::DiagnosticSink, name}; |
29 | use ra_arena::map::ArenaMap; | 31 | use ra_arena::map::ArenaMap; |
@@ -40,10 +42,9 @@ use crate::{ | |||
40 | code_model::TypeAlias, | 42 | code_model::TypeAlias, |
41 | db::HirDatabase, | 43 | db::HirDatabase, |
42 | expr::{BindingAnnotation, Body, ExprId, PatId}, | 44 | expr::{BindingAnnotation, Body, ExprId, PatId}, |
43 | resolve::{HasResolver, Resolver, TypeNs}, | ||
44 | ty::infer::diagnostics::InferenceDiagnostic, | 45 | ty::infer::diagnostics::InferenceDiagnostic, |
45 | Adt, AssocItem, ConstData, DefWithBody, FloatTy, FnData, Function, HasBody, IntTy, Path, | 46 | Adt, AssocItem, ConstData, DefWithBody, FloatTy, FnData, Function, HasBody, IntTy, Path, |
46 | StructField, VariantDef, | 47 | StructField, Trait, VariantDef, |
47 | }; | 48 | }; |
48 | 49 | ||
49 | macro_rules! ty_app { | 50 | macro_rules! ty_app { |
@@ -64,7 +65,7 @@ mod coerce; | |||
64 | /// The entry point of type inference. | 65 | /// The entry point of type inference. |
65 | pub fn infer_query(db: &impl HirDatabase, def: DefWithBody) -> Arc<InferenceResult> { | 66 | pub fn infer_query(db: &impl HirDatabase, def: DefWithBody) -> Arc<InferenceResult> { |
66 | let _p = profile("infer_query"); | 67 | let _p = profile("infer_query"); |
67 | let resolver = def.resolver(db); | 68 | let resolver = DefWithBodyId::from(def).resolver(db); |
68 | let mut ctx = InferenceContext::new(db, def, resolver); | 69 | let mut ctx = InferenceContext::new(db, def, resolver); |
69 | 70 | ||
70 | match def { | 71 | match def { |
@@ -377,8 +378,9 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> { | |||
377 | for obligation in obligations { | 378 | for obligation in obligations { |
378 | let in_env = InEnvironment::new(self.trait_env.clone(), obligation.clone()); | 379 | let in_env = InEnvironment::new(self.trait_env.clone(), obligation.clone()); |
379 | let canonicalized = self.canonicalizer().canonicalize_obligation(in_env); | 380 | let canonicalized = self.canonicalizer().canonicalize_obligation(in_env); |
380 | let solution = | 381 | let solution = self |
381 | self.db.trait_solve(self.resolver.krate().unwrap(), canonicalized.value.clone()); | 382 | .db |
383 | .trait_solve(self.resolver.krate().unwrap().into(), canonicalized.value.clone()); | ||
382 | 384 | ||
383 | match solution { | 385 | match solution { |
384 | Some(Solution::Unique(substs)) => { | 386 | Some(Solution::Unique(substs)) => { |
@@ -518,17 +520,17 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> { | |||
518 | // FIXME: this should resolve assoc items as well, see this example: | 520 | // FIXME: this should resolve assoc items as well, see this example: |
519 | // https://play.rust-lang.org/?gist=087992e9e22495446c01c0d4e2d69521 | 521 | // https://play.rust-lang.org/?gist=087992e9e22495446c01c0d4e2d69521 |
520 | match resolver.resolve_path_in_type_ns_fully(self.db, &path) { | 522 | match resolver.resolve_path_in_type_ns_fully(self.db, &path) { |
521 | Some(TypeNs::Adt(Adt::Struct(it))) => it.into(), | 523 | Some(TypeNs::AdtId(AdtId::StructId(it))) => it.into(), |
522 | Some(TypeNs::Adt(Adt::Union(it))) => it.into(), | 524 | Some(TypeNs::AdtId(AdtId::UnionId(it))) => it.into(), |
523 | Some(TypeNs::AdtSelfType(adt)) => adt.into(), | 525 | Some(TypeNs::AdtSelfType(adt)) => adt.into(), |
524 | Some(TypeNs::EnumVariant(it)) => it.into(), | 526 | Some(TypeNs::EnumVariantId(it)) => it.into(), |
525 | Some(TypeNs::TypeAlias(it)) => it.into(), | 527 | Some(TypeNs::TypeAliasId(it)) => it.into(), |
526 | 528 | ||
527 | Some(TypeNs::SelfType(_)) | | 529 | Some(TypeNs::SelfType(_)) | |
528 | Some(TypeNs::GenericParam(_)) | | 530 | Some(TypeNs::GenericParam(_)) | |
529 | Some(TypeNs::BuiltinType(_)) | | 531 | Some(TypeNs::BuiltinType(_)) | |
530 | Some(TypeNs::Trait(_)) | | 532 | Some(TypeNs::TraitId(_)) | |
531 | Some(TypeNs::Adt(Adt::Enum(_))) | | 533 | Some(TypeNs::AdtId(AdtId::EnumId(_))) | |
532 | None => { | 534 | None => { |
533 | return (Ty::Unknown, None) | 535 | return (Ty::Unknown, None) |
534 | } | 536 | } |
@@ -576,26 +578,26 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> { | |||
576 | 578 | ||
577 | fn resolve_into_iter_item(&self) -> Option<TypeAlias> { | 579 | fn resolve_into_iter_item(&self) -> Option<TypeAlias> { |
578 | let path = known::std_iter_into_iterator(); | 580 | let path = known::std_iter_into_iterator(); |
579 | let trait_ = self.resolver.resolve_known_trait(self.db, &path)?; | 581 | let trait_: Trait = self.resolver.resolve_known_trait(self.db, &path)?.into(); |
580 | trait_.associated_type_by_name(self.db, &name::ITEM_TYPE) | 582 | trait_.associated_type_by_name(self.db, &name::ITEM_TYPE) |
581 | } | 583 | } |
582 | 584 | ||
583 | fn resolve_ops_try_ok(&self) -> Option<TypeAlias> { | 585 | fn resolve_ops_try_ok(&self) -> Option<TypeAlias> { |
584 | let path = known::std_ops_try(); | 586 | let path = known::std_ops_try(); |
585 | let trait_ = self.resolver.resolve_known_trait(self.db, &path)?; | 587 | let trait_: Trait = self.resolver.resolve_known_trait(self.db, &path)?.into(); |
586 | trait_.associated_type_by_name(self.db, &name::OK_TYPE) | 588 | trait_.associated_type_by_name(self.db, &name::OK_TYPE) |
587 | } | 589 | } |
588 | 590 | ||
589 | fn resolve_future_future_output(&self) -> Option<TypeAlias> { | 591 | fn resolve_future_future_output(&self) -> Option<TypeAlias> { |
590 | let path = known::std_future_future(); | 592 | let path = known::std_future_future(); |
591 | let trait_ = self.resolver.resolve_known_trait(self.db, &path)?; | 593 | let trait_: Trait = self.resolver.resolve_known_trait(self.db, &path)?.into(); |
592 | trait_.associated_type_by_name(self.db, &name::OUTPUT_TYPE) | 594 | trait_.associated_type_by_name(self.db, &name::OUTPUT_TYPE) |
593 | } | 595 | } |
594 | 596 | ||
595 | fn resolve_boxed_box(&self) -> Option<Adt> { | 597 | fn resolve_boxed_box(&self) -> Option<Adt> { |
596 | let path = known::std_boxed_box(); | 598 | let path = known::std_boxed_box(); |
597 | let struct_ = self.resolver.resolve_known_struct(self.db, &path)?; | 599 | let struct_ = self.resolver.resolve_known_struct(self.db, &path)?; |
598 | Some(Adt::Struct(struct_)) | 600 | Some(Adt::Struct(struct_.into())) |
599 | } | 601 | } |
600 | } | 602 | } |
601 | 603 | ||
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 | ||
7 | use hir_def::resolver::Resolver; | ||
7 | use rustc_hash::FxHashMap; | 8 | use rustc_hash::FxHashMap; |
8 | |||
9 | use test_utils::tested_by; | 9 | use test_utils::tested_by; |
10 | 10 | ||
11 | use super::{InferTy, InferenceContext, TypeVarValue}; | ||
12 | use crate::{ | 11 | use 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 | ||
18 | use super::{InferTy, InferenceContext, TypeVarValue}; | ||
19 | |||
20 | impl<'a, D: HirDatabase> InferenceContext<'a, D> { | 20 | impl<'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..ac570075f 100644 --- a/crates/ra_hir/src/ty/infer/expr.rs +++ b/crates/ra_hir/src/ty/infer/expr.rs | |||
@@ -6,13 +6,13 @@ use std::sync::Arc; | |||
6 | use hir_def::{ | 6 | use hir_def::{ |
7 | builtin_type::Signedness, | 7 | builtin_type::Signedness, |
8 | path::{GenericArg, GenericArgs}, | 8 | path::{GenericArg, GenericArgs}, |
9 | resolver::resolver_for_expr, | ||
9 | }; | 10 | }; |
10 | use hir_expand::name; | 11 | use hir_expand::name; |
11 | 12 | ||
12 | use super::{BindingMode, Expectation, InferenceContext, InferenceDiagnostic, TypeMismatch}; | ||
13 | use crate::{ | 13 | use crate::{ |
14 | db::HirDatabase, | 14 | db::HirDatabase, |
15 | expr::{self, Array, BinaryOp, Expr, ExprId, Literal, Statement, UnaryOp}, | 15 | expr::{Array, BinaryOp, Expr, ExprId, Literal, Statement, UnaryOp}, |
16 | generics::{GenericParams, HasGenericParams}, | 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, |
@@ -22,6 +22,8 @@ use crate::{ | |||
22 | Adt, Name, | 22 | Adt, Name, |
23 | }; | 23 | }; |
24 | 24 | ||
25 | use super::{BindingMode, Expectation, InferenceContext, InferenceDiagnostic, TypeMismatch}; | ||
26 | |||
25 | impl<'a, D: HirDatabase> InferenceContext<'a, D> { | 27 | impl<'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), |
diff --git a/crates/ra_hir/src/ty/infer/path.rs b/crates/ra_hir/src/ty/infer/path.rs index 31ca675aa..70136e514 100644 --- a/crates/ra_hir/src/ty/infer/path.rs +++ b/crates/ra_hir/src/ty/infer/path.rs | |||
@@ -1,16 +1,19 @@ | |||
1 | //! Path expression resolution. | 1 | //! Path expression resolution. |
2 | 2 | ||
3 | use hir_def::path::PathSegment; | 3 | use hir_def::{ |
4 | path::PathSegment, | ||
5 | resolver::{ResolveValueResult, Resolver, TypeNs, ValueNs}, | ||
6 | }; | ||
4 | 7 | ||
5 | use super::{ExprOrPatId, InferenceContext, TraitRef}; | ||
6 | use crate::{ | 8 | use crate::{ |
7 | db::HirDatabase, | 9 | db::HirDatabase, |
8 | generics::HasGenericParams, | 10 | generics::HasGenericParams, |
9 | resolve::{ResolveValueResult, Resolver, TypeNs, ValueNs}, | ||
10 | ty::{method_resolution, Namespace, Substs, Ty, TypableDef, TypeWalk}, | 11 | ty::{method_resolution, Namespace, Substs, Ty, TypableDef, TypeWalk}, |
11 | AssocItem, Container, Name, Path, | 12 | AssocItem, Container, Function, Name, Path, |
12 | }; | 13 | }; |
13 | 14 | ||
15 | use super::{ExprOrPatId, InferenceContext, TraitRef}; | ||
16 | |||
14 | impl<'a, D: HirDatabase> InferenceContext<'a, D> { | 17 | impl<'a, D: HirDatabase> InferenceContext<'a, D> { |
15 | pub(super) fn infer_path( | 18 | pub(super) fn infer_path( |
16 | &mut self, | 19 | &mut self, |
@@ -60,11 +63,11 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> { | |||
60 | let ty = self.resolve_ty_as_possible(&mut vec![], ty); | 63 | let ty = self.resolve_ty_as_possible(&mut vec![], ty); |
61 | return Some(ty); | 64 | return Some(ty); |
62 | } | 65 | } |
63 | ValueNs::Function(it) => it.into(), | 66 | ValueNs::FunctionId(it) => it.into(), |
64 | ValueNs::Const(it) => it.into(), | 67 | ValueNs::ConstId(it) => it.into(), |
65 | ValueNs::Static(it) => it.into(), | 68 | ValueNs::StaticId(it) => it.into(), |
66 | ValueNs::Struct(it) => it.into(), | 69 | ValueNs::StructId(it) => it.into(), |
67 | ValueNs::EnumVariant(it) => it.into(), | 70 | ValueNs::EnumVariantId(it) => it.into(), |
68 | }; | 71 | }; |
69 | 72 | ||
70 | let mut ty = self.db.type_for_def(typable, Namespace::Values); | 73 | let mut ty = self.db.type_for_def(typable, Namespace::Values); |
@@ -94,13 +97,13 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> { | |||
94 | let is_before_last = remaining_segments.len() == 1; | 97 | let is_before_last = remaining_segments.len() == 1; |
95 | 98 | ||
96 | match (def, is_before_last) { | 99 | match (def, is_before_last) { |
97 | (TypeNs::Trait(trait_), true) => { | 100 | (TypeNs::TraitId(trait_), true) => { |
98 | let segment = | 101 | let segment = |
99 | remaining_segments.last().expect("there should be at least one segment here"); | 102 | remaining_segments.last().expect("there should be at least one segment here"); |
100 | let trait_ref = TraitRef::from_resolved_path( | 103 | let trait_ref = TraitRef::from_resolved_path( |
101 | self.db, | 104 | self.db, |
102 | &self.resolver, | 105 | &self.resolver, |
103 | trait_, | 106 | trait_.into(), |
104 | resolved_segment, | 107 | resolved_segment, |
105 | None, | 108 | None, |
106 | ); | 109 | ); |
@@ -160,8 +163,8 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> { | |||
160 | AssocItem::TypeAlias(_) => None, | 163 | AssocItem::TypeAlias(_) => None, |
161 | })?; | 164 | })?; |
162 | let def = match item { | 165 | let def = match item { |
163 | AssocItem::Function(f) => ValueNs::Function(f), | 166 | AssocItem::Function(f) => ValueNs::FunctionId(f.id), |
164 | AssocItem::Const(c) => ValueNs::Const(c), | 167 | AssocItem::Const(c) => ValueNs::ConstId(c.id), |
165 | AssocItem::TypeAlias(_) => unreachable!(), | 168 | AssocItem::TypeAlias(_) => unreachable!(), |
166 | }; | 169 | }; |
167 | let substs = Substs::build_for_def(self.db, item) | 170 | let substs = Substs::build_for_def(self.db, item) |
@@ -193,8 +196,8 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> { | |||
193 | method_resolution::LookupMode::Path, | 196 | method_resolution::LookupMode::Path, |
194 | move |_ty, item| { | 197 | move |_ty, item| { |
195 | let def = match item { | 198 | let def = match item { |
196 | AssocItem::Function(f) => ValueNs::Function(f), | 199 | AssocItem::Function(f) => ValueNs::FunctionId(f.id), |
197 | AssocItem::Const(c) => ValueNs::Const(c), | 200 | AssocItem::Const(c) => ValueNs::ConstId(c.id), |
198 | AssocItem::TypeAlias(_) => unreachable!(), | 201 | AssocItem::TypeAlias(_) => unreachable!(), |
199 | }; | 202 | }; |
200 | let substs = match item.container(self.db) { | 203 | let substs = match item.container(self.db) { |
@@ -224,7 +227,8 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> { | |||
224 | } | 227 | } |
225 | 228 | ||
226 | fn find_self_types(&self, def: &ValueNs, actual_def_ty: Ty) -> Option<Substs> { | 229 | fn find_self_types(&self, def: &ValueNs, actual_def_ty: Ty) -> Option<Substs> { |
227 | if let ValueNs::Function(func) = def { | 230 | if let ValueNs::FunctionId(func) = def { |
231 | let func = Function::from(*func); | ||
228 | // We only do the infer if parent has generic params | 232 | // We only do the infer if parent has generic params |
229 | let gen = func.generic_params(self.db); | 233 | let gen = func.generic_params(self.db); |
230 | if gen.count_parent_params() == 0 { | 234 | if gen.count_parent_params() == 0 { |
diff --git a/crates/ra_hir/src/ty/lower.rs b/crates/ra_hir/src/ty/lower.rs index 397ee7d5f..c6ad0811b 100644 --- a/crates/ra_hir/src/ty/lower.rs +++ b/crates/ra_hir/src/ty/lower.rs | |||
@@ -11,7 +11,9 @@ use std::sync::Arc; | |||
11 | use hir_def::{ | 11 | use hir_def::{ |
12 | builtin_type::{BuiltinFloat, BuiltinInt, BuiltinType}, | 12 | builtin_type::{BuiltinFloat, BuiltinInt, BuiltinType}, |
13 | path::{GenericArg, PathSegment}, | 13 | path::{GenericArg, PathSegment}, |
14 | resolver::{HasResolver, Resolver, TypeNs}, | ||
14 | type_ref::{TypeBound, TypeRef}, | 15 | type_ref::{TypeBound, TypeRef}, |
16 | GenericDefId, | ||
15 | }; | 17 | }; |
16 | 18 | ||
17 | use super::{ | 19 | use super::{ |
@@ -22,14 +24,13 @@ use crate::{ | |||
22 | db::HirDatabase, | 24 | db::HirDatabase, |
23 | generics::HasGenericParams, | 25 | generics::HasGenericParams, |
24 | generics::{GenericDef, WherePredicate}, | 26 | generics::{GenericDef, WherePredicate}, |
25 | resolve::{HasResolver, Resolver, TypeNs}, | ||
26 | ty::{ | 27 | ty::{ |
27 | primitive::{FloatTy, IntTy, Uncertain}, | 28 | primitive::{FloatTy, IntTy, Uncertain}, |
28 | Adt, | 29 | Adt, |
29 | }, | 30 | }, |
30 | util::make_mut_slice, | 31 | util::make_mut_slice, |
31 | Const, Enum, EnumVariant, Function, ModuleDef, Path, Static, Struct, StructField, Trait, | 32 | Const, Enum, EnumVariant, Function, ImplBlock, ModuleDef, Path, Static, Struct, StructField, |
32 | TypeAlias, Union, VariantDef, | 33 | Trait, TypeAlias, Union, VariantDef, |
33 | }; | 34 | }; |
34 | 35 | ||
35 | // FIXME: this is only really used in `type_for_def`, which contains a bunch of | 36 | // FIXME: this is only really used in `type_for_def`, which contains a bunch of |
@@ -156,9 +157,14 @@ impl Ty { | |||
156 | remaining_segments: &[PathSegment], | 157 | remaining_segments: &[PathSegment], |
157 | ) -> Ty { | 158 | ) -> Ty { |
158 | let ty = match resolution { | 159 | let ty = match resolution { |
159 | TypeNs::Trait(trait_) => { | 160 | TypeNs::TraitId(trait_) => { |
160 | let trait_ref = | 161 | let trait_ref = TraitRef::from_resolved_path( |
161 | TraitRef::from_resolved_path(db, resolver, trait_, resolved_segment, None); | 162 | db, |
163 | resolver, | ||
164 | trait_.into(), | ||
165 | resolved_segment, | ||
166 | None, | ||
167 | ); | ||
162 | return if remaining_segments.len() == 1 { | 168 | return if remaining_segments.len() == 1 { |
163 | let segment = &remaining_segments[0]; | 169 | let segment = &remaining_segments[0]; |
164 | match trait_ref | 170 | match trait_ref |
@@ -189,18 +195,18 @@ impl Ty { | |||
189 | let name = resolved_segment.name.clone(); | 195 | let name = resolved_segment.name.clone(); |
190 | Ty::Param { idx, name } | 196 | Ty::Param { idx, name } |
191 | } | 197 | } |
192 | TypeNs::SelfType(impl_block) => impl_block.target_ty(db), | 198 | TypeNs::SelfType(impl_block) => ImplBlock::from(impl_block).target_ty(db), |
193 | TypeNs::AdtSelfType(adt) => adt.ty(db), | 199 | TypeNs::AdtSelfType(adt) => Adt::from(adt).ty(db), |
194 | 200 | ||
195 | TypeNs::Adt(it) => Ty::from_hir_path_inner(db, resolver, resolved_segment, it.into()), | 201 | TypeNs::AdtId(it) => Ty::from_hir_path_inner(db, resolver, resolved_segment, it.into()), |
196 | TypeNs::BuiltinType(it) => { | 202 | TypeNs::BuiltinType(it) => { |
197 | Ty::from_hir_path_inner(db, resolver, resolved_segment, it.into()) | 203 | Ty::from_hir_path_inner(db, resolver, resolved_segment, it.into()) |
198 | } | 204 | } |
199 | TypeNs::TypeAlias(it) => { | 205 | TypeNs::TypeAliasId(it) => { |
200 | Ty::from_hir_path_inner(db, resolver, resolved_segment, it.into()) | 206 | Ty::from_hir_path_inner(db, resolver, resolved_segment, it.into()) |
201 | } | 207 | } |
202 | // FIXME: report error | 208 | // FIXME: report error |
203 | TypeNs::EnumVariant(_) => return Ty::Unknown, | 209 | TypeNs::EnumVariantId(_) => return Ty::Unknown, |
204 | }; | 210 | }; |
205 | 211 | ||
206 | Ty::from_type_relative_path(db, resolver, ty, remaining_segments) | 212 | Ty::from_type_relative_path(db, resolver, ty, remaining_segments) |
@@ -247,7 +253,7 @@ impl Ty { | |||
247 | Some(def) => def, | 253 | Some(def) => def, |
248 | None => return Ty::Unknown, // this can't actually happen | 254 | None => return Ty::Unknown, // this can't actually happen |
249 | }; | 255 | }; |
250 | let predicates = db.generic_predicates_for_param(def, param_idx); | 256 | let predicates = db.generic_predicates_for_param(def.into(), param_idx); |
251 | let traits_from_env = predicates.iter().filter_map(|pred| match pred { | 257 | let traits_from_env = predicates.iter().filter_map(|pred| match pred { |
252 | GenericPredicate::Implemented(tr) if tr.self_ty() == &self_ty => Some(tr.trait_), | 258 | GenericPredicate::Implemented(tr) if tr.self_ty() == &self_ty => Some(tr.trait_), |
253 | _ => None, | 259 | _ => None, |
@@ -391,11 +397,11 @@ impl TraitRef { | |||
391 | explicit_self_ty: Option<Ty>, | 397 | explicit_self_ty: Option<Ty>, |
392 | ) -> Option<Self> { | 398 | ) -> Option<Self> { |
393 | let resolved = match resolver.resolve_path_in_type_ns_fully(db, &path)? { | 399 | let resolved = match resolver.resolve_path_in_type_ns_fully(db, &path)? { |
394 | TypeNs::Trait(tr) => tr, | 400 | TypeNs::TraitId(tr) => tr, |
395 | _ => return None, | 401 | _ => return None, |
396 | }; | 402 | }; |
397 | let segment = path.segments.last().expect("path should have at least one segment"); | 403 | let segment = path.segments.last().expect("path should have at least one segment"); |
398 | Some(TraitRef::from_resolved_path(db, resolver, resolved, segment, explicit_self_ty)) | 404 | Some(TraitRef::from_resolved_path(db, resolver, resolved.into(), segment, explicit_self_ty)) |
399 | } | 405 | } |
400 | 406 | ||
401 | pub(super) fn from_resolved_path( | 407 | pub(super) fn from_resolved_path( |
@@ -548,8 +554,8 @@ pub(crate) fn callable_item_sig(db: &impl HirDatabase, def: CallableDef) -> FnSi | |||
548 | pub(crate) fn type_for_field(db: &impl HirDatabase, field: StructField) -> Ty { | 554 | pub(crate) fn type_for_field(db: &impl HirDatabase, field: StructField) -> Ty { |
549 | let parent_def = field.parent_def(db); | 555 | let parent_def = field.parent_def(db); |
550 | let resolver = match parent_def { | 556 | let resolver = match parent_def { |
551 | VariantDef::Struct(it) => it.resolver(db), | 557 | VariantDef::Struct(it) => it.id.resolver(db), |
552 | VariantDef::EnumVariant(it) => it.parent_enum(db).resolver(db), | 558 | VariantDef::EnumVariant(it) => it.parent.id.resolver(db), |
553 | }; | 559 | }; |
554 | let var_data = parent_def.variant_data(db); | 560 | let var_data = parent_def.variant_data(db); |
555 | let type_ref = &var_data.fields().unwrap()[field.id].type_ref; | 561 | let type_ref = &var_data.fields().unwrap()[field.id].type_ref; |
@@ -569,7 +575,7 @@ pub(crate) fn generic_predicates_for_param_query( | |||
569 | def: GenericDef, | 575 | def: GenericDef, |
570 | param_idx: u32, | 576 | param_idx: u32, |
571 | ) -> Arc<[GenericPredicate]> { | 577 | ) -> Arc<[GenericPredicate]> { |
572 | let resolver = def.resolver(db); | 578 | let resolver = GenericDefId::from(def).resolver(db); |
573 | resolver | 579 | resolver |
574 | .where_predicates_in_scope() | 580 | .where_predicates_in_scope() |
575 | // we have to filter out all other predicates *first*, before attempting to lower them | 581 | // we have to filter out all other predicates *first*, before attempting to lower them |
@@ -595,7 +601,7 @@ pub(crate) fn generic_predicates_query( | |||
595 | db: &impl HirDatabase, | 601 | db: &impl HirDatabase, |
596 | def: GenericDef, | 602 | def: GenericDef, |
597 | ) -> Arc<[GenericPredicate]> { | 603 | ) -> Arc<[GenericPredicate]> { |
598 | let resolver = def.resolver(db); | 604 | let resolver = GenericDefId::from(def).resolver(db); |
599 | resolver | 605 | resolver |
600 | .where_predicates_in_scope() | 606 | .where_predicates_in_scope() |
601 | .flat_map(|pred| GenericPredicate::from_where_predicate(db, &resolver, pred)) | 607 | .flat_map(|pred| GenericPredicate::from_where_predicate(db, &resolver, pred)) |
@@ -604,7 +610,7 @@ pub(crate) fn generic_predicates_query( | |||
604 | 610 | ||
605 | /// Resolve the default type params from generics | 611 | /// Resolve the default type params from generics |
606 | pub(crate) fn generic_defaults_query(db: &impl HirDatabase, def: GenericDef) -> Substs { | 612 | pub(crate) fn generic_defaults_query(db: &impl HirDatabase, def: GenericDef) -> Substs { |
607 | let resolver = def.resolver(db); | 613 | let resolver = GenericDefId::from(def).resolver(db); |
608 | let generic_params = def.generic_params(db); | 614 | let generic_params = def.generic_params(db); |
609 | 615 | ||
610 | let defaults = generic_params | 616 | let defaults = generic_params |
@@ -618,7 +624,7 @@ pub(crate) fn generic_defaults_query(db: &impl HirDatabase, def: GenericDef) -> | |||
618 | 624 | ||
619 | fn fn_sig_for_fn(db: &impl HirDatabase, def: Function) -> FnSig { | 625 | fn fn_sig_for_fn(db: &impl HirDatabase, def: Function) -> FnSig { |
620 | let data = def.data(db); | 626 | let data = def.data(db); |
621 | let resolver = def.resolver(db); | 627 | let resolver = def.id.resolver(db); |
622 | let params = data.params().iter().map(|tr| Ty::from_hir(db, &resolver, tr)).collect::<Vec<_>>(); | 628 | let params = data.params().iter().map(|tr| Ty::from_hir(db, &resolver, tr)).collect::<Vec<_>>(); |
623 | let ret = Ty::from_hir(db, &resolver, data.ret_type()); | 629 | let ret = Ty::from_hir(db, &resolver, data.ret_type()); |
624 | FnSig::from_params_and_return(params, ret) | 630 | FnSig::from_params_and_return(params, ret) |
@@ -635,7 +641,7 @@ fn type_for_fn(db: &impl HirDatabase, def: Function) -> Ty { | |||
635 | /// Build the declared type of a const. | 641 | /// Build the declared type of a const. |
636 | fn type_for_const(db: &impl HirDatabase, def: Const) -> Ty { | 642 | fn type_for_const(db: &impl HirDatabase, def: Const) -> Ty { |
637 | let data = def.data(db); | 643 | let data = def.data(db); |
638 | let resolver = def.resolver(db); | 644 | let resolver = def.id.resolver(db); |
639 | 645 | ||
640 | Ty::from_hir(db, &resolver, data.type_ref()) | 646 | Ty::from_hir(db, &resolver, data.type_ref()) |
641 | } | 647 | } |
@@ -643,7 +649,7 @@ fn type_for_const(db: &impl HirDatabase, def: Const) -> Ty { | |||
643 | /// Build the declared type of a static. | 649 | /// Build the declared type of a static. |
644 | fn type_for_static(db: &impl HirDatabase, def: Static) -> Ty { | 650 | fn type_for_static(db: &impl HirDatabase, def: Static) -> Ty { |
645 | let data = def.data(db); | 651 | let data = def.data(db); |
646 | let resolver = def.resolver(db); | 652 | let resolver = def.id.resolver(db); |
647 | 653 | ||
648 | Ty::from_hir(db, &resolver, data.type_ref()) | 654 | Ty::from_hir(db, &resolver, data.type_ref()) |
649 | } | 655 | } |
@@ -695,7 +701,7 @@ fn fn_sig_for_struct_constructor(db: &impl HirDatabase, def: Struct) -> FnSig { | |||
695 | Some(fields) => fields, | 701 | Some(fields) => fields, |
696 | None => panic!("fn_sig_for_struct_constructor called on unit struct"), | 702 | None => panic!("fn_sig_for_struct_constructor called on unit struct"), |
697 | }; | 703 | }; |
698 | let resolver = def.resolver(db); | 704 | let resolver = def.id.resolver(db); |
699 | let params = fields | 705 | let params = fields |
700 | .iter() | 706 | .iter() |
701 | .map(|(_, field)| Ty::from_hir(db, &resolver, &field.type_ref)) | 707 | .map(|(_, field)| Ty::from_hir(db, &resolver, &field.type_ref)) |
@@ -721,7 +727,7 @@ fn fn_sig_for_enum_variant_constructor(db: &impl HirDatabase, def: EnumVariant) | |||
721 | Some(fields) => fields, | 727 | Some(fields) => fields, |
722 | None => panic!("fn_sig_for_enum_variant_constructor called for unit variant"), | 728 | None => panic!("fn_sig_for_enum_variant_constructor called for unit variant"), |
723 | }; | 729 | }; |
724 | let resolver = def.parent_enum(db).resolver(db); | 730 | let resolver = def.parent.id.resolver(db); |
725 | let params = fields | 731 | let params = fields |
726 | .iter() | 732 | .iter() |
727 | .map(|(_, field)| Ty::from_hir(db, &resolver, &field.type_ref)) | 733 | .map(|(_, field)| Ty::from_hir(db, &resolver, &field.type_ref)) |
@@ -750,7 +756,7 @@ fn type_for_adt(db: &impl HirDatabase, adt: impl Into<Adt> + HasGenericParams) - | |||
750 | 756 | ||
751 | fn type_for_type_alias(db: &impl HirDatabase, t: TypeAlias) -> Ty { | 757 | fn type_for_type_alias(db: &impl HirDatabase, t: TypeAlias) -> Ty { |
752 | let generics = t.generic_params(db); | 758 | let generics = t.generic_params(db); |
753 | let resolver = t.resolver(db); | 759 | let resolver = t.id.resolver(db); |
754 | let type_ref = t.type_ref(db); | 760 | let type_ref = t.type_ref(db); |
755 | let substs = Substs::identity(&generics); | 761 | let substs = Substs::identity(&generics); |
756 | let inner = Ty::from_hir(db, &resolver, &type_ref.unwrap_or(TypeRef::Error)); | 762 | let inner = Ty::from_hir(db, &resolver, &type_ref.unwrap_or(TypeRef::Error)); |
diff --git a/crates/ra_hir/src/ty/method_resolution.rs b/crates/ra_hir/src/ty/method_resolution.rs index f377fca48..64adb814d 100644 --- a/crates/ra_hir/src/ty/method_resolution.rs +++ b/crates/ra_hir/src/ty/method_resolution.rs | |||
@@ -5,11 +5,11 @@ | |||
5 | use std::sync::Arc; | 5 | use std::sync::Arc; |
6 | 6 | ||
7 | use arrayvec::ArrayVec; | 7 | use arrayvec::ArrayVec; |
8 | use hir_def::resolver::Resolver; | ||
8 | use rustc_hash::FxHashMap; | 9 | use rustc_hash::FxHashMap; |
9 | 10 | ||
10 | use crate::{ | 11 | use crate::{ |
11 | db::HirDatabase, | 12 | db::HirDatabase, |
12 | resolve::Resolver, | ||
13 | ty::primitive::{FloatBitness, Uncertain}, | 13 | ty::primitive::{FloatBitness, Uncertain}, |
14 | ty::{Ty, TypeCtor}, | 14 | ty::{Ty, TypeCtor}, |
15 | AssocItem, Crate, Function, ImplBlock, Module, Mutability, Name, Trait, | 15 | AssocItem, Crate, Function, ImplBlock, Module, Mutability, Name, Trait, |
@@ -172,9 +172,14 @@ pub(crate) fn iterate_method_candidates<T>( | |||
172 | // rustc does an autoderef and then autoref again). | 172 | // rustc does an autoderef and then autoref again). |
173 | 173 | ||
174 | for derefed_ty in autoderef::autoderef(db, resolver, ty.clone()) { | 174 | for derefed_ty in autoderef::autoderef(db, resolver, ty.clone()) { |
175 | if let Some(result) = | 175 | if let Some(result) = iterate_inherent_methods( |
176 | iterate_inherent_methods(&derefed_ty, db, name, mode, krate, &mut callback) | 176 | &derefed_ty, |
177 | { | 177 | db, |
178 | name, | ||
179 | mode, | ||
180 | krate.into(), | ||
181 | &mut callback, | ||
182 | ) { | ||
178 | return Some(result); | 183 | return Some(result); |
179 | } | 184 | } |
180 | if let Some(result) = iterate_trait_method_candidates( | 185 | if let Some(result) = iterate_trait_method_candidates( |
@@ -192,7 +197,7 @@ pub(crate) fn iterate_method_candidates<T>( | |||
192 | LookupMode::Path => { | 197 | LookupMode::Path => { |
193 | // No autoderef for path lookups | 198 | // No autoderef for path lookups |
194 | if let Some(result) = | 199 | if let Some(result) = |
195 | iterate_inherent_methods(&ty, db, name, mode, krate, &mut callback) | 200 | iterate_inherent_methods(&ty, db, name, mode, krate.into(), &mut callback) |
196 | { | 201 | { |
197 | return Some(result); | 202 | return Some(result); |
198 | } | 203 | } |
@@ -224,7 +229,9 @@ fn iterate_trait_method_candidates<T>( | |||
224 | .trait_predicates_for_self_ty(&ty.value) | 229 | .trait_predicates_for_self_ty(&ty.value) |
225 | .map(|tr| tr.trait_) | 230 | .map(|tr| tr.trait_) |
226 | .flat_map(|t| t.all_super_traits(db)); | 231 | .flat_map(|t| t.all_super_traits(db)); |
227 | let traits = inherent_trait.chain(traits_from_env).chain(resolver.traits_in_scope(db)); | 232 | let traits = inherent_trait |
233 | .chain(traits_from_env) | ||
234 | .chain(resolver.traits_in_scope(db).into_iter().map(Trait::from)); | ||
228 | 'traits: for t in traits { | 235 | 'traits: for t in traits { |
229 | let data = t.trait_data(db); | 236 | let data = t.trait_data(db); |
230 | 237 | ||
@@ -238,7 +245,7 @@ fn iterate_trait_method_candidates<T>( | |||
238 | } | 245 | } |
239 | if !known_implemented { | 246 | if !known_implemented { |
240 | let goal = generic_implements_goal(db, env.clone(), t, ty.clone()); | 247 | let goal = generic_implements_goal(db, env.clone(), t, ty.clone()); |
241 | if db.trait_solve(krate, goal).is_none() { | 248 | if db.trait_solve(krate.into(), goal).is_none() { |
242 | continue 'traits; | 249 | continue 'traits; |
243 | } | 250 | } |
244 | } | 251 | } |