diff options
Diffstat (limited to 'crates/ra_hir/src/ty/infer.rs')
-rw-r--r-- | crates/ra_hir/src/ty/infer.rs | 90 |
1 files changed, 46 insertions, 44 deletions
diff --git a/crates/ra_hir/src/ty/infer.rs b/crates/ra_hir/src/ty/infer.rs index 7b59ebfe7..3911695df 100644 --- a/crates/ra_hir/src/ty/infer.rs +++ b/crates/ra_hir/src/ty/infer.rs | |||
@@ -297,7 +297,7 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> { | |||
297 | | (other, Ty::Infer(InferTy::IntVar(tv))) | 297 | | (other, Ty::Infer(InferTy::IntVar(tv))) |
298 | | (Ty::Infer(InferTy::FloatVar(tv)), other) | 298 | | (Ty::Infer(InferTy::FloatVar(tv)), other) |
299 | | (other, Ty::Infer(InferTy::FloatVar(tv))) | 299 | | (other, Ty::Infer(InferTy::FloatVar(tv))) |
300 | if !Self::is_never(other) => | 300 | if !is_never(other) => |
301 | { | 301 | { |
302 | // the type var is unknown since we tried to resolve it | 302 | // the type var is unknown since we tried to resolve it |
303 | self.var_unification_table.union_value(*tv, TypeVarValue::Known(other.clone())); | 303 | self.var_unification_table.union_value(*tv, TypeVarValue::Known(other.clone())); |
@@ -984,24 +984,20 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> { | |||
984 | Expr::If { condition, then_branch, else_branch } => { | 984 | Expr::If { condition, then_branch, else_branch } => { |
985 | // if let is desugared to match, so this is always simple if | 985 | // if let is desugared to match, so this is always simple if |
986 | self.infer_expr(*condition, &Expectation::has_type(Ty::simple(TypeCtor::Bool))); | 986 | self.infer_expr(*condition, &Expectation::has_type(Ty::simple(TypeCtor::Bool))); |
987 | let then_ty = self.infer_expr(*then_branch, expected); | 987 | |
988 | let mut branch_tys = Vec::with_capacity(2); | ||
989 | let then_ty = self.infer_expr(*then_branch, &expected); | ||
988 | match else_branch { | 990 | match else_branch { |
989 | Some(else_branch) => { | 991 | Some(else_branch) => { |
990 | let else_ty = self.infer_expr(*else_branch, expected); | 992 | branch_tys.push(self.infer_expr(*else_branch, &expected)); |
991 | if Self::is_never(&then_ty) { | ||
992 | tested_by!(if_never); | ||
993 | else_ty | ||
994 | } else { | ||
995 | tested_by!(if_else_never); | ||
996 | then_ty | ||
997 | } | ||
998 | } | 993 | } |
999 | None => { | 994 | None => { |
1000 | // no else branch -> unit | 995 | // no else branch -> unit |
1001 | self.unify(&then_ty, &Ty::unit()); // actually coerce | 996 | self.unify(&then_ty, &Ty::unit()); // actually coerce |
1002 | then_ty | ||
1003 | } | 997 | } |
1004 | } | 998 | }; |
999 | branch_tys.push(then_ty); | ||
1000 | calculate_least_upper_bound(expected.ty.clone(), branch_tys) | ||
1005 | } | 1001 | } |
1006 | Expr::Block { statements, tail } => self.infer_block(statements, *tail, expected), | 1002 | Expr::Block { statements, tail } => self.infer_block(statements, *tail, expected), |
1007 | Expr::TryBlock { body } => { | 1003 | Expr::TryBlock { body } => { |
@@ -1081,15 +1077,14 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> { | |||
1081 | Expr::MethodCall { receiver, args, method_name, generic_args } => self | 1077 | Expr::MethodCall { receiver, args, method_name, generic_args } => self |
1082 | .infer_method_call(tgt_expr, *receiver, &args, &method_name, generic_args.as_ref()), | 1078 | .infer_method_call(tgt_expr, *receiver, &args, &method_name, generic_args.as_ref()), |
1083 | Expr::Match { expr, arms } => { | 1079 | Expr::Match { expr, arms } => { |
1080 | let input_ty = self.infer_expr(*expr, &Expectation::none()); | ||
1084 | let expected = if expected.ty == Ty::Unknown { | 1081 | let expected = if expected.ty == Ty::Unknown { |
1085 | Expectation::has_type(self.new_type_var()) | 1082 | Expectation::has_type(self.new_type_var()) |
1086 | } else { | 1083 | } else { |
1087 | expected.clone() | 1084 | expected.clone() |
1088 | }; | 1085 | }; |
1089 | let input_ty = self.infer_expr(*expr, &Expectation::none()); | ||
1090 | 1086 | ||
1091 | let mut resulting_match_ty = None; | 1087 | let mut arm_tys = Vec::with_capacity(arms.len()); |
1092 | let mut all_arms_never = !arms.is_empty(); | ||
1093 | 1088 | ||
1094 | for arm in arms { | 1089 | for arm in arms { |
1095 | for &pat in &arm.pats { | 1090 | for &pat in &arm.pats { |
@@ -1101,28 +1096,9 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> { | |||
1101 | &Expectation::has_type(Ty::simple(TypeCtor::Bool)), | 1096 | &Expectation::has_type(Ty::simple(TypeCtor::Bool)), |
1102 | ); | 1097 | ); |
1103 | } | 1098 | } |
1104 | let arm_ty = self.infer_expr(arm.expr, &expected); | 1099 | arm_tys.push(self.infer_expr(arm.expr, &expected)); |
1105 | if all_arms_never && Self::is_never(&arm_ty) { | ||
1106 | tested_by!(match_first_arm_never); | ||
1107 | resulting_match_ty = Some(arm_ty); | ||
1108 | } else { | ||
1109 | tested_by!(match_second_arm_never); | ||
1110 | all_arms_never = false; | ||
1111 | resulting_match_ty = None; | ||
1112 | } | ||
1113 | } | ||
1114 | |||
1115 | if let (Ty::Infer(expected_tv), Some(match_ty)) = | ||
1116 | (&expected.ty, &resulting_match_ty) | ||
1117 | { | ||
1118 | tested_by!(match_all_arms_never); | ||
1119 | self.var_unification_table | ||
1120 | .union_value(expected_tv.to_inner(), TypeVarValue::Known(match_ty.clone())); | ||
1121 | match_ty.clone() | ||
1122 | } else { | ||
1123 | tested_by!(match_no_never_arms); | ||
1124 | expected.ty | ||
1125 | } | 1100 | } |
1101 | calculate_least_upper_bound(expected.ty.clone(), arm_tys) | ||
1126 | } | 1102 | } |
1127 | Expr::Path(p) => { | 1103 | Expr::Path(p) => { |
1128 | // FIXME this could be more efficient... | 1104 | // FIXME this could be more efficient... |
@@ -1397,14 +1373,6 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> { | |||
1397 | ty | 1373 | ty |
1398 | } | 1374 | } |
1399 | 1375 | ||
1400 | fn is_never(ty: &Ty) -> bool { | ||
1401 | if let Ty::Apply(ApplicationTy { ctor: TypeCtor::Never, .. }) = ty { | ||
1402 | true | ||
1403 | } else { | ||
1404 | false | ||
1405 | } | ||
1406 | } | ||
1407 | |||
1408 | fn infer_block( | 1376 | fn infer_block( |
1409 | &mut self, | 1377 | &mut self, |
1410 | statements: &[Statement], | 1378 | statements: &[Statement], |
@@ -1653,3 +1621,37 @@ mod diagnostics { | |||
1653 | } | 1621 | } |
1654 | } | 1622 | } |
1655 | } | 1623 | } |
1624 | |||
1625 | fn is_never(ty: &Ty) -> bool { | ||
1626 | if let Ty::Apply(ApplicationTy { ctor: TypeCtor::Never, .. }) = ty { | ||
1627 | true | ||
1628 | } else { | ||
1629 | false | ||
1630 | } | ||
1631 | } | ||
1632 | |||
1633 | fn calculate_least_upper_bound(expected_ty: Ty, actual_tys: Vec<Ty>) -> Ty { | ||
1634 | let mut all_never = true; | ||
1635 | let mut last_never_ty = None; | ||
1636 | let mut least_upper_bound = expected_ty; | ||
1637 | |||
1638 | for actual_ty in actual_tys { | ||
1639 | if is_never(&actual_ty) { | ||
1640 | last_never_ty = Some(actual_ty); | ||
1641 | } else { | ||
1642 | all_never = false; | ||
1643 | least_upper_bound = match (&actual_ty, &least_upper_bound) { | ||
1644 | (_, Ty::Unknown) | ||
1645 | | (Ty::Infer(_), Ty::Infer(InferTy::TypeVar(_))) | ||
1646 | | (Ty::Apply(_), _) => actual_ty, | ||
1647 | _ => least_upper_bound, | ||
1648 | } | ||
1649 | } | ||
1650 | } | ||
1651 | |||
1652 | if all_never && last_never_ty.is_some() { | ||
1653 | last_never_ty.unwrap() | ||
1654 | } else { | ||
1655 | least_upper_bound | ||
1656 | } | ||
1657 | } | ||