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_def/src/body/lower.rs | 19 +++------- crates/ra_hir_def/src/expr.rs | 38 +++++++------------ crates/ra_hir_ty/src/infer/expr.rs | 76 ++++++++++++++++++------------------- 3 files changed, 56 insertions(+), 77 deletions(-) diff --git a/crates/ra_hir_def/src/body/lower.rs b/crates/ra_hir_def/src/body/lower.rs index d18964d54..be1eaa523 100644 --- a/crates/ra_hir_def/src/body/lower.rs +++ b/crates/ra_hir_def/src/body/lower.rs @@ -8,7 +8,7 @@ use hir_expand::{ use ra_arena::Arena; use ra_syntax::{ ast::{ - self, ArgListOwner, ArrayExprKind, LiteralKind, LoopBodyOwner, NameOwner, RangeOp, + self, ArgListOwner, ArrayExprKind, LiteralKind, LoopBodyOwner, NameOwner, TypeAscriptionOwner, }, AstNode, AstPtr, @@ -432,20 +432,11 @@ where ast::Expr::RangeExpr(e) => { let lhs = e.start().map(|lhs| self.collect_expr(lhs)); let rhs = e.end().map(|rhs| self.collect_expr(rhs)); - match (lhs, e.op_kind(), rhs) { - (None, _, None) => self.alloc_expr(Expr::RangeFull, syntax_ptr), - (Some(lhs), _, None) => self.alloc_expr(Expr::RangeFrom { lhs }, syntax_ptr), - (None, Some(RangeOp::Inclusive), Some(rhs)) => { - self.alloc_expr(Expr::RangeToInclusive { rhs }, syntax_ptr) - } - (Some(lhs), Some(RangeOp::Inclusive), Some(rhs)) => { - self.alloc_expr(Expr::RangeInclusive { lhs, rhs }, syntax_ptr) - } - // If RangeOp is missing, fallback to exclusive range. - (None, _, Some(rhs)) => self.alloc_expr(Expr::RangeTo { rhs }, syntax_ptr), - (Some(lhs), _, Some(rhs)) => { - self.alloc_expr(Expr::Range { lhs, rhs }, syntax_ptr) + match e.op_kind() { + Some(range_type) => { + self.alloc_expr(Expr::Range { lhs, rhs, range_type }, syntax_ptr) } + None => self.alloc_expr(Expr::Missing, syntax_ptr), } } diff --git a/crates/ra_hir_def/src/expr.rs b/crates/ra_hir_def/src/expr.rs index 115090218..6fad80a8d 100644 --- a/crates/ra_hir_def/src/expr.rs +++ b/crates/ra_hir_def/src/expr.rs @@ -14,6 +14,7 @@ use hir_expand::name::Name; use ra_arena::{impl_arena_id, RawId}; +use ra_syntax::ast::RangeOp; use crate::{ builtin_type::{BuiltinFloat, BuiltinInt}, @@ -130,23 +131,10 @@ pub enum Expr { rhs: ExprId, op: Option, }, - RangeFull, - RangeFrom { - lhs: ExprId, - }, - RangeTo { - rhs: ExprId, - }, Range { - lhs: ExprId, - rhs: ExprId, - }, - RangeToInclusive { - rhs: ExprId, - }, - RangeInclusive { - lhs: ExprId, - rhs: ExprId, + lhs: Option, + rhs: Option, + range_type: RangeOp, }, Index { base: ExprId, @@ -302,21 +290,23 @@ impl Expr { Expr::Lambda { body, .. } => { f(*body); } - Expr::BinaryOp { lhs, rhs, .. } - | Expr::Range { lhs, rhs } - | Expr::RangeInclusive { lhs, rhs } => { + Expr::BinaryOp { lhs, rhs, .. } => { f(*lhs); f(*rhs); } + Expr::Range { lhs, rhs, .. } => { + if let Some(lhs) = rhs { + f(*lhs); + } + if let Some(rhs) = lhs { + f(*rhs); + } + } Expr::Index { base, index } => { f(*base); f(*index); } - Expr::RangeFull => {} - Expr::RangeFrom { lhs: expr } - | Expr::RangeTo { rhs: expr } - | Expr::RangeToInclusive { rhs: expr } - | Expr::Field { expr, .. } + Expr::Field { expr, .. } | Expr::Await { expr } | Expr::Try { expr } | Expr::Cast { expr, .. } 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