From 2cb684bbce1c487b2efb5a8154afe66e4907ceac Mon Sep 17 00:00:00 2001 From: oxalica Date: Fri, 29 Nov 2019 14:49:12 +0800 Subject: Reduce variants of Expr --- crates/ra_hir_ty/src/infer/expr.rs | 76 +++++++++++++++++++------------------- 1 file changed, 37 insertions(+), 39 deletions(-) (limited to 'crates/ra_hir_ty/src/infer') diff --git a/crates/ra_hir_ty/src/infer/expr.rs b/crates/ra_hir_ty/src/infer/expr.rs index a00aa426a..4014f4732 100644 --- a/crates/ra_hir_ty/src/infer/expr.rs +++ b/crates/ra_hir_ty/src/infer/expr.rs @@ -12,6 +12,7 @@ use hir_def::{ AdtId, ContainerId, Lookup, StructFieldId, }; use hir_expand::name::{self, Name}; +use ra_syntax::ast::RangeOp; use crate::{ autoderef, db::HirDatabase, method_resolution, op, traits::InEnvironment, utils::variant_data, @@ -415,45 +416,42 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> { } _ => Ty::Unknown, }, - Expr::RangeFull => match self.resolve_range_full() { - Some(adt) => Ty::simple(TypeCtor::Adt(adt)), - None => Ty::Unknown, - }, - Expr::Range { lhs, rhs } => { - let lhs_ty = self.infer_expr(*lhs, &Expectation::none()); - let rhs_ty = self.infer_expr(*rhs, &Expectation::has_type(lhs_ty)); - match self.resolve_range() { - Some(adt) => Ty::apply_one(TypeCtor::Adt(adt), rhs_ty), - None => Ty::Unknown, - } - } - Expr::RangeInclusive { lhs, rhs } => { - let lhs_ty = self.infer_expr(*lhs, &Expectation::none()); - let rhs_ty = self.infer_expr(*rhs, &Expectation::has_type(lhs_ty)); - match self.resolve_range_inclusive() { - Some(adt) => Ty::apply_one(TypeCtor::Adt(adt), rhs_ty), - None => Ty::Unknown, - } - } - Expr::RangeFrom { lhs } => { - let ty = self.infer_expr(*lhs, &Expectation::none()); - match self.resolve_range_from() { - Some(adt) => Ty::apply_one(TypeCtor::Adt(adt), ty), - None => Ty::Unknown, - } - } - Expr::RangeTo { rhs } => { - let ty = self.infer_expr(*rhs, &Expectation::none()); - match self.resolve_range_to() { - Some(adt) => Ty::apply_one(TypeCtor::Adt(adt), ty), - None => Ty::Unknown, - } - } - Expr::RangeToInclusive { rhs } => { - let ty = self.infer_expr(*rhs, &Expectation::none()); - match self.resolve_range_to_inclusive() { - Some(adt) => Ty::apply_one(TypeCtor::Adt(adt), ty), - None => Ty::Unknown, + Expr::Range { lhs, rhs, range_type } => { + let lhs_ty = lhs.map(|e| self.infer_expr(e, &Expectation::none())); + let rhs_expect = lhs_ty + .as_ref() + .map_or_else(Expectation::none, |ty| Expectation::has_type(ty.clone())); + let rhs_ty = rhs.map(|e| self.infer_expr(e, &rhs_expect)); + match (range_type, lhs_ty, rhs_ty) { + (RangeOp::Exclusive, None, None) => match self.resolve_range_full() { + Some(adt) => Ty::simple(TypeCtor::Adt(adt)), + None => Ty::Unknown, + }, + (RangeOp::Exclusive, None, Some(ty)) => match self.resolve_range_to() { + Some(adt) => Ty::apply_one(TypeCtor::Adt(adt), ty), + None => Ty::Unknown, + }, + (RangeOp::Inclusive, None, Some(ty)) => { + match self.resolve_range_to_inclusive() { + Some(adt) => Ty::apply_one(TypeCtor::Adt(adt), ty), + None => Ty::Unknown, + } + } + (RangeOp::Exclusive, Some(_), Some(ty)) => match self.resolve_range() { + Some(adt) => Ty::apply_one(TypeCtor::Adt(adt), ty), + None => Ty::Unknown, + }, + (RangeOp::Inclusive, Some(_), Some(ty)) => { + match self.resolve_range_inclusive() { + Some(adt) => Ty::apply_one(TypeCtor::Adt(adt), ty), + None => Ty::Unknown, + } + } + (RangeOp::Exclusive, Some(ty), None) => match self.resolve_range_from() { + Some(adt) => Ty::apply_one(TypeCtor::Adt(adt), ty), + None => Ty::Unknown, + }, + (RangeOp::Inclusive, _, None) => Ty::Unknown, } } Expr::Index { base, index } => { -- cgit v1.2.3