diff options
Diffstat (limited to 'crates/ra_hir/src/ty/infer.rs')
-rw-r--r-- | crates/ra_hir/src/ty/infer.rs | 264 |
1 files changed, 146 insertions, 118 deletions
diff --git a/crates/ra_hir/src/ty/infer.rs b/crates/ra_hir/src/ty/infer.rs index 69af8fb92..0dafd8cbd 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, FnSig}; | 41 | use super::{Ty, TypableDef, Substs, primitive, op, FnSig, ApplicationTy, TypeName}; |
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> { |
@@ -237,28 +237,9 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> { | |||
237 | match (&*ty1, &*ty2) { | 237 | match (&*ty1, &*ty2) { |
238 | (Ty::Unknown, ..) => true, | 238 | (Ty::Unknown, ..) => true, |
239 | (.., Ty::Unknown) => true, | 239 | (.., Ty::Unknown) => true, |
240 | (Ty::Int(t1), Ty::Int(t2)) => match (t1, t2) { | 240 | (Ty::Apply(a_ty1), Ty::Apply(a_ty2)) if a_ty1.name == a_ty2.name => { |
241 | (primitive::UncertainIntTy::Unknown, _) | 241 | self.unify_substs(&a_ty1.parameters, &a_ty2.parameters, depth + 1) |
242 | | (_, primitive::UncertainIntTy::Unknown) => true, | ||
243 | _ => t1 == t2, | ||
244 | }, | ||
245 | (Ty::Float(t1), Ty::Float(t2)) => match (t1, t2) { | ||
246 | (primitive::UncertainFloatTy::Unknown, _) | ||
247 | | (_, primitive::UncertainFloatTy::Unknown) => true, | ||
248 | _ => t1 == t2, | ||
249 | }, | ||
250 | (Ty::Bool, _) | (Ty::Str, _) | (Ty::Never, _) | (Ty::Char, _) => ty1 == ty2, | ||
251 | ( | ||
252 | Ty::Adt { def_id: def_id1, substs: substs1, .. }, | ||
253 | Ty::Adt { def_id: def_id2, substs: substs2, .. }, | ||
254 | ) if def_id1 == def_id2 => self.unify_substs(substs1, substs2, depth + 1), | ||
255 | (Ty::Slice(t1), Ty::Slice(t2)) => self.unify_inner(t1, t2, depth + 1), | ||
256 | (Ty::RawPtr(t1, m1), Ty::RawPtr(t2, m2)) if m1 == m2 => { | ||
257 | self.unify_inner(t1, t2, depth + 1) | ||
258 | } | 242 | } |
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)) => self.unify_substs(sig1, sig2, depth + 1), | ||
261 | (Ty::Tuple(ts1), Ty::Tuple(ts2)) => self.unify_substs(ts1, ts2, depth + 1), | ||
262 | (Ty::Infer(InferTy::TypeVar(tv1)), Ty::Infer(InferTy::TypeVar(tv2))) | 243 | (Ty::Infer(InferTy::TypeVar(tv1)), Ty::Infer(InferTy::TypeVar(tv2))) |
263 | | (Ty::Infer(InferTy::IntVar(tv1)), Ty::Infer(InferTy::IntVar(tv2))) | 244 | | (Ty::Infer(InferTy::IntVar(tv1)), Ty::Infer(InferTy::IntVar(tv2))) |
264 | | (Ty::Infer(InferTy::FloatVar(tv1)), Ty::Infer(InferTy::FloatVar(tv2))) => { | 245 | | (Ty::Infer(InferTy::FloatVar(tv1)), Ty::Infer(InferTy::FloatVar(tv2))) => { |
@@ -296,8 +277,14 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> { | |||
296 | fn insert_type_vars_shallow(&mut self, ty: Ty) -> Ty { | 277 | fn insert_type_vars_shallow(&mut self, ty: Ty) -> Ty { |
297 | match ty { | 278 | match ty { |
298 | Ty::Unknown => self.new_type_var(), | 279 | Ty::Unknown => self.new_type_var(), |
299 | Ty::Int(primitive::UncertainIntTy::Unknown) => self.new_integer_var(), | 280 | Ty::Apply(ApplicationTy { |
300 | Ty::Float(primitive::UncertainFloatTy::Unknown) => self.new_float_var(), | 281 | name: TypeName::Int(primitive::UncertainIntTy::Unknown), |
282 | .. | ||
283 | }) => self.new_integer_var(), | ||
284 | Ty::Apply(ApplicationTy { | ||
285 | name: TypeName::Float(primitive::UncertainFloatTy::Unknown), | ||
286 | .. | ||
287 | }) => self.new_float_var(), | ||
301 | _ => ty, | 288 | _ => ty, |
302 | } | 289 | } |
303 | } | 290 | } |
@@ -608,12 +595,12 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> { | |||
608 | Pat::Wild | Pat::Bind { .. } | Pat::Ref { .. } | Pat::Missing => false, | 595 | Pat::Wild | Pat::Bind { .. } | Pat::Ref { .. } | Pat::Missing => false, |
609 | }; | 596 | }; |
610 | if is_non_ref_pat { | 597 | if is_non_ref_pat { |
611 | while let Ty::Ref(inner, mutability) = expected { | 598 | while let Some((inner, mutability)) = expected.as_reference() { |
612 | expected = inner; | 599 | expected = inner; |
613 | default_bm = match default_bm { | 600 | default_bm = match default_bm { |
614 | BindingMode::Move => BindingMode::Ref(*mutability), | 601 | BindingMode::Move => BindingMode::Ref(mutability), |
615 | BindingMode::Ref(Mutability::Shared) => BindingMode::Ref(Mutability::Shared), | 602 | BindingMode::Ref(Mutability::Shared) => BindingMode::Ref(Mutability::Shared), |
616 | BindingMode::Ref(Mutability::Mut) => BindingMode::Ref(*mutability), | 603 | BindingMode::Ref(Mutability::Mut) => BindingMode::Ref(mutability), |
617 | } | 604 | } |
618 | } | 605 | } |
619 | } else if let Pat::Ref { .. } = &body[pat] { | 606 | } else if let Pat::Ref { .. } = &body[pat] { |
@@ -629,8 +616,8 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> { | |||
629 | 616 | ||
630 | let ty = match &body[pat] { | 617 | let ty = match &body[pat] { |
631 | Pat::Tuple(ref args) => { | 618 | Pat::Tuple(ref args) => { |
632 | let expectations = match *expected { | 619 | let expectations = match expected.as_tuple() { |
633 | Ty::Tuple(ref tuple_args) => &*tuple_args.0, | 620 | Some(parameters) => &*parameters.0, |
634 | _ => &[], | 621 | _ => &[], |
635 | }; | 622 | }; |
636 | let expectations_iter = expectations.iter().chain(repeat(&Ty::Unknown)); | 623 | let expectations_iter = expectations.iter().chain(repeat(&Ty::Unknown)); |
@@ -642,20 +629,20 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> { | |||
642 | .collect::<Vec<_>>() | 629 | .collect::<Vec<_>>() |
643 | .into(); | 630 | .into(); |
644 | 631 | ||
645 | Ty::Tuple(Substs(inner_tys)) | 632 | Ty::apply(TypeName::Tuple, Substs(inner_tys)) |
646 | } | 633 | } |
647 | Pat::Ref { pat, mutability } => { | 634 | Pat::Ref { pat, mutability } => { |
648 | let expectation = match *expected { | 635 | let expectation = match expected.as_reference() { |
649 | Ty::Ref(ref sub_ty, exp_mut) => { | 636 | Some((inner_ty, exp_mut)) => { |
650 | if *mutability != exp_mut { | 637 | if *mutability != exp_mut { |
651 | // TODO: emit type error? | 638 | // TODO: emit type error? |
652 | } | 639 | } |
653 | &**sub_ty | 640 | inner_ty |
654 | } | 641 | } |
655 | _ => &Ty::Unknown, | 642 | _ => &Ty::Unknown, |
656 | }; | 643 | }; |
657 | let subty = self.infer_pat(*pat, expectation, default_bm); | 644 | let subty = self.infer_pat(*pat, expectation, default_bm); |
658 | Ty::Ref(subty.into(), *mutability) | 645 | Ty::apply_one(TypeName::Ref(*mutability), subty.into()) |
659 | } | 646 | } |
660 | Pat::TupleStruct { path: ref p, args: ref subpats } => { | 647 | Pat::TupleStruct { path: ref p, args: ref subpats } => { |
661 | self.infer_tuple_struct_pat(p.as_ref(), subpats, expected, default_bm) | 648 | self.infer_tuple_struct_pat(p.as_ref(), subpats, expected, default_bm) |
@@ -682,7 +669,9 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> { | |||
682 | let inner_ty = self.insert_type_vars_shallow(inner_ty); | 669 | let inner_ty = self.insert_type_vars_shallow(inner_ty); |
683 | 670 | ||
684 | let bound_ty = match mode { | 671 | let bound_ty = match mode { |
685 | BindingMode::Ref(mutability) => Ty::Ref(inner_ty.clone().into(), mutability), | 672 | BindingMode::Ref(mutability) => { |
673 | Ty::apply_one(TypeName::Ref(mutability), inner_ty.clone().into()) | ||
674 | } | ||
686 | BindingMode::Move => inner_ty.clone(), | 675 | BindingMode::Move => inner_ty.clone(), |
687 | }; | 676 | }; |
688 | let bound_ty = self.resolve_ty_as_possible(&mut vec![], bound_ty); | 677 | let bound_ty = self.resolve_ty_as_possible(&mut vec![], bound_ty); |
@@ -736,7 +725,7 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> { | |||
736 | Expr::Missing => Ty::Unknown, | 725 | Expr::Missing => Ty::Unknown, |
737 | Expr::If { condition, then_branch, else_branch } => { | 726 | Expr::If { condition, then_branch, else_branch } => { |
738 | // if let is desugared to match, so this is always simple if | 727 | // if let is desugared to match, so this is always simple if |
739 | self.infer_expr(*condition, &Expectation::has_type(Ty::Bool)); | 728 | self.infer_expr(*condition, &Expectation::has_type(Ty::simple(TypeName::Bool))); |
740 | let then_ty = self.infer_expr(*then_branch, expected); | 729 | let then_ty = self.infer_expr(*then_branch, expected); |
741 | match else_branch { | 730 | match else_branch { |
742 | Some(else_branch) => { | 731 | Some(else_branch) => { |
@@ -753,11 +742,11 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> { | |||
753 | Expr::Loop { body } => { | 742 | Expr::Loop { body } => { |
754 | self.infer_expr(*body, &Expectation::has_type(Ty::unit())); | 743 | self.infer_expr(*body, &Expectation::has_type(Ty::unit())); |
755 | // TODO handle break with value | 744 | // TODO handle break with value |
756 | Ty::Never | 745 | Ty::simple(TypeName::Never) |
757 | } | 746 | } |
758 | Expr::While { condition, body } => { | 747 | Expr::While { condition, body } => { |
759 | // while let is desugared to a match loop, so this is always simple while | 748 | // while let is desugared to a match loop, so this is always simple while |
760 | self.infer_expr(*condition, &Expectation::has_type(Ty::Bool)); | 749 | self.infer_expr(*condition, &Expectation::has_type(Ty::simple(TypeName::Bool))); |
761 | self.infer_expr(*body, &Expectation::has_type(Ty::unit())); | 750 | self.infer_expr(*body, &Expectation::has_type(Ty::unit())); |
762 | Ty::unit() | 751 | Ty::unit() |
763 | } | 752 | } |
@@ -787,17 +776,23 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> { | |||
787 | Expr::Call { callee, args } => { | 776 | Expr::Call { callee, args } => { |
788 | let callee_ty = self.infer_expr(*callee, &Expectation::none()); | 777 | let callee_ty = self.infer_expr(*callee, &Expectation::none()); |
789 | let (param_tys, ret_ty) = match &callee_ty { | 778 | let (param_tys, ret_ty) = match &callee_ty { |
790 | Ty::FnPtr(sig) => { | 779 | Ty::Apply(a_ty) => match a_ty.name { |
791 | let sig = FnSig::from_fn_ptr_substs(sig); | 780 | TypeName::FnPtr => { |
792 | (sig.params().to_vec(), sig.ret().clone()) | 781 | let sig = FnSig::from_fn_ptr_substs(&a_ty.parameters); |
793 | } | 782 | (sig.params().to_vec(), sig.ret().clone()) |
794 | Ty::FnDef { substs, def, .. } => { | 783 | } |
795 | let sig = self.db.callable_item_signature(*def); | 784 | TypeName::FnDef(def) => { |
796 | let ret_ty = sig.ret().clone().subst(&substs); | 785 | let sig = self.db.callable_item_signature(def); |
797 | let param_tys = | 786 | let ret_ty = sig.ret().clone().subst(&a_ty.parameters); |
798 | sig.params().iter().map(|ty| ty.clone().subst(&substs)).collect(); | 787 | let param_tys = sig |
799 | (param_tys, ret_ty) | 788 | .params() |
800 | } | 789 | .iter() |
790 | .map(|ty| ty.clone().subst(&a_ty.parameters)) | ||
791 | .collect(); | ||
792 | (param_tys, ret_ty) | ||
793 | } | ||
794 | _ => (Vec::new(), Ty::Unknown), | ||
795 | }, | ||
801 | _ => { | 796 | _ => { |
802 | // not callable | 797 | // not callable |
803 | // TODO report an error? | 798 | // TODO report an error? |
@@ -828,32 +823,43 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> { | |||
828 | let method_ty = method_ty.apply_substs(substs); | 823 | let method_ty = method_ty.apply_substs(substs); |
829 | let method_ty = self.insert_type_vars(method_ty); | 824 | let method_ty = self.insert_type_vars(method_ty); |
830 | let (expected_receiver_ty, param_tys, ret_ty) = match &method_ty { | 825 | let (expected_receiver_ty, param_tys, ret_ty) = match &method_ty { |
831 | Ty::FnPtr(sig) => { | 826 | Ty::Apply(a_ty) => match a_ty.name { |
832 | let sig = FnSig::from_fn_ptr_substs(sig); | 827 | TypeName::FnPtr => { |
833 | if !sig.params().is_empty() { | 828 | let sig = FnSig::from_fn_ptr_substs(&a_ty.parameters); |
834 | (sig.params()[0].clone(), sig.params()[1..].to_vec(), sig.ret().clone()) | 829 | if !sig.params().is_empty() { |
835 | } else { | 830 | ( |
836 | (Ty::Unknown, Vec::new(), sig.ret().clone()) | 831 | sig.params()[0].clone(), |
832 | sig.params()[1..].to_vec(), | ||
833 | sig.ret().clone(), | ||
834 | ) | ||
835 | } else { | ||
836 | (Ty::Unknown, Vec::new(), sig.ret().clone()) | ||
837 | } | ||
837 | } | 838 | } |
838 | } | 839 | TypeName::FnDef(def) => { |
839 | Ty::FnDef { substs, def, .. } => { | 840 | let sig = self.db.callable_item_signature(def); |
840 | let sig = self.db.callable_item_signature(*def); | 841 | let ret_ty = sig.ret().clone().subst(&a_ty.parameters); |
841 | let ret_ty = sig.ret().clone().subst(&substs); | 842 | |
842 | 843 | if !sig.params().is_empty() { | |
843 | if !sig.params().is_empty() { | 844 | let mut params_iter = sig |
844 | let mut params_iter = | 845 | .params() |
845 | sig.params().iter().map(|ty| ty.clone().subst(&substs)); | 846 | .iter() |
846 | let receiver_ty = params_iter.next().unwrap(); | 847 | .map(|ty| ty.clone().subst(&a_ty.parameters)); |
847 | (receiver_ty, params_iter.collect(), ret_ty) | 848 | let receiver_ty = params_iter.next().unwrap(); |
848 | } else { | 849 | (receiver_ty, params_iter.collect(), ret_ty) |
849 | (Ty::Unknown, Vec::new(), ret_ty) | 850 | } else { |
851 | (Ty::Unknown, Vec::new(), ret_ty) | ||
852 | } | ||
850 | } | 853 | } |
851 | } | 854 | _ => (Ty::Unknown, Vec::new(), Ty::Unknown), |
855 | }, | ||
852 | _ => (Ty::Unknown, Vec::new(), Ty::Unknown), | 856 | _ => (Ty::Unknown, Vec::new(), Ty::Unknown), |
853 | }; | 857 | }; |
854 | // Apply autoref so the below unification works correctly | 858 | // Apply autoref so the below unification works correctly |
855 | let actual_receiver_ty = match expected_receiver_ty { | 859 | let actual_receiver_ty = match expected_receiver_ty.as_reference() { |
856 | Ty::Ref(_, mutability) => Ty::Ref(Arc::new(derefed_receiver_ty), mutability), | 860 | Some((_, mutability)) => { |
861 | Ty::apply_one(TypeName::Ref(mutability), derefed_receiver_ty) | ||
862 | } | ||
857 | _ => derefed_receiver_ty, | 863 | _ => derefed_receiver_ty, |
858 | }; | 864 | }; |
859 | self.unify(&expected_receiver_ty, &actual_receiver_ty); | 865 | self.unify(&expected_receiver_ty, &actual_receiver_ty); |
@@ -877,7 +883,10 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> { | |||
877 | let _pat_ty = self.infer_pat(pat, &input_ty, BindingMode::default()); | 883 | let _pat_ty = self.infer_pat(pat, &input_ty, BindingMode::default()); |
878 | } | 884 | } |
879 | if let Some(guard_expr) = arm.guard { | 885 | if let Some(guard_expr) = arm.guard { |
880 | self.infer_expr(guard_expr, &Expectation::has_type(Ty::Bool)); | 886 | self.infer_expr( |
887 | guard_expr, | ||
888 | &Expectation::has_type(Ty::simple(TypeName::Bool)), | ||
889 | ); | ||
881 | } | 890 | } |
882 | self.infer_expr(arm.expr, &expected); | 891 | self.infer_expr(arm.expr, &expected); |
883 | } | 892 | } |
@@ -889,19 +898,19 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> { | |||
889 | let resolver = expr::resolver_for_expr(self.body.clone(), self.db, tgt_expr); | 898 | let resolver = expr::resolver_for_expr(self.body.clone(), self.db, tgt_expr); |
890 | self.infer_path_expr(&resolver, p, tgt_expr.into()).unwrap_or(Ty::Unknown) | 899 | self.infer_path_expr(&resolver, p, tgt_expr.into()).unwrap_or(Ty::Unknown) |
891 | } | 900 | } |
892 | Expr::Continue => Ty::Never, | 901 | Expr::Continue => Ty::simple(TypeName::Never), |
893 | Expr::Break { expr } => { | 902 | Expr::Break { expr } => { |
894 | if let Some(expr) = expr { | 903 | if let Some(expr) = expr { |
895 | // TODO handle break with value | 904 | // TODO handle break with value |
896 | self.infer_expr(*expr, &Expectation::none()); | 905 | self.infer_expr(*expr, &Expectation::none()); |
897 | } | 906 | } |
898 | Ty::Never | 907 | Ty::simple(TypeName::Never) |
899 | } | 908 | } |
900 | Expr::Return { expr } => { | 909 | Expr::Return { expr } => { |
901 | if let Some(expr) = expr { | 910 | if let Some(expr) = expr { |
902 | self.infer_expr(*expr, &Expectation::has_type(self.return_ty.clone())); | 911 | self.infer_expr(*expr, &Expectation::has_type(self.return_ty.clone())); |
903 | } | 912 | } |
904 | Ty::Never | 913 | Ty::simple(TypeName::Never) |
905 | } | 914 | } |
906 | Expr::StructLit { path, fields, spread } => { | 915 | Expr::StructLit { path, fields, spread } => { |
907 | let (ty, def_id) = self.resolve_variant(path.as_ref()); | 916 | let (ty, def_id) = self.resolve_variant(path.as_ref()); |
@@ -923,16 +932,19 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> { | |||
923 | let ty = receiver_ty | 932 | let ty = receiver_ty |
924 | .autoderef(self.db) | 933 | .autoderef(self.db) |
925 | .find_map(|derefed_ty| match derefed_ty { | 934 | .find_map(|derefed_ty| match derefed_ty { |
926 | Ty::Tuple(fields) => { | 935 | Ty::Apply(a_ty) => match a_ty.name { |
927 | let i = name.to_string().parse::<usize>().ok(); | 936 | TypeName::Tuple => { |
928 | i.and_then(|i| fields.0.get(i).cloned()) | 937 | let i = name.to_string().parse::<usize>().ok(); |
929 | } | 938 | i.and_then(|i| a_ty.parameters.0.get(i).cloned()) |
930 | Ty::Adt { def_id: AdtDef::Struct(s), ref substs, .. } => { | 939 | } |
931 | s.field(self.db, name).map(|field| { | 940 | TypeName::Adt(AdtDef::Struct(s)) => { |
932 | self.write_field_resolution(tgt_expr, field); | 941 | s.field(self.db, name).map(|field| { |
933 | field.ty(self.db).subst(substs) | 942 | self.write_field_resolution(tgt_expr, field); |
934 | }) | 943 | field.ty(self.db).subst(&a_ty.parameters) |
935 | } | 944 | }) |
945 | } | ||
946 | _ => None, | ||
947 | }, | ||
936 | _ => None, | 948 | _ => None, |
937 | }) | 949 | }) |
938 | .unwrap_or(Ty::Unknown); | 950 | .unwrap_or(Ty::Unknown); |
@@ -949,18 +961,19 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> { | |||
949 | cast_ty | 961 | cast_ty |
950 | } | 962 | } |
951 | Expr::Ref { expr, mutability } => { | 963 | Expr::Ref { expr, mutability } => { |
952 | let expectation = if let Ty::Ref(ref subty, expected_mutability) = expected.ty { | 964 | let expectation = |
953 | if expected_mutability == Mutability::Mut && *mutability == Mutability::Shared { | 965 | if let Some((exp_inner, exp_mutability)) = &expected.ty.as_reference() { |
954 | // TODO: throw type error - expected mut reference but found shared ref, | 966 | if *exp_mutability == Mutability::Mut && *mutability == Mutability::Shared { |
955 | // which cannot be coerced | 967 | // TODO: throw type error - expected mut reference but found shared ref, |
956 | } | 968 | // which cannot be coerced |
957 | Expectation::has_type((**subty).clone()) | 969 | } |
958 | } else { | 970 | Expectation::has_type(Ty::clone(exp_inner)) |
959 | Expectation::none() | 971 | } else { |
960 | }; | 972 | Expectation::none() |
973 | }; | ||
961 | // TODO reference coercions etc. | 974 | // TODO reference coercions etc. |
962 | let inner_ty = self.infer_expr(*expr, &expectation); | 975 | let inner_ty = self.infer_expr(*expr, &expectation); |
963 | Ty::Ref(Arc::new(inner_ty), *mutability) | 976 | Ty::apply_one(TypeName::Ref(*mutability), inner_ty) |
964 | } | 977 | } |
965 | Expr::UnaryOp { expr, op } => { | 978 | Expr::UnaryOp { expr, op } => { |
966 | let inner_ty = self.infer_expr(*expr, &Expectation::none()); | 979 | let inner_ty = self.infer_expr(*expr, &Expectation::none()); |
@@ -974,19 +987,27 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> { | |||
974 | } | 987 | } |
975 | } | 988 | } |
976 | UnaryOp::Neg => { | 989 | UnaryOp::Neg => { |
977 | match inner_ty { | 990 | match &inner_ty { |
978 | Ty::Int(primitive::UncertainIntTy::Unknown) | 991 | Ty::Apply(a_ty) => match a_ty.name { |
979 | | Ty::Int(primitive::UncertainIntTy::Signed(..)) | 992 | TypeName::Int(primitive::UncertainIntTy::Unknown) |
980 | | Ty::Infer(InferTy::IntVar(..)) | 993 | | TypeName::Int(primitive::UncertainIntTy::Signed(..)) |
981 | | Ty::Infer(InferTy::FloatVar(..)) | 994 | | TypeName::Float(..) => inner_ty, |
982 | | Ty::Float(..) => inner_ty, | 995 | _ => Ty::Unknown, |
996 | }, | ||
997 | Ty::Infer(InferTy::IntVar(..)) | Ty::Infer(InferTy::FloatVar(..)) => { | ||
998 | inner_ty | ||
999 | } | ||
983 | // TODO: resolve ops::Neg trait | 1000 | // TODO: resolve ops::Neg trait |
984 | _ => Ty::Unknown, | 1001 | _ => Ty::Unknown, |
985 | } | 1002 | } |
986 | } | 1003 | } |
987 | UnaryOp::Not => { | 1004 | UnaryOp::Not => { |
988 | match inner_ty { | 1005 | match &inner_ty { |
989 | Ty::Bool | Ty::Int(_) | Ty::Infer(InferTy::IntVar(..)) => inner_ty, | 1006 | Ty::Apply(a_ty) => match a_ty.name { |
1007 | TypeName::Bool | TypeName::Int(_) => inner_ty, | ||
1008 | _ => Ty::Unknown, | ||
1009 | }, | ||
1010 | Ty::Infer(InferTy::IntVar(..)) => inner_ty, | ||
990 | // TODO: resolve ops::Not trait for inner_ty | 1011 | // TODO: resolve ops::Not trait for inner_ty |
991 | _ => Ty::Unknown, | 1012 | _ => Ty::Unknown, |
992 | } | 1013 | } |
@@ -997,7 +1018,7 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> { | |||
997 | Some(op) => { | 1018 | Some(op) => { |
998 | let lhs_expectation = match op { | 1019 | let lhs_expectation = match op { |
999 | BinaryOp::BooleanAnd | BinaryOp::BooleanOr => { | 1020 | BinaryOp::BooleanAnd | BinaryOp::BooleanOr => { |
1000 | Expectation::has_type(Ty::Bool) | 1021 | Expectation::has_type(Ty::simple(TypeName::Bool)) |
1001 | } | 1022 | } |
1002 | _ => Expectation::none(), | 1023 | _ => Expectation::none(), |
1003 | }; | 1024 | }; |
@@ -1018,11 +1039,16 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> { | |||
1018 | ty_vec.push(self.infer_expr(*arg, &Expectation::none())); | 1039 | ty_vec.push(self.infer_expr(*arg, &Expectation::none())); |
1019 | } | 1040 | } |
1020 | 1041 | ||
1021 | Ty::Tuple(Substs(ty_vec.into())) | 1042 | Ty::apply(TypeName::Tuple, Substs(ty_vec.into())) |
1022 | } | 1043 | } |
1023 | Expr::Array { exprs } => { | 1044 | Expr::Array { exprs } => { |
1024 | let elem_ty = match &expected.ty { | 1045 | let elem_ty = match &expected.ty { |
1025 | Ty::Slice(inner) | Ty::Array(inner) => Ty::clone(&inner), | 1046 | Ty::Apply(a_ty) => match a_ty.name { |
1047 | TypeName::Slice | TypeName::Array => { | ||
1048 | Ty::clone(&a_ty.parameters.as_single()) | ||
1049 | } | ||
1050 | _ => self.new_type_var(), | ||
1051 | }, | ||
1026 | _ => self.new_type_var(), | 1052 | _ => self.new_type_var(), |
1027 | }; | 1053 | }; |
1028 | 1054 | ||
@@ -1030,21 +1056,23 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> { | |||
1030 | self.infer_expr(*expr, &Expectation::has_type(elem_ty.clone())); | 1056 | self.infer_expr(*expr, &Expectation::has_type(elem_ty.clone())); |
1031 | } | 1057 | } |
1032 | 1058 | ||
1033 | Ty::Array(Arc::new(elem_ty)) | 1059 | Ty::apply_one(TypeName::Array, elem_ty) |
1034 | } | 1060 | } |
1035 | Expr::Literal(lit) => match lit { | 1061 | Expr::Literal(lit) => match lit { |
1036 | Literal::Bool(..) => Ty::Bool, | 1062 | Literal::Bool(..) => Ty::simple(TypeName::Bool), |
1037 | Literal::String(..) => Ty::Ref(Arc::new(Ty::Str), Mutability::Shared), | 1063 | Literal::String(..) => { |
1064 | Ty::apply_one(TypeName::Ref(Mutability::Shared), Ty::simple(TypeName::Str)) | ||
1065 | } | ||
1038 | Literal::ByteString(..) => { | 1066 | Literal::ByteString(..) => { |
1039 | let byte_type = Arc::new(Ty::Int(primitive::UncertainIntTy::Unsigned( | 1067 | let byte_type = Ty::simple(TypeName::Int(primitive::UncertainIntTy::Unsigned( |
1040 | primitive::UintTy::U8, | 1068 | primitive::UintTy::U8, |
1041 | ))); | 1069 | ))); |
1042 | let slice_type = Arc::new(Ty::Slice(byte_type)); | 1070 | let slice_type = Ty::apply_one(TypeName::Slice, byte_type); |
1043 | Ty::Ref(slice_type, Mutability::Shared) | 1071 | Ty::apply_one(TypeName::Ref(Mutability::Shared), slice_type) |
1044 | } | 1072 | } |
1045 | Literal::Char(..) => Ty::Char, | 1073 | Literal::Char(..) => Ty::simple(TypeName::Char), |
1046 | Literal::Int(_v, ty) => Ty::Int(*ty), | 1074 | Literal::Int(_v, ty) => Ty::simple(TypeName::Int(*ty)), |
1047 | Literal::Float(_v, ty) => Ty::Float(*ty), | 1075 | Literal::Float(_v, ty) => Ty::simple(TypeName::Float(*ty)), |
1048 | }, | 1076 | }, |
1049 | }; | 1077 | }; |
1050 | // use a new type variable if we got Ty::Unknown here | 1078 | // use a new type variable if we got Ty::Unknown here |
@@ -1180,11 +1208,11 @@ impl InferTy { | |||
1180 | match self { | 1208 | match self { |
1181 | InferTy::TypeVar(..) => Ty::Unknown, | 1209 | InferTy::TypeVar(..) => Ty::Unknown, |
1182 | InferTy::IntVar(..) => { | 1210 | InferTy::IntVar(..) => { |
1183 | Ty::Int(primitive::UncertainIntTy::Signed(primitive::IntTy::I32)) | 1211 | Ty::simple(TypeName::Int(primitive::UncertainIntTy::Signed(primitive::IntTy::I32))) |
1184 | } | ||
1185 | InferTy::FloatVar(..) => { | ||
1186 | Ty::Float(primitive::UncertainFloatTy::Known(primitive::FloatTy::F64)) | ||
1187 | } | 1212 | } |
1213 | InferTy::FloatVar(..) => Ty::simple(TypeName::Float( | ||
1214 | primitive::UncertainFloatTy::Known(primitive::FloatTy::F64), | ||
1215 | )), | ||
1188 | } | 1216 | } |
1189 | } | 1217 | } |
1190 | } | 1218 | } |