From b082cd679ad1ae7646d03261bcccda435443365c Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Sat, 17 Aug 2019 17:51:01 +0300 Subject: normalize ordering ops --- crates/ra_hir/src/expr.rs | 30 ++++++++++++++++++++---------- crates/ra_hir/src/ty/op.rs | 32 ++++++++++++++++---------------- 2 files changed, 36 insertions(+), 26 deletions(-) (limited to 'crates/ra_hir/src') diff --git a/crates/ra_hir/src/expr.rs b/crates/ra_hir/src/expr.rs index ea3fa4417..5430a0c9f 100644 --- a/crates/ra_hir/src/expr.rs +++ b/crates/ra_hir/src/expr.rs @@ -273,12 +273,14 @@ pub enum LogicOp { #[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)] pub enum CmpOp { - Equal, - NotEqual, + Eq { negated: bool }, + Ord { ordering: Ordering, strict: bool }, +} + +#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)] +pub enum Ordering { Less, - LessOrEqual, Greater, - GreaterOrEqual, } #[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)] @@ -1080,12 +1082,20 @@ impl From for BinaryOp { match ast_op { ast::BinOp::BooleanOr => BinaryOp::LogicOp(LogicOp::Or), ast::BinOp::BooleanAnd => BinaryOp::LogicOp(LogicOp::And), - ast::BinOp::EqualityTest => BinaryOp::CmpOp(CmpOp::Equal), - ast::BinOp::NegatedEqualityTest => BinaryOp::CmpOp(CmpOp::NotEqual), - ast::BinOp::LesserEqualTest => BinaryOp::CmpOp(CmpOp::LessOrEqual), - ast::BinOp::GreaterEqualTest => BinaryOp::CmpOp(CmpOp::GreaterOrEqual), - ast::BinOp::LesserTest => BinaryOp::CmpOp(CmpOp::Less), - ast::BinOp::GreaterTest => BinaryOp::CmpOp(CmpOp::Greater), + ast::BinOp::EqualityTest => BinaryOp::CmpOp(CmpOp::Eq { negated: false }), + ast::BinOp::NegatedEqualityTest => BinaryOp::CmpOp(CmpOp::Eq { negated: true }), + ast::BinOp::LesserEqualTest => { + BinaryOp::CmpOp(CmpOp::Ord { ordering: Ordering::Less, strict: false }) + } + ast::BinOp::GreaterEqualTest => { + BinaryOp::CmpOp(CmpOp::Ord { ordering: Ordering::Greater, strict: false }) + } + ast::BinOp::LesserTest => { + BinaryOp::CmpOp(CmpOp::Ord { ordering: Ordering::Less, strict: true }) + } + ast::BinOp::GreaterTest => { + BinaryOp::CmpOp(CmpOp::Ord { ordering: Ordering::Greater, strict: true }) + } ast::BinOp::Addition => BinaryOp::ArithOp(ArithOp::Add), ast::BinOp::Multiplication => BinaryOp::ArithOp(ArithOp::Mul), ast::BinOp::Subtraction => BinaryOp::ArithOp(ArithOp::Sub), diff --git a/crates/ra_hir/src/ty/op.rs b/crates/ra_hir/src/ty/op.rs index 1d089f1b0..1b30a5b9b 100644 --- a/crates/ra_hir/src/ty/op.rs +++ b/crates/ra_hir/src/ty/op.rs @@ -22,29 +22,29 @@ pub(super) fn binary_op_return_ty(op: BinaryOp, rhs_ty: Ty) -> Ty { pub(super) fn binary_op_rhs_expectation(op: BinaryOp, lhs_ty: Ty) -> Ty { match op { BinaryOp::LogicOp(..) => Ty::simple(TypeCtor::Bool), - BinaryOp::Assignment { op: None } - | BinaryOp::CmpOp(CmpOp::Equal) - | BinaryOp::CmpOp(CmpOp::NotEqual) => match lhs_ty { - Ty::Apply(ApplicationTy { ctor, .. }) => match ctor { - TypeCtor::Int(..) - | TypeCtor::Float(..) - | TypeCtor::Str - | TypeCtor::Char - | TypeCtor::Bool => lhs_ty, - _ => Ty::Unknown, - }, - Ty::Infer(InferTy::IntVar(..)) | Ty::Infer(InferTy::FloatVar(..)) => lhs_ty, - _ => Ty::Unknown, - }, - BinaryOp::CmpOp(_) | BinaryOp::Assignment { op: Some(_) } | BinaryOp::ArithOp(_) => { + BinaryOp::Assignment { op: None } | BinaryOp::CmpOp(CmpOp::Eq { negated: _ }) => { match lhs_ty { Ty::Apply(ApplicationTy { ctor, .. }) => match ctor { - TypeCtor::Int(..) | TypeCtor::Float(..) => lhs_ty, + TypeCtor::Int(..) + | TypeCtor::Float(..) + | TypeCtor::Str + | TypeCtor::Char + | TypeCtor::Bool => lhs_ty, _ => Ty::Unknown, }, Ty::Infer(InferTy::IntVar(..)) | Ty::Infer(InferTy::FloatVar(..)) => lhs_ty, _ => Ty::Unknown, } } + BinaryOp::CmpOp(CmpOp::Ord { .. }) + | BinaryOp::Assignment { op: Some(_) } + | BinaryOp::ArithOp(_) => match lhs_ty { + Ty::Apply(ApplicationTy { ctor, .. }) => match ctor { + TypeCtor::Int(..) | TypeCtor::Float(..) => lhs_ty, + _ => Ty::Unknown, + }, + Ty::Infer(InferTy::IntVar(..)) | Ty::Infer(InferTy::FloatVar(..)) => lhs_ty, + _ => Ty::Unknown, + }, } } -- cgit v1.2.3