aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_hir_ty/src/op.rs
diff options
context:
space:
mode:
authorbors[bot] <26634292+bors[bot]@users.noreply.github.com>2019-11-27 18:27:45 +0000
committerGitHub <[email protected]>2019-11-27 18:27:45 +0000
commit4946169a96f3d442f463724af481fdb760381ccb (patch)
treeb68c3da4bb47e0a285968319bfb33b44cbfd5546 /crates/ra_hir_ty/src/op.rs
parent2798beeeb05ab0e71773a2ed51b7b0c90bf6b06a (diff)
parent47ec2ceb12df756b3482ddd2b1947e4b38f23706 (diff)
Merge #2429
2429: Move type inference to a separate crate r=matklad a=matklad Co-authored-by: Aleksey Kladov <[email protected]>
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}