aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_hir/src/ty/infer.rs
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ra_hir/src/ty/infer.rs')
-rw-r--r--crates/ra_hir/src/ty/infer.rs78
1 files changed, 39 insertions, 39 deletions
diff --git a/crates/ra_hir/src/ty/infer.rs b/crates/ra_hir/src/ty/infer.rs
index 0dafd8cbd..e975f9217 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};
41use super::{Ty, TypableDef, Substs, primitive, op, FnSig, ApplicationTy, TypeName}; 41use super::{Ty, TypableDef, Substs, primitive, op, FnSig, ApplicationTy, TypeCtor};
42 42
43/// The entry point of type inference. 43/// The entry point of type inference.
44pub fn infer(db: &impl HirDatabase, func: Function) -> Arc<InferenceResult> { 44pub fn infer(db: &impl HirDatabase, func: Function) -> Arc<InferenceResult> {
@@ -278,11 +278,11 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> {
278 match ty { 278 match ty {
279 Ty::Unknown => self.new_type_var(), 279 Ty::Unknown => self.new_type_var(),
280 Ty::Apply(ApplicationTy { 280 Ty::Apply(ApplicationTy {
281 name: TypeName::Int(primitive::UncertainIntTy::Unknown), 281 name: TypeCtor::Int(primitive::UncertainIntTy::Unknown),
282 .. 282 ..
283 }) => self.new_integer_var(), 283 }) => self.new_integer_var(),
284 Ty::Apply(ApplicationTy { 284 Ty::Apply(ApplicationTy {
285 name: TypeName::Float(primitive::UncertainFloatTy::Unknown), 285 name: TypeCtor::Float(primitive::UncertainFloatTy::Unknown),
286 .. 286 ..
287 }) => self.new_float_var(), 287 }) => self.new_float_var(),
288 _ => ty, 288 _ => ty,
@@ -629,7 +629,7 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> {
629 .collect::<Vec<_>>() 629 .collect::<Vec<_>>()
630 .into(); 630 .into();
631 631
632 Ty::apply(TypeName::Tuple, Substs(inner_tys)) 632 Ty::apply(TypeCtor::Tuple, Substs(inner_tys))
633 } 633 }
634 Pat::Ref { pat, mutability } => { 634 Pat::Ref { pat, mutability } => {
635 let expectation = match expected.as_reference() { 635 let expectation = match expected.as_reference() {
@@ -642,7 +642,7 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> {
642 _ => &Ty::Unknown, 642 _ => &Ty::Unknown,
643 }; 643 };
644 let subty = self.infer_pat(*pat, expectation, default_bm); 644 let subty = self.infer_pat(*pat, expectation, default_bm);
645 Ty::apply_one(TypeName::Ref(*mutability), subty.into()) 645 Ty::apply_one(TypeCtor::Ref(*mutability), subty.into())
646 } 646 }
647 Pat::TupleStruct { path: ref p, args: ref subpats } => { 647 Pat::TupleStruct { path: ref p, args: ref subpats } => {
648 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)
@@ -670,7 +670,7 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> {
670 670
671 let bound_ty = match mode { 671 let bound_ty = match mode {
672 BindingMode::Ref(mutability) => { 672 BindingMode::Ref(mutability) => {
673 Ty::apply_one(TypeName::Ref(mutability), inner_ty.clone().into()) 673 Ty::apply_one(TypeCtor::Ref(mutability), inner_ty.clone().into())
674 } 674 }
675 BindingMode::Move => inner_ty.clone(), 675 BindingMode::Move => inner_ty.clone(),
676 }; 676 };
@@ -725,7 +725,7 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> {
725 Expr::Missing => Ty::Unknown, 725 Expr::Missing => Ty::Unknown,
726 Expr::If { condition, then_branch, else_branch } => { 726 Expr::If { condition, then_branch, else_branch } => {
727 // 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
728 self.infer_expr(*condition, &Expectation::has_type(Ty::simple(TypeName::Bool))); 728 self.infer_expr(*condition, &Expectation::has_type(Ty::simple(TypeCtor::Bool)));
729 let then_ty = self.infer_expr(*then_branch, expected); 729 let then_ty = self.infer_expr(*then_branch, expected);
730 match else_branch { 730 match else_branch {
731 Some(else_branch) => { 731 Some(else_branch) => {
@@ -742,11 +742,11 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> {
742 Expr::Loop { body } => { 742 Expr::Loop { body } => {
743 self.infer_expr(*body, &Expectation::has_type(Ty::unit())); 743 self.infer_expr(*body, &Expectation::has_type(Ty::unit()));
744 // TODO handle break with value 744 // TODO handle break with value
745 Ty::simple(TypeName::Never) 745 Ty::simple(TypeCtor::Never)
746 } 746 }
747 Expr::While { condition, body } => { 747 Expr::While { condition, body } => {
748 // 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
749 self.infer_expr(*condition, &Expectation::has_type(Ty::simple(TypeName::Bool))); 749 self.infer_expr(*condition, &Expectation::has_type(Ty::simple(TypeCtor::Bool)));
750 self.infer_expr(*body, &Expectation::has_type(Ty::unit())); 750 self.infer_expr(*body, &Expectation::has_type(Ty::unit()));
751 Ty::unit() 751 Ty::unit()
752 } 752 }
@@ -777,11 +777,11 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> {
777 let callee_ty = self.infer_expr(*callee, &Expectation::none()); 777 let callee_ty = self.infer_expr(*callee, &Expectation::none());
778 let (param_tys, ret_ty) = match &callee_ty { 778 let (param_tys, ret_ty) = match &callee_ty {
779 Ty::Apply(a_ty) => match a_ty.name { 779 Ty::Apply(a_ty) => match a_ty.name {
780 TypeName::FnPtr => { 780 TypeCtor::FnPtr => {
781 let sig = FnSig::from_fn_ptr_substs(&a_ty.parameters); 781 let sig = FnSig::from_fn_ptr_substs(&a_ty.parameters);
782 (sig.params().to_vec(), sig.ret().clone()) 782 (sig.params().to_vec(), sig.ret().clone())
783 } 783 }
784 TypeName::FnDef(def) => { 784 TypeCtor::FnDef(def) => {
785 let sig = self.db.callable_item_signature(def); 785 let sig = self.db.callable_item_signature(def);
786 let ret_ty = sig.ret().clone().subst(&a_ty.parameters); 786 let ret_ty = sig.ret().clone().subst(&a_ty.parameters);
787 let param_tys = sig 787 let param_tys = sig
@@ -824,7 +824,7 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> {
824 let method_ty = self.insert_type_vars(method_ty); 824 let method_ty = self.insert_type_vars(method_ty);
825 let (expected_receiver_ty, param_tys, ret_ty) = match &method_ty { 825 let (expected_receiver_ty, param_tys, ret_ty) = match &method_ty {
826 Ty::Apply(a_ty) => match a_ty.name { 826 Ty::Apply(a_ty) => match a_ty.name {
827 TypeName::FnPtr => { 827 TypeCtor::FnPtr => {
828 let sig = FnSig::from_fn_ptr_substs(&a_ty.parameters); 828 let sig = FnSig::from_fn_ptr_substs(&a_ty.parameters);
829 if !sig.params().is_empty() { 829 if !sig.params().is_empty() {
830 ( 830 (
@@ -836,7 +836,7 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> {
836 (Ty::Unknown, Vec::new(), sig.ret().clone()) 836 (Ty::Unknown, Vec::new(), sig.ret().clone())
837 } 837 }
838 } 838 }
839 TypeName::FnDef(def) => { 839 TypeCtor::FnDef(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(&a_ty.parameters);
842 842
@@ -858,7 +858,7 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> {
858 // Apply autoref so the below unification works correctly 858 // Apply autoref so the below unification works correctly
859 let actual_receiver_ty = match expected_receiver_ty.as_reference() { 859 let actual_receiver_ty = match expected_receiver_ty.as_reference() {
860 Some((_, mutability)) => { 860 Some((_, mutability)) => {
861 Ty::apply_one(TypeName::Ref(mutability), derefed_receiver_ty) 861 Ty::apply_one(TypeCtor::Ref(mutability), derefed_receiver_ty)
862 } 862 }
863 _ => derefed_receiver_ty, 863 _ => derefed_receiver_ty,
864 }; 864 };
@@ -885,7 +885,7 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> {
885 if let Some(guard_expr) = arm.guard { 885 if let Some(guard_expr) = arm.guard {
886 self.infer_expr( 886 self.infer_expr(
887 guard_expr, 887 guard_expr,
888 &Expectation::has_type(Ty::simple(TypeName::Bool)), 888 &Expectation::has_type(Ty::simple(TypeCtor::Bool)),
889 ); 889 );
890 } 890 }
891 self.infer_expr(arm.expr, &expected); 891 self.infer_expr(arm.expr, &expected);
@@ -898,19 +898,19 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> {
898 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);
899 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)
900 } 900 }
901 Expr::Continue => Ty::simple(TypeName::Never), 901 Expr::Continue => Ty::simple(TypeCtor::Never),
902 Expr::Break { expr } => { 902 Expr::Break { expr } => {
903 if let Some(expr) = expr { 903 if let Some(expr) = expr {
904 // TODO handle break with value 904 // TODO handle break with value
905 self.infer_expr(*expr, &Expectation::none()); 905 self.infer_expr(*expr, &Expectation::none());
906 } 906 }
907 Ty::simple(TypeName::Never) 907 Ty::simple(TypeCtor::Never)
908 } 908 }
909 Expr::Return { expr } => { 909 Expr::Return { expr } => {
910 if let Some(expr) = expr { 910 if let Some(expr) = expr {
911 self.infer_expr(*expr, &Expectation::has_type(self.return_ty.clone())); 911 self.infer_expr(*expr, &Expectation::has_type(self.return_ty.clone()));
912 } 912 }
913 Ty::simple(TypeName::Never) 913 Ty::simple(TypeCtor::Never)
914 } 914 }
915 Expr::StructLit { path, fields, spread } => { 915 Expr::StructLit { path, fields, spread } => {
916 let (ty, def_id) = self.resolve_variant(path.as_ref()); 916 let (ty, def_id) = self.resolve_variant(path.as_ref());
@@ -933,11 +933,11 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> {
933 .autoderef(self.db) 933 .autoderef(self.db)
934 .find_map(|derefed_ty| match derefed_ty { 934 .find_map(|derefed_ty| match derefed_ty {
935 Ty::Apply(a_ty) => match a_ty.name { 935 Ty::Apply(a_ty) => match a_ty.name {
936 TypeName::Tuple => { 936 TypeCtor::Tuple => {
937 let i = name.to_string().parse::<usize>().ok(); 937 let i = name.to_string().parse::<usize>().ok();
938 i.and_then(|i| a_ty.parameters.0.get(i).cloned()) 938 i.and_then(|i| a_ty.parameters.0.get(i).cloned())
939 } 939 }
940 TypeName::Adt(AdtDef::Struct(s)) => { 940 TypeCtor::Adt(AdtDef::Struct(s)) => {
941 s.field(self.db, name).map(|field| { 941 s.field(self.db, name).map(|field| {
942 self.write_field_resolution(tgt_expr, field); 942 self.write_field_resolution(tgt_expr, field);
943 field.ty(self.db).subst(&a_ty.parameters) 943 field.ty(self.db).subst(&a_ty.parameters)
@@ -973,7 +973,7 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> {
973 }; 973 };
974 // TODO reference coercions etc. 974 // TODO reference coercions etc.
975 let inner_ty = self.infer_expr(*expr, &expectation); 975 let inner_ty = self.infer_expr(*expr, &expectation);
976 Ty::apply_one(TypeName::Ref(*mutability), inner_ty) 976 Ty::apply_one(TypeCtor::Ref(*mutability), inner_ty)
977 } 977 }
978 Expr::UnaryOp { expr, op } => { 978 Expr::UnaryOp { expr, op } => {
979 let inner_ty = self.infer_expr(*expr, &Expectation::none()); 979 let inner_ty = self.infer_expr(*expr, &Expectation::none());
@@ -989,9 +989,9 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> {
989 UnaryOp::Neg => { 989 UnaryOp::Neg => {
990 match &inner_ty { 990 match &inner_ty {
991 Ty::Apply(a_ty) => match a_ty.name { 991 Ty::Apply(a_ty) => match a_ty.name {
992 TypeName::Int(primitive::UncertainIntTy::Unknown) 992 TypeCtor::Int(primitive::UncertainIntTy::Unknown)
993 | TypeName::Int(primitive::UncertainIntTy::Signed(..)) 993 | TypeCtor::Int(primitive::UncertainIntTy::Signed(..))
994 | TypeName::Float(..) => inner_ty, 994 | TypeCtor::Float(..) => inner_ty,
995 _ => Ty::Unknown, 995 _ => Ty::Unknown,
996 }, 996 },
997 Ty::Infer(InferTy::IntVar(..)) | Ty::Infer(InferTy::FloatVar(..)) => { 997 Ty::Infer(InferTy::IntVar(..)) | Ty::Infer(InferTy::FloatVar(..)) => {
@@ -1004,7 +1004,7 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> {
1004 UnaryOp::Not => { 1004 UnaryOp::Not => {
1005 match &inner_ty { 1005 match &inner_ty {
1006 Ty::Apply(a_ty) => match a_ty.name { 1006 Ty::Apply(a_ty) => match a_ty.name {
1007 TypeName::Bool | TypeName::Int(_) => inner_ty, 1007 TypeCtor::Bool | TypeCtor::Int(_) => inner_ty,
1008 _ => Ty::Unknown, 1008 _ => Ty::Unknown,
1009 }, 1009 },
1010 Ty::Infer(InferTy::IntVar(..)) => inner_ty, 1010 Ty::Infer(InferTy::IntVar(..)) => inner_ty,
@@ -1018,7 +1018,7 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> {
1018 Some(op) => { 1018 Some(op) => {
1019 let lhs_expectation = match op { 1019 let lhs_expectation = match op {
1020 BinaryOp::BooleanAnd | BinaryOp::BooleanOr => { 1020 BinaryOp::BooleanAnd | BinaryOp::BooleanOr => {
1021 Expectation::has_type(Ty::simple(TypeName::Bool)) 1021 Expectation::has_type(Ty::simple(TypeCtor::Bool))
1022 } 1022 }
1023 _ => Expectation::none(), 1023 _ => Expectation::none(),
1024 }; 1024 };
@@ -1039,12 +1039,12 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> {
1039 ty_vec.push(self.infer_expr(*arg, &Expectation::none())); 1039 ty_vec.push(self.infer_expr(*arg, &Expectation::none()));
1040 } 1040 }
1041 1041
1042 Ty::apply(TypeName::Tuple, Substs(ty_vec.into())) 1042 Ty::apply(TypeCtor::Tuple, Substs(ty_vec.into()))
1043 } 1043 }
1044 Expr::Array { exprs } => { 1044 Expr::Array { exprs } => {
1045 let elem_ty = match &expected.ty { 1045 let elem_ty = match &expected.ty {
1046 Ty::Apply(a_ty) => match a_ty.name { 1046 Ty::Apply(a_ty) => match a_ty.name {
1047 TypeName::Slice | TypeName::Array => { 1047 TypeCtor::Slice | TypeCtor::Array => {
1048 Ty::clone(&a_ty.parameters.as_single()) 1048 Ty::clone(&a_ty.parameters.as_single())
1049 } 1049 }
1050 _ => self.new_type_var(), 1050 _ => self.new_type_var(),
@@ -1056,23 +1056,23 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> {
1056 self.infer_expr(*expr, &Expectation::has_type(elem_ty.clone())); 1056 self.infer_expr(*expr, &Expectation::has_type(elem_ty.clone()));
1057 } 1057 }
1058 1058
1059 Ty::apply_one(TypeName::Array, elem_ty) 1059 Ty::apply_one(TypeCtor::Array, elem_ty)
1060 } 1060 }
1061 Expr::Literal(lit) => match lit { 1061 Expr::Literal(lit) => match lit {
1062 Literal::Bool(..) => Ty::simple(TypeName::Bool), 1062 Literal::Bool(..) => Ty::simple(TypeCtor::Bool),
1063 Literal::String(..) => { 1063 Literal::String(..) => {
1064 Ty::apply_one(TypeName::Ref(Mutability::Shared), Ty::simple(TypeName::Str)) 1064 Ty::apply_one(TypeCtor::Ref(Mutability::Shared), Ty::simple(TypeCtor::Str))
1065 } 1065 }
1066 Literal::ByteString(..) => { 1066 Literal::ByteString(..) => {
1067 let byte_type = Ty::simple(TypeName::Int(primitive::UncertainIntTy::Unsigned( 1067 let byte_type = Ty::simple(TypeCtor::Int(primitive::UncertainIntTy::Unsigned(
1068 primitive::UintTy::U8, 1068 primitive::UintTy::U8,
1069 ))); 1069 )));
1070 let slice_type = Ty::apply_one(TypeName::Slice, byte_type); 1070 let slice_type = Ty::apply_one(TypeCtor::Slice, byte_type);
1071 Ty::apply_one(TypeName::Ref(Mutability::Shared), slice_type) 1071 Ty::apply_one(TypeCtor::Ref(Mutability::Shared), slice_type)
1072 } 1072 }
1073 Literal::Char(..) => Ty::simple(TypeName::Char), 1073 Literal::Char(..) => Ty::simple(TypeCtor::Char),
1074 Literal::Int(_v, ty) => Ty::simple(TypeName::Int(*ty)), 1074 Literal::Int(_v, ty) => Ty::simple(TypeCtor::Int(*ty)),
1075 Literal::Float(_v, ty) => Ty::simple(TypeName::Float(*ty)), 1075 Literal::Float(_v, ty) => Ty::simple(TypeCtor::Float(*ty)),
1076 }, 1076 },
1077 }; 1077 };
1078 // use a new type variable if we got Ty::Unknown here 1078 // use a new type variable if we got Ty::Unknown here
@@ -1208,9 +1208,9 @@ impl InferTy {
1208 match self { 1208 match self {
1209 InferTy::TypeVar(..) => Ty::Unknown, 1209 InferTy::TypeVar(..) => Ty::Unknown,
1210 InferTy::IntVar(..) => { 1210 InferTy::IntVar(..) => {
1211 Ty::simple(TypeName::Int(primitive::UncertainIntTy::Signed(primitive::IntTy::I32))) 1211 Ty::simple(TypeCtor::Int(primitive::UncertainIntTy::Signed(primitive::IntTy::I32)))
1212 } 1212 }
1213 InferTy::FloatVar(..) => Ty::simple(TypeName::Float( 1213 InferTy::FloatVar(..) => Ty::simple(TypeCtor::Float(
1214 primitive::UncertainFloatTy::Known(primitive::FloatTy::F64), 1214 primitive::UncertainFloatTy::Known(primitive::FloatTy::F64),
1215 )), 1215 )),
1216 } 1216 }