diff options
author | oxalica <[email protected]> | 2019-11-29 06:49:12 +0000 |
---|---|---|
committer | oxalica <[email protected]> | 2019-11-29 06:49:12 +0000 |
commit | 2cb684bbce1c487b2efb5a8154afe66e4907ceac (patch) | |
tree | f93ef014cc82793949e8567634cec341ffc58d62 | |
parent | 4992d2bf79e9da6db759eb8e1715f90f31ec7eb9 (diff) |
Reduce variants of Expr
-rw-r--r-- | crates/ra_hir_def/src/body/lower.rs | 19 | ||||
-rw-r--r-- | crates/ra_hir_def/src/expr.rs | 38 | ||||
-rw-r--r-- | 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::{ | |||
8 | use ra_arena::Arena; | 8 | use ra_arena::Arena; |
9 | use ra_syntax::{ | 9 | use ra_syntax::{ |
10 | ast::{ | 10 | ast::{ |
11 | self, ArgListOwner, ArrayExprKind, LiteralKind, LoopBodyOwner, NameOwner, RangeOp, | 11 | self, ArgListOwner, ArrayExprKind, LiteralKind, LoopBodyOwner, NameOwner, |
12 | TypeAscriptionOwner, | 12 | TypeAscriptionOwner, |
13 | }, | 13 | }, |
14 | AstNode, AstPtr, | 14 | AstNode, AstPtr, |
@@ -432,20 +432,11 @@ where | |||
432 | ast::Expr::RangeExpr(e) => { | 432 | ast::Expr::RangeExpr(e) => { |
433 | let lhs = e.start().map(|lhs| self.collect_expr(lhs)); | 433 | let lhs = e.start().map(|lhs| self.collect_expr(lhs)); |
434 | let rhs = e.end().map(|rhs| self.collect_expr(rhs)); | 434 | let rhs = e.end().map(|rhs| self.collect_expr(rhs)); |
435 | match (lhs, e.op_kind(), rhs) { | 435 | match e.op_kind() { |
436 | (None, _, None) => self.alloc_expr(Expr::RangeFull, syntax_ptr), | 436 | Some(range_type) => { |
437 | (Some(lhs), _, None) => self.alloc_expr(Expr::RangeFrom { lhs }, syntax_ptr), | 437 | self.alloc_expr(Expr::Range { lhs, rhs, range_type }, syntax_ptr) |
438 | (None, Some(RangeOp::Inclusive), Some(rhs)) => { | ||
439 | self.alloc_expr(Expr::RangeToInclusive { rhs }, syntax_ptr) | ||
440 | } | ||
441 | (Some(lhs), Some(RangeOp::Inclusive), Some(rhs)) => { | ||
442 | self.alloc_expr(Expr::RangeInclusive { lhs, rhs }, syntax_ptr) | ||
443 | } | ||
444 | // If RangeOp is missing, fallback to exclusive range. | ||
445 | (None, _, Some(rhs)) => self.alloc_expr(Expr::RangeTo { rhs }, syntax_ptr), | ||
446 | (Some(lhs), _, Some(rhs)) => { | ||
447 | self.alloc_expr(Expr::Range { lhs, rhs }, syntax_ptr) | ||
448 | } | 438 | } |
439 | None => self.alloc_expr(Expr::Missing, syntax_ptr), | ||
449 | } | 440 | } |
450 | } | 441 | } |
451 | 442 | ||
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 @@ | |||
14 | 14 | ||
15 | use hir_expand::name::Name; | 15 | use hir_expand::name::Name; |
16 | use ra_arena::{impl_arena_id, RawId}; | 16 | use ra_arena::{impl_arena_id, RawId}; |
17 | use ra_syntax::ast::RangeOp; | ||
17 | 18 | ||
18 | use crate::{ | 19 | use crate::{ |
19 | builtin_type::{BuiltinFloat, BuiltinInt}, | 20 | builtin_type::{BuiltinFloat, BuiltinInt}, |
@@ -130,23 +131,10 @@ pub enum Expr { | |||
130 | rhs: ExprId, | 131 | rhs: ExprId, |
131 | op: Option<BinaryOp>, | 132 | op: Option<BinaryOp>, |
132 | }, | 133 | }, |
133 | RangeFull, | ||
134 | RangeFrom { | ||
135 | lhs: ExprId, | ||
136 | }, | ||
137 | RangeTo { | ||
138 | rhs: ExprId, | ||
139 | }, | ||
140 | Range { | 134 | Range { |
141 | lhs: ExprId, | 135 | lhs: Option<ExprId>, |
142 | rhs: ExprId, | 136 | rhs: Option<ExprId>, |
143 | }, | 137 | range_type: RangeOp, |
144 | RangeToInclusive { | ||
145 | rhs: ExprId, | ||
146 | }, | ||
147 | RangeInclusive { | ||
148 | lhs: ExprId, | ||
149 | rhs: ExprId, | ||
150 | }, | 138 | }, |
151 | Index { | 139 | Index { |
152 | base: ExprId, | 140 | base: ExprId, |
@@ -302,21 +290,23 @@ impl Expr { | |||
302 | Expr::Lambda { body, .. } => { | 290 | Expr::Lambda { body, .. } => { |
303 | f(*body); | 291 | f(*body); |
304 | } | 292 | } |
305 | Expr::BinaryOp { lhs, rhs, .. } | 293 | Expr::BinaryOp { lhs, rhs, .. } => { |
306 | | Expr::Range { lhs, rhs } | ||
307 | | Expr::RangeInclusive { lhs, rhs } => { | ||
308 | f(*lhs); | 294 | f(*lhs); |
309 | f(*rhs); | 295 | f(*rhs); |
310 | } | 296 | } |
297 | Expr::Range { lhs, rhs, .. } => { | ||
298 | if let Some(lhs) = rhs { | ||
299 | f(*lhs); | ||
300 | } | ||
301 | if let Some(rhs) = lhs { | ||
302 | f(*rhs); | ||
303 | } | ||
304 | } | ||
311 | Expr::Index { base, index } => { | 305 | Expr::Index { base, index } => { |
312 | f(*base); | 306 | f(*base); |
313 | f(*index); | 307 | f(*index); |
314 | } | 308 | } |
315 | Expr::RangeFull => {} | 309 | Expr::Field { expr, .. } |
316 | Expr::RangeFrom { lhs: expr } | ||
317 | | Expr::RangeTo { rhs: expr } | ||
318 | | Expr::RangeToInclusive { rhs: expr } | ||
319 | | Expr::Field { expr, .. } | ||
320 | | Expr::Await { expr } | 310 | | Expr::Await { expr } |
321 | | Expr::Try { expr } | 311 | | Expr::Try { expr } |
322 | | Expr::Cast { expr, .. } | 312 | | 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::{ | |||
12 | AdtId, ContainerId, Lookup, StructFieldId, | 12 | AdtId, ContainerId, Lookup, StructFieldId, |
13 | }; | 13 | }; |
14 | use hir_expand::name::{self, Name}; | 14 | use hir_expand::name::{self, Name}; |
15 | use ra_syntax::ast::RangeOp; | ||
15 | 16 | ||
16 | use crate::{ | 17 | use crate::{ |
17 | autoderef, db::HirDatabase, method_resolution, op, traits::InEnvironment, utils::variant_data, | 18 | autoderef, db::HirDatabase, method_resolution, op, traits::InEnvironment, utils::variant_data, |
@@ -415,45 +416,42 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> { | |||
415 | } | 416 | } |
416 | _ => Ty::Unknown, | 417 | _ => Ty::Unknown, |
417 | }, | 418 | }, |
418 | Expr::RangeFull => match self.resolve_range_full() { | 419 | Expr::Range { lhs, rhs, range_type } => { |
419 | Some(adt) => Ty::simple(TypeCtor::Adt(adt)), | 420 | let lhs_ty = lhs.map(|e| self.infer_expr(e, &Expectation::none())); |
420 | None => Ty::Unknown, | 421 | let rhs_expect = lhs_ty |
421 | }, | 422 | .as_ref() |
422 | Expr::Range { lhs, rhs } => { | 423 | .map_or_else(Expectation::none, |ty| Expectation::has_type(ty.clone())); |
423 | let lhs_ty = self.infer_expr(*lhs, &Expectation::none()); | 424 | let rhs_ty = rhs.map(|e| self.infer_expr(e, &rhs_expect)); |
424 | let rhs_ty = self.infer_expr(*rhs, &Expectation::has_type(lhs_ty)); | 425 | match (range_type, lhs_ty, rhs_ty) { |
425 | match self.resolve_range() { | 426 | (RangeOp::Exclusive, None, None) => match self.resolve_range_full() { |
426 | Some(adt) => Ty::apply_one(TypeCtor::Adt(adt), rhs_ty), | 427 | Some(adt) => Ty::simple(TypeCtor::Adt(adt)), |
427 | None => Ty::Unknown, | 428 | None => Ty::Unknown, |
428 | } | 429 | }, |
429 | } | 430 | (RangeOp::Exclusive, None, Some(ty)) => match self.resolve_range_to() { |
430 | Expr::RangeInclusive { lhs, rhs } => { | 431 | Some(adt) => Ty::apply_one(TypeCtor::Adt(adt), ty), |
431 | let lhs_ty = self.infer_expr(*lhs, &Expectation::none()); | 432 | None => Ty::Unknown, |
432 | let rhs_ty = self.infer_expr(*rhs, &Expectation::has_type(lhs_ty)); | 433 | }, |
433 | match self.resolve_range_inclusive() { | 434 | (RangeOp::Inclusive, None, Some(ty)) => { |
434 | Some(adt) => Ty::apply_one(TypeCtor::Adt(adt), rhs_ty), | 435 | match self.resolve_range_to_inclusive() { |
435 | None => Ty::Unknown, | 436 | Some(adt) => Ty::apply_one(TypeCtor::Adt(adt), ty), |
436 | } | 437 | None => Ty::Unknown, |
437 | } | 438 | } |
438 | Expr::RangeFrom { lhs } => { | 439 | } |
439 | let ty = self.infer_expr(*lhs, &Expectation::none()); | 440 | (RangeOp::Exclusive, Some(_), Some(ty)) => match self.resolve_range() { |
440 | match self.resolve_range_from() { | 441 | Some(adt) => Ty::apply_one(TypeCtor::Adt(adt), ty), |
441 | Some(adt) => Ty::apply_one(TypeCtor::Adt(adt), ty), | 442 | None => Ty::Unknown, |
442 | None => Ty::Unknown, | 443 | }, |
443 | } | 444 | (RangeOp::Inclusive, Some(_), Some(ty)) => { |
444 | } | 445 | match self.resolve_range_inclusive() { |
445 | Expr::RangeTo { rhs } => { | 446 | Some(adt) => Ty::apply_one(TypeCtor::Adt(adt), ty), |
446 | let ty = self.infer_expr(*rhs, &Expectation::none()); | 447 | None => Ty::Unknown, |
447 | match self.resolve_range_to() { | 448 | } |
448 | Some(adt) => Ty::apply_one(TypeCtor::Adt(adt), ty), | 449 | } |
449 | None => Ty::Unknown, | 450 | (RangeOp::Exclusive, Some(ty), None) => match self.resolve_range_from() { |
450 | } | 451 | Some(adt) => Ty::apply_one(TypeCtor::Adt(adt), ty), |
451 | } | 452 | None => Ty::Unknown, |
452 | Expr::RangeToInclusive { rhs } => { | 453 | }, |
453 | let ty = self.infer_expr(*rhs, &Expectation::none()); | 454 | (RangeOp::Inclusive, _, None) => Ty::Unknown, |
454 | match self.resolve_range_to_inclusive() { | ||
455 | Some(adt) => Ty::apply_one(TypeCtor::Adt(adt), ty), | ||
456 | None => Ty::Unknown, | ||
457 | } | 455 | } |
458 | } | 456 | } |
459 | Expr::Index { base, index } => { | 457 | Expr::Index { base, index } => { |