aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_hir_ty/src/op.rs
diff options
context:
space:
mode:
authorSeivan Heidari <[email protected]>2019-11-28 07:19:14 +0000
committerSeivan Heidari <[email protected]>2019-11-28 07:19:14 +0000
commit18a0937585b836ec5ed054b9ae48e0156ab6d9ef (patch)
tree9de2c0267ddcc00df717f90034d0843d751a851b /crates/ra_hir_ty/src/op.rs
parenta7394b44c870f585eacfeb3036a33471aff49ff8 (diff)
parent484acc8a61d599662ed63a4cbda091d38a982551 (diff)
Merge branch 'master' of https://github.com/rust-analyzer/rust-analyzer into feature/themes
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}