diff options
Diffstat (limited to 'crates/ra_hir/src/ty')
-rw-r--r-- | crates/ra_hir/src/ty/infer.rs | 22 | ||||
-rw-r--r-- | crates/ra_hir/src/ty/lower.rs | 4 |
2 files changed, 14 insertions, 12 deletions
diff --git a/crates/ra_hir/src/ty/infer.rs b/crates/ra_hir/src/ty/infer.rs index 735cdecb9..69af8fb92 100644 --- a/crates/ra_hir/src/ty/infer.rs +++ b/crates/ra_hir/src/ty/infer.rs | |||
@@ -38,7 +38,7 @@ use crate::{ | |||
38 | resolve::{Resolver, Resolution}, | 38 | resolve::{Resolver, Resolution}, |
39 | nameres::Namespace | 39 | nameres::Namespace |
40 | }; | 40 | }; |
41 | use super::{Ty, TypableDef, Substs, primitive, op}; | 41 | use super::{Ty, TypableDef, Substs, primitive, op, FnSig}; |
42 | 42 | ||
43 | /// The entry point of type inference. | 43 | /// The entry point of type inference. |
44 | pub fn infer(db: &impl HirDatabase, func: Function) -> Arc<InferenceResult> { | 44 | pub fn infer(db: &impl HirDatabase, func: Function) -> Arc<InferenceResult> { |
@@ -257,10 +257,8 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> { | |||
257 | self.unify_inner(t1, t2, depth + 1) | 257 | self.unify_inner(t1, t2, depth + 1) |
258 | } | 258 | } |
259 | (Ty::Ref(t1, m1), Ty::Ref(t2, m2)) if m1 == m2 => self.unify_inner(t1, t2, depth + 1), | 259 | (Ty::Ref(t1, m1), Ty::Ref(t2, m2)) if m1 == m2 => self.unify_inner(t1, t2, depth + 1), |
260 | (Ty::FnPtr(sig1), Ty::FnPtr(sig2)) if sig1 == sig2 => true, | 260 | (Ty::FnPtr(sig1), Ty::FnPtr(sig2)) => self.unify_substs(sig1, sig2, depth + 1), |
261 | (Ty::Tuple(ts1), Ty::Tuple(ts2)) if ts1.len() == ts2.len() => { | 261 | (Ty::Tuple(ts1), Ty::Tuple(ts2)) => self.unify_substs(ts1, ts2, depth + 1), |
262 | ts1.iter().zip(ts2.iter()).all(|(t1, t2)| self.unify_inner(t1, t2, depth + 1)) | ||
263 | } | ||
264 | (Ty::Infer(InferTy::TypeVar(tv1)), Ty::Infer(InferTy::TypeVar(tv2))) | 262 | (Ty::Infer(InferTy::TypeVar(tv1)), Ty::Infer(InferTy::TypeVar(tv2))) |
265 | | (Ty::Infer(InferTy::IntVar(tv1)), Ty::Infer(InferTy::IntVar(tv2))) | 263 | | (Ty::Infer(InferTy::IntVar(tv1)), Ty::Infer(InferTy::IntVar(tv2))) |
266 | | (Ty::Infer(InferTy::FloatVar(tv1)), Ty::Infer(InferTy::FloatVar(tv2))) => { | 264 | | (Ty::Infer(InferTy::FloatVar(tv1)), Ty::Infer(InferTy::FloatVar(tv2))) => { |
@@ -632,7 +630,7 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> { | |||
632 | let ty = match &body[pat] { | 630 | let ty = match &body[pat] { |
633 | Pat::Tuple(ref args) => { | 631 | Pat::Tuple(ref args) => { |
634 | let expectations = match *expected { | 632 | let expectations = match *expected { |
635 | Ty::Tuple(ref tuple_args) => &**tuple_args, | 633 | Ty::Tuple(ref tuple_args) => &*tuple_args.0, |
636 | _ => &[], | 634 | _ => &[], |
637 | }; | 635 | }; |
638 | let expectations_iter = expectations.iter().chain(repeat(&Ty::Unknown)); | 636 | let expectations_iter = expectations.iter().chain(repeat(&Ty::Unknown)); |
@@ -644,7 +642,7 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> { | |||
644 | .collect::<Vec<_>>() | 642 | .collect::<Vec<_>>() |
645 | .into(); | 643 | .into(); |
646 | 644 | ||
647 | Ty::Tuple(inner_tys) | 645 | Ty::Tuple(Substs(inner_tys)) |
648 | } | 646 | } |
649 | Pat::Ref { pat, mutability } => { | 647 | Pat::Ref { pat, mutability } => { |
650 | let expectation = match *expected { | 648 | let expectation = match *expected { |
@@ -789,7 +787,10 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> { | |||
789 | Expr::Call { callee, args } => { | 787 | Expr::Call { callee, args } => { |
790 | let callee_ty = self.infer_expr(*callee, &Expectation::none()); | 788 | let callee_ty = self.infer_expr(*callee, &Expectation::none()); |
791 | let (param_tys, ret_ty) = match &callee_ty { | 789 | let (param_tys, ret_ty) = match &callee_ty { |
792 | Ty::FnPtr(sig) => (sig.params().to_vec(), sig.ret().clone()), | 790 | Ty::FnPtr(sig) => { |
791 | let sig = FnSig::from_fn_ptr_substs(sig); | ||
792 | (sig.params().to_vec(), sig.ret().clone()) | ||
793 | } | ||
793 | Ty::FnDef { substs, def, .. } => { | 794 | Ty::FnDef { substs, def, .. } => { |
794 | let sig = self.db.callable_item_signature(*def); | 795 | let sig = self.db.callable_item_signature(*def); |
795 | let ret_ty = sig.ret().clone().subst(&substs); | 796 | let ret_ty = sig.ret().clone().subst(&substs); |
@@ -828,6 +829,7 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> { | |||
828 | let method_ty = self.insert_type_vars(method_ty); | 829 | let method_ty = self.insert_type_vars(method_ty); |
829 | let (expected_receiver_ty, param_tys, ret_ty) = match &method_ty { | 830 | let (expected_receiver_ty, param_tys, ret_ty) = match &method_ty { |
830 | Ty::FnPtr(sig) => { | 831 | Ty::FnPtr(sig) => { |
832 | let sig = FnSig::from_fn_ptr_substs(sig); | ||
831 | if !sig.params().is_empty() { | 833 | if !sig.params().is_empty() { |
832 | (sig.params()[0].clone(), sig.params()[1..].to_vec(), sig.ret().clone()) | 834 | (sig.params()[0].clone(), sig.params()[1..].to_vec(), sig.ret().clone()) |
833 | } else { | 835 | } else { |
@@ -923,7 +925,7 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> { | |||
923 | .find_map(|derefed_ty| match derefed_ty { | 925 | .find_map(|derefed_ty| match derefed_ty { |
924 | Ty::Tuple(fields) => { | 926 | Ty::Tuple(fields) => { |
925 | let i = name.to_string().parse::<usize>().ok(); | 927 | let i = name.to_string().parse::<usize>().ok(); |
926 | i.and_then(|i| fields.get(i).cloned()) | 928 | i.and_then(|i| fields.0.get(i).cloned()) |
927 | } | 929 | } |
928 | Ty::Adt { def_id: AdtDef::Struct(s), ref substs, .. } => { | 930 | Ty::Adt { def_id: AdtDef::Struct(s), ref substs, .. } => { |
929 | s.field(self.db, name).map(|field| { | 931 | s.field(self.db, name).map(|field| { |
@@ -1016,7 +1018,7 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> { | |||
1016 | ty_vec.push(self.infer_expr(*arg, &Expectation::none())); | 1018 | ty_vec.push(self.infer_expr(*arg, &Expectation::none())); |
1017 | } | 1019 | } |
1018 | 1020 | ||
1019 | Ty::Tuple(Arc::from(ty_vec)) | 1021 | Ty::Tuple(Substs(ty_vec.into())) |
1020 | } | 1022 | } |
1021 | Expr::Array { exprs } => { | 1023 | Expr::Array { exprs } => { |
1022 | let elem_ty = match &expected.ty { | 1024 | let elem_ty = match &expected.ty { |
diff --git a/crates/ra_hir/src/ty/lower.rs b/crates/ra_hir/src/ty/lower.rs index 389a2fc68..20e6273a6 100644 --- a/crates/ra_hir/src/ty/lower.rs +++ b/crates/ra_hir/src/ty/lower.rs | |||
@@ -30,7 +30,7 @@ impl Ty { | |||
30 | TypeRef::Tuple(inner) => { | 30 | TypeRef::Tuple(inner) => { |
31 | let inner_tys = | 31 | let inner_tys = |
32 | inner.iter().map(|tr| Ty::from_hir(db, resolver, tr)).collect::<Vec<_>>(); | 32 | inner.iter().map(|tr| Ty::from_hir(db, resolver, tr)).collect::<Vec<_>>(); |
33 | Ty::Tuple(inner_tys.into()) | 33 | Ty::Tuple(Substs(inner_tys.into())) |
34 | } | 34 | } |
35 | TypeRef::Path(path) => Ty::from_hir_path(db, resolver, path), | 35 | TypeRef::Path(path) => Ty::from_hir_path(db, resolver, path), |
36 | TypeRef::RawPtr(inner, mutability) => { | 36 | TypeRef::RawPtr(inner, mutability) => { |
@@ -53,7 +53,7 @@ impl Ty { | |||
53 | TypeRef::Fn(params) => { | 53 | TypeRef::Fn(params) => { |
54 | let inner_tys = | 54 | let inner_tys = |
55 | params.iter().map(|tr| Ty::from_hir(db, resolver, tr)).collect::<Vec<_>>(); | 55 | params.iter().map(|tr| Ty::from_hir(db, resolver, tr)).collect::<Vec<_>>(); |
56 | let sig = FnSig { params_and_return: inner_tys.into() }; | 56 | let sig = Substs(inner_tys.into()); |
57 | Ty::FnPtr(sig) | 57 | Ty::FnPtr(sig) |
58 | } | 58 | } |
59 | TypeRef::Error => Ty::Unknown, | 59 | TypeRef::Error => Ty::Unknown, |