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 ++++++++++++++----------------------- 2 files changed, 19 insertions(+), 38 deletions(-) (limited to 'crates/ra_hir_def/src') 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, .. } -- cgit v1.2.3