Skip to content

Commit 2cb684b

Browse files
committed
Reduce variants of Expr
1 parent 4992d2b commit 2cb684b

File tree

3 files changed

+56
-77
lines changed

3 files changed

+56
-77
lines changed

crates/ra_hir_def/src/body/lower.rs

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use hir_expand::{
88
use ra_arena::Arena;
99
use ra_syntax::{
1010
ast::{
11-
self, ArgListOwner, ArrayExprKind, LiteralKind, LoopBodyOwner, NameOwner, RangeOp,
11+
self, ArgListOwner, ArrayExprKind, LiteralKind, LoopBodyOwner, NameOwner,
1212
TypeAscriptionOwner,
1313
},
1414
AstNode, AstPtr,
@@ -432,20 +432,11 @@ where
432432
ast::Expr::RangeExpr(e) => {
433433
let lhs = e.start().map(|lhs| self.collect_expr(lhs));
434434
let rhs = e.end().map(|rhs| self.collect_expr(rhs));
435-
match (lhs, e.op_kind(), rhs) {
436-
(None, _, None) => self.alloc_expr(Expr::RangeFull, syntax_ptr),
437-
(Some(lhs), _, None) => self.alloc_expr(Expr::RangeFrom { lhs }, 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)
435+
match e.op_kind() {
436+
Some(range_type) => {
437+
self.alloc_expr(Expr::Range { lhs, rhs, range_type }, syntax_ptr)
448438
}
439+
None => self.alloc_expr(Expr::Missing, syntax_ptr),
449440
}
450441
}
451442

crates/ra_hir_def/src/expr.rs

Lines changed: 14 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
1515
use hir_expand::name::Name;
1616
use ra_arena::{impl_arena_id, RawId};
17+
use ra_syntax::ast::RangeOp;
1718

1819
use crate::{
1920
builtin_type::{BuiltinFloat, BuiltinInt},
@@ -130,23 +131,10 @@ pub enum Expr {
130131
rhs: ExprId,
131132
op: Option<BinaryOp>,
132133
},
133-
RangeFull,
134-
RangeFrom {
135-
lhs: ExprId,
136-
},
137-
RangeTo {
138-
rhs: ExprId,
139-
},
140134
Range {
141-
lhs: ExprId,
142-
rhs: ExprId,
143-
},
144-
RangeToInclusive {
145-
rhs: ExprId,
146-
},
147-
RangeInclusive {
148-
lhs: ExprId,
149-
rhs: ExprId,
135+
lhs: Option<ExprId>,
136+
rhs: Option<ExprId>,
137+
range_type: RangeOp,
150138
},
151139
Index {
152140
base: ExprId,
@@ -302,21 +290,23 @@ impl Expr {
302290
Expr::Lambda { body, .. } => {
303291
f(*body);
304292
}
305-
Expr::BinaryOp { lhs, rhs, .. }
306-
| Expr::Range { lhs, rhs }
307-
| Expr::RangeInclusive { lhs, rhs } => {
293+
Expr::BinaryOp { lhs, rhs, .. } => {
308294
f(*lhs);
309295
f(*rhs);
310296
}
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+
}
311305
Expr::Index { base, index } => {
312306
f(*base);
313307
f(*index);
314308
}
315-
Expr::RangeFull => {}
316-
Expr::RangeFrom { lhs: expr }
317-
| Expr::RangeTo { rhs: expr }
318-
| Expr::RangeToInclusive { rhs: expr }
319-
| Expr::Field { expr, .. }
309+
Expr::Field { expr, .. }
320310
| Expr::Await { expr }
321311
| Expr::Try { expr }
322312
| Expr::Cast { expr, .. }

crates/ra_hir_ty/src/infer/expr.rs

Lines changed: 37 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ use hir_def::{
1212
AdtId, ContainerId, Lookup, StructFieldId,
1313
};
1414
use hir_expand::name::{self, Name};
15+
use ra_syntax::ast::RangeOp;
1516

1617
use crate::{
1718
autoderef, db::HirDatabase, method_resolution, op, traits::InEnvironment, utils::variant_data,
@@ -415,45 +416,42 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> {
415416
}
416417
_ => Ty::Unknown,
417418
},
418-
Expr::RangeFull => match self.resolve_range_full() {
419-
Some(adt) => Ty::simple(TypeCtor::Adt(adt)),
420-
None => Ty::Unknown,
421-
},
422-
Expr::Range { lhs, rhs } => {
423-
let lhs_ty = self.infer_expr(*lhs, &Expectation::none());
424-
let rhs_ty = self.infer_expr(*rhs, &Expectation::has_type(lhs_ty));
425-
match self.resolve_range() {
426-
Some(adt) => Ty::apply_one(TypeCtor::Adt(adt), rhs_ty),
427-
None => Ty::Unknown,
428-
}
429-
}
430-
Expr::RangeInclusive { lhs, rhs } => {
431-
let lhs_ty = self.infer_expr(*lhs, &Expectation::none());
432-
let rhs_ty = self.infer_expr(*rhs, &Expectation::has_type(lhs_ty));
433-
match self.resolve_range_inclusive() {
434-
Some(adt) => Ty::apply_one(TypeCtor::Adt(adt), rhs_ty),
435-
None => Ty::Unknown,
436-
}
437-
}
438-
Expr::RangeFrom { lhs } => {
439-
let ty = self.infer_expr(*lhs, &Expectation::none());
440-
match self.resolve_range_from() {
441-
Some(adt) => Ty::apply_one(TypeCtor::Adt(adt), ty),
442-
None => Ty::Unknown,
443-
}
444-
}
445-
Expr::RangeTo { rhs } => {
446-
let ty = self.infer_expr(*rhs, &Expectation::none());
447-
match self.resolve_range_to() {
448-
Some(adt) => Ty::apply_one(TypeCtor::Adt(adt), ty),
449-
None => Ty::Unknown,
450-
}
451-
}
452-
Expr::RangeToInclusive { rhs } => {
453-
let ty = self.infer_expr(*rhs, &Expectation::none());
454-
match self.resolve_range_to_inclusive() {
455-
Some(adt) => Ty::apply_one(TypeCtor::Adt(adt), ty),
456-
None => Ty::Unknown,
419+
Expr::Range { lhs, rhs, range_type } => {
420+
let lhs_ty = lhs.map(|e| self.infer_expr(e, &Expectation::none()));
421+
let rhs_expect = lhs_ty
422+
.as_ref()
423+
.map_or_else(Expectation::none, |ty| Expectation::has_type(ty.clone()));
424+
let rhs_ty = rhs.map(|e| self.infer_expr(e, &rhs_expect));
425+
match (range_type, lhs_ty, rhs_ty) {
426+
(RangeOp::Exclusive, None, None) => match self.resolve_range_full() {
427+
Some(adt) => Ty::simple(TypeCtor::Adt(adt)),
428+
None => Ty::Unknown,
429+
},
430+
(RangeOp::Exclusive, None, Some(ty)) => match self.resolve_range_to() {
431+
Some(adt) => Ty::apply_one(TypeCtor::Adt(adt), ty),
432+
None => Ty::Unknown,
433+
},
434+
(RangeOp::Inclusive, None, Some(ty)) => {
435+
match self.resolve_range_to_inclusive() {
436+
Some(adt) => Ty::apply_one(TypeCtor::Adt(adt), ty),
437+
None => Ty::Unknown,
438+
}
439+
}
440+
(RangeOp::Exclusive, Some(_), Some(ty)) => match self.resolve_range() {
441+
Some(adt) => Ty::apply_one(TypeCtor::Adt(adt), ty),
442+
None => Ty::Unknown,
443+
},
444+
(RangeOp::Inclusive, Some(_), Some(ty)) => {
445+
match self.resolve_range_inclusive() {
446+
Some(adt) => Ty::apply_one(TypeCtor::Adt(adt), ty),
447+
None => Ty::Unknown,
448+
}
449+
}
450+
(RangeOp::Exclusive, Some(ty), None) => match self.resolve_range_from() {
451+
Some(adt) => Ty::apply_one(TypeCtor::Adt(adt), ty),
452+
None => Ty::Unknown,
453+
},
454+
(RangeOp::Inclusive, _, None) => Ty::Unknown,
457455
}
458456
}
459457
Expr::Index { base, index } => {

0 commit comments

Comments
 (0)