rust/crates/ra_hir/src/ty/op.rs

95 lines
3.2 KiB
Rust
Raw Normal View History

use crate::{ ty::ApplicationTy, expr::BinaryOp};
2019-03-21 16:20:03 -05:00
use super::{Ty, TypeCtor, InferTy};
pub(super) fn binary_op_return_ty(op: BinaryOp, rhs_ty: Ty) -> Ty {
match op {
BinaryOp::BooleanOr
| BinaryOp::BooleanAnd
| BinaryOp::EqualityTest
| BinaryOp::NegatedEqualityTest
| BinaryOp::LesserEqualTest
| BinaryOp::GreaterEqualTest
| BinaryOp::LesserTest
2019-03-21 16:20:03 -05:00
| BinaryOp::GreaterTest => Ty::simple(TypeCtor::Bool),
BinaryOp::Assignment
| BinaryOp::AddAssign
| BinaryOp::SubAssign
| BinaryOp::DivAssign
| BinaryOp::MulAssign
| BinaryOp::RemAssign
| BinaryOp::ShrAssign
| BinaryOp::ShlAssign
| BinaryOp::BitAndAssign
| BinaryOp::BitOrAssign
| BinaryOp::BitXorAssign => Ty::unit(),
BinaryOp::Addition
| BinaryOp::Subtraction
| BinaryOp::Multiplication
| BinaryOp::Division
| BinaryOp::Remainder
| BinaryOp::LeftShift
| BinaryOp::RightShift
| BinaryOp::BitwiseAnd
| BinaryOp::BitwiseOr
| BinaryOp::BitwiseXor => match rhs_ty {
Ty::Apply(ApplicationTy { name, .. }) => match name {
2019-03-21 16:20:03 -05:00
TypeCtor::Int(..) | TypeCtor::Float(..) => rhs_ty,
_ => Ty::Unknown,
},
Ty::Infer(InferTy::IntVar(..)) | Ty::Infer(InferTy::FloatVar(..)) => rhs_ty,
_ => Ty::Unknown,
},
BinaryOp::RangeRightOpen | BinaryOp::RangeRightClosed => Ty::Unknown,
}
}
pub(super) fn binary_op_rhs_expectation(op: BinaryOp, lhs_ty: Ty) -> Ty {
match op {
2019-03-21 16:20:03 -05:00
BinaryOp::BooleanAnd | BinaryOp::BooleanOr => Ty::simple(TypeCtor::Bool),
BinaryOp::Assignment | BinaryOp::EqualityTest => match lhs_ty {
Ty::Apply(ApplicationTy { name, .. }) => match name {
2019-03-21 16:20:03 -05:00
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::LesserEqualTest
| BinaryOp::GreaterEqualTest
| BinaryOp::LesserTest
| BinaryOp::GreaterTest
| BinaryOp::AddAssign
| BinaryOp::SubAssign
| BinaryOp::DivAssign
| BinaryOp::MulAssign
| BinaryOp::RemAssign
| BinaryOp::ShrAssign
| BinaryOp::ShlAssign
| BinaryOp::BitAndAssign
| BinaryOp::BitOrAssign
| BinaryOp::BitXorAssign
| BinaryOp::Addition
| BinaryOp::Subtraction
| BinaryOp::Multiplication
| BinaryOp::Division
| BinaryOp::Remainder
| BinaryOp::LeftShift
| BinaryOp::RightShift
| BinaryOp::BitwiseAnd
| BinaryOp::BitwiseOr
| BinaryOp::BitwiseXor => match lhs_ty {
Ty::Apply(ApplicationTy { name, .. }) => match name {
2019-03-21 16:20:03 -05:00
TypeCtor::Int(..) | TypeCtor::Float(..) => lhs_ty,
_ => Ty::Unknown,
},
Ty::Infer(InferTy::IntVar(..)) | Ty::Infer(InferTy::FloatVar(..)) => lhs_ty,
_ => Ty::Unknown,
},
_ => Ty::Unknown,
}
}