Reduce variants of Expr
This commit is contained in:
parent
4992d2bf79
commit
2cb684bbce
3 changed files with 56 additions and 77 deletions
|
@ -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),
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -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<BinaryOp>,
|
||||
},
|
||||
RangeFull,
|
||||
RangeFrom {
|
||||
lhs: ExprId,
|
||||
},
|
||||
RangeTo {
|
||||
rhs: ExprId,
|
||||
},
|
||||
Range {
|
||||
lhs: ExprId,
|
||||
rhs: ExprId,
|
||||
},
|
||||
RangeToInclusive {
|
||||
rhs: ExprId,
|
||||
},
|
||||
RangeInclusive {
|
||||
lhs: ExprId,
|
||||
rhs: ExprId,
|
||||
lhs: Option<ExprId>,
|
||||
rhs: Option<ExprId>,
|
||||
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, .. }
|
||||
|
|
|
@ -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 } => {
|
||||
|
|
Loading…
Add table
Reference in a new issue