aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_hir_ty/src/op.rs
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ra_hir_ty/src/op.rs')
-rw-r--r--crates/ra_hir_ty/src/op.rs50
1 files changed, 50 insertions, 0 deletions
diff --git a/crates/ra_hir_ty/src/op.rs b/crates/ra_hir_ty/src/op.rs
new file mode 100644
index 000000000..09c47a76d
--- /dev/null
+++ b/crates/ra_hir_ty/src/op.rs
@@ -0,0 +1,50 @@
1//! FIXME: write short doc here
2use hir_def::expr::{BinaryOp, CmpOp};
3
4use super::{InferTy, Ty, TypeCtor};
5use crate::ApplicationTy;
6
7pub(super) fn binary_op_return_ty(op: BinaryOp, rhs_ty: Ty) -> Ty {
8 match op {
9 BinaryOp::LogicOp(_) | BinaryOp::CmpOp(_) => Ty::simple(TypeCtor::Bool),
10 BinaryOp::Assignment { .. } => Ty::unit(),
11 BinaryOp::ArithOp(_) => match rhs_ty {
12 Ty::Apply(ApplicationTy { ctor, .. }) => match ctor {
13 TypeCtor::Int(..) | TypeCtor::Float(..) => rhs_ty,
14 _ => Ty::Unknown,
15 },
16 Ty::Infer(InferTy::IntVar(..)) | Ty::Infer(InferTy::FloatVar(..)) => rhs_ty,
17 _ => Ty::Unknown,
18 },
19 }
20}
21
22pub(super) fn binary_op_rhs_expectation(op: BinaryOp, lhs_ty: Ty) -> Ty {
23 match op {
24 BinaryOp::LogicOp(..) => Ty::simple(TypeCtor::Bool),
25 BinaryOp::Assignment { op: None } | BinaryOp::CmpOp(CmpOp::Eq { negated: _ }) => {
26 match lhs_ty {
27 Ty::Apply(ApplicationTy { ctor, .. }) => match ctor {
28 TypeCtor::Int(..)
29 | TypeCtor::Float(..)
30 | TypeCtor::Str
31 | TypeCtor::Char
32 | TypeCtor::Bool => lhs_ty,
33 _ => Ty::Unknown,
34 },
35 Ty::Infer(InferTy::IntVar(..)) | Ty::Infer(InferTy::FloatVar(..)) => lhs_ty,
36 _ => Ty::Unknown,
37 }
38 }
39 BinaryOp::CmpOp(CmpOp::Ord { .. })
40 | BinaryOp::Assignment { op: Some(_) }
41 | BinaryOp::ArithOp(_) => match lhs_ty {
42 Ty::Apply(ApplicationTy { ctor, .. }) => match ctor {
43 TypeCtor::Int(..) | TypeCtor::Float(..) => lhs_ty,
44 _ => Ty::Unknown,
45 },
46 Ty::Infer(InferTy::IntVar(..)) | Ty::Infer(InferTy::FloatVar(..)) => lhs_ty,
47 _ => Ty::Unknown,
48 },
49 }
50}