mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-10-01 14:21:44 +00:00
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_arena::Arena;
|
||||||
use ra_syntax::{
|
use ra_syntax::{
|
||||||
ast::{
|
ast::{
|
||||||
self, ArgListOwner, ArrayExprKind, LiteralKind, LoopBodyOwner, NameOwner, RangeOp,
|
self, ArgListOwner, ArrayExprKind, LiteralKind, LoopBodyOwner, NameOwner,
|
||||||
TypeAscriptionOwner,
|
TypeAscriptionOwner,
|
||||||
},
|
},
|
||||||
AstNode, AstPtr,
|
AstNode, AstPtr,
|
||||||
|
@ -432,20 +432,11 @@ where
|
||||||
ast::Expr::RangeExpr(e) => {
|
ast::Expr::RangeExpr(e) => {
|
||||||
let lhs = e.start().map(|lhs| self.collect_expr(lhs));
|
let lhs = e.start().map(|lhs| self.collect_expr(lhs));
|
||||||
let rhs = e.end().map(|rhs| self.collect_expr(rhs));
|
let rhs = e.end().map(|rhs| self.collect_expr(rhs));
|
||||||
match (lhs, e.op_kind(), rhs) {
|
match e.op_kind() {
|
||||||
(None, _, None) => self.alloc_expr(Expr::RangeFull, syntax_ptr),
|
Some(range_type) => {
|
||||||
(Some(lhs), _, None) => self.alloc_expr(Expr::RangeFrom { lhs }, syntax_ptr),
|
self.alloc_expr(Expr::Range { lhs, rhs, range_type }, 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)
|
|
||||||
}
|
}
|
||||||
|
None => self.alloc_expr(Expr::Missing, syntax_ptr),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -14,6 +14,7 @@
|
||||||
|
|
||||||
use hir_expand::name::Name;
|
use hir_expand::name::Name;
|
||||||
use ra_arena::{impl_arena_id, RawId};
|
use ra_arena::{impl_arena_id, RawId};
|
||||||
|
use ra_syntax::ast::RangeOp;
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
builtin_type::{BuiltinFloat, BuiltinInt},
|
builtin_type::{BuiltinFloat, BuiltinInt},
|
||||||
|
@ -130,23 +131,10 @@ pub enum Expr {
|
||||||
rhs: ExprId,
|
rhs: ExprId,
|
||||||
op: Option<BinaryOp>,
|
op: Option<BinaryOp>,
|
||||||
},
|
},
|
||||||
RangeFull,
|
|
||||||
RangeFrom {
|
|
||||||
lhs: ExprId,
|
|
||||||
},
|
|
||||||
RangeTo {
|
|
||||||
rhs: ExprId,
|
|
||||||
},
|
|
||||||
Range {
|
Range {
|
||||||
lhs: ExprId,
|
lhs: Option<ExprId>,
|
||||||
rhs: ExprId,
|
rhs: Option<ExprId>,
|
||||||
},
|
range_type: RangeOp,
|
||||||
RangeToInclusive {
|
|
||||||
rhs: ExprId,
|
|
||||||
},
|
|
||||||
RangeInclusive {
|
|
||||||
lhs: ExprId,
|
|
||||||
rhs: ExprId,
|
|
||||||
},
|
},
|
||||||
Index {
|
Index {
|
||||||
base: ExprId,
|
base: ExprId,
|
||||||
|
@ -302,21 +290,23 @@ impl Expr {
|
||||||
Expr::Lambda { body, .. } => {
|
Expr::Lambda { body, .. } => {
|
||||||
f(*body);
|
f(*body);
|
||||||
}
|
}
|
||||||
Expr::BinaryOp { lhs, rhs, .. }
|
Expr::BinaryOp { lhs, rhs, .. } => {
|
||||||
| Expr::Range { lhs, rhs }
|
|
||||||
| Expr::RangeInclusive { lhs, rhs } => {
|
|
||||||
f(*lhs);
|
f(*lhs);
|
||||||
f(*rhs);
|
f(*rhs);
|
||||||
}
|
}
|
||||||
|
Expr::Range { lhs, rhs, .. } => {
|
||||||
|
if let Some(lhs) = rhs {
|
||||||
|
f(*lhs);
|
||||||
|
}
|
||||||
|
if let Some(rhs) = lhs {
|
||||||
|
f(*rhs);
|
||||||
|
}
|
||||||
|
}
|
||||||
Expr::Index { base, index } => {
|
Expr::Index { base, index } => {
|
||||||
f(*base);
|
f(*base);
|
||||||
f(*index);
|
f(*index);
|
||||||
}
|
}
|
||||||
Expr::RangeFull => {}
|
Expr::Field { expr, .. }
|
||||||
Expr::RangeFrom { lhs: expr }
|
|
||||||
| Expr::RangeTo { rhs: expr }
|
|
||||||
| Expr::RangeToInclusive { rhs: expr }
|
|
||||||
| Expr::Field { expr, .. }
|
|
||||||
| Expr::Await { expr }
|
| Expr::Await { expr }
|
||||||
| Expr::Try { expr }
|
| Expr::Try { expr }
|
||||||
| Expr::Cast { expr, .. }
|
| Expr::Cast { expr, .. }
|
||||||
|
|
|
@ -12,6 +12,7 @@ use hir_def::{
|
||||||
AdtId, ContainerId, Lookup, StructFieldId,
|
AdtId, ContainerId, Lookup, StructFieldId,
|
||||||
};
|
};
|
||||||
use hir_expand::name::{self, Name};
|
use hir_expand::name::{self, Name};
|
||||||
|
use ra_syntax::ast::RangeOp;
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
autoderef, db::HirDatabase, method_resolution, op, traits::InEnvironment, utils::variant_data,
|
autoderef, db::HirDatabase, method_resolution, op, traits::InEnvironment, utils::variant_data,
|
||||||
|
@ -415,45 +416,42 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> {
|
||||||
}
|
}
|
||||||
_ => Ty::Unknown,
|
_ => Ty::Unknown,
|
||||||
},
|
},
|
||||||
Expr::RangeFull => match self.resolve_range_full() {
|
Expr::Range { lhs, rhs, range_type } => {
|
||||||
Some(adt) => Ty::simple(TypeCtor::Adt(adt)),
|
let lhs_ty = lhs.map(|e| self.infer_expr(e, &Expectation::none()));
|
||||||
None => Ty::Unknown,
|
let rhs_expect = lhs_ty
|
||||||
},
|
.as_ref()
|
||||||
Expr::Range { lhs, rhs } => {
|
.map_or_else(Expectation::none, |ty| Expectation::has_type(ty.clone()));
|
||||||
let lhs_ty = self.infer_expr(*lhs, &Expectation::none());
|
let rhs_ty = rhs.map(|e| self.infer_expr(e, &rhs_expect));
|
||||||
let rhs_ty = self.infer_expr(*rhs, &Expectation::has_type(lhs_ty));
|
match (range_type, lhs_ty, rhs_ty) {
|
||||||
match self.resolve_range() {
|
(RangeOp::Exclusive, None, None) => match self.resolve_range_full() {
|
||||||
Some(adt) => Ty::apply_one(TypeCtor::Adt(adt), rhs_ty),
|
Some(adt) => Ty::simple(TypeCtor::Adt(adt)),
|
||||||
None => Ty::Unknown,
|
None => Ty::Unknown,
|
||||||
}
|
},
|
||||||
}
|
(RangeOp::Exclusive, None, Some(ty)) => match self.resolve_range_to() {
|
||||||
Expr::RangeInclusive { lhs, rhs } => {
|
Some(adt) => Ty::apply_one(TypeCtor::Adt(adt), ty),
|
||||||
let lhs_ty = self.infer_expr(*lhs, &Expectation::none());
|
None => Ty::Unknown,
|
||||||
let rhs_ty = self.infer_expr(*rhs, &Expectation::has_type(lhs_ty));
|
},
|
||||||
match self.resolve_range_inclusive() {
|
(RangeOp::Inclusive, None, Some(ty)) => {
|
||||||
Some(adt) => Ty::apply_one(TypeCtor::Adt(adt), rhs_ty),
|
match self.resolve_range_to_inclusive() {
|
||||||
None => Ty::Unknown,
|
Some(adt) => Ty::apply_one(TypeCtor::Adt(adt), ty),
|
||||||
}
|
None => Ty::Unknown,
|
||||||
}
|
}
|
||||||
Expr::RangeFrom { lhs } => {
|
}
|
||||||
let ty = self.infer_expr(*lhs, &Expectation::none());
|
(RangeOp::Exclusive, Some(_), Some(ty)) => match self.resolve_range() {
|
||||||
match self.resolve_range_from() {
|
Some(adt) => Ty::apply_one(TypeCtor::Adt(adt), ty),
|
||||||
Some(adt) => Ty::apply_one(TypeCtor::Adt(adt), ty),
|
None => Ty::Unknown,
|
||||||
None => Ty::Unknown,
|
},
|
||||||
}
|
(RangeOp::Inclusive, Some(_), Some(ty)) => {
|
||||||
}
|
match self.resolve_range_inclusive() {
|
||||||
Expr::RangeTo { rhs } => {
|
Some(adt) => Ty::apply_one(TypeCtor::Adt(adt), ty),
|
||||||
let ty = self.infer_expr(*rhs, &Expectation::none());
|
None => Ty::Unknown,
|
||||||
match self.resolve_range_to() {
|
}
|
||||||
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,
|
||||||
Expr::RangeToInclusive { rhs } => {
|
},
|
||||||
let ty = self.infer_expr(*rhs, &Expectation::none());
|
(RangeOp::Inclusive, _, None) => Ty::Unknown,
|
||||||
match self.resolve_range_to_inclusive() {
|
|
||||||
Some(adt) => Ty::apply_one(TypeCtor::Adt(adt), ty),
|
|
||||||
None => Ty::Unknown,
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Expr::Index { base, index } => {
|
Expr::Index { base, index } => {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue