diff options
author | Aleksey Kladov <[email protected]> | 2020-03-13 15:05:46 +0000 |
---|---|---|
committer | Aleksey Kladov <[email protected]> | 2020-03-16 16:42:30 +0000 |
commit | 9faea2364dee4fbc9391ad233c570b70256ef002 (patch) | |
tree | 160af959553ce57fdfcbc0a6c79bafcc3611aeea /crates/ra_hir_ty/src/infer | |
parent | 648df02953a6ebf87a5876668eceba208687e8a7 (diff) |
Use `dyn Trait` for working with databse
It improves compile time in `--release` mode quite a bit, it doesn't
really slow things down and, conceptually, it seems closer to what we
want the physical architecture to look like (we don't want to
monomorphise EVERYTHING in a single leaf crate).
Diffstat (limited to 'crates/ra_hir_ty/src/infer')
-rw-r--r-- | crates/ra_hir_ty/src/infer/coerce.rs | 8 | ||||
-rw-r--r-- | crates/ra_hir_ty/src/infer/expr.rs | 23 | ||||
-rw-r--r-- | crates/ra_hir_ty/src/infer/pat.rs | 8 | ||||
-rw-r--r-- | crates/ra_hir_ty/src/infer/path.rs | 15 | ||||
-rw-r--r-- | crates/ra_hir_ty/src/infer/unify.rs | 18 |
5 files changed, 35 insertions, 37 deletions
diff --git a/crates/ra_hir_ty/src/infer/coerce.rs b/crates/ra_hir_ty/src/infer/coerce.rs index 95ac3c713..959b1e212 100644 --- a/crates/ra_hir_ty/src/infer/coerce.rs +++ b/crates/ra_hir_ty/src/infer/coerce.rs | |||
@@ -7,13 +7,11 @@ | |||
7 | use hir_def::{lang_item::LangItemTarget, type_ref::Mutability}; | 7 | use hir_def::{lang_item::LangItemTarget, type_ref::Mutability}; |
8 | use test_utils::tested_by; | 8 | use test_utils::tested_by; |
9 | 9 | ||
10 | use crate::{ | 10 | use crate::{autoderef, traits::Solution, Obligation, Substs, TraitRef, Ty, TypeCtor}; |
11 | autoderef, db::HirDatabase, traits::Solution, Obligation, Substs, TraitRef, Ty, TypeCtor, | ||
12 | }; | ||
13 | 11 | ||
14 | use super::{unify::TypeVarValue, InEnvironment, InferTy, InferenceContext}; | 12 | use super::{unify::TypeVarValue, InEnvironment, InferTy, InferenceContext}; |
15 | 13 | ||
16 | impl<'a, D: HirDatabase> InferenceContext<'a, D> { | 14 | impl<'a> InferenceContext<'a> { |
17 | /// Unify two types, but may coerce the first one to the second one | 15 | /// Unify two types, but may coerce the first one to the second one |
18 | /// using "implicit coercion rules" if needed. | 16 | /// using "implicit coercion rules" if needed. |
19 | pub(super) fn coerce(&mut self, from_ty: &Ty, to_ty: &Ty) -> bool { | 17 | pub(super) fn coerce(&mut self, from_ty: &Ty, to_ty: &Ty) -> bool { |
@@ -126,7 +124,7 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> { | |||
126 | _ => return None, | 124 | _ => return None, |
127 | }; | 125 | }; |
128 | 126 | ||
129 | let generic_params = crate::utils::generics(self.db, coerce_unsized_trait.into()); | 127 | let generic_params = crate::utils::generics(self.db.upcast(), coerce_unsized_trait.into()); |
130 | if generic_params.len() != 2 { | 128 | if generic_params.len() != 2 { |
131 | // The CoerceUnsized trait should have two generic params: Self and T. | 129 | // The CoerceUnsized trait should have two generic params: Self and T. |
132 | return None; | 130 | return None; |
diff --git a/crates/ra_hir_ty/src/infer/expr.rs b/crates/ra_hir_ty/src/infer/expr.rs index e89cc7298..1fdb235a0 100644 --- a/crates/ra_hir_ty/src/infer/expr.rs +++ b/crates/ra_hir_ty/src/infer/expr.rs | |||
@@ -14,9 +14,7 @@ use hir_expand::name::Name; | |||
14 | use ra_syntax::ast::RangeOp; | 14 | use ra_syntax::ast::RangeOp; |
15 | 15 | ||
16 | use crate::{ | 16 | use crate::{ |
17 | autoderef, | 17 | autoderef, method_resolution, op, |
18 | db::HirDatabase, | ||
19 | method_resolution, op, | ||
20 | traits::InEnvironment, | 18 | traits::InEnvironment, |
21 | utils::{generics, variant_data, Generics}, | 19 | utils::{generics, variant_data, Generics}, |
22 | ApplicationTy, Binders, CallableDef, InferTy, IntTy, Mutability, Obligation, Substs, TraitRef, | 20 | ApplicationTy, Binders, CallableDef, InferTy, IntTy, Mutability, Obligation, Substs, TraitRef, |
@@ -25,7 +23,7 @@ use crate::{ | |||
25 | 23 | ||
26 | use super::{BindingMode, Expectation, InferenceContext, InferenceDiagnostic, TypeMismatch}; | 24 | use super::{BindingMode, Expectation, InferenceContext, InferenceDiagnostic, TypeMismatch}; |
27 | 25 | ||
28 | impl<'a, D: HirDatabase> InferenceContext<'a, D> { | 26 | impl<'a> InferenceContext<'a> { |
29 | pub(super) fn infer_expr(&mut self, tgt_expr: ExprId, expected: &Expectation) -> Ty { | 27 | pub(super) fn infer_expr(&mut self, tgt_expr: ExprId, expected: &Expectation) -> Ty { |
30 | let ty = self.infer_expr_inner(tgt_expr, expected); | 28 | let ty = self.infer_expr_inner(tgt_expr, expected); |
31 | let could_unify = self.unify(&ty, &expected.ty); | 29 | let could_unify = self.unify(&ty, &expected.ty); |
@@ -184,7 +182,7 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> { | |||
184 | } | 182 | } |
185 | Expr::Path(p) => { | 183 | Expr::Path(p) => { |
186 | // FIXME this could be more efficient... | 184 | // FIXME this could be more efficient... |
187 | let resolver = resolver_for_expr(self.db, self.owner, tgt_expr); | 185 | let resolver = resolver_for_expr(self.db.upcast(), self.owner, tgt_expr); |
188 | self.infer_path(&resolver, p, tgt_expr.into()).unwrap_or(Ty::Unknown) | 186 | self.infer_path(&resolver, p, tgt_expr.into()).unwrap_or(Ty::Unknown) |
189 | } | 187 | } |
190 | Expr::Continue => Ty::simple(TypeCtor::Never), | 188 | Expr::Continue => Ty::simple(TypeCtor::Never), |
@@ -214,7 +212,7 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> { | |||
214 | 212 | ||
215 | let substs = ty.substs().unwrap_or_else(Substs::empty); | 213 | let substs = ty.substs().unwrap_or_else(Substs::empty); |
216 | let field_types = def_id.map(|it| self.db.field_types(it)).unwrap_or_default(); | 214 | let field_types = def_id.map(|it| self.db.field_types(it)).unwrap_or_default(); |
217 | let variant_data = def_id.map(|it| variant_data(self.db, it)); | 215 | let variant_data = def_id.map(|it| variant_data(self.db.upcast(), it)); |
218 | for (field_idx, field) in fields.iter().enumerate() { | 216 | for (field_idx, field) in fields.iter().enumerate() { |
219 | let field_def = | 217 | let field_def = |
220 | variant_data.as_ref().and_then(|it| match it.field(&field.name) { | 218 | variant_data.as_ref().and_then(|it| match it.field(&field.name) { |
@@ -579,7 +577,7 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> { | |||
579 | let receiver_ty = self.infer_expr(receiver, &Expectation::none()); | 577 | let receiver_ty = self.infer_expr(receiver, &Expectation::none()); |
580 | let canonicalized_receiver = self.canonicalizer().canonicalize_ty(receiver_ty.clone()); | 578 | let canonicalized_receiver = self.canonicalizer().canonicalize_ty(receiver_ty.clone()); |
581 | 579 | ||
582 | let traits_in_scope = self.resolver.traits_in_scope(self.db); | 580 | let traits_in_scope = self.resolver.traits_in_scope(self.db.upcast()); |
583 | 581 | ||
584 | let resolved = self.resolver.krate().and_then(|krate| { | 582 | let resolved = self.resolver.krate().and_then(|krate| { |
585 | method_resolution::lookup_method( | 583 | method_resolution::lookup_method( |
@@ -595,7 +593,7 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> { | |||
595 | Some((ty, func)) => { | 593 | Some((ty, func)) => { |
596 | let ty = canonicalized_receiver.decanonicalize_ty(ty); | 594 | let ty = canonicalized_receiver.decanonicalize_ty(ty); |
597 | self.write_method_resolution(tgt_expr, func); | 595 | self.write_method_resolution(tgt_expr, func); |
598 | (ty, self.db.value_ty(func.into()), Some(generics(self.db, func.into()))) | 596 | (ty, self.db.value_ty(func.into()), Some(generics(self.db.upcast(), func.into()))) |
599 | } | 597 | } |
600 | None => (receiver_ty, Binders::new(0, Ty::Unknown), None), | 598 | None => (receiver_ty, Binders::new(0, Ty::Unknown), None), |
601 | }; | 599 | }; |
@@ -703,10 +701,13 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> { | |||
703 | // add obligation for trait implementation, if this is a trait method | 701 | // add obligation for trait implementation, if this is a trait method |
704 | match def { | 702 | match def { |
705 | CallableDef::FunctionId(f) => { | 703 | CallableDef::FunctionId(f) => { |
706 | if let AssocContainerId::TraitId(trait_) = f.lookup(self.db).container { | 704 | if let AssocContainerId::TraitId(trait_) = |
705 | f.lookup(self.db.upcast()).container | ||
706 | { | ||
707 | // construct a TraitDef | 707 | // construct a TraitDef |
708 | let substs = | 708 | let substs = a_ty |
709 | a_ty.parameters.prefix(generics(self.db, trait_.into()).len()); | 709 | .parameters |
710 | .prefix(generics(self.db.upcast(), trait_.into()).len()); | ||
710 | self.obligations.push(Obligation::Trait(TraitRef { trait_, substs })); | 711 | self.obligations.push(Obligation::Trait(TraitRef { trait_, substs })); |
711 | } | 712 | } |
712 | } | 713 | } |
diff --git a/crates/ra_hir_ty/src/infer/pat.rs b/crates/ra_hir_ty/src/infer/pat.rs index 7a84e47f8..baed6225b 100644 --- a/crates/ra_hir_ty/src/infer/pat.rs +++ b/crates/ra_hir_ty/src/infer/pat.rs | |||
@@ -12,9 +12,9 @@ use hir_expand::name::Name; | |||
12 | use test_utils::tested_by; | 12 | use test_utils::tested_by; |
13 | 13 | ||
14 | use super::{BindingMode, InferenceContext}; | 14 | use super::{BindingMode, InferenceContext}; |
15 | use crate::{db::HirDatabase, utils::variant_data, Substs, Ty, TypeCtor}; | 15 | use crate::{utils::variant_data, Substs, Ty, TypeCtor}; |
16 | 16 | ||
17 | impl<'a, D: HirDatabase> InferenceContext<'a, D> { | 17 | impl<'a> InferenceContext<'a> { |
18 | fn infer_tuple_struct_pat( | 18 | fn infer_tuple_struct_pat( |
19 | &mut self, | 19 | &mut self, |
20 | path: Option<&Path>, | 20 | path: Option<&Path>, |
@@ -23,7 +23,7 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> { | |||
23 | default_bm: BindingMode, | 23 | default_bm: BindingMode, |
24 | ) -> Ty { | 24 | ) -> Ty { |
25 | let (ty, def) = self.resolve_variant(path); | 25 | let (ty, def) = self.resolve_variant(path); |
26 | let var_data = def.map(|it| variant_data(self.db, it)); | 26 | let var_data = def.map(|it| variant_data(self.db.upcast(), it)); |
27 | self.unify(&ty, expected); | 27 | self.unify(&ty, expected); |
28 | 28 | ||
29 | let substs = ty.substs().unwrap_or_else(Substs::empty); | 29 | let substs = ty.substs().unwrap_or_else(Substs::empty); |
@@ -51,7 +51,7 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> { | |||
51 | id: PatId, | 51 | id: PatId, |
52 | ) -> Ty { | 52 | ) -> Ty { |
53 | let (ty, def) = self.resolve_variant(path); | 53 | let (ty, def) = self.resolve_variant(path); |
54 | let var_data = def.map(|it| variant_data(self.db, it)); | 54 | let var_data = def.map(|it| variant_data(self.db.upcast(), it)); |
55 | if let Some(variant) = def { | 55 | if let Some(variant) = def { |
56 | self.write_variant_resolution(id.into(), variant); | 56 | self.write_variant_resolution(id.into(), variant); |
57 | } | 57 | } |
diff --git a/crates/ra_hir_ty/src/infer/path.rs b/crates/ra_hir_ty/src/infer/path.rs index c733b9e1d..318652c61 100644 --- a/crates/ra_hir_ty/src/infer/path.rs +++ b/crates/ra_hir_ty/src/infer/path.rs | |||
@@ -9,11 +9,11 @@ use hir_def::{ | |||
9 | }; | 9 | }; |
10 | use hir_expand::name::Name; | 10 | use hir_expand::name::Name; |
11 | 11 | ||
12 | use crate::{db::HirDatabase, method_resolution, Substs, Ty, ValueTyDefId}; | 12 | use crate::{method_resolution, Substs, Ty, ValueTyDefId}; |
13 | 13 | ||
14 | use super::{ExprOrPatId, InferenceContext, TraitRef}; | 14 | use super::{ExprOrPatId, InferenceContext, TraitRef}; |
15 | 15 | ||
16 | impl<'a, D: HirDatabase> InferenceContext<'a, D> { | 16 | impl<'a> InferenceContext<'a> { |
17 | pub(super) fn infer_path( | 17 | pub(super) fn infer_path( |
18 | &mut self, | 18 | &mut self, |
19 | resolver: &Resolver, | 19 | resolver: &Resolver, |
@@ -47,7 +47,8 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> { | |||
47 | id, | 47 | id, |
48 | )? | 48 | )? |
49 | } else { | 49 | } else { |
50 | let value_or_partial = resolver.resolve_path_in_value_ns(self.db, path.mod_path())?; | 50 | let value_or_partial = |
51 | resolver.resolve_path_in_value_ns(self.db.upcast(), path.mod_path())?; | ||
51 | 52 | ||
52 | match value_or_partial { | 53 | match value_or_partial { |
53 | ResolveValueResult::ValueNs(it) => (it, None), | 54 | ResolveValueResult::ValueNs(it) => (it, None), |
@@ -192,7 +193,7 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> { | |||
192 | 193 | ||
193 | let canonical_ty = self.canonicalizer().canonicalize_ty(ty.clone()); | 194 | let canonical_ty = self.canonicalizer().canonicalize_ty(ty.clone()); |
194 | let krate = self.resolver.krate()?; | 195 | let krate = self.resolver.krate()?; |
195 | let traits_in_scope = self.resolver.traits_in_scope(self.db); | 196 | let traits_in_scope = self.resolver.traits_in_scope(self.db.upcast()); |
196 | 197 | ||
197 | method_resolution::iterate_method_candidates( | 198 | method_resolution::iterate_method_candidates( |
198 | &canonical_ty.value, | 199 | &canonical_ty.value, |
@@ -205,9 +206,11 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> { | |||
205 | move |_ty, item| { | 206 | move |_ty, item| { |
206 | let (def, container) = match item { | 207 | let (def, container) = match item { |
207 | AssocItemId::FunctionId(f) => { | 208 | AssocItemId::FunctionId(f) => { |
208 | (ValueNs::FunctionId(f), f.lookup(self.db).container) | 209 | (ValueNs::FunctionId(f), f.lookup(self.db.upcast()).container) |
210 | } | ||
211 | AssocItemId::ConstId(c) => { | ||
212 | (ValueNs::ConstId(c), c.lookup(self.db.upcast()).container) | ||
209 | } | 213 | } |
210 | AssocItemId::ConstId(c) => (ValueNs::ConstId(c), c.lookup(self.db).container), | ||
211 | AssocItemId::TypeAliasId(_) => unreachable!(), | 214 | AssocItemId::TypeAliasId(_) => unreachable!(), |
212 | }; | 215 | }; |
213 | let substs = match container { | 216 | let substs = match container { |
diff --git a/crates/ra_hir_ty/src/infer/unify.rs b/crates/ra_hir_ty/src/infer/unify.rs index 82b85d570..0bf8fbd63 100644 --- a/crates/ra_hir_ty/src/infer/unify.rs +++ b/crates/ra_hir_ty/src/infer/unify.rs | |||
@@ -7,10 +7,10 @@ use ena::unify::{InPlaceUnificationTable, NoError, UnifyKey, UnifyValue}; | |||
7 | use test_utils::tested_by; | 7 | use test_utils::tested_by; |
8 | 8 | ||
9 | use super::{InferenceContext, Obligation}; | 9 | use super::{InferenceContext, Obligation}; |
10 | use crate::{db::HirDatabase, Canonical, InEnvironment, InferTy, Substs, Ty, TypeCtor, TypeWalk}; | 10 | use crate::{Canonical, InEnvironment, InferTy, Substs, Ty, TypeCtor, TypeWalk}; |
11 | 11 | ||
12 | impl<'a, D: HirDatabase> InferenceContext<'a, D> { | 12 | impl<'a> InferenceContext<'a> { |
13 | pub(super) fn canonicalizer<'b>(&'b mut self) -> Canonicalizer<'a, 'b, D> | 13 | pub(super) fn canonicalizer<'b>(&'b mut self) -> Canonicalizer<'a, 'b> |
14 | where | 14 | where |
15 | 'a: 'b, | 15 | 'a: 'b, |
16 | { | 16 | { |
@@ -18,11 +18,11 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> { | |||
18 | } | 18 | } |
19 | } | 19 | } |
20 | 20 | ||
21 | pub(super) struct Canonicalizer<'a, 'b, D: HirDatabase> | 21 | pub(super) struct Canonicalizer<'a, 'b> |
22 | where | 22 | where |
23 | 'a: 'b, | 23 | 'a: 'b, |
24 | { | 24 | { |
25 | ctx: &'b mut InferenceContext<'a, D>, | 25 | ctx: &'b mut InferenceContext<'a>, |
26 | free_vars: Vec<InferTy>, | 26 | free_vars: Vec<InferTy>, |
27 | /// A stack of type variables that is used to detect recursive types (which | 27 | /// A stack of type variables that is used to detect recursive types (which |
28 | /// are an error, but we need to protect against them to avoid stack | 28 | /// are an error, but we need to protect against them to avoid stack |
@@ -35,7 +35,7 @@ pub(super) struct Canonicalized<T> { | |||
35 | free_vars: Vec<InferTy>, | 35 | free_vars: Vec<InferTy>, |
36 | } | 36 | } |
37 | 37 | ||
38 | impl<'a, 'b, D: HirDatabase> Canonicalizer<'a, 'b, D> | 38 | impl<'a, 'b> Canonicalizer<'a, 'b> |
39 | where | 39 | where |
40 | 'a: 'b, | 40 | 'a: 'b, |
41 | { | 41 | { |
@@ -123,11 +123,7 @@ impl<T> Canonicalized<T> { | |||
123 | ty | 123 | ty |
124 | } | 124 | } |
125 | 125 | ||
126 | pub fn apply_solution( | 126 | pub fn apply_solution(&self, ctx: &mut InferenceContext<'_>, solution: Canonical<Vec<Ty>>) { |
127 | &self, | ||
128 | ctx: &mut InferenceContext<'_, impl HirDatabase>, | ||
129 | solution: Canonical<Vec<Ty>>, | ||
130 | ) { | ||
131 | // the solution may contain new variables, which we need to convert to new inference vars | 127 | // the solution may contain new variables, which we need to convert to new inference vars |
132 | let new_vars = Substs((0..solution.num_vars).map(|_| ctx.table.new_type_var()).collect()); | 128 | let new_vars = Substs((0..solution.num_vars).map(|_| ctx.table.new_type_var()).collect()); |
133 | for (i, ty) in solution.value.into_iter().enumerate() { | 129 | for (i, ty) in solution.value.into_iter().enumerate() { |