aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_hir/src/ty/op.rs
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ra_hir/src/ty/op.rs')
-rw-r--r--crates/ra_hir/src/ty/op.rs81
1 files changed, 81 insertions, 0 deletions
diff --git a/crates/ra_hir/src/ty/op.rs b/crates/ra_hir/src/ty/op.rs
new file mode 100644
index 000000000..8703cf236
--- /dev/null
+++ b/crates/ra_hir/src/ty/op.rs
@@ -0,0 +1,81 @@
1use crate::expr::BinaryOp;
2use super::{Ty, InferTy};
3
4pub(super) fn binary_op_return_ty(op: BinaryOp, rhs_ty: Ty) -> Ty {
5 match op {
6 BinaryOp::BooleanOr
7 | BinaryOp::BooleanAnd
8 | BinaryOp::EqualityTest
9 | BinaryOp::NegatedEqualityTest
10 | BinaryOp::LesserEqualTest
11 | BinaryOp::GreaterEqualTest
12 | BinaryOp::LesserTest
13 | BinaryOp::GreaterTest => Ty::Bool,
14 BinaryOp::Assignment
15 | BinaryOp::AddAssign
16 | BinaryOp::SubAssign
17 | BinaryOp::DivAssign
18 | BinaryOp::MulAssign
19 | BinaryOp::RemAssign
20 | BinaryOp::ShrAssign
21 | BinaryOp::ShlAssign
22 | BinaryOp::BitAndAssign
23 | BinaryOp::BitOrAssign
24 | BinaryOp::BitXorAssign => Ty::unit(),
25 BinaryOp::Addition
26 | BinaryOp::Subtraction
27 | BinaryOp::Multiplication
28 | BinaryOp::Division
29 | BinaryOp::Remainder
30 | BinaryOp::LeftShift
31 | BinaryOp::RightShift
32 | BinaryOp::BitwiseAnd
33 | BinaryOp::BitwiseOr
34 | BinaryOp::BitwiseXor => match rhs_ty {
35 Ty::Int(..)
36 | Ty::Float(..)
37 | Ty::Infer(InferTy::IntVar(..))
38 | Ty::Infer(InferTy::FloatVar(..)) => rhs_ty,
39 _ => Ty::Unknown,
40 },
41 BinaryOp::RangeRightOpen | BinaryOp::RangeRightClosed => Ty::Unknown,
42 }
43}
44
45pub(super) fn binary_op_rhs_expectation(op: BinaryOp, lhs_ty: Ty) -> Ty {
46 match op {
47 BinaryOp::BooleanAnd | BinaryOp::BooleanOr => Ty::Bool,
48 BinaryOp::Assignment | BinaryOp::EqualityTest => match lhs_ty {
49 Ty::Int(..) | Ty::Float(..) | Ty::Str | Ty::Char | Ty::Bool => lhs_ty,
50 _ => Ty::Unknown,
51 },
52 BinaryOp::LesserEqualTest
53 | BinaryOp::GreaterEqualTest
54 | BinaryOp::LesserTest
55 | BinaryOp::GreaterTest
56 | BinaryOp::AddAssign
57 | BinaryOp::SubAssign
58 | BinaryOp::DivAssign
59 | BinaryOp::MulAssign
60 | BinaryOp::RemAssign
61 | BinaryOp::ShrAssign
62 | BinaryOp::ShlAssign
63 | BinaryOp::BitAndAssign
64 | BinaryOp::BitOrAssign
65 | BinaryOp::BitXorAssign
66 | BinaryOp::Addition
67 | BinaryOp::Subtraction
68 | BinaryOp::Multiplication
69 | BinaryOp::Division
70 | BinaryOp::Remainder
71 | BinaryOp::LeftShift
72 | BinaryOp::RightShift
73 | BinaryOp::BitwiseAnd
74 | BinaryOp::BitwiseOr
75 | BinaryOp::BitwiseXor => match lhs_ty {
76 Ty::Int(..) | Ty::Float(..) => lhs_ty,
77 _ => Ty::Unknown,
78 },
79 _ => Ty::Unknown,
80 }
81}